Search in sources :

Example 1 with PassThroughFilterChain

use of org.springframework.mock.web.test.PassThroughFilterChain 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 2 with PassThroughFilterChain

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

the class CommonsMultipartResolverTests method withServletContextAndFilter.

@Test
public void withServletContextAndFilter() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("class", "notWritable");
    filterConfig.addInitParameter("unknownParam", "someValue");
    final MultipartFilter filter = new MultipartFilter();
    filter.init(filterConfig);
    final List<MultipartFile> files = new ArrayList<>();
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
            files.addAll(request.getFileMap().values());
        }
    };
    FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    filter.doFilter(originalRequest, response, filterChain2);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 3 with PassThroughFilterChain

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

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewFilter.

@Test
public void testOpenEntityManagerInViewFilter() 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);
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    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 FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            assertTrue(TransactionSynchronizationManager.hasResource(factory));
            servletRequest.setAttribute("invoked", Boolean.TRUE);
        }
    };
    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);
        }
    };
    FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    filter2.doFilter(request, response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    assertNotNull(request.getAttribute("invoked"));
    verify(manager).close();
    verify(manager2).close();
    wac.close();
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) EntityManager(javax.persistence.EntityManager) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) EntityManagerFactory(javax.persistence.EntityManagerFactory) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) 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

FilterChain (javax.servlet.FilterChain)3 ServletRequest (javax.servlet.ServletRequest)3 ServletResponse (javax.servlet.ServletResponse)3 Test (org.junit.Test)3 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 PassThroughFilterChain (org.springframework.mock.web.test.PassThroughFilterChain)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 EntityManager (javax.persistence.EntityManager)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1