58
58
import static org .hamcrest .Matchers .in ;
59
59
import static org .hamcrest .Matchers .is ;
60
60
import static org .junit .jupiter .api .Assertions .assertEquals ;
61
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
61
62
import static org .junit .jupiter .api .Assertions .assertNotEquals ;
62
63
import static org .junit .jupiter .api .Assertions .assertNotNull ;
63
64
import static org .junit .jupiter .api .Assertions .assertNull ;
@@ -92,7 +93,7 @@ public void testListenerWithInvalidation() throws Exception
92
93
TestServer server = new TestServer (0 , inactivePeriod , scavengePeriod ,
93
94
cacheFactory , storeFactory );
94
95
ServletContextHandler context = server .addContext (contextPath );
95
- TestHttpSessionListener listener = new TestHttpSessionListener (true );
96
+ TestHttpSessionListener listener = new TestHttpSessionListener (true , true );
96
97
context .getSessionHandler ().addEventListener (listener );
97
98
TestServlet servlet = new TestServlet ();
98
99
ServletHolder holder = new ServletHolder (servlet );
@@ -136,6 +137,72 @@ public void testListenerWithInvalidation() throws Exception
136
137
LifeCycle .stop (server );
137
138
}
138
139
}
140
+
141
+ /**
142
+ * Test that if a session listener throws an exception during sessionDestroyed the session is still invalidated
143
+ */
144
+ @ Test
145
+ public void testListenerWithInvalidationException () throws Exception
146
+ {
147
+ String contextPath = "" ;
148
+ String servletMapping = "/server" ;
149
+ int inactivePeriod = 6 ;
150
+ int scavengePeriod = -1 ;
151
+
152
+ DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory ();
153
+ cacheFactory .setEvictionPolicy (SessionCache .NEVER_EVICT );
154
+ TestSessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory ();
155
+ storeFactory .setGracePeriodSec (scavengePeriod );
156
+
157
+ TestServer server = new TestServer (0 , inactivePeriod , scavengePeriod ,
158
+ cacheFactory , storeFactory );
159
+ ServletContextHandler context = server .addContext (contextPath );
160
+ ThrowingSessionListener listener = new ThrowingSessionListener ();
161
+ context .getSessionHandler ().addEventListener (listener );
162
+ TestServlet servlet = new TestServlet ();
163
+ ServletHolder holder = new ServletHolder (servlet );
164
+ context .addServlet (holder , servletMapping );
165
+
166
+ try
167
+ {
168
+ server .start ();
169
+ int port1 = server .getPort ();
170
+
171
+ HttpClient client = new HttpClient ();
172
+ client .start ();
173
+ try
174
+ {
175
+ String url = "http://localhost:" + port1 + contextPath + servletMapping ;
176
+ // Create the session
177
+ ContentResponse response1 = client .GET (url + "?action=init" );
178
+ assertEquals (HttpServletResponse .SC_OK , response1 .getStatus ());
179
+ String sessionCookie = response1 .getHeaders ().get ("Set-Cookie" );
180
+ assertNotNull (sessionCookie );
181
+ assertTrue (TestServlet .bindingListener .bound );
182
+
183
+ String sessionId = TestServer .extractSessionId (sessionCookie );
184
+
185
+ // Make a request which will invalidate the existing session
186
+ Request request2 = client .newRequest (url + "?action=test" );
187
+ ContentResponse response2 = request2 .send ();
188
+ assertEquals (HttpServletResponse .SC_OK , response2 .getStatus ());
189
+
190
+ assertTrue (TestServlet .bindingListener .unbound );
191
+
192
+ //check session no longer exists
193
+ assertFalse (context .getSessionHandler ().getSessionCache ().contains (sessionId ));
194
+ assertFalse (context .getSessionHandler ().getSessionCache ().getSessionDataStore ().exists (sessionId ));
195
+ }
196
+ finally
197
+ {
198
+ LifeCycle .stop (client );
199
+ }
200
+ }
201
+ finally
202
+ {
203
+ LifeCycle .stop (server );
204
+ }
205
+ }
139
206
140
207
/**
141
208
* Test that listeners are called when a session expires
@@ -177,7 +244,7 @@ public void testSessionExpiresWithListener() throws Exception
177
244
ServletContextHandler context = server1 .addContext (contextPath );
178
245
context .setClassLoader (contextClassLoader );
179
246
context .addServlet (holder , servletMapping );
180
- TestHttpSessionListener listener = new TestHttpSessionListenerWithWebappClasses (true );
247
+ TestHttpSessionListener listener = new TestHttpSessionListenerWithWebappClasses (true , true );
181
248
context .getSessionHandler ().addEventListener (listener );
182
249
183
250
try
@@ -206,7 +273,8 @@ public void testSessionExpiresWithListener() throws Exception
206
273
207
274
assertThat (sessionId , is (in (listener .destroyedSessions )));
208
275
209
- assertNull (listener .ex );
276
+ assertNull (listener .attributeException );
277
+ assertNull (listener .accessTimeException );
210
278
}
211
279
finally
212
280
{
@@ -241,7 +309,7 @@ public void testExpiredSession() throws Exception
241
309
ServletHolder holder = new ServletHolder (servlet );
242
310
ServletContextHandler context = server1 .addContext (contextPath );
243
311
context .addServlet (holder , servletMapping );
244
- TestHttpSessionListener listener = new TestHttpSessionListener ();
312
+ TestHttpSessionListener listener = new TestHttpSessionListener (true , true );
245
313
246
314
context .getSessionHandler ().addEventListener (listener );
247
315
@@ -276,7 +344,8 @@ public void testExpiredSession() throws Exception
276
344
277
345
assertTrue (listener .destroyedSessions .contains ("1234" ));
278
346
279
- assertNull (listener .ex );
347
+ assertNull (listener .attributeException );
348
+ assertNull (listener .accessTimeException );
280
349
}
281
350
finally
282
351
{
@@ -301,6 +370,22 @@ public void sessionDestroyed(HttpSessionEvent se)
301
370
{
302
371
}
303
372
}
373
+
374
+ public static class ThrowingSessionListener implements HttpSessionListener
375
+ {
376
+
377
+ @ Override
378
+ public void sessionCreated (HttpSessionEvent se )
379
+ {
380
+ }
381
+
382
+ @ Override
383
+ public void sessionDestroyed (HttpSessionEvent se )
384
+ {
385
+ throw new IllegalStateException ("Exception during sessionDestroyed" );
386
+ }
387
+
388
+ }
304
389
305
390
@ Test
306
391
public void testSessionListeners ()
0 commit comments