01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
03: * Steven Grimm <koreth[remove] at midwinter dot com>
04: * Distributed under the terms of either:
05: * - the common development and distribution license (CDDL), v1.0; or
06: * - the GNU Lesser General Public License, v2.1 or later
07: * $Id: TestPurgingMemorySessions.java 3634 2007-01-08 21:42:24Z gbevin $
08: */
09: package com.uwyn.rife.authentication.sessionmanagers;
10:
11: import junit.framework.TestCase;
12:
13: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
14: import com.uwyn.rife.tools.ExceptionUtils;
15:
16: public class TestPurgingMemorySessions extends TestCase {
17: public TestPurgingMemorySessions(String name) {
18: super (name);
19: }
20:
21: public void testInstantiation() {
22: PurgingSessionManager sessions = null;
23:
24: sessions = new PurgingSessionManager(new MemorySessions());
25:
26: assertNotNull(sessions);
27: }
28:
29: public void testStartSession() {
30: PurgingSessionManager sessions = new PurgingSessionManager(
31: new MemorySessions());
32: sessions.setSessionPurgeFrequency(0);
33: try {
34: sessions.eraseAllSessions();
35: } catch (SessionManagerException e) {
36: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
37: }
38:
39: int user_id = 143;
40: String host_ip = "189.38.987.43";
41:
42: String auth_id = null;
43: try {
44: auth_id = sessions.startSession(user_id, host_ip, false);
45:
46: assertNotNull(auth_id);
47: assertTrue(auth_id.length() > 0);
48:
49: assertEquals(1, sessions.countSessions());
50: } catch (SessionManagerException e) {
51: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
52: }
53: }
54:
55: public void testPurgeSessions() {
56: PurgingSessionManager sessions = new PurgingSessionManager(
57: new MemorySessions());
58: sessions.setSessionDuration(2000);
59: sessions.setSessionPurgeFrequency(1);
60: sessions.setSessionPurgeScale(1);
61:
62: int user_id = 9478;
63: String host_ip = "98.232.12.456";
64:
65: try {
66: sessions.eraseAllSessions();
67: assertEquals(0, sessions.countSessions());
68:
69: sessions.startSession(user_id, host_ip, false);
70: assertEquals(1, sessions.countSessions());
71:
72: Thread.sleep(2010);
73:
74: sessions.startSession(user_id, host_ip, false);
75: assertEquals(1, sessions.countSessions());
76: } catch (InterruptedException e) {
77: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
78: } catch (SessionManagerException e) {
79: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
80: }
81: }
82: }
|