Search in sources :

Example 66 with MockServletContext

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

the class CommonsMultipartResolverTests method doTestWithApplicationContext.

private void doTestWithApplicationContext(boolean lazy) throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    MockCommonsMultipartResolver resolver = new MockCommonsMultipartResolver();
    resolver.setMaxUploadSize(1000);
    resolver.setMaxInMemorySize(100);
    resolver.setDefaultEncoding("enc");
    if (lazy) {
        resolver.setResolveLazily(false);
    }
    resolver.setServletContext(wac.getServletContext());
    assertEquals(1000, resolver.getFileUpload().getSizeMax());
    assertEquals(100, resolver.getFileItemFactory().getSizeThreshold());
    assertEquals("enc", resolver.getFileUpload().getHeaderEncoding());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    originalRequest.addParameter("getField", "getValue");
    assertTrue(resolver.isMultipart(originalRequest));
    MultipartHttpServletRequest request = resolver.resolveMultipart(originalRequest);
    doTestParameters(request);
    doTestFiles(request);
    doTestBinding(resolver, originalRequest, request);
    wac.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockServletContext(org.springframework.mock.web.test.MockServletContext) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 67 with MockServletContext

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

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptor.

@Test
public void testOpenEntityManagerInViewInterceptor() throws Exception {
    OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
    interceptor.setEntityManagerFactory(this.factory);
    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    interceptor.preHandle(new ServletWebRequest(request));
    assertTrue(TransactionSynchronizationManager.hasResource(this.factory));
    // check that further invocations simply participate
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(new ServletWebRequest(request), null);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
    given(manager.isOpen()).willReturn(true);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    verify(manager).close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 68 with MockServletContext

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

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewFilterAsyncScenario.

@Test
public void testOpenEntityManagerInViewFilterAsyncScenario() throws Exception {
    given(manager.isOpen()).willReturn(true);
    final EntityManagerFactory factory2 = mock(EntityManagerFactory.class);
    final EntityManager manager2 = mock(EntityManager.class);
    given(factory2.createEntityManager()).willReturn(manager2);
    given(manager2.isOpen()).willReturn(true);
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", factory);
    wac.getDefaultListableBeanFactory().registerSingleton("myEntityManagerFactory", factory2);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
    filterConfig2.addInitParameter("entityManagerFactoryBeanName", "myEntityManagerFactory");
    final OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
    filter.init(filterConfig);
    final OpenEntityManagerInViewFilter filter2 = new OpenEntityManagerInViewFilter();
    filter2.init(filterConfig2);
    final AtomicInteger count = new AtomicInteger(0);
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            assertTrue(TransactionSynchronizationManager.hasResource(factory));
            servletRequest.setAttribute("invoked", Boolean.TRUE);
            count.incrementAndGet();
        }
    };
    final AtomicInteger count2 = new AtomicInteger(0);
    final FilterChain filterChain2 = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertTrue(TransactionSynchronizationManager.hasResource(factory2));
            filter.doFilter(servletRequest, servletResponse, filterChain);
            count2.incrementAndGet();
        }
    };
    FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
    AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
    given(asyncWebRequest.isAsyncStarted()).willReturn(true);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
    asyncManager.setTaskExecutor(new SyncTaskExecutor());
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    asyncManager.startCallableProcessing(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "anything";
        }
    });
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    filter2.doFilter(this.request, this.response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    assertEquals(1, count.get());
    assertEquals(1, count2.get());
    assertNotNull(request.getAttribute("invoked"));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).startAsync();
    // Async dispatch after concurrent handling produces result ...
    reset(asyncWebRequest);
    given(asyncWebRequest.isAsyncStarted()).willReturn(false);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    filter.doFilter(this.request, this.response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    assertEquals(2, count.get());
    assertEquals(2, count2.get());
    verify(this.manager).close();
    verify(manager2).close();
    wac.close();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntityManagerFactory(javax.persistence.EntityManagerFactory) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) Test(org.junit.Test)

Example 69 with MockServletContext

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

the class CharacterEncodingFilterTests method withBeanInitialization.

@Test
public void withBeanInitialization() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    given(request.getCharacterEncoding()).willReturn(null);
    given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX)).willReturn(null);
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding(ENCODING);
    filter.setBeanName(FILTER_NAME);
    filter.setServletContext(new MockServletContext());
    filter.doFilter(request, response, filterChain);
    verify(request).setCharacterEncoding(ENCODING);
    verify(request).setAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, Boolean.TRUE);
    verify(request).removeAttribute(FILTER_NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX);
    verify(filterChain).doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 70 with MockServletContext

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

the class CompositeFilterTests method testCompositeFilter.

@Test
public void testCompositeFilter() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    MockFilter targetFilter = new MockFilter();
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    CompositeFilter filterProxy = new CompositeFilter();
    filterProxy.setFilters(Arrays.asList(targetFilter));
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNotNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9