KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > cos > CosNamingContextManagerTest


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

23
24 package com.rift.coad.lib.naming.cos;
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 javax.naming.NameNotFoundException JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.net.URLClassLoader JavaDoc;
35 import java.net.URL JavaDoc;
36
37 import java.util.Set JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import com.rift.coad.lib.naming.NamingDirector;
40 import com.rift.coad.lib.interceptor.InterceptorFactory;
41 import com.rift.coad.lib.security.RoleManager;
42 import com.rift.coad.lib.security.ThreadsPermissionContainer;
43 import com.rift.coad.lib.security.ThreadPermissionSession;
44 import com.rift.coad.lib.security.UserSession;
45 import com.rift.coad.lib.security.user.UserSessionManager;
46 import com.rift.coad.lib.security.user.UserStoreManager;
47 import com.rift.coad.lib.security.SessionManager;
48 import com.rift.coad.lib.security.login.LoginManager;
49 import com.rift.coad.lib.thread.CoadunationThreadGroup;
50
51
52 /**
53  *
54  * @author Brett Chaldecott
55  */

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

77     public void testContextManager() throws Exception JavaDoc {
78         System.out.println("testContextManager");
79         
80         // init the session information
81
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
82         SessionManager.init(permissions);
83         UserStoreManager userStoreManager = new UserStoreManager();
84         UserSessionManager sessionManager = new UserSessionManager(permissions,
85                 userStoreManager);
86         LoginManager.init(sessionManager,userStoreManager);
87         // instanciate the thread manager
88
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
89             userStoreManager);
90         
91         // add a user to the session for the current thread
92
RoleManager.getInstance();
93         
94         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
95         
96         // add a new user object and add to the permission
97
Set JavaDoc set = new HashSet JavaDoc();
98         set.add("test");
99         UserSession user = new UserSession("test1", set);
100         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
101                 new ThreadPermissionSession(
102                 new Long JavaDoc(Thread.currentThread().getId()),user));
103         
104         
105         NamingDirector.init(threadGroup);
106         
107         Context JavaDoc context = new InitialContext JavaDoc();
108         
109         context.bind("java:comp/env/test","fred");
110         context.bind("java:comp/env/test2","fred2");
111         
112         if (!context.lookup("java:comp/env/test").equals("fred")) {
113            fail("Could not retrieve the value for test");
114         }
115         if (!context.lookup("java:comp/env/test2").equals("fred2")) {
116            fail("Could not retrieve the value for test2");
117         }
118         System.out.println("Creating the sub context");
119         Context JavaDoc subContext = context.createSubcontext("java:comp/env/test3/test3");
120         System.out.println("Adding the binding for bob to the sub context");
121         subContext.bind("bob","bob");
122         System.out.println("Looking up the binding for bob on the sub context.");
123         Object JavaDoc value = subContext.lookup("bob");
124         System.out.println("Object type is : " + value.getClass().getName());
125         if (!value.equals("bob")) {
126            fail("Could not retrieve the value bob");
127         }
128         if (!context.lookup("java:comp/env/test3/test3/bob").equals("bob")) {
129            fail("Could not retrieve the value bob");
130         }
131         
132         ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[0]);
133         ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
134         Thread.currentThread().setContextClassLoader(loader);
135         NamingDirector.getInstance().initContext();
136         
137         context.bind("java:comp/env/test5","fred5");
138         if (!context.lookup("java:comp/env/test5").equals("fred5")) {
139            fail("Could not retrieve the value fred5");
140         }
141         
142         Thread.currentThread().setContextClassLoader(original);
143         
144         try{
145             context.lookup("java:comp/env/test5");
146             fail("Failed retrieve a value that should not exist");
147         } catch (NameNotFoundException JavaDoc ex) {
148             // ignore
149
}
150         
151         Thread.currentThread().setContextClassLoader(loader);
152         
153         NamingDirector.getInstance().releaseContext();
154         
155         try{
156             context.lookup("java:comp/env/test5");
157             fail("Failed retrieve a value that should not exist");
158         } catch (NameNotFoundException JavaDoc ex) {
159             // ignore
160
}
161         Thread.currentThread().setContextClassLoader(original);
162         System.out.println("Add value 1");
163         context.bind("basic","basic");
164         System.out.println("Add value 2");
165         context.bind("basic2/bob","basic2");
166         if (!context.lookup("basic").equals("basic")) {
167             fail("Could not retrieve the basic value from the JNDI");
168         }
169         if (!context.lookup("basic2/bob").equals("basic2")) {
170             fail("Could not retrieve the basic value from the JNDI");
171         }
172         
173         try {
174             context.bind("java:network/env/test","fred");
175             fail("This should have thrown as only relative urls can be used " +
176                     "JNDI");
177         } catch (NamingException JavaDoc ex) {
178             // ignore
179
}
180         
181         try {
182             context.unbind("java:network/env/test");
183             fail("This should have thrown as only relative urls can be used " +
184                     "JNDI");
185         } catch (NamingException JavaDoc ex) {
186             // ignore
187
}
188         context.rebind("basic","test1");
189         context.rebind("basic2/bob","test2");
190         
191         if (!context.lookup("basic").equals("test1")) {
192             fail("Could not retrieve the basic value from the JNDI");
193         }
194         if (!context.lookup("basic2/bob").equals("test2")) {
195             fail("Could not retrieve the basic value from the JNDI");
196         }
197         
198         context.unbind("basic");
199         context.unbind("basic2/bob");
200         
201         try{
202             context.lookup("basic2/bob");
203             fail("The basic bob value could still be found");
204         } catch (NameNotFoundException JavaDoc ex) {
205             // ignore
206
}
207         
208         NamingDirector.getInstance().shutdown();
209     }
210
211     
212 }
213
Popular Tags