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: TestPurgingDatabaseRemember.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication.remembermanagers;
010:
011: import com.uwyn.rife.authentication.exceptions.RememberManagerException;
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 TestPurgingDatabaseRemember extends TestCase {
018: private Datasource mDatasource = null;
019: private HierarchicalProperties mProperties = null;
020:
021: public TestPurgingDatabaseRemember(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: DatabaseRemember manager = new DatabaseRememberFactory()
031: .getRememberManager(mProperties);
032: assertNotNull(manager);
033: }
034:
035: public void testStartSession() {
036: PurgingRememberManager remember = new PurgingRememberManager(
037: new DatabaseRememberFactory()
038: .getRememberManager(mProperties));
039: remember.setRememberPurgeFrequency(0);
040:
041: int user_id = 143;
042:
043: String remember_id = null;
044: try {
045: ((DatabaseRemember) remember.getRememberManager())
046: .install();
047:
048: remember_id = remember.createRememberId(user_id,
049: "123.98.23.3");
050:
051: assertNotNull(remember_id);
052: assertTrue(remember_id.length() > 0);
053:
054: assertEquals(user_id, remember
055: .getRememberedUserId(remember_id));
056: } catch (RememberManagerException e) {
057: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
058: } finally {
059: try {
060: ((DatabaseRemember) remember.getRememberManager())
061: .remove();
062: } catch (RememberManagerException e) {
063: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
064: false);
065: }
066: }
067: }
068:
069: public void testPurgeRemember() {
070: PurgingRememberManager remember = new PurgingRememberManager(
071: new DatabaseRememberFactory()
072: .getRememberManager(mProperties));
073: remember.setRememberDuration(2000);
074: remember.setRememberPurgeFrequency(1);
075: remember.setRememberPurgeScale(1);
076:
077: try {
078: ((DatabaseRemember) remember.getRememberManager())
079: .install();
080:
081: remember.eraseAllRememberIds();
082:
083: int user_id1 = 143;
084: String remember_id1 = remember.createRememberId(user_id1,
085: "123.98.23.3");
086:
087: assertEquals(user_id1, remember
088: .getRememberedUserId(remember_id1));
089:
090: Thread.sleep(2000);
091:
092: int user_id2 = 143;
093: String remember_id2 = remember.createRememberId(user_id2,
094: "123.98.23.39");
095:
096: assertEquals(user_id2, remember
097: .getRememberedUserId(remember_id2));
098: assertEquals(-1, remember.getRememberedUserId(remember_id1));
099: } catch (InterruptedException e) {
100: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
101: } catch (RememberManagerException e) {
102: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
103: } finally {
104: try {
105: ((DatabaseRemember) remember.getRememberManager())
106: .remove();
107: } catch (RememberManagerException e) {
108: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
109: false);
110: }
111: }
112: }
113: }
|