Search in sources :

Example 6 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterface.

@SuppressWarnings("rawtypes")
@Test
public void parameterizedAnnotatedInterface() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            context.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
        }
    }, MyParameterizedControllerImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("page1", request.getAttribute("viewName"));
    HttpSession session = request.getSession();
    assertTrue(session.getAttribute("object1") != null);
    assertTrue(session.getAttribute("object2") != null);
    assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("page2", request.getAttribute("viewName"));
    assertTrue(session.getAttribute("object1") != null);
    assertTrue(session.getAttribute("object2") != null);
    assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) 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 7 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtmlWithSuffixPresent.

@Test
public void responseBodyAsHtmlWithSuffixPresent() 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 = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
    request.setContent(content);
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    assertEquals("text/html;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 8 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method emptyParameterListHandleMethod.

@Test
public void emptyParameterListHandleMethod() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
            vrDef.getPropertyValues().add("suffix", ".jsp");
            context.registerBeanDefinition("viewResolver", vrDef);
        }
    }, EmptyParameterListHandlerMethodController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
    MockHttpServletResponse response = new MockHttpServletResponse();
    EmptyParameterListHandlerMethodController.called = false;
    getServlet().service(request, response);
    assertTrue(EmptyParameterListHandlerMethodController.called);
    assertEquals("", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 9 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterCsvAsStringArray.

@Test
public void parameterCsvAsStringArray() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
            RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
            wbiDef.getPropertyValues().add("conversionService", csDef);
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, CsvController.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/integerArray");
    request.setMethod("POST");
    request.addParameter("content", "1,2");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("1-2", response.getContentAsString());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with GenericWebApplicationContext

use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.

@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setClassesToBeBound(A.class, B.class);
            try {
                marshaller.afterPropertiesSet();
            } catch (Exception ex) {
                throw new BeanCreationException(ex.getMessage(), ex);
            }
            MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("messageConverters", messageConverter);
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, RequestBodyArgMismatchController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "<b/>";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "application/xml; charset=utf-8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(400, response.getStatus());
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) 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)

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