KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > bean > BeanManagerTest


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

23
24 package com.rift.coad.lib.deployment.bean;
25
26 import com.rift.coad.lib.transaction.TransactionDirector;
27 import junit.framework.*;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import org.apache.log4j.BasicConfigurator;
37
38 // coaduntion imports
39
import com.rift.coad.lib.security.ThreadsPermissionContainer;
40 import com.rift.coad.lib.cache.CacheRegistry;
41 import com.rift.coad.lib.configuration.ConfigurationFactory;
42 import com.rift.coad.lib.configuration.Configuration;
43 import com.rift.coad.lib.deployment.DeploymentLoader;
44 import com.rift.coad.lib.naming.NamingDirector;
45 import com.rift.coad.lib.security.user.UserStoreManager;
46 import com.rift.coad.lib.security.ThreadsPermissionContainer;
47 import com.rift.coad.lib.security.ThreadPermissionSession;
48 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
49 import com.rift.coad.lib.security.SessionManager;
50 import com.rift.coad.lib.security.UserSession;
51 import com.rift.coad.lib.security.RoleManager;
52 import com.rift.coad.lib.security.Validator;
53 import com.rift.coad.lib.security.user.UserSessionManager;
54 import com.rift.coad.lib.security.login.LoginManager;
55 import com.rift.coad.lib.thread.CoadunationThreadGroup;
56 import com.rift.coad.lib.interceptor.InterceptorFactory;
57
58 /**
59  * The test class
60  *
61  * @author Brett Chaldecott
62  */

63 public class BeanManagerTest extends TestCase {
64     
65     public BeanManagerTest(String JavaDoc testName) {
66         super(testName);
67         BasicConfigurator.configure();
68     }
69
70     protected void setUp() throws Exception JavaDoc {
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74     }
75
76     public static Test suite() {
77         TestSuite suite = new TestSuite(BeanManagerTest.class);
78         
79         return suite;
80     }
81
82     /**
83      * Test of load method, of class com.rift.coad.lib.deployment.bean.BeanManager.
84      */

85     public void testBeanManager() throws Exception JavaDoc {
86         System.out.println("testBeanManager");
87         
88         System.out.println("Jar path : " + System.getProperty("test.jar"));
89         
90         // instanciate the deployment loader
91
ThreadsPermissionContainer permissionContainer =
92                 new ThreadsPermissionContainer();
93         
94         // initialize the thread permissions
95
SessionManager.init(permissionContainer);
96         UserStoreManager userStoreManager = new UserStoreManager();
97         UserSessionManager sessionManager = new UserSessionManager(
98                 permissionContainer,userStoreManager);
99         LoginManager.init(sessionManager,userStoreManager);
100         
101         // add a user to the session for the current thread
102
RoleManager.getInstance();
103         
104         // instanciate the thread manager
105
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
106             userStoreManager);
107         
108         // setup the interceptor factory
109
InterceptorFactory.init(permissionContainer,sessionManager,
110                 userStoreManager);
111         
112         // add a new user object and add to the permission
113
Set JavaDoc set = new HashSet JavaDoc();
114         set.add("test");
115         UserSession user = new UserSession("test1", set);
116         permissionContainer.putSession(new Long JavaDoc(Thread.currentThread().getId()),
117                 new ThreadPermissionSession(
118                 new Long JavaDoc(Thread.currentThread().getId()),user));
119         
120         // setup the naming
121
NamingDirector.init(threadGroup);
122         
123         // instanciate the transaction director
124
TransactionDirector transactionDirector = TransactionDirector.init();
125         
126         // setup the initial context
127
Context JavaDoc ctx = new InitialContext JavaDoc();
128         
129         // setup the cache registry
130
CacheRegistry.init(threadGroup);
131         
132         DeploymentLoader deploymentLoader = new DeploymentLoader(
133                 new java.io.File JavaDoc(System.getProperty("test.jar")));
134         
135         BeanManager instance = new BeanManager(permissionContainer,threadGroup);
136         
137         // setup the class loader
138
ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
139         Thread.currentThread().setContextClassLoader(
140                 deploymentLoader.getClassLoader());
141         NamingDirector.getInstance().initContext();
142         
143         // load the bean
144
instance.load(deploymentLoader);
145         
146         // retrieve the list of beans
147
Set JavaDoc keys = instance.getKeys();
148         
149         if (keys.size() != 2) {
150             fail("There should only be one bean in the list");
151         }
152         
153         if (keys.contains("testbean") == false) {
154             fail("The bean that should be loaded should be called [testbean]");
155         }
156         
157         Object JavaDoc ref = ctx.lookup("java:comp/env/bean/testbean");
158         
159         // unload the bean
160
instance.unLoad(deploymentLoader);
161         NamingDirector.getInstance().releaseContext();
162         Thread.currentThread().setContextClassLoader(original);
163         
164         // check that the beans get unloaded
165
keys = instance.getKeys();
166         if (keys.size() != 0) {
167             fail("There should be no beans in the list");
168         }
169         
170         try {
171             ref = ctx.lookup("java:comp/env/bean/testbean");
172             fail("The context is still bound");
173         } catch (Exception JavaDoc ex) {
174             // do nothing
175
}
176         
177         CacheRegistry.getInstance().shutdown();
178         
179         NamingDirector.getInstance().shutdown();
180     }
181     
182 }
183
Popular Tags