Search in sources :

Example 6 with MockServletConfig

use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.

@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletConfig should have been set", bean.getServletConfig());
    assertEquals(servletConfig, bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 7 with MockServletConfig

use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.

the class DispatcherServletTests method dispatcherServletContextRefresh.

@Test
public void dispatcherServletContextRefresh() throws ServletException {
    MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.init(new MockServletConfig(servletContext, "empty"));
    ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
    ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertSame(servletContext, contextBean.getServletContext());
    assertSame(servlet.getServletConfig(), configBean.getServletConfig());
    MultipartResolver multipartResolver = servlet.getMultipartResolver();
    assertNotNull(multipartResolver);
    ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh();
    ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
    ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertSame(servletContext, contextBean2.getServletContext());
    assertSame(servlet.getServletConfig(), configBean2.getServletConfig());
    assertTrue(contextBean != contextBean2);
    assertTrue(configBean != configBean2);
    MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
    assertTrue(multipartResolver != multipartResolver2);
    servlet.destroy();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ServletContextAwareBean(org.springframework.web.context.ServletContextAwareBean) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MultipartResolver(org.springframework.web.multipart.MultipartResolver) ServletConfigAwareBean(org.springframework.web.context.ServletConfigAwareBean) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 8 with MockServletConfig

use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", new HttpRequestHandler() {

        @Override
        public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            assertSame(request, req);
            assertSame(response, res);
            String exception = request.getParameter("exception");
            if ("ServletException".equals(exception)) {
                throw new ServletException("test");
            }
            if ("IOException".equals(exception)) {
                throw new IOException("test");
            }
            res.getWriter().write("myResponse");
        }
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertEquals("myResponse", response.getContentAsString());
    try {
        request.setParameter("exception", "ServletException");
        servlet.service(request, response);
        fail("Should have thrown ServletException");
    } catch (ServletException ex) {
        assertEquals("test", ex.getMessage());
    }
    try {
        request.setParameter("exception", "IOException");
        servlet.service(request, response);
        fail("Should have thrown IOException");
    } catch (IOException ex) {
        assertEquals("test", ex.getMessage());
    }
}
Also used : HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletException(javax.servlet.ServletException) Servlet(javax.servlet.Servlet) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 9 with MockServletConfig

use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.

the class ViewResolutionIntegrationTests method runTest.

private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
    String basePath = "org/springframework/web/servlet/config/annotation";
    MockServletContext servletContext = new MockServletContext(basePath);
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(configClass);
    context.setServletContext(servletContext);
    context.refresh();
    DispatcherServlet servlet = new DispatcherServlet(context);
    servlet.init(servletConfig);
    servlet.service(request, response);
    return response;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 10 with MockServletConfig

use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.

the class CglibProxyControllerTests method initServlet.

@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
    servlet = new DispatcherServlet() {

        @Override
        protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
            GenericWebApplicationContext wac = new GenericWebApplicationContext();
            wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
            DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
            autoProxyCreator.setProxyTargetClass(true);
            autoProxyCreator.setBeanFactory(wac.getBeanFactory());
            wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
            wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
            wac.refresh();
            return wac;
        }
    };
    servlet.init(new MockServletConfig());
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) SimpleTraceInterceptor(org.springframework.aop.interceptor.SimpleTraceInterceptor) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Aggregations

MockServletConfig (org.springframework.mock.web.test.MockServletConfig)29 Test (org.junit.Test)24 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)14 MockServletContext (org.springframework.mock.web.test.MockServletContext)13 ServletConfig (javax.servlet.ServletConfig)8 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)7 ServletContext (javax.servlet.ServletContext)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)5 ServletException (javax.servlet.ServletException)4 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 IOException (java.io.IOException)2 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)2 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)2 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)2 ServletConfigAwareBean (org.springframework.web.context.ServletConfigAwareBean)2 ServletContextAwareBean (org.springframework.web.context.ServletContextAwareBean)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2