Search in sources :

Example 1 with GenericWebApplicationContext

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();
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 2 with GenericWebApplicationContext

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);
}
Also used : InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Before(org.junit.Before)

Example 3 with GenericWebApplicationContext

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;
}
Also used : ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 4 with GenericWebApplicationContext

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());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with GenericWebApplicationContext

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"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)93 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)33 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)32 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)31 Test (org.junit.Test)30 Test (org.junit.jupiter.api.Test)30 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)9 MockServletContext (org.springframework.mock.web.MockServletContext)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 Method (java.lang.reflect.Method)5 HttpSession (javax.servlet.http.HttpSession)5 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)5 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)5 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4