KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > ContextManagerTest


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  * ContextManagerTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.naming;
25
26 import junit.framework.*;
27 import org.apache.log4j.Logger;
28 import java.util.StringTokenizer JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import com.rift.coad.lib.interceptor.InterceptorFactory;
36 import com.rift.coad.lib.security.RoleManager;
37 import com.rift.coad.lib.security.ThreadsPermissionContainer;
38 import com.rift.coad.lib.security.ThreadPermissionSession;
39 import com.rift.coad.lib.security.UserSession;
40 import com.rift.coad.lib.security.user.UserSessionManager;
41 import com.rift.coad.lib.security.user.UserStoreManager;
42 import com.rift.coad.lib.security.SessionManager;
43 import com.rift.coad.lib.security.login.LoginManager;
44 import com.rift.coad.lib.thread.CoadunationThreadGroup;
45
46
47 /**
48  *
49  * @author Brett Chaldecott
50  */

51 public class ContextManagerTest extends TestCase {
52     
53     public ContextManagerTest(String JavaDoc testName) {
54         super(testName);
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58     }
59
60     protected void tearDown() throws Exception JavaDoc {
61     }
62
63     public static Test suite() {
64         TestSuite suite = new TestSuite(ContextManagerTest.class);
65         
66         return suite;
67     }
68
69     /**
70      * Test of testContextManager, of class com.rift.coad.lib.naming.ContextManager.
71      */

72     public void testContextManager() throws Exception JavaDoc {
73         System.out.println("testContextManager");
74         
75         // init the session information
76
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
77         SessionManager.init(permissions);
78         UserStoreManager userStoreManager = new UserStoreManager();
79         UserSessionManager sessionManager = new UserSessionManager(permissions,
80                 userStoreManager);
81         LoginManager.init(sessionManager,userStoreManager);
82         // instanciate the thread manager
83
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
84             userStoreManager);
85         
86         // add a user to the session for the current thread
87
RoleManager.getInstance();
88         
89         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
90         
91         // add a new user object and add to the permission
92
Set JavaDoc set = new HashSet JavaDoc();
93         set.add("test");
94         UserSession user = new UserSession("test1", set);
95         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
96                 new ThreadPermissionSession(
97                 new Long JavaDoc(Thread.currentThread().getId()),user));
98         
99         
100         NamingDirector.init(threadGroup);
101         
102         Context JavaDoc startCtx = new InitialContext JavaDoc();
103         
104         ContextManager instance1 = new ContextManager("java:comp/env/test1");
105         ContextManager instance2 = new ContextManager("java:comp/env/test2");
106         
107         System.out.println("Bind value 1");
108         instance1.bind("test1","value1");
109         System.out.println("Bind value 2");
110         instance2.bind("test2","value2");
111         
112         System.out.println("Perform lookup");
113         String JavaDoc value1 = (String JavaDoc)startCtx.lookup("java:comp/env/test1/test1");
114         System.out.println("User the values");
115         
116         if ((value1 == null) || (value1.equals("value1") == false))
117         {
118             fail("Failed to retrieve value 1");
119         }
120         System.out.println("Test value 1 : " + value1);
121         
122         String JavaDoc value2 = (String JavaDoc)startCtx.lookup("java:comp/env/test2/test2");
123         if ((value2 == null) || (value2.equals("value2") == false))
124         {
125             fail("Failed to retrieve value 2");
126         }
127         System.out.println("Test value 2 : " + value2);
128         
129         instance1.unbind("test1");
130         try {
131             value1 = (String JavaDoc)startCtx.lookup("java:comp/env/test1/test1");
132             fail("Test 1 value is still bound");
133         } catch (Exception JavaDoc ex) {
134             // ignore
135
}
136         
137         NamingDirector.getInstance().shutdown();
138     }
139
140     
141 }
142
Popular Tags