KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > jmxbean > JMXBeanManagerTest


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

23
24 package com.rift.coad.lib.deployment.jmxbean;
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.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import java.lang.management.ManagementFactory JavaDoc;
39 import org.apache.log4j.Logger;
40 import com.rift.coad.lib.bean.BeanWrapper;
41
42
43 // coaduntion imports
44
import com.rift.coad.lib.security.ThreadsPermissionContainer;
45 import com.rift.coad.lib.deployment.DeploymentLoader;
46 import com.rift.coad.lib.security.user.UserSessionManager;
47 import com.rift.coad.lib.configuration.ConfigurationFactory;
48 import com.rift.coad.lib.configuration.Configuration;
49 import com.rift.coad.lib.naming.NamingDirector;
50 import com.rift.coad.lib.security.user.UserStoreManager;
51 import com.rift.coad.lib.security.ThreadsPermissionContainer;
52 import com.rift.coad.lib.security.ThreadPermissionSession;
53 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
54 import com.rift.coad.lib.security.SessionManager;
55 import com.rift.coad.lib.security.UserSession;
56 import com.rift.coad.lib.security.RoleManager;
57 import com.rift.coad.lib.security.Validator;
58 import com.rift.coad.lib.security.login.LoginManager;
59 import com.rift.coad.lib.thread.CoadunationThreadGroup;
60 import com.rift.coad.lib.cache.CacheRegistry;
61 import com.rift.coad.lib.interceptor.InterceptorFactory;
62
63 /**
64  *
65  *
66  * @author Brett Chaldecott
67  */

68 public class JMXBeanManagerTest extends TestCase {
69     
70     public JMXBeanManagerTest(String JavaDoc testName) {
71         super(testName);
72     }
73
74     protected void setUp() throws Exception JavaDoc {
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78     }
79
80     public static Test suite() {
81         TestSuite suite = new TestSuite(JMXBeanManagerTest.class);
82         
83         return suite;
84     }
85
86     /**
87      * Test of class com.rift.coad.lib.deployment.jmxbean.JMXBeanManager.
88      */

89     public void testJMXBeanManager() throws Exception JavaDoc {
90         System.out.println("testJMXBeanManager");
91         
92         System.out.println("Jar path : " + System.getProperty("test.jar"));
93         
94         // instanciate the deployment loader
95
ThreadsPermissionContainer permissionContainer =
96                 new ThreadsPermissionContainer();
97         
98         // initialize the thread permissions
99
SessionManager.init(permissionContainer);
100         UserStoreManager userStoreManager = new UserStoreManager();
101         UserSessionManager sessionManager = new UserSessionManager(
102                 permissionContainer,userStoreManager);
103         LoginManager.init(sessionManager,userStoreManager);
104         
105         // add a user to the session for the current thread
106
RoleManager.getInstance();
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         // instanciate the thread manager
121
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
122             userStoreManager);
123         
124         // setup the naming context
125
NamingDirector.init(threadGroup);
126         
127         // instanciate the transaction director
128
TransactionDirector transactionDirector = TransactionDirector.init();
129
130         // setup the initial context
131
Context JavaDoc ctx = new InitialContext JavaDoc();
132         
133         CacheRegistry.init(threadGroup);
134         
135         DeploymentLoader deploymentLoader = new DeploymentLoader(
136                 new java.io.File JavaDoc(System.getProperty("test.jar")));
137         
138         JMXBeanManager instance = new JMXBeanManager(permissionContainer,
139                 threadGroup);
140         
141         // setup the class loader
142
ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[0]);
143         ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
144         Thread.currentThread().setContextClassLoader(loader);
145         NamingDirector.getInstance().initContext();
146         
147         // load the object into memory
148
instance.load(deploymentLoader);
149         
150         Set JavaDoc objectKeys = instance.getObjectKeys();
151         if (objectKeys.size() != 1) {
152           fail("There should be atleast one object [" + objectKeys.size() + "]");
153         }
154         
155         if (objectKeys.contains("com.test2:type=JMXBean1") == false) {
156             fail("Object [com.test2:type=JMXBean1] not found.");
157         }
158         
159         if (null == instance.getObject("com.test2:type=JMXBean1")) {
160             fail("Failed to retrieve the Object [com.test2:type=JMXBean1]");
161         }
162         
163         Set JavaDoc bindKeys = instance.getBindKeys();
164         if (bindKeys.size() != 1) {
165           fail("There should be atleast one bind [" + bindKeys.size() + "]");
166         }
167         
168         if (bindKeys.contains("testjmxbean") == false) {
169             fail("Object [com.test2:type=JMXBean1] not found.");
170         }
171         
172         if (null == instance.getBindObject("testjmxbean")) {
173             fail("Failed to retrieve the Object [testjmxbean]");
174         }
175         
176         Object JavaDoc ref = ctx.lookup("java:comp/env/jmx/testjmxbean");
177         
178         // unload the object
179
instance.unLoad(deploymentLoader);
180         NamingDirector.getInstance().releaseContext();
181         Thread.currentThread().setContextClassLoader(loader);
182         
183         bindKeys = instance.getBindKeys();
184         if (bindKeys.size() != 0) {
185           fail("There should be no keys [" + bindKeys.size() + "]");
186         }
187         
188         objectKeys = instance.getObjectKeys();
189         if (objectKeys.size() != 0) {
190           fail("There should be no object [" + objectKeys.size() + "]");
191         }
192         
193         try {
194             ref = ctx.lookup("java:comp/env/jmx/testjmxbean");
195             fail("JMX Bean still bound");
196         } catch (Exception JavaDoc ex) {
197             // do nothing
198
}
199         
200         CacheRegistry.getInstance().shutdown();
201         NamingDirector.getInstance().shutdown();
202     }
203     
204 }
205
Popular Tags