001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * Steven Grimm <koreth[remove] at midwinter dot com>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: TestPurgingDatabaseSessions.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication.sessionmanagers;
010:
011: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
012: import com.uwyn.rife.database.Datasource;
013: import com.uwyn.rife.ioc.HierarchicalProperties;
014: import com.uwyn.rife.tools.ExceptionUtils;
015: import junit.framework.TestCase;
016:
017: public class TestPurgingDatabaseSessions extends TestCase {
018: private Datasource mDatasource = null;
019: private HierarchicalProperties mProperties = null;
020:
021: public TestPurgingDatabaseSessions(Datasource datasource,
022: String datasourceName, String name) {
023: super (name);
024: mDatasource = datasource;
025: mProperties = new HierarchicalProperties();
026: mProperties.put("datasource", mDatasource);
027: }
028:
029: public void testInstantiation() {
030: PurgingSessionManager manager = new PurgingSessionManager(
031: new DatabaseSessionsFactory().getManager(mProperties));
032: assertNotNull(manager);
033: }
034:
035: public void testStartSession() {
036: PurgingSessionManager sessions = new PurgingSessionManager(
037: new DatabaseSessionsFactory().getManager(mProperties));
038: sessions.setSessionPurgeFrequency(0);
039:
040: int user_id = 143;
041: String host_ip = "189.38.987.43";
042:
043: String auth_id = null;
044: try {
045: ((DatabaseSessions) sessions.getSessionManager()).install();
046:
047: auth_id = sessions.startSession(user_id, host_ip, false);
048:
049: assertEquals(1, sessions.countSessions());
050:
051: assertNotNull(auth_id);
052: assertTrue(auth_id.length() > 0);
053: } catch (SessionManagerException e) {
054: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
055: } finally {
056: try {
057: ((DatabaseSessions) sessions.getSessionManager())
058: .remove();
059: } catch (SessionManagerException e) {
060: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
061: false);
062: }
063: }
064: }
065:
066: public void testPurgeSessions() {
067: PurgingSessionManager sessions = new PurgingSessionManager(
068: new DatabaseSessionsFactory().getManager(mProperties));
069: sessions.setSessionDuration(2000);
070: sessions.setSessionPurgeFrequency(1);
071: sessions.setSessionPurgeScale(1);
072:
073: int user_id = 9478;
074: String host_ip = "98.232.12.456";
075:
076: try {
077: ((DatabaseSessions) sessions.getSessionManager()).install();
078:
079: sessions.eraseAllSessions();
080: assertEquals(0, sessions.countSessions());
081:
082: sessions.startSession(user_id, host_ip, false);
083: assertEquals(1, sessions.countSessions());
084:
085: Thread.sleep(2010);
086:
087: sessions.startSession(user_id, host_ip, false);
088: assertEquals(1, sessions.countSessions());
089: } catch (InterruptedException e) {
090: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
091: } catch (SessionManagerException e) {
092: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
093: } finally {
094: try {
095: ((DatabaseSessions) sessions.getSessionManager())
096: .remove();
097: } catch (SessionManagerException e) {
098: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
099: false);
100: }
101: }
102: }
103: }
|