Search in sources :

Example 6 with StandardServletAsyncWebRequest

use of org.springframework.web.context.request.async.StandardServletAsyncWebRequest in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncScenario.

@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {
    // Initial request thread
    OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
    interceptor.setEntityManagerFactory(factory);
    given(factory.createEntityManager()).willReturn(this.manager);
    interceptor.preHandle(this.webRequest);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
    asyncManager.setTaskExecutor(new SyncTaskExecutor());
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    asyncManager.startCallableProcessing(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "anything";
        }
    });
    interceptor.afterConcurrentHandlingStarted(this.webRequest);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    // Async dispatch thread
    interceptor.preHandle(this.webRequest);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
    asyncManager.clearConcurrentResult();
    // 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(this.webRequest, null);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
    given(this.manager.isOpen()).willReturn(true);
    interceptor.afterCompletion(this.webRequest, null);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    verify(this.manager).close();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)6 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)5 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 Before (org.junit.Before)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)3 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Test (org.junit.Test)2 AsyncEvent (javax.servlet.AsyncEvent)1 AsyncListener (javax.servlet.AsyncListener)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)1