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: * BeanManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.deployment.bean;
025:
026: import com.rift.coad.lib.transaction.TransactionDirector;
027: import junit.framework.*;
028: import java.net.URL;
029: import java.net.URLClassLoader;
030: import java.util.Map;
031: import java.util.Set;
032: import java.util.HashSet;
033: import java.util.HashMap;
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import org.apache.log4j.BasicConfigurator;
037:
038: // coaduntion imports
039: import com.rift.coad.lib.security.ThreadsPermissionContainer;
040: import com.rift.coad.lib.cache.CacheRegistry;
041: import com.rift.coad.lib.configuration.ConfigurationFactory;
042: import com.rift.coad.lib.configuration.Configuration;
043: import com.rift.coad.lib.deployment.DeploymentLoader;
044: import com.rift.coad.lib.naming.NamingDirector;
045: import com.rift.coad.lib.security.user.UserStoreManager;
046: import com.rift.coad.lib.security.ThreadsPermissionContainer;
047: import com.rift.coad.lib.security.ThreadPermissionSession;
048: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
049: import com.rift.coad.lib.security.SessionManager;
050: import com.rift.coad.lib.security.UserSession;
051: import com.rift.coad.lib.security.RoleManager;
052: import com.rift.coad.lib.security.Validator;
053: import com.rift.coad.lib.security.user.UserSessionManager;
054: import com.rift.coad.lib.security.login.LoginManager;
055: import com.rift.coad.lib.thread.CoadunationThreadGroup;
056: import com.rift.coad.lib.interceptor.InterceptorFactory;
057:
058: /**
059: * The test class
060: *
061: * @author Brett Chaldecott
062: */
063: public class BeanManagerTest extends TestCase {
064:
065: public BeanManagerTest(String testName) {
066: super (testName);
067: BasicConfigurator.configure();
068: }
069:
070: protected void setUp() throws Exception {
071: }
072:
073: protected void tearDown() throws Exception {
074: }
075:
076: public static Test suite() {
077: TestSuite suite = new TestSuite(BeanManagerTest.class);
078:
079: return suite;
080: }
081:
082: /**
083: * Test of load method, of class com.rift.coad.lib.deployment.bean.BeanManager.
084: */
085: public void testBeanManager() throws Exception {
086: System.out.println("testBeanManager");
087:
088: System.out.println("Jar path : "
089: + System.getProperty("test.jar"));
090:
091: // instanciate the deployment loader
092: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
093:
094: // initialize the thread permissions
095: SessionManager.init(permissionContainer);
096: UserStoreManager userStoreManager = new UserStoreManager();
097: UserSessionManager sessionManager = new UserSessionManager(
098: permissionContainer, userStoreManager);
099: 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(
106: sessionManager, 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 set = new HashSet();
114: set.add("test");
115: UserSession user = new UserSession("test1", set);
116: permissionContainer.putSession(new Long(Thread.currentThread()
117: .getId()), new ThreadPermissionSession(new Long(Thread
118: .currentThread().getId()), user));
119:
120: // setup the naming
121: NamingDirector.init(threadGroup);
122:
123: // instanciate the transaction director
124: TransactionDirector transactionDirector = TransactionDirector
125: .init();
126:
127: // setup the initial context
128: Context ctx = new InitialContext();
129:
130: // setup the cache registry
131: CacheRegistry.init(threadGroup);
132:
133: DeploymentLoader deploymentLoader = new DeploymentLoader(
134: new java.io.File(System.getProperty("test.jar")));
135:
136: BeanManager instance = new BeanManager(permissionContainer,
137: threadGroup);
138:
139: // setup the class loader
140: ClassLoader original = Thread.currentThread()
141: .getContextClassLoader();
142: Thread.currentThread().setContextClassLoader(
143: deploymentLoader.getClassLoader());
144: NamingDirector.getInstance().initContext();
145:
146: // load the bean
147: instance.load(deploymentLoader);
148:
149: // retrieve the list of beans
150: Set keys = instance.getKeys();
151:
152: if (keys.size() != 2) {
153: fail("There should only be one bean in the list");
154: }
155:
156: if (keys.contains("testbean") == false) {
157: fail("The bean that should be loaded should be called [testbean]");
158: }
159:
160: Object ref = ctx.lookup("java:comp/env/bean/testbean");
161:
162: // unload the bean
163: instance.unLoad(deploymentLoader);
164: NamingDirector.getInstance().releaseContext();
165: Thread.currentThread().setContextClassLoader(original);
166:
167: // check that the beans get unloaded
168: keys = instance.getKeys();
169: if (keys.size() != 0) {
170: fail("There should be no beans in the list");
171: }
172:
173: try {
174: ref = ctx.lookup("java:comp/env/bean/testbean");
175: fail("The context is still bound");
176: } catch (Exception ex) {
177: // do nothing
178: }
179:
180: CacheRegistry.getInstance().shutdown();
181:
182: NamingDirector.getInstance().shutdown();
183: }
184:
185: }
|