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 .hibernate3 .support ;
18
18
19
- import junit .framework .TestCase ;
20
-
21
- import org .easymock .MockControl ;
22
- import org .hibernate .classic .Session ;
23
- import org .hibernate .SessionFactory ;
24
-
25
19
import java .io .IOException ;
26
20
import java .sql .Connection ;
27
-
28
21
import javax .servlet .FilterChain ;
29
22
import javax .servlet .ServletException ;
30
23
import javax .servlet .ServletRequest ;
31
24
import javax .servlet .ServletResponse ;
32
25
import javax .transaction .TransactionManager ;
33
26
34
-
27
+ import junit .framework .TestCase ;
28
+ import org .easymock .MockControl ;
35
29
import org .hibernate .FlushMode ;
36
30
import org .hibernate .HibernateException ;
31
+ import org .hibernate .SessionFactory ;
37
32
import org .hibernate .Transaction ;
33
+ import org .hibernate .classic .Session ;
38
34
import org .hibernate .engine .SessionFactoryImplementor ;
39
35
40
36
import org .springframework .mock .web .MockFilterConfig ;
50
46
import org .springframework .transaction .support .DefaultTransactionDefinition ;
51
47
import org .springframework .transaction .support .TransactionSynchronizationManager ;
52
48
import org .springframework .web .context .WebApplicationContext ;
49
+ import org .springframework .web .context .request .ServletWebRequest ;
53
50
import org .springframework .web .context .support .StaticWebApplicationContext ;
54
- import org .springframework .web .servlet .HandlerInterceptor ;
55
- import org .springframework .web .servlet .handler .WebRequestHandlerInterceptorAdapter ;
56
51
57
52
/**
58
53
* @author Juergen Hoeller
@@ -70,16 +65,12 @@ public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception
70
65
MockControl sessionControl = MockControl .createControl (Session .class );
71
66
Session session = (Session ) sessionControl .getMock ();
72
67
73
-
74
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
75
- rawInterceptor .setSessionFactory (sf );
76
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
68
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
69
+ interceptor .setSessionFactory (sf );
77
70
78
71
MockServletContext sc = new MockServletContext ();
79
72
MockHttpServletRequest request = new MockHttpServletRequest (sc );
80
- MockHttpServletResponse response = new MockHttpServletResponse ();
81
73
82
-
83
74
//expect(mockStorage.size()).andReturn(expectedValue);
84
75
85
76
//expect(sf.openSession()).andReturn(session);
@@ -93,24 +84,24 @@ public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception
93
84
sessionControl .setVoidCallable (1 );
94
85
sfControl .replay ();
95
86
sessionControl .replay ();
96
- interceptor .preHandle (request , response , "handler" );
87
+ interceptor .preHandle (new ServletWebRequest ( request ) );
97
88
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
98
89
99
90
// check that further invocations simply participate
100
- interceptor .preHandle (request , response , "handler" );
91
+ interceptor .preHandle (new ServletWebRequest ( request ) );
101
92
102
93
assertEquals (session , SessionFactoryUtils .getSession (sf , false ));
103
94
104
- interceptor .preHandle (request , response , "handler" );
105
- interceptor .postHandle (request , response , "handler" , null );
106
- interceptor .afterCompletion (request , response , "handler" , null );
95
+ interceptor .preHandle (new ServletWebRequest ( request ) );
96
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
97
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
107
98
108
- interceptor .postHandle (request , response , "handler" , null );
109
- interceptor .afterCompletion (request , response , "handler" , null );
99
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
100
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
110
101
111
- interceptor .preHandle (request , response , "handler" );
112
- interceptor .postHandle (request , response , "handler" , null );
113
- interceptor .afterCompletion (request , response , "handler" , null );
102
+ interceptor .preHandle (new ServletWebRequest ( request ) );
103
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
104
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
114
105
115
106
sfControl .verify ();
116
107
sessionControl .verify ();
@@ -119,7 +110,7 @@ public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception
119
110
sessionControl .reset ();
120
111
sfControl .replay ();
121
112
sessionControl .replay ();
122
- interceptor .postHandle (request , response , "handler" , null );
113
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
123
114
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
124
115
sfControl .verify ();
125
116
sessionControl .verify ();
@@ -130,7 +121,7 @@ public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception
130
121
sessionControl .setReturnValue (null , 1 );
131
122
sfControl .replay ();
132
123
sessionControl .replay ();
133
- interceptor .afterCompletion (request , response , "handler" , null );
124
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
134
125
assertFalse (TransactionSynchronizationManager .hasResource (sf ));
135
126
sfControl .verify ();
136
127
sessionControl .verify ();
@@ -147,9 +138,8 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws E
147
138
tm .getTransaction ();
148
139
tmControl .setReturnValue (null , 2 );
149
140
150
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
151
- rawInterceptor .setSessionFactory (sf );
152
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
141
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
142
+ interceptor .setSessionFactory (sf );
153
143
154
144
MockServletContext sc = new MockServletContext ();
155
145
MockHttpServletRequest request = new MockHttpServletRequest (sc );
@@ -168,24 +158,24 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws E
168
158
sfControl .replay ();
169
159
sessionControl .replay ();
170
160
171
- interceptor .preHandle (request , response , "handler" );
161
+ interceptor .preHandle (new ServletWebRequest ( request ) );
172
162
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
173
163
174
164
// check that further invocations simply participate
175
- interceptor .preHandle (request , response , "handler" );
165
+ interceptor .preHandle (new ServletWebRequest ( request ) );
176
166
177
167
assertEquals (session , SessionFactoryUtils .getSession (sf , false ));
178
168
179
- interceptor .preHandle (request , response , "handler" );
180
- interceptor .postHandle (request , response , "handler" , null );
181
- interceptor .afterCompletion (request , response , "handler" , null );
169
+ interceptor .preHandle (new ServletWebRequest ( request ) );
170
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
171
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
182
172
183
- interceptor .postHandle (request , response , "handler" , null );
184
- interceptor .afterCompletion (request , response , "handler" , null );
173
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
174
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
185
175
186
- interceptor .preHandle (request , response , "handler" );
187
- interceptor .postHandle (request , response , "handler" , null );
188
- interceptor .afterCompletion (request , response , "handler" , null );
176
+ interceptor .preHandle (new ServletWebRequest ( request ) );
177
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
178
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
189
179
190
180
sfControl .verify ();
191
181
sessionControl .verify ();
@@ -194,7 +184,7 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws E
194
184
sessionControl .reset ();
195
185
sfControl .replay ();
196
186
sessionControl .replay ();
197
- interceptor .postHandle (request , response , "handler" , null );
187
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
198
188
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
199
189
sfControl .verify ();
200
190
sessionControl .verify ();
@@ -205,7 +195,7 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws E
205
195
sessionControl .setReturnValue (null , 1 );
206
196
sfControl .replay ();
207
197
sessionControl .replay ();
208
- interceptor .afterCompletion (request , response , "handler" , null );
198
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
209
199
assertFalse (TransactionSynchronizationManager .hasResource (sf ));
210
200
sfControl .verify ();
211
201
sessionControl .verify ();
@@ -217,10 +207,9 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws E
217
207
MockControl sessionControl = MockControl .createControl (Session .class );
218
208
Session session = (Session ) sessionControl .getMock ();
219
209
220
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
221
- rawInterceptor .setSessionFactory (sf );
222
- rawInterceptor .setFlushMode (HibernateAccessor .FLUSH_AUTO );
223
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
210
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
211
+ interceptor .setSessionFactory (sf );
212
+ interceptor .setFlushMode (HibernateAccessor .FLUSH_AUTO );
224
213
225
214
MockServletContext sc = new MockServletContext ();
226
215
MockHttpServletRequest request = new MockHttpServletRequest (sc );
@@ -232,7 +221,7 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws E
232
221
sessionControl .setReturnValue (sf );
233
222
sfControl .replay ();
234
223
sessionControl .replay ();
235
- interceptor .preHandle (request , response , "handler" );
224
+ interceptor .preHandle (new ServletWebRequest ( request ) );
236
225
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
237
226
sfControl .verify ();
238
227
sessionControl .verify ();
@@ -243,7 +232,7 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws E
243
232
sessionControl .setVoidCallable (1 );
244
233
sfControl .replay ();
245
234
sessionControl .replay ();
246
- interceptor .postHandle (request , response , "handler" , null );
235
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
247
236
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
248
237
sfControl .verify ();
249
238
sessionControl .verify ();
@@ -254,7 +243,7 @@ public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws E
254
243
sessionControl .setReturnValue (null , 1 );
255
244
sfControl .replay ();
256
245
sessionControl .replay ();
257
- interceptor .afterCompletion (request , response , "handler" , null );
246
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
258
247
assertFalse (TransactionSynchronizationManager .hasResource (sf ));
259
248
sfControl .verify ();
260
249
sessionControl .verify ();
@@ -266,10 +255,9 @@ public void testOpenSessionInViewInterceptorAndDeferredClose() throws Exception
266
255
MockControl sessionControl = MockControl .createControl (Session .class );
267
256
Session session = (Session ) sessionControl .getMock ();
268
257
269
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
270
- rawInterceptor .setSessionFactory (sf );
271
- rawInterceptor .setSingleSession (false );
272
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
258
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
259
+ interceptor .setSessionFactory (sf );
260
+ interceptor .setSingleSession (false );
273
261
274
262
MockServletContext sc = new MockServletContext ();
275
263
MockHttpServletRequest request = new MockHttpServletRequest (sc );
@@ -284,23 +272,23 @@ public void testOpenSessionInViewInterceptorAndDeferredClose() throws Exception
284
272
sfControl .replay ();
285
273
sessionControl .replay ();
286
274
287
- interceptor .preHandle (request , response , "handler" );
275
+ interceptor .preHandle (new ServletWebRequest ( request ) );
288
276
org .hibernate .Session sess = SessionFactoryUtils .getSession (sf , true );
289
277
SessionFactoryUtils .releaseSession (sess , sf );
290
278
291
279
// check that further invocations simply participate
292
- interceptor .preHandle (request , response , "handler" );
280
+ interceptor .preHandle (new ServletWebRequest ( request ) );
293
281
294
- interceptor .preHandle (request , response , "handler" );
295
- interceptor .postHandle (request , response , "handler" , null );
296
- interceptor .afterCompletion (request , response , "handler" , null );
282
+ interceptor .preHandle (new ServletWebRequest ( request ) );
283
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
284
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
297
285
298
- interceptor .postHandle (request , response , "handler" , null );
299
- interceptor .afterCompletion (request , response , "handler" , null );
286
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
287
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
300
288
301
- interceptor .preHandle (request , response , "handler" );
302
- interceptor .postHandle (request , response , "handler" , null );
303
- interceptor .afterCompletion (request , response , "handler" , null );
289
+ interceptor .preHandle (new ServletWebRequest ( request ) );
290
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
291
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
304
292
305
293
sfControl .verify ();
306
294
sessionControl .verify ();
@@ -312,8 +300,8 @@ public void testOpenSessionInViewInterceptorAndDeferredClose() throws Exception
312
300
sfControl .replay ();
313
301
sessionControl .replay ();
314
302
315
- interceptor .postHandle (request , response , "handler" , null );
316
- interceptor .afterCompletion (request , response , "handler" , null );
303
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
304
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
317
305
sfControl .verify ();
318
306
sessionControl .verify ();
319
307
}
@@ -433,11 +421,10 @@ public void testOpenSessionInViewFilterWithSingleSessionAndPreBoundSession() thr
433
421
MockFilterConfig filterConfig2 = new MockFilterConfig (wac .getServletContext (), "filter2" );
434
422
filterConfig2 .addInitParameter ("sessionFactoryBeanName" , "mySessionFactory" );
435
423
436
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
437
- rawInterceptor .setSessionFactory (sf );
438
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
424
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
425
+ interceptor .setSessionFactory (sf );
439
426
440
- interceptor .preHandle (request , response , "handler" );
427
+ interceptor .preHandle (new ServletWebRequest ( request ) );
441
428
442
429
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter ();
443
430
filter .init (filterConfig );
@@ -454,8 +441,8 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
454
441
assertTrue (TransactionSynchronizationManager .hasResource (sf ));
455
442
assertNotNull (request .getAttribute ("invoked" ));
456
443
457
- interceptor .postHandle (request , response , "handler" , null );
458
- interceptor .afterCompletion (request , response , "handler" , null );
444
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
445
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
459
446
460
447
sfControl .verify ();
461
448
sessionControl .verify ();
@@ -616,12 +603,11 @@ public void testOpenSessionInViewFilterWithDeferredCloseAndAlreadyActiveDeferred
616
603
filterConfig2 .addInitParameter ("singleSession" , "false" );
617
604
filterConfig2 .addInitParameter ("sessionFactoryBeanName" , "mySessionFactory" );
618
605
619
- OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor ();
620
- rawInterceptor .setSessionFactory (sf );
621
- rawInterceptor .setSingleSession (false );
622
- HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter (rawInterceptor );
606
+ OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor ();
607
+ interceptor .setSessionFactory (sf );
608
+ interceptor .setSingleSession (false );
623
609
624
- interceptor .preHandle (request , response , "handler" );
610
+ interceptor .preHandle (new ServletWebRequest ( request ) );
625
611
626
612
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter ();
627
613
filter .init (filterConfig );
@@ -661,8 +647,8 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
661
647
filter .doFilter (request , response , filterChain2 );
662
648
assertNotNull (request .getAttribute ("invoked" ));
663
649
664
- interceptor .postHandle (request , response , "handler" , null );
665
- interceptor .afterCompletion (request , response , "handler" , null );
650
+ interceptor .postHandle (new ServletWebRequest ( request ) , null );
651
+ interceptor .afterCompletion (new ServletWebRequest ( request ) , null );
666
652
667
653
sfControl .verify ();
668
654
sessionControl .verify ();
0 commit comments