|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2007 the original author or authors. |
| 2 | + * Copyright 2002-2011 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.orm.jpa.support;
|
18 | 18 |
|
19 |
| -import org.junit.Ignore; |
20 |
| -import org.junit.Test; |
| 19 | +import java.io.IOException; |
| 20 | +import javax.persistence.EntityManager; |
| 21 | +import javax.persistence.EntityManagerFactory; |
| 22 | +import javax.servlet.FilterChain; |
| 23 | +import javax.servlet.ServletException; |
| 24 | +import javax.servlet.ServletRequest; |
| 25 | +import javax.servlet.ServletResponse; |
| 26 | + |
| 27 | +import junit.framework.TestCase; |
| 28 | +import org.easymock.MockControl; |
| 29 | + |
| 30 | +import org.springframework.mock.web.MockFilterConfig; |
| 31 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 32 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 33 | +import org.springframework.mock.web.MockServletContext; |
| 34 | +import org.springframework.mock.web.PassThroughFilterChain; |
| 35 | +import org.springframework.orm.jpa.JpaTemplate; |
| 36 | +import org.springframework.transaction.support.TransactionSynchronizationManager; |
| 37 | +import org.springframework.web.context.WebApplicationContext; |
| 38 | +import org.springframework.web.context.request.ServletWebRequest; |
| 39 | +import org.springframework.web.context.support.StaticWebApplicationContext; |
21 | 40 |
|
22 | 41 | /**
|
23 | 42 | * @author Costin Leau
|
24 | 43 | * @author Juergen Hoeller
|
25 | 44 | * @author Chris Beams
|
26 | 45 | */
|
27 |
| -@Ignore // dependency issues after moving from .testsuite -> .test |
28 |
| -public class OpenEntityManagerInViewTests { |
| 46 | +public class OpenEntityManagerInViewTests extends TestCase { |
29 | 47 |
|
30 |
| -// private MockControl factoryControl, managerControl; |
31 |
| -// |
32 |
| -// private EntityManager manager; |
33 |
| -// |
34 |
| -// private EntityManagerFactory factory; |
35 |
| -// |
36 |
| -// private JpaTemplate template; |
37 |
| -// |
38 |
| -// |
39 |
| -// @Override |
40 |
| -// protected void setUp() throws Exception { |
41 |
| -// factoryControl = MockControl.createControl(EntityManagerFactory.class); |
42 |
| -// factory = (EntityManagerFactory) factoryControl.getMock(); |
43 |
| -// managerControl = MockControl.createControl(EntityManager.class); |
44 |
| -// manager = (EntityManager) managerControl.getMock(); |
45 |
| -// |
46 |
| -// template = new JpaTemplate(factory); |
47 |
| -// template.afterPropertiesSet(); |
48 |
| -// |
49 |
| -// factoryControl.expectAndReturn(factory.createEntityManager(), manager); |
50 |
| -// } |
51 |
| -// |
52 |
| -// @Override |
53 |
| -// protected void tearDown() throws Exception { |
54 |
| -// assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); |
55 |
| -// assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); |
56 |
| -// assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
57 |
| -// assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); |
58 |
| -// } |
59 |
| -// |
60 |
| -// public void testOpenEntityManagerInterceptorInView() throws Exception { |
61 |
| -// OpenEntityManagerInViewInterceptor rawInterceptor = new OpenEntityManagerInViewInterceptor(); |
62 |
| -// rawInterceptor.setEntityManagerFactory(factory); |
63 |
| -// HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor); |
64 |
| -// |
65 |
| -// MockServletContext sc = new MockServletContext(); |
66 |
| -// MockHttpServletRequest request = new MockHttpServletRequest(sc); |
67 |
| -// MockHttpServletResponse response = new MockHttpServletResponse(); |
68 |
| -// |
69 |
| -// managerControl.replay(); |
70 |
| -// factoryControl.replay(); |
71 |
| -// |
72 |
| -// interceptor.preHandle(request, response, "handler"); |
73 |
| -// assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
74 |
| -// |
75 |
| -// // check that further invocations simply participate |
76 |
| -// interceptor.preHandle(request, response, "handler"); |
77 |
| -// |
78 |
| -// interceptor.preHandle(request, response, "handler"); |
79 |
| -// interceptor.postHandle(request, response, "handler", null); |
80 |
| -// interceptor.afterCompletion(request, response, "handler", null); |
81 |
| -// |
82 |
| -// interceptor.postHandle(request, response, "handler", null); |
83 |
| -// interceptor.afterCompletion(request, response, "handler", null); |
84 |
| -// |
85 |
| -// interceptor.preHandle(request, response, "handler"); |
86 |
| -// interceptor.postHandle(request, response, "handler", null); |
87 |
| -// interceptor.afterCompletion(request, response, "handler", null); |
88 |
| -// |
89 |
| -// factoryControl.verify(); |
90 |
| -// managerControl.verify(); |
91 |
| -// |
92 |
| -// managerControl.reset(); |
93 |
| -// factoryControl.reset(); |
94 |
| -// managerControl.replay(); |
95 |
| -// factoryControl.replay(); |
96 |
| -// |
97 |
| -// interceptor.postHandle(request, response, "handler", null); |
98 |
| -// assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
99 |
| -// |
100 |
| -// factoryControl.verify(); |
101 |
| -// managerControl.verify(); |
102 |
| -// |
103 |
| -// managerControl.reset(); |
104 |
| -// factoryControl.reset(); |
105 |
| -// |
106 |
| -// manager.close(); |
107 |
| -// |
108 |
| -// managerControl.replay(); |
109 |
| -// factoryControl.replay(); |
110 |
| -// |
111 |
| -// interceptor.afterCompletion(request, response, "handler", null); |
112 |
| -// assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
113 |
| -// |
114 |
| -// factoryControl.verify(); |
115 |
| -// managerControl.verify(); |
116 |
| -// } |
117 |
| -// |
118 |
| -// public void testOpenEntityManagerInViewFilter() throws Exception { |
119 |
| -// manager.close(); |
120 |
| -// |
121 |
| -// managerControl.replay(); |
122 |
| -// factoryControl.replay(); |
123 |
| -// |
124 |
| -// MockControl factoryControl2 = MockControl.createControl(EntityManagerFactory.class); |
125 |
| -// final EntityManagerFactory factory2 = (EntityManagerFactory) factoryControl2.getMock(); |
126 |
| -// |
127 |
| -// MockControl managerControl2 = MockControl.createControl(EntityManager.class); |
128 |
| -// EntityManager manager2 = (EntityManager) managerControl2.getMock(); |
129 |
| -// |
130 |
| -// factoryControl2.expectAndReturn(factory2.createEntityManager(), manager2); |
131 |
| -// manager2.close(); |
132 |
| -// |
133 |
| -// factoryControl2.replay(); |
134 |
| -// managerControl2.replay(); |
135 |
| -// |
136 |
| -// MockServletContext sc = new MockServletContext(); |
137 |
| -// StaticWebApplicationContext wac = new StaticWebApplicationContext(); |
138 |
| -// wac.setServletContext(sc); |
139 |
| -// wac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", factory); |
140 |
| -// wac.getDefaultListableBeanFactory().registerSingleton("myEntityManagerFactory", factory2); |
141 |
| -// wac.refresh(); |
142 |
| -// sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); |
143 |
| -// MockHttpServletRequest request = new MockHttpServletRequest(sc); |
144 |
| -// MockHttpServletResponse response = new MockHttpServletResponse(); |
145 |
| -// |
146 |
| -// MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); |
147 |
| -// MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); |
148 |
| -// filterConfig2.addInitParameter("entityManagerFactoryBeanName", "myEntityManagerFactory"); |
149 |
| -// |
150 |
| -// final OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter(); |
151 |
| -// filter.init(filterConfig); |
152 |
| -// final OpenEntityManagerInViewFilter filter2 = new OpenEntityManagerInViewFilter(); |
153 |
| -// filter2.init(filterConfig2); |
154 |
| -// |
155 |
| -// final FilterChain filterChain = new FilterChain() { |
156 |
| -// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { |
157 |
| -// assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
158 |
| -// servletRequest.setAttribute("invoked", Boolean.TRUE); |
159 |
| -// } |
160 |
| -// }; |
161 |
| -// |
162 |
| -// final FilterChain filterChain2 = new FilterChain() { |
163 |
| -// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) |
164 |
| -// throws IOException, ServletException { |
165 |
| -// assertTrue(TransactionSynchronizationManager.hasResource(factory2)); |
166 |
| -// filter.doFilter(servletRequest, servletResponse, filterChain); |
167 |
| -// } |
168 |
| -// }; |
169 |
| -// |
170 |
| -// FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2); |
171 |
| -// |
172 |
| -// assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
173 |
| -// assertFalse(TransactionSynchronizationManager.hasResource(factory2)); |
174 |
| -// filter2.doFilter(request, response, filterChain3); |
175 |
| -// assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
176 |
| -// assertFalse(TransactionSynchronizationManager.hasResource(factory2)); |
177 |
| -// assertNotNull(request.getAttribute("invoked")); |
178 |
| -// |
179 |
| -// factoryControl.verify(); |
180 |
| -// managerControl.verify(); |
181 |
| -// factoryControl2.verify(); |
182 |
| -// managerControl2.verify(); |
183 |
| -// |
184 |
| -// wac.close(); |
185 |
| -// } |
| 48 | + private MockControl factoryControl, managerControl; |
| 49 | + |
| 50 | + private EntityManager manager; |
| 51 | + |
| 52 | + private EntityManagerFactory factory; |
| 53 | + |
| 54 | + private JpaTemplate template; |
| 55 | + |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void setUp() throws Exception { |
| 59 | + factoryControl = MockControl.createControl(EntityManagerFactory.class); |
| 60 | + factory = (EntityManagerFactory) factoryControl.getMock(); |
| 61 | + managerControl = MockControl.createControl(EntityManager.class); |
| 62 | + manager = (EntityManager) managerControl.getMock(); |
| 63 | + |
| 64 | + template = new JpaTemplate(factory); |
| 65 | + template.afterPropertiesSet(); |
| 66 | + |
| 67 | + factoryControl.expectAndReturn(factory.createEntityManager(), manager); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void tearDown() throws Exception { |
| 72 | + assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); |
| 73 | + assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); |
| 74 | + assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
| 75 | + assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); |
| 76 | + } |
| 77 | + |
| 78 | + public void testOpenEntityManagerInViewInterceptor() throws Exception { |
| 79 | + OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor(); |
| 80 | + interceptor.setEntityManagerFactory(factory); |
| 81 | + |
| 82 | + MockServletContext sc = new MockServletContext(); |
| 83 | + MockHttpServletRequest request = new MockHttpServletRequest(sc); |
| 84 | + |
| 85 | + managerControl.replay(); |
| 86 | + factoryControl.replay(); |
| 87 | + |
| 88 | + interceptor.preHandle(new ServletWebRequest(request)); |
| 89 | + assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
| 90 | + |
| 91 | + // check that further invocations simply participate |
| 92 | + interceptor.preHandle(new ServletWebRequest(request)); |
| 93 | + |
| 94 | + interceptor.preHandle(new ServletWebRequest(request)); |
| 95 | + interceptor.postHandle(new ServletWebRequest(request), null); |
| 96 | + interceptor.afterCompletion(new ServletWebRequest(request), null); |
| 97 | + |
| 98 | + interceptor.postHandle(new ServletWebRequest(request), null); |
| 99 | + interceptor.afterCompletion(new ServletWebRequest(request), null); |
| 100 | + |
| 101 | + interceptor.preHandle(new ServletWebRequest(request)); |
| 102 | + interceptor.postHandle(new ServletWebRequest(request), null); |
| 103 | + interceptor.afterCompletion(new ServletWebRequest(request), null); |
| 104 | + |
| 105 | + factoryControl.verify(); |
| 106 | + managerControl.verify(); |
| 107 | + |
| 108 | + managerControl.reset(); |
| 109 | + factoryControl.reset(); |
| 110 | + managerControl.replay(); |
| 111 | + factoryControl.replay(); |
| 112 | + |
| 113 | + interceptor.postHandle(new ServletWebRequest(request), null); |
| 114 | + assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
| 115 | + |
| 116 | + factoryControl.verify(); |
| 117 | + managerControl.verify(); |
| 118 | + |
| 119 | + managerControl.reset(); |
| 120 | + factoryControl.reset(); |
| 121 | + |
| 122 | + managerControl.expectAndReturn(manager.isOpen(), true); |
| 123 | + manager.close(); |
| 124 | + |
| 125 | + managerControl.replay(); |
| 126 | + factoryControl.replay(); |
| 127 | + |
| 128 | + interceptor.afterCompletion(new ServletWebRequest(request), null); |
| 129 | + assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
| 130 | + |
| 131 | + factoryControl.verify(); |
| 132 | + managerControl.verify(); |
| 133 | + } |
| 134 | + |
| 135 | + public void testOpenEntityManagerInViewFilter() throws Exception { |
| 136 | + managerControl.expectAndReturn(manager.isOpen(), true); |
| 137 | + manager.close(); |
| 138 | + |
| 139 | + managerControl.replay(); |
| 140 | + factoryControl.replay(); |
| 141 | + |
| 142 | + MockControl factoryControl2 = MockControl.createControl(EntityManagerFactory.class); |
| 143 | + final EntityManagerFactory factory2 = (EntityManagerFactory) factoryControl2.getMock(); |
| 144 | + |
| 145 | + MockControl managerControl2 = MockControl.createControl(EntityManager.class); |
| 146 | + EntityManager manager2 = (EntityManager) managerControl2.getMock(); |
| 147 | + |
| 148 | + factoryControl2.expectAndReturn(factory2.createEntityManager(), manager2); |
| 149 | + managerControl2.expectAndReturn(manager2.isOpen(), true); |
| 150 | + manager2.close(); |
| 151 | + |
| 152 | + factoryControl2.replay(); |
| 153 | + managerControl2.replay(); |
| 154 | + |
| 155 | + MockServletContext sc = new MockServletContext(); |
| 156 | + StaticWebApplicationContext wac = new StaticWebApplicationContext(); |
| 157 | + wac.setServletContext(sc); |
| 158 | + wac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", factory); |
| 159 | + wac.getDefaultListableBeanFactory().registerSingleton("myEntityManagerFactory", factory2); |
| 160 | + wac.refresh(); |
| 161 | + sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); |
| 162 | + MockHttpServletRequest request = new MockHttpServletRequest(sc); |
| 163 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 164 | + |
| 165 | + MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter"); |
| 166 | + MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2"); |
| 167 | + filterConfig2.addInitParameter("entityManagerFactoryBeanName", "myEntityManagerFactory"); |
| 168 | + |
| 169 | + final OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter(); |
| 170 | + filter.init(filterConfig); |
| 171 | + final OpenEntityManagerInViewFilter filter2 = new OpenEntityManagerInViewFilter(); |
| 172 | + filter2.init(filterConfig2); |
| 173 | + |
| 174 | + final FilterChain filterChain = new FilterChain() { |
| 175 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { |
| 176 | + assertTrue(TransactionSynchronizationManager.hasResource(factory)); |
| 177 | + servletRequest.setAttribute("invoked", Boolean.TRUE); |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + final FilterChain filterChain2 = new FilterChain() { |
| 182 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) |
| 183 | + throws IOException, ServletException { |
| 184 | + assertTrue(TransactionSynchronizationManager.hasResource(factory2)); |
| 185 | + filter.doFilter(servletRequest, servletResponse, filterChain); |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2); |
| 190 | + |
| 191 | + assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
| 192 | + assertFalse(TransactionSynchronizationManager.hasResource(factory2)); |
| 193 | + filter2.doFilter(request, response, filterChain3); |
| 194 | + assertFalse(TransactionSynchronizationManager.hasResource(factory)); |
| 195 | + assertFalse(TransactionSynchronizationManager.hasResource(factory2)); |
| 196 | + assertNotNull(request.getAttribute("invoked")); |
| 197 | + |
| 198 | + factoryControl.verify(); |
| 199 | + managerControl.verify(); |
| 200 | + factoryControl2.verify(); |
| 201 | + managerControl2.verify(); |
| 202 | + |
| 203 | + wac.close(); |
| 204 | + } |
186 | 205 |
|
187 | 206 | }
|
0 commit comments