1 16 17 package org.springframework.orm.jdo.support; 18 19 import java.io.IOException ; 20 21 import javax.jdo.PersistenceManager; 22 import javax.jdo.PersistenceManagerFactory; 23 import javax.servlet.FilterChain ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletResponse ; 27 28 import junit.framework.TestCase; 29 import org.easymock.MockControl; 30 31 import org.springframework.mock.web.MockFilterConfig; 32 import org.springframework.mock.web.MockHttpServletRequest; 33 import org.springframework.mock.web.MockHttpServletResponse; 34 import org.springframework.mock.web.MockServletContext; 35 import org.springframework.transaction.support.TransactionSynchronizationManager; 36 import org.springframework.web.context.WebApplicationContext; 37 import org.springframework.web.context.support.StaticWebApplicationContext; 38 39 43 public class OpenPersistenceManagerInViewTests extends TestCase { 44 45 public void testOpenPersistenceManagerInViewInterceptor() throws Exception { 46 MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class); 47 PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock(); 48 MockControl pmControl = MockControl.createControl(PersistenceManager.class); 49 PersistenceManager pm = (PersistenceManager) pmControl.getMock(); 50 51 OpenPersistenceManagerInViewInterceptor interceptor = new OpenPersistenceManagerInViewInterceptor(); 52 interceptor.setPersistenceManagerFactory(pmf); 53 MockServletContext sc = new MockServletContext(); 54 MockHttpServletRequest request = new MockHttpServletRequest(sc); 55 MockHttpServletResponse response = new MockHttpServletResponse(); 56 57 pmf.getPersistenceManager(); 58 pmfControl.setReturnValue(pm, 1); 59 pmfControl.replay(); 60 pmControl.replay(); 61 interceptor.preHandle(request, response, "handler"); 62 assertTrue(TransactionSynchronizationManager.hasResource(pmf)); 63 64 interceptor.preHandle(request, response, "handler"); 66 67 interceptor.preHandle(request, response, "handler"); 68 interceptor.postHandle(request, response, "handler", null); 69 interceptor.afterCompletion(request, response, "handler", null); 70 71 interceptor.postHandle(request, response, "handler", null); 72 interceptor.afterCompletion(request, response, "handler", null); 73 74 interceptor.preHandle(request, response, "handler"); 75 interceptor.postHandle(request, response, "handler", null); 76 interceptor.afterCompletion(request, response, "handler", null); 77 78 pmfControl.verify(); 79 pmControl.verify(); 80 81 pmfControl.reset(); 82 pmControl.reset(); 83 pmfControl.replay(); 84 pmControl.replay(); 85 interceptor.postHandle(request, response, "handler", null); 86 assertTrue(TransactionSynchronizationManager.hasResource(pmf)); 87 pmfControl.verify(); 88 pmControl.verify(); 89 90 pmfControl.reset(); 91 pmControl.reset(); 92 pm.close(); 93 pmControl.setVoidCallable(1); 94 pmfControl.replay(); 95 pmControl.replay(); 96 interceptor.afterCompletion(request, response, "handler", null); 97 assertFalse(TransactionSynchronizationManager.hasResource(pmf)); 98 pmfControl.verify(); 99 pmControl.verify(); 100 } 101 102 public void testOpenPersistenceManagerInViewFilter() throws Exception { 103 MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class); 104 final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock(); 105 MockControl pmControl = MockControl.createControl(PersistenceManager.class); 106 PersistenceManager pm = (PersistenceManager) pmControl.getMock(); 107 108 pmf.getPersistenceManager(); 109 pmfControl.setReturnValue(pm, 1); 110 pm.close(); 111 pmControl.setVoidCallable(1); 112 pmfControl.replay(); 113 pmControl.replay(); 114 115 MockControl pmf2Control = MockControl.createControl(PersistenceManagerFactory.class); 116 final PersistenceManagerFactory pmf2 = (PersistenceManagerFactory) pmf2Control.getMock(); 117 MockControl pm2Control = MockControl.createControl(PersistenceManager.class); 118 PersistenceManager pm2 = (PersistenceManager) pm2Control.getMock(); 119 120 pmf2.getPersistenceManager(); 121 pmf2Control.setReturnValue(pm2, 1); 122 pm2.close(); 123 pm2Control.setVoidCallable(1); 124 pmf2Control.replay(); 125 pm2Control.replay(); 126 127 MockServletContext sc = new MockServletContext(); 128 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 129 wac.setServletContext(sc); 130 wac.getDefaultListableBeanFactory().registerSingleton("persistenceManagerFactory", pmf); 131 wac.getDefaultListableBeanFactory().registerSingleton("myPersistenceManagerFactory", pmf2); 132 wac.refresh(); 133 sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 134 MockHttpServletRequest request = new MockHttpServletRequest(sc); 135 MockHttpServletResponse response = new MockHttpServletResponse(); 136 137 MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); 138 MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); 139 filterConfig2.addInitParameter("persistenceManagerFactoryBeanName", "myPersistenceManagerFactory"); 140 141 final OpenPersistenceManagerInViewFilter filter = new OpenPersistenceManagerInViewFilter(); 142 filter.init(filterConfig); 143 final OpenPersistenceManagerInViewFilter filter2 = new OpenPersistenceManagerInViewFilter(); 144 filter2.init(filterConfig2); 145 146 final FilterChain filterChain = new FilterChain () { 147 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 148 throws IOException , ServletException { 149 assertTrue(TransactionSynchronizationManager.hasResource(pmf)); 150 servletRequest.setAttribute("invoked", Boolean.TRUE); 151 } 152 }; 153 154 final FilterChain filterChain2 = new FilterChain () { 155 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 156 throws IOException , ServletException { 157 assertTrue(TransactionSynchronizationManager.hasResource(pmf2)); 158 filter.doFilter(servletRequest, servletResponse, filterChain); 159 } 160 }; 161 162 FilterChain filterChain3 = new FilterChain () { 163 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) 164 throws IOException , ServletException { 165 filter2.doFilter(servletRequest, servletResponse, filterChain2); 166 } 167 }; 168 169 assertFalse(TransactionSynchronizationManager.hasResource(pmf)); 170 assertFalse(TransactionSynchronizationManager.hasResource(pmf2)); 171 filter2.doFilter(request, response, filterChain3); 172 assertFalse(TransactionSynchronizationManager.hasResource(pmf)); 173 assertFalse(TransactionSynchronizationManager.hasResource(pmf2)); 174 assertNotNull(request.getAttribute("invoked")); 175 176 pmfControl.verify(); 177 pmControl.verify(); 178 pmfControl.verify(); 179 pmControl.verify(); 180 181 wac.close(); 182 } 183 184 } 185 | Popular Tags |