use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ResourceHandlerRegistryTests method setUp.
@Before
public void setUp() {
this.registry = new ResourceHandlerRegistry(new GenericWebApplicationContext(), new MockServletContext());
this.registration = registry.addResourceHandler("/resources/**");
this.registration.addResourceLocations("classpath:org/springframework/web/servlet/config/annotation/");
this.response = new MockHttpServletResponse();
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class MvcNamespaceTests method setUp.
@Before
public void setUp() throws Exception {
TestMockServletContext servletContext = new TestMockServletContext();
appContext = new GenericWebApplicationContext();
appContext.setServletContext(servletContext);
LocaleContextHolder.setLocale(Locale.US);
String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
appContext.getServletContext().setAttribute(attributeName, appContext);
handler = new TestController();
Method method = TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class);
handlerMethod = new InvocableHandlerMethod(handler, method);
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class AbstractServletHandlerMethodTests method initServlet.
/**
* Initialize a DispatcherServlet instance registering zero or more controller classes
* and also providing additional bean definitions through a callback.
*/
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(final ApplicationContextInitializer<GenericWebApplicationContext> initializer, final Class<?>... controllerClasses) throws ServletException {
final GenericWebApplicationContext wac = new GenericWebApplicationContext();
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
for (Class<?> clazz : controllerClasses) {
wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
}
Class<?> mappingType = RequestMappingHandlerMapping.class;
RootBeanDefinition beanDef = new RootBeanDefinition(mappingType);
beanDef.getPropertyValues().add("removeSemicolonContent", "false");
wac.registerBeanDefinition("handlerMapping", beanDef);
Class<?> adapterType = RequestMappingHandlerAdapter.class;
wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));
Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));
resolverType = ResponseStatusExceptionResolver.class;
wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));
resolverType = DefaultHandlerExceptionResolver.class;
wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));
if (initializer != null) {
initializer.initialize(wac);
}
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
return wac;
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsTextWithCssExtension.
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
factoryBean.afterPropertiesSet();
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, TextRestController.class);
byte[] content = "body".getBytes(StandardCharsets.ISO_8859_1);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
request.setContent(content);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(200, response.getStatus());
assertEquals("text/css;charset=ISO-8859-1", response.getContentType());
assertNull(response.getHeader("Content-Disposition"));
assertArrayEquals(content, response.getContentAsByteArray());
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method overlappingMessageConvertersRequestBody.
/*
* See SPR-6877
*/
@Test
public void overlappingMessageConvertersRequestBody() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new SimpleMessageConverter(new MediaType("application", "json"), MediaType.ALL));
adapterDef.getPropertyValues().add("messageConverters", messageConverters);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
request.setContent("Hello World".getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/json, text/javascript, */*");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Invalid content-type", "application/json;charset=ISO-8859-1", response.getHeader("Content-Type"));
}
Aggregations