KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > bean > BeanHandlerTest


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

22
23 package com.rift.coad.lib.bean;
24
25 import com.rift.coad.lib.db.DBSourceManager;
26 import com.rift.coad.lib.naming.NamingDirector;
27 import com.rift.coad.lib.transaction.TransactionDirector;
28 import com.rift.coad.util.lock.ObjectLockFactory;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import junit.framework.*;
32 import java.lang.ClassLoader JavaDoc;
33 import java.lang.reflect.InvocationHandler JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41 import java.lang.reflect.Proxy JavaDoc;
42 import org.apache.log4j.Logger;
43 import com.rift.coad.lib.ResourceIndex;
44 import com.rift.coad.lib.bean.test.*;
45 import com.rift.coad.lib.cache.CacheRegistry;
46 import com.rift.coad.lib.cache.CacheEntry;
47 import com.rift.coad.lib.cache.KeySyncCache;
48 import com.rift.coad.lib.cache.KeySyncCacheManager;
49 import com.rift.coad.lib.common.ClassUtil;
50 import com.rift.coad.lib.common.RandomGuid;
51 import com.rift.coad.lib.deployment.BeanInfo;
52 import com.rift.coad.lib.deployment.DeploymentLoader;
53 import com.rift.coad.lib.interceptor.InterceptorFactory;
54 import com.rift.coad.lib.security.Validator;
55 import com.rift.coad.lib.security.ThreadsPermissionContainer;
56 import com.rift.coad.lib.configuration.ConfigurationFactory;
57 import com.rift.coad.lib.configuration.Configuration;
58 import com.rift.coad.lib.thread.BasicThread;
59 import com.rift.coad.lib.thread.ThreadStateMonitor;
60 import com.rift.coad.lib.thread.CoadunationThreadGroup;
61 import com.rift.coad.lib.security.UserSession;
62 import com.rift.coad.lib.security.user.UserSessionManager;
63 import com.rift.coad.lib.security.user.UserStoreManager;
64 import com.rift.coad.lib.security.ThreadsPermissionContainer;
65 import com.rift.coad.lib.security.ThreadPermissionSession;
66 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
67 import com.rift.coad.lib.security.SessionManager;
68 import com.rift.coad.lib.security.RoleManager;
69 import com.rift.coad.lib.security.login.LoginManager;
70 import com.rift.coad.util.transaction.TransactionManager;
71
72 /**
73  *
74  * @author Brett Chaldecott
75  */

