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: * SessionLoginTest.java
020: *
021: * JUnit based test
022: */
023:
024: // package path
025: package com.rift.coad.lib.security.login;
026:
027: import junit.framework.*;
028:
029: // java imports
030: import org.apache.log4j.BasicConfigurator;
031:
032: // coadunation imports
033: import com.rift.coad.lib.security.ThreadsPermissionContainer;
034: import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
035: import com.rift.coad.lib.security.user.UserStoreManager;
036: import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
037: import com.rift.coad.lib.security.user.UserSessionManager;
038: import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
039: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
040: import com.rift.coad.lib.security.SessionManager;
041: import com.rift.coad.lib.security.RoleManager;
042: import com.rift.coad.lib.security.Validator;
043: import com.rift.coad.lib.security.UserSession;
044: import com.rift.coad.lib.thread.BasicThread;
045: import com.rift.coad.lib.thread.CoadunationThreadGroup;
046: import com.rift.coad.lib.security.sudo.Sudo;
047: import com.rift.coad.lib.security.sudo.SudoCallbackHandler;
048:
049: /**
050: * The class that tests the session login
051: *
052: * @author Brett Chaldecott
053: */
054: public class SessionLoginTest extends TestCase implements
055: SudoCallbackHandler {
056:
057: /**
058: * This object will sudo by user
059: */
060: public class SudoBySessionIdTest extends BasicThread {
061:
062: private String sessionId = null;
063: private SudoCallbackHandler handler = null;
064:
065: /**
066: * The constructor of the handler
067: */
068: public SudoBySessionIdTest(String sessionId,
069: SudoCallbackHandler handler) throws Exception {
070: this .sessionId = sessionId;
071: this .handler = handler;
072: }
073:
074: /**
075: * This method will process the result
076: */
077: public void process() {
078: try {
079: Sudo.sudoThreadBySessionId(sessionId, handler);
080: } catch (Exception ex) {
081: System.out.println("Failed to process : "
082: + ex.getMessage());
083: ex.printStackTrace(System.out);
084: }
085: }
086: }
087:
088: // private member variables
089: private boolean called = false;
090:
091: public SessionLoginTest(String testName) {
092: super (testName);
093: BasicConfigurator.configure();
094: }
095:
096: protected void setUp() throws Exception {
097: }
098:
099: protected void tearDown() throws Exception {
100: }
101:
102: public static Test suite() {
103: TestSuite suite = new TestSuite(SessionLoginTest.class);
104:
105: return suite;
106: }
107:
108: /**
109: * Test of login method, of class com.rift.coad.lib.security.login.SessionLogin.
110: */
111: public void testLogin() throws Exception {
112: System.out.println("login");
113:
114: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
115: ThreadsPermissionContainerAccessor.init(permissions);
116: SessionManager.init(permissions);
117: UserStoreManager userStoreManager = new UserStoreManager();
118: UserStoreManagerAccessor.init(userStoreManager);
119: UserSessionManager sessionManager = new UserSessionManager(
120: permissions, userStoreManager);
121: UserSessionManagerAccessor.init(sessionManager);
122: LoginManager.init(sessionManager, userStoreManager);
123: // instanciate the thread manager
124: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
125: sessionManager, userStoreManager);
126:
127: // add a user to the session for the current thread
128: RoleManager.getInstance();
129:
130: SessionLogin instance = new SessionLogin(
131: new PasswordInfoHandler("test1", "112233"));
132:
133: instance.login();
134:
135: // retrieve the user object
136: UserSession user = instance.getUser();
137: if (!user.getName().equals("test1")) {
138: fail("Login failed");
139: }
140:
141: // run as a user
142: called = false;
143: SudoBySessionIdTest sudoBySessionIdTest = new SudoBySessionIdTest(
144: user.getSessionId(), this );
145: threadGroup.addThread(sudoBySessionIdTest, "test1");
146: sudoBySessionIdTest.start();
147: sudoBySessionIdTest.join();
148: if (!called) {
149: fail("Failed to call");
150: }
151: }
152:
153: /**
154: * This method is called to process
155: */
156: public void process() {
157: called = true;
158: }
159: }
|