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: * JMXBeanManagerTest.java
020: *
021: * JUnit based test
022: */
023:
024: package com.rift.coad.lib.deployment.jmxbean;
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.Iterator;
031: import java.util.Map;
032: import java.util.HashMap;
033: import java.util.Set;
034: import java.util.HashSet;
035: import javax.management.MBeanServer;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038: import java.lang.management.ManagementFactory;
039: import org.apache.log4j.Logger;
040: import com.rift.coad.lib.bean.BeanWrapper;
041:
042: // coaduntion imports
043: import com.rift.coad.lib.security.ThreadsPermissionContainer;
044: import com.rift.coad.lib.deployment.DeploymentLoader;
045: import com.rift.coad.lib.security.user.UserSessionManager;
046: import com.rift.coad.lib.configuration.ConfigurationFactory;
047: import com.rift.coad.lib.configuration.Configuration;
048: import com.rift.coad.lib.naming.NamingDirector;
049: import com.rift.coad.lib.security.user.UserStoreManager;
050: import com.rift.coad.lib.security.ThreadsPermissionContainer;
051: import com.rift.coad.lib.security.ThreadPermissionSession;
052: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
053: import com.rift.coad.lib.security.SessionManager;
054: import com.rift.coad.lib.security.UserSession;
055: import com.rift.coad.lib.security.RoleManager;
056: import com.rift.coad.lib.security.Validator;
057: import com.rift.coad.lib.security.login.LoginManager;
058: import com.rift.coad.lib.thread.CoadunationThreadGroup;
059: import com.rift.coad.lib.cache.CacheRegistry;
060: import com.rift.coad.lib.interceptor.InterceptorFactory;
061:
062: /**
063: *
064: *
065: * @author Brett Chaldecott
066: */
067: public class JMXBeanManagerTest extends TestCase {
068:
069: public JMXBeanManagerTest(String testName) {
070: super (testName);
071: }
072:
073: protected void setUp() throws Exception {
074: }
075:
076: protected void tearDown() throws Exception {
077: }
078:
079: public static Test suite() {
080: TestSuite suite = new TestSuite(JMXBeanManagerTest.class);
081:
082: return suite;
083: }
084:
085: /**
086: * Test of class com.rift.coad.lib.deployment.jmxbean.JMXBeanManager.
087: */
088: public void testJMXBeanManager() throws Exception {
089: System.out.println("testJMXBeanManager");
090:
091: System.out.println("Jar path : "
092: + System.getProperty("test.jar"));
093:
094: // instanciate the deployment loader
095: ThreadsPermissionContainer permissionContainer = new ThreadsPermissionContainer();
096:
097: // initialize the thread permissions
098: SessionManager.init(permissionContainer);
099: UserStoreManager userStoreManager = new UserStoreManager();
100: UserSessionManager sessionManager = new UserSessionManager(
101: permissionContainer, userStoreManager);
102: LoginManager.init(sessionManager, userStoreManager);
103:
104: // add a user to the session for the current thread
105: RoleManager.getInstance();
106:
107: // setup the interceptor factory
108: InterceptorFactory.init(permissionContainer, sessionManager,
109: userStoreManager);
110:
111: // add a new user object and add to the permission
112: Set set = new HashSet();
113: set.add("test");
114: UserSession user = new UserSession("test1", set);
115: permissionContainer.putSession(new Long(Thread.currentThread()
116: .getId()), new ThreadPermissionSession(new Long(Thread
117: .currentThread().getId()), user));
118:
119: // instanciate the thread manager
120: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
121: sessionManager, userStoreManager);
122:
123: // setup the naming context
124: NamingDirector.init(threadGroup);
125:
126: // instanciate the transaction director
127: TransactionDirector transactionDirector = TransactionDirector
128: .init();
129:
130: // setup the initial context
131: Context ctx = new InitialContext();
132:
133: CacheRegistry.init(threadGroup);
134:
135: DeploymentLoader deploymentLoader = new DeploymentLoader(
136: new java.io.File(System.getProperty("test.jar")));
137:
138: JMXBeanManager instance = new JMXBeanManager(
139: permissionContainer, threadGroup);
140:
141: // setup the class loader
142: ClassLoader loader = new URLClassLoader(new URL[0]);
143: ClassLoader original = Thread.currentThread()
144: .getContextClassLoader();
145: Thread.currentThread().setContextClassLoader(loader);
146: NamingDirector.getInstance().initContext();
147:
148: // load the object into memory
149: instance.load(deploymentLoader);
150:
151: Set objectKeys = instance.getObjectKeys();
152: if (objectKeys.size() != 1) {
153: fail("There should be atleast one object ["
154: + objectKeys.size() + "]");
155: }
156:
157: if (objectKeys.contains("com.test2:type=JMXBean1") == false) {
158: fail("Object [com.test2:type=JMXBean1] not found.");
159: }
160:
161: if (null == instance.getObject("com.test2:type=JMXBean1")) {
162: fail("Failed to retrieve the Object [com.test2:type=JMXBean1]");
163: }
164:
165: Set bindKeys = instance.getBindKeys();
166: if (bindKeys.size() != 1) {
167: fail("There should be atleast one bind [" + bindKeys.size()
168: + "]");
169: }
170:
171: if (bindKeys.contains("testjmxbean") == false) {
172: fail("Object [com.test2:type=JMXBean1] not found.");
173: }
174:
175: if (null == instance.getBindObject("testjmxbean")) {
176: fail("Failed to retrieve the Object [testjmxbean]");
177: }
178:
179: Object ref = ctx.lookup("java:comp/env/jmx/testjmxbean");
180:
181: // unload the object
182: instance.unLoad(deploymentLoader);
183: NamingDirector.getInstance().releaseContext();
184: Thread.currentThread().setContextClassLoader(loader);
185:
186: bindKeys = instance.getBindKeys();
187: if (bindKeys.size() != 0) {
188: fail("There should be no keys [" + bindKeys.size() + "]");
189: }
190:
191: objectKeys = instance.getObjectKeys();
192: if (objectKeys.size() != 0) {
193: fail("There should be no object [" + objectKeys.size()
194: + "]");
195: }
196:
197: try {
198: ref = ctx.lookup("java:comp/env/jmx/testjmxbean");
199: fail("JMX Bean still bound");
200: } catch (Exception ex) {
201: // do nothing
202: }
203:
204: CacheRegistry.getInstance().shutdown();
205: NamingDirector.getInstance().shutdown();
206: }
207:
208: }
|