76 public class BeanHandlerTest extends TestCase {
77     
78     private UserTransaction JavaDoc ut = null;
79     
80     
81     public BeanHandlerTest(String JavaDoc testName) {
82         super(testName);
83     }
84
85     protected void setUp() throws Exception JavaDoc {
86     }
87
88     protected void tearDown() throws Exception JavaDoc {
89     }
90
91     public static Test suite() {
92         TestSuite suite = new TestSuite(BeanHandlerTest.class);
93         
94         return suite;
95     }
96
97     /**
98      * Test of invoke method, of class com.rift.coad.lib.bean.BeanHandler.
99      */

100     public void testBeanHandler() throws Exception JavaDoc {
101         System.out.println("testBeanHandler");
102         
103         // init the session information
104
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
105         SessionManager.init(permissions);
106         UserStoreManager userStoreManager = new UserStoreManager();
107         UserSessionManager sessionManager = new UserSessionManager(permissions,
108                 userStoreManager);
109         LoginManager.init(sessionManager,userStoreManager);
110         // instanciate the thread manager
111
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
112             userStoreManager);
113         
114         // add a user to the session for the current thread
115
RoleManager.getInstance();
116         
117         InterceptorFactory.init(permissions,sessionManager,userStoreManager);
118         
119         // add a new user object and add to the permission
120
Set JavaDoc set = new HashSet JavaDoc();
121         set.add("test");
122         UserSession user = new UserSession("test1", set);
123         permissions.putSession(new Long JavaDoc(Thread.currentThread().getId()),
124                 new ThreadPermissionSession(
125                 new Long JavaDoc(Thread.currentThread().getId()),user));
126         
127         // init the naming director
128
NamingDirector.init(threadGroup);
129         
130         // instanciate the transaction director
131
TransactionDirector transactionDirector = TransactionDirector.init();
132         
133         // init the database source
134
DBSourceManager.init();
135         Context JavaDoc context = new InitialContext JavaDoc();
136         ObjectLockFactory.init();
137         TransactionManager.init();
138         ut = (UserTransaction JavaDoc)context.lookup("java:comp/UserTransaction");
139         
140         // check the singleton
141
CacheRegistry registry = CacheRegistry.init(threadGroup);
142         
143         // setup the registry for this class loader
144
Thread.currentThread().setContextClassLoader(
145                 BeanHandler.class.getClassLoader());
146         registry.initCache();
147         
148         // instanciate a bean infomration object
149
BeanInfo beanInfo1 = new BeanInfo();
150         beanInfo1.setCacheResults(true);
151         beanInfo1.setRole("test");
152         beanInfo1.setCacheTimeout(500);
153         BeanInfo beanInfo2 = new BeanInfo();
154         beanInfo2.setCacheResults(false);
155         beanInfo2.setRole("test");
156         beanInfo2.setCacheTimeout(500);
157         
158         // setup the main interface
159
MainInterface1Impl subObject1 = new MainInterface1Impl();
160         
161         BeanHandler handler = new BeanHandler(beanInfo1, subObject1,
162                 beanInfo1.getRole(),permissions,
163                 BeanHandler.class.getClassLoader());
164         MainInterface1 proxy = (MainInterface1)Proxy.newProxyInstance(
165                 BeanHandler.class.getClassLoader(),
166                 subObject1.getClass().getInterfaces(),handler);
167         
168         proxy.callVoid();
169         
170         try {
171             proxy.throwException();
172             fail("Failed to throw an exception");
173         } catch (TestProxyException ex) {
174             // worked
175
}
176         
177         // make the test calls on this object
178
if (proxy.getInt() != 1) {
179             fail("Failed to retrieve value 1");
180         }
181         if (!proxy.getString().equals("Test1")) {
182             fail("Failed to retrieve value 1");
183         }
184         TestKey testKey = proxy.getAKeyValue();
185         if (!(testKey.getKey1().equals("1")) &&
186                 !(testKey.getKey2().equals("key1"))) {
187             fail("Failed to retrieve the key");
188         }
189         
190         SubInterface1 interface1 = proxy.addSubInterface("key1");
191         if (interface1 != proxy.getSubInterface("key1")) {
192             fail("Failed to retrieve from cache");
193         }
194         if (MainInterface1Impl.calledBean != 0) {
195             fail("Failed the bean got called");
196         }
197         
198         SubInterface1 interface2 = proxy.addSubInterface("key2");
199         
200         for (int count = 0; count < 4; count++) {
201             Thread.sleep(450);
202             interface1.getName();
203         }
204         interface1.getName();
205         boolean called = false;
206         try {
207             interface2.getName();
208             called = true;
209         } catch(Exception JavaDoc ex) {
210             System.out.println("Exception " + ex.getMessage());
211             ex.printStackTrace(System.out);
212         }
213         if (called) {
214             fail("Called the sub object successfully");
215         }
216         
217         // create a new proxy
218
handler = new BeanHandler(beanInfo2, subObject1,
219                 beanInfo1.getRole(),permissions,
220                 BeanHandler.class.getClassLoader());
221         proxy = (MainInterface1)Proxy.newProxyInstance(
222                 BeanHandler.class.getClassLoader(),
223                 subObject1.getClass().getInterfaces(),handler);
224         
225         MainInterface1Impl.calledBean = 0;
226         interface1 = proxy.getSubInterface("key1");
227         interface1 = proxy.getSubInterface("key1");
228         interface1 = proxy.getSubInterface("key1");
229         if (MainInterface1Impl.calledBean != 3) {
230             fail("Failed to call sub object hitting cache");
231         }
232         interface2 = proxy.addSubInterface("key2");
233         
234         for (int count = 0; count < 4; count++) {
235             Thread.sleep(450);
236             interface1.getName();
237         }
238         interface1.getName();
239         called = false;
240         try {
241             interface2.getName();
242             called = true;
243         } catch(Exception JavaDoc ex) {
244             System.out.println("Exception " + ex.getMessage());
245             ex.printStackTrace(System.out);
246         }
247         if (called) {
248             fail("Called the sub object successfully");
249         }
250         
251         
252         // transaction based methods
253
MainInterface1Impl.calledBean = 0;
254         beanInfo1.setTransaction(true);
255         beanInfo2.setTransaction(true);
256         handler = new BeanHandler(beanInfo1, subObject1,
257                 beanInfo1.getRole(),permissions,
258                 BeanHandler.class.getClassLoader());
259         proxy = (MainInterface1)Proxy.newProxyInstance(
260                 BeanHandler.class.getClassLoader(),
261                 subObject1.getClass().getInterfaces(),handler);
262         
263         proxy.callVoid();
264         
265         try {
266             proxy.throwException();
267             fail("Failed to throw an exception");
268         } catch (TestProxyException ex) {
269             // worked
270
}
271         
272         // make the test calls on this object
273
if (proxy.getInt() != 1) {
274             fail("Failed to retrieve value 1");
275         }
276         if (!proxy.getString().equals("Test1")) {
277             fail("Failed to retrieve value 1");
278         }
279         testKey = proxy.getAKeyValue();
280         if (!(testKey.getKey1().equals("1")) &&
281                 !(testKey.getKey2().equals("key1"))) {
282             fail("Failed to retrieve the key");
283         }
284         
285         interface1 = proxy.addSubInterface("key3");
286         if (interface1 != proxy.getSubInterface("key3")) {
287             fail("Failed to retrieve from cache");
288         }
289         if (MainInterface1Impl.calledBean != 0) {
290             fail("Failed the bean got called");
291         }
292         
293         ut.begin();
294         interface2 = proxy.addSubInterface("key4");
295         ut.commit();
296         
297         for (int count = 0; count < 4; count++) {
298             Thread.sleep(450);
299             interface1.getName();
300         }
301         interface1.getName();
302         called = false;
303         try {
304             interface2.getName();
305             called = true;
306         } catch(Exception JavaDoc ex) {
307             System.out.println("Exception " + ex.getMessage());
308             ex.printStackTrace(System.out);
309         }
310         if (called) {
311             fail("Called the sub object successfully");
312         }
313         
314         // create a new proxy
315
handler = new BeanHandler(beanInfo2, subObject1,
316                 beanInfo1.getRole(),permissions,
317                 BeanHandler.class.getClassLoader());
318         proxy = (MainInterface1)Proxy.newProxyInstance(
319                 BeanHandler.class.getClassLoader(),
320                 subObject1.getClass().getInterfaces(),handler);
321         
322         MainInterface1Impl.calledBean = 0;
323         ut.begin();
324         interface1 = proxy.getSubInterface("key3");
325         interface1 = proxy.getSubInterface("key3");
326         interface1 = proxy.getSubInterface("key3");
327         ut.commit();
328         if (MainInterface1Impl.calledBean != 3) {
329             fail("Failed to call sub object hitting cache");
330         }
331         interface2 = proxy.addSubInterface("key4");
332         
333         for (int count = 0; count < 4; count++) {
334             Thread.sleep(450);
335             interface1.getName();
336         }
337         interface1.getName();
338         called = false;
339         try {
340             interface2.getName();
341             called = true;
342         } catch(Exception JavaDoc ex) {
343             System.out.println("Exception " + ex.getMessage());
344             ex.printStackTrace(System.out);
345         }
346         if (called) {
347             fail("Called the sub object successfully");
348         }
349         
350         // terminate the cache registry
351
registry.shutdown();
352     }
353 }
354
Popular Tags