Search in sources :

Example 56 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method binderInitializingCommandProvidingFormController.

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

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
        }
    }, MyBinderInitializingCommandProvidingFormController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    request.addParameter("defaultName", "myDefaultName");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue", 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) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method commandProvidingFormControllerWithCustomEditor.

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

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("webBindingInitializer", new MyWebBindingInitializer());
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, MyCommandProvidingFormController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    request.addParameter("defaultName", "myDefaultName");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue", 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) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method proxiedFormController.

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

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
            DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
            autoProxyCreator.setBeanFactory(wac.getBeanFactory());
            wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
            wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
        }
    }, MyFormController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    request.addParameter("name", "name1");
    request.addParameter("age", "value2");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString());
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 59 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method sessionAttributeExposureWithInterface.

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

        @Override
        public void initialize(GenericWebApplicationContext context) {
            context.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
            DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
            autoProxyCreator.setBeanFactory(context.getBeanFactory());
            context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
            context.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
        }
    }, MySessionAttributesControllerImpl.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"));
    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"));
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 60 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method typedCommandProvidingFormController.

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

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("webBindingInitializer", new MyWebBindingInitializer());
            List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
            argumentResolvers.add(new ServletWebArgumentResolverAdapter(new MySpecialArgumentResolver()));
            adapterDef.getPropertyValues().add("customArgumentResolvers", argumentResolvers);
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, MyTypedCommandProvidingFormController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-Integer:10-typeMismatch-tb1-myOriginalValue", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myOtherPath.do");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "value2");
    request.addParameter("date", "2007-10-02");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-myName-typeMismatch-tb1-myOriginalValue", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myThirdPath.do");
    request.addParameter("defaultName", "10");
    request.addParameter("age", "100");
    request.addParameter("date", "2007-10-02");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView-special-99-special-99", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) 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