001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * UserSessionManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.security.user;
025:
026: import java.util.HashSet;
027: import junit.framework.*;
028: import com.rift.coad.lib.security.ThreadsPermissionContainer;
029: import com.rift.coad.lib.security.ThreadPermissionSession;
030: import com.rift.coad.lib.security.UserSession;
031:
032: /**
033: * The definition of the user session manager class.
034: *
035: * @author Brett Chaldecott
036: */
037: public class UserSessionManagerTest extends TestCase {
038:
039: public UserSessionManagerTest(String testName) {
040: super (testName);
041: }
042:
043: protected void setUp() throws Exception {
044: }
045:
046: protected void tearDown() throws Exception {
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(UserSessionManagerTest.class);
051:
052: return suite;
053: }
054:
055: /**
056: * Test of initSessionForUser method, of class com.rift.coad.lib.security.user.UserSessionManager.
057: */
058: public void testInitSessionForUser() throws Exception {
059: System.out.println("initSessionForUser");
060:
061: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
062: UserStoreManager userStoreManager = new UserStoreManager();
063: UserSessionManager instance = new UserSessionManager(
064: permissions, userStoreManager);
065: instance.startCleanup();
066: // attempt to init the user
067: instance.initSessionForUser("test1");
068:
069: // create a test user
070: UserSession user = new UserSession("test2", new HashSet());
071: user.setExpiryTime(500);
072: instance.initSessionForUser(user);
073: UserSession user2 = new UserSession("test2", new HashSet());
074: user2.setExpiryTime(500);
075: instance.initSessionForUser(user2);
076:
077: // retrieve the session via user
078: if (user != instance.getSessionById(user.getSessionId())) {
079: fail("The user session could not be found by id.");
080: }
081: if (user2 != instance.getSessionById(user2.getSessionId())) {
082: fail("The user 2 session could not be found by id.");
083: }
084:
085: for (int count = 0; count < 4; count++) {
086: Thread.sleep(450);
087: user2.touch();
088: }
089:
090: try {
091: instance.getSessionById(user.getSessionId());
092: fail("The user session could be found.");
093: } catch (UserException ex) {
094: // ignore
095: }
096: if (user2 != instance.getSessionById(user2.getSessionId())) {
097: fail("The user 2 session could be found.");
098: }
099:
100: instance.shutdown();
101: }
102:
103: }
|