KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > authenticator > UserPasswordAuthenticatorTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * UserPasswordAuthenticatorTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.interceptor.authenticator;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.util.concurrent.ConcurrentHashMap JavaDoc;
30 import java.util.concurrent.ConcurrentMap JavaDoc;
31
32 // unit test
33
import junit.framework.*;
34
35 // logging
36
import org.apache.log4j.Logger;
37
38 // coadunation
39
import com.rift.coad.lib.configuration.Configuration;
40 import com.rift.coad.lib.configuration.ConfigurationFactory;
41 import com.rift.coad.lib.interceptor.credentials.Credential;
42 import com.rift.coad.lib.interceptor.InterceptorAuthenticator;
43 import com.rift.coad.lib.interceptor.InterceptorException;
44 import com.rift.coad.lib.interceptor.InterceptorFactory;
45 import com.rift.coad.lib.interceptor.InterceptorWrapper;
46 import com.rift.coad.lib.interceptor.ServerInterceptor;
47 import com.rift.coad.lib.interceptor.credentials.Credential;
48 import com.rift.coad.lib.interceptor.credentials.Login;
49 import com.rift.coad.lib.interceptor.credentials.Session;
50 import com.rift.coad.lib.interceptor.authenticator.SessionAuthenticator;
51 import com.rift.coad.lib.security.AuthorizationException;
52 import com.rift.coad.lib.security.RoleManager;
53 import com.rift.coad.lib.security.ThreadsPermissionContainer;
54 import com.rift.coad.lib.security.ThreadPermissionSession;
55 import com.rift.coad.lib.security.UserSession;
56 import com.rift.coad.lib.security.user.UserSessionManager;
57 import com.rift.coad.lib.security.user.UserStoreManager;
58 import com.rift.coad.lib.security.SessionManager;
59 import com.rift.coad.lib.security.login.LoginManager;
60 import com.rift.coad.lib.thread.CoadunationThreadGroup;
61
62
63 /**
64  * Test the user password authentication
65  *
66  * @author Brett Chaldecott
67  */

68 public class UserPasswordAuthenticatorTest extends TestCase {
69     
70     /**
71      * This class is responsible for supplying access to the Server
72      * interceptor methods
73      */

74     public class TestInterceptor extends InterceptorWrapper {
75         
76         // private member variables
77
private ServerInterceptor interceptor = null;
78         
79         
80         /**
81          * The constructor of the test interceptor.
82          */

83         public TestInterceptor() throws Exception JavaDoc {
84             interceptor = this.getServerInterceptor();
85         }
86         
87         
88         /**
89          * This method creates a new session using the credentials passed in.
90          *
91          * @param credential The credentials to create a new session for.
92          * @exception Exception
93          */

94         public void createSession(Credential credential) throws Exception JavaDoc {
95             interceptor.createSession(credential);
96         }
97         
98         
99         /**
100          * This method releases this thread, removing all user information from it.
101          *
102          * @exception InterceptorException
103          */

104         public void release() throws InterceptorException {
105             interceptor.release();
106         }
107         
108     }
109     
110     public UserPasswordAuthenticatorTest(String JavaDoc testName) {
111         super(testName);
112     }
113
114     protected void setUp() throws Exception JavaDoc {
115     }
116
117     protected void tearDown() throws Exception JavaDoc {
118     }
119
120     public static Test suite() {
121         TestSuite suite = new TestSuite(UserPasswordAuthenticatorTest.class);
122         
123         return suite;
124     }
125
126     /**
127      * Test of authenticate method, of class com.rift.coad.lib.interceptor.authenticator.UserPasswordAuthenticator.
128      */

129     public void testAuthenticate() throws Exception JavaDoc {
130         System.out.println("authenticate");
131         
132         // init the session information
133
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
134         SessionManager.init(permissions);
135         UserStoreManager userStoreManager = new UserStoreManager();
136         UserSessionManager sessionManager = new UserSessionManager(permissions,
137                 userStoreManager);
138         LoginManager.init(sessionManager,userStoreManager);
139         // instanciate the thread manager
140
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
141             userStoreManager);
142         
143         // add a user to the session for the current thread
144
RoleManager.getInstance();
145         
146         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
147         
148         TestInterceptor testInterceptor = new TestInterceptor();
149         
150         Login login = new Login("test","112233");
151         
152         testInterceptor.createSession(login);
153         
154         ThreadPermissionSession threadSession = permissions.getSession();
155         
156         if (!threadSession.getUser().getName().equals("test")) {
157             fail("The user name is not test");
158         }
159         
160         testInterceptor.release();
161     }
162     
163 }
164
Popular Tags