1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.concurrent;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.ui.session.HttpSessionDestroyedEvent;
21
22 import org.springframework.mock.web.MockHttpSession;
23
24 import java.util.Date;
25
26
27 /***
28 * Tests {@link SessionRegistryImpl}.
29 *
30 * @author Ben Alex
31 * @version $Id: SessionRegistryImplTests.java,v 1.2 2005/11/17 00:55:48 benalex Exp $
32 */
33 public class SessionRegistryImplTests extends TestCase {
34
35
36 public void testEventPublishing() {
37 MockHttpSession httpSession = new MockHttpSession();
38 Object principal = "Some principal object";
39 String sessionId = httpSession.getId();
40 assertNotNull(sessionId);
41
42 SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
43
44
45 sessionRegistry.registerNewSession(sessionId, principal);
46
47
48 sessionRegistry.onApplicationEvent(new HttpSessionDestroyedEvent(
49 httpSession));
50
51
52 assertNull(sessionRegistry.getSessionInformation(sessionId));
53 }
54
55 public void testSessionInformationLifecycle() throws Exception {
56 Object principal = "Some principal object";
57 String sessionId = "1234567890";
58 SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
59
60
61 sessionRegistry.registerNewSession(sessionId, principal);
62
63
64 Date currentDateTime = sessionRegistry.getSessionInformation(sessionId)
65 .getLastRequest();
66 assertEquals(principal,
67 sessionRegistry.getSessionInformation(sessionId).getPrincipal());
68 assertEquals(sessionId,
69 sessionRegistry.getSessionInformation(sessionId).getSessionId());
70 assertNotNull(sessionRegistry.getSessionInformation(sessionId)
71 .getLastRequest());
72
73
74 assertEquals(1, sessionRegistry.getAllSessions(principal).length);
75
76
77 Thread.sleep(1000);
78
79
80 sessionRegistry.refreshLastRequest(sessionId);
81
82 Date retrieved = sessionRegistry.getSessionInformation(sessionId)
83 .getLastRequest();
84 assertTrue(retrieved.after(currentDateTime));
85
86
87 assertEquals(retrieved,
88 sessionRegistry.getAllSessions(principal)[0].getLastRequest());
89
90
91 sessionRegistry.removeSessionInformation(sessionId);
92
93
94 assertNull(sessionRegistry.getSessionInformation(sessionId));
95 assertNull(sessionRegistry.getAllSessions(principal));
96 }
97
98 public void testTwoSessionsOnePrincipalHandling() throws Exception {
99 Object principal = "Some principal object";
100 String sessionId1 = "1234567890";
101 String sessionId2 = "9876543210";
102 SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
103
104
105 sessionRegistry.registerNewSession(sessionId1, principal);
106 assertEquals(1, sessionRegistry.getAllSessions(principal).length);
107 assertEquals(sessionId1,
108 sessionRegistry.getAllSessions(principal)[0].getSessionId());
109
110
111 sessionRegistry.registerNewSession(sessionId2, principal);
112 assertEquals(2, sessionRegistry.getAllSessions(principal).length);
113 assertEquals(sessionId2,
114 sessionRegistry.getAllSessions(principal)[1].getSessionId());
115
116
117 sessionRegistry.removeSessionInformation(sessionId1);
118 assertEquals(1, sessionRegistry.getAllSessions(principal).length);
119 assertEquals(sessionId2,
120 sessionRegistry.getAllSessions(principal)[0].getSessionId());
121
122
123 sessionRegistry.removeSessionInformation(sessionId2);
124 assertNull(sessionRegistry.getSessionInformation(sessionId2));
125 assertNull(sessionRegistry.getAllSessions(principal));
126 }
127
128 public void testTwoSessionsOnePrincipalExpiring() throws Exception {
129 Object principal = "Some principal object";
130 String sessionId1 = "1234567890";
131 String sessionId2 = "9876543210";
132 SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
133
134
135 sessionRegistry.registerNewSession(sessionId1, principal);
136 assertEquals(1, sessionRegistry.getAllSessions(principal).length);
137 assertEquals(sessionId1,
138 sessionRegistry.getAllSessions(principal)[0].getSessionId());
139
140
141 sessionRegistry.registerNewSession(sessionId2, principal);
142 assertEquals(2, sessionRegistry.getAllSessions(principal).length);
143 assertEquals(sessionId2,
144 sessionRegistry.getAllSessions(principal)[1].getSessionId());
145
146
147 SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
148 session.expireNow();
149
150
151 assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
152 assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
153 }
154
155 }