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: * BeanHandlerTest.java
020: * JUnit based test
021: */
022:
023: package com.rift.coad.lib.bean;
024:
025: import com.rift.coad.lib.db.DBSourceManager;
026: import com.rift.coad.lib.naming.NamingDirector;
027: import com.rift.coad.lib.transaction.TransactionDirector;
028: import com.rift.coad.util.lock.ObjectLockFactory;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import junit.framework.*;
032: import java.lang.ClassLoader;
033: import java.lang.reflect.InvocationHandler;
034: import java.lang.reflect.Method;
035: import java.util.Date;
036: import java.util.HashSet;
037: import java.util.Set;
038: import java.util.regex.Matcher;
039: import java.util.regex.Pattern;
040: import javax.transaction.UserTransaction;
041: import java.lang.reflect.Proxy;
042: import org.apache.log4j.Logger;
043: import org.apache.log4j.BasicConfigurator;
044: import com.rift.coad.lib.ResourceIndex;
045: import com.rift.coad.lib.bean.test.*;
046: import com.rift.coad.lib.cache.CacheRegistry;
047: import com.rift.coad.lib.cache.CacheEntry;
048: import com.rift.coad.lib.cache.KeySyncCache;
049: import com.rift.coad.lib.cache.KeySyncCacheManager;
050: import com.rift.coad.lib.common.ClassUtil;
051: import com.rift.coad.lib.common.RandomGuid;
052: import com.rift.coad.lib.deployment.BeanInfo;
053: import com.rift.coad.lib.deployment.DeploymentLoader;
054: import com.rift.coad.lib.interceptor.InterceptorFactory;
055: import com.rift.coad.lib.security.Validator;
056: import com.rift.coad.lib.security.ThreadsPermissionContainer;
057: import com.rift.coad.lib.configuration.ConfigurationFactory;
058: import com.rift.coad.lib.configuration.Configuration;
059: import com.rift.coad.lib.thread.BasicThread;
060: import com.rift.coad.lib.thread.ThreadStateMonitor;
061: import com.rift.coad.lib.thread.CoadunationThreadGroup;
062: import com.rift.coad.lib.security.UserSession;
063: import com.rift.coad.lib.security.user.UserSessionManager;
064: import com.rift.coad.lib.security.user.UserStoreManager;
065: import com.rift.coad.lib.security.ThreadsPermissionContainer;
066: import com.rift.coad.lib.security.ThreadPermissionSession;
067: import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
068: import com.rift.coad.lib.security.SessionManager;
069: import com.rift.coad.lib.security.RoleManager;
070: import com.rift.coad.lib.security.login.LoginManager;
071: import com.rift.coad.util.transaction.TransactionManager;
072:
073: /**
074: *
075: * @author Brett Chaldecott
076: */
077: public class BeanHandlerTest extends TestCase {
078:
079: private UserTransaction ut = null;
080:
081: public BeanHandlerTest(String testName) {
082: super (testName);
083: BasicConfigurator.configure();
084: }
085:
086: protected void setUp() throws Exception {
087: }
088:
089: protected void tearDown() throws Exception {
090: }
091:
092: public static Test suite() {
093: TestSuite suite = new TestSuite(BeanHandlerTest.class);
094:
095: return suite;
096: }
097:
098: /**
099: * Test of invoke method, of class com.rift.coad.lib.bean.BeanHandler.
100: */
101: public void testBeanHandler() throws Exception {
102: System.out.println("testBeanHandler");
103:
104: // init the session information
105: ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
106: SessionManager.init(permissions);
107: UserStoreManager userStoreManager = new UserStoreManager();
108: UserSessionManager sessionManager = new UserSessionManager(
109: permissions, userStoreManager);
110: LoginManager.init(sessionManager, userStoreManager);
111: // instanciate the thread manager
112: CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(
113: sessionManager, userStoreManager);
114:
115: // add a user to the session for the current thread
116: RoleManager.getInstance();
117:
118: InterceptorFactory.init(permissions, sessionManager,
119: userStoreManager);
120:
121: // add a new user object and add to the permission
122: Set set = new HashSet();
123: set.add("test");
124: UserSession user = new UserSession("test1", set);
125: permissions.putSession(
126: new Long(Thread.currentThread().getId()),
127: new ThreadPermissionSession(new Long(Thread
128: .currentThread().getId()), user));
129:
130: // init the naming director
131: NamingDirector.init(threadGroup);
132:
133: // instanciate the transaction director
134: TransactionDirector transactionDirector = TransactionDirector
135: .init();
136:
137: // init the database source
138: DBSourceManager.init();
139: Context context = new InitialContext();
140: ObjectLockFactory.init();
141: TransactionManager.init();
142: ut = (UserTransaction) context
143: .lookup("java:comp/UserTransaction");
144:
145: // check the singleton
146: CacheRegistry registry = CacheRegistry.init(threadGroup);
147:
148: // setup the registry for this class loader
149: Thread.currentThread().setContextClassLoader(
150: BeanHandler.class.getClassLoader());
151: registry.initCache();
152:
153: // instanciate a bean infomration object
154: BeanInfo beanInfo1 = new BeanInfo();
155: beanInfo1.setCacheResults(true);
156: beanInfo1.setRole("test");
157: beanInfo1.setCacheTimeout(500);
158: BeanInfo beanInfo2 = new BeanInfo();
159: beanInfo2.setCacheResults(false);
160: beanInfo2.setRole("test");
161: beanInfo2.setCacheTimeout(500);
162:
163: // setup the main interface
164: MainInterface1Impl subObject1 = new MainInterface1Impl();
165:
166: BeanHandler handler = new BeanHandler(beanInfo1, subObject1,
167: beanInfo1.getRole(), permissions, BeanHandler.class
168: .getClassLoader());
169: MainInterface1 proxy = (MainInterface1) Proxy.newProxyInstance(
170: BeanHandler.class.getClassLoader(), subObject1
171: .getClass().getInterfaces(), handler);
172:
173: proxy.callVoid();
174:
175: try {
176: proxy.throwException();
177: fail("Failed to throw an exception");
178: } catch (TestProxyException ex) {
179: // worked
180: }
181:
182: // make the test calls on this object
183: if (proxy.getInt() != 1) {
184: fail("Failed to retrieve value 1");
185: }
186: if (!proxy.getString().equals("Test1")) {
187: fail("Failed to retrieve value 1");
188: }
189: TestKey testKey = proxy.getAKeyValue();
190: if (!(testKey.getKey1().equals("1"))
191: && !(testKey.getKey2().equals("key1"))) {
192: fail("Failed to retrieve the key");
193: }
194:
195: SubInterface1 interface1 = proxy.addSubInterface("key1");
196: if (interface1 != proxy.getSubInterface("key1")) {
197: fail("Failed to retrieve from cache");
198: }
199: if (MainInterface1Impl.calledBean != 0) {
200: fail("Failed the bean got called");
201: }
202:
203: SubInterface1 interface2 = proxy.addSubInterface("key2");
204:
205: for (int count = 0; count < 4; count++) {
206: Thread.sleep(450);
207: interface1.getName();
208: }
209: interface1.getName();
210: boolean called = false;
211: try {
212: interface2.getName();
213: called = true;
214: } catch (Exception ex) {
215: System.out.println("Exception " + ex.getMessage());
216: ex.printStackTrace(System.out);
217: }
218: if (called) {
219: fail("Called the sub object successfully");
220: }
221:
222: // create a new proxy
223: handler = new BeanHandler(beanInfo2, subObject1, beanInfo1
224: .getRole(), permissions, BeanHandler.class
225: .getClassLoader());
226: proxy = (MainInterface1) Proxy.newProxyInstance(
227: BeanHandler.class.getClassLoader(), subObject1
228: .getClass().getInterfaces(), handler);
229:
230: MainInterface1Impl.calledBean = 0;
231: interface1 = proxy.getSubInterface("key1");
232: interface1 = proxy.getSubInterface("key1");
233: interface1 = proxy.getSubInterface("key1");
234: if (MainInterface1Impl.calledBean != 3) {
235: fail("Failed to call sub object hitting cache");
236: }
237: interface2 = proxy.addSubInterface("key2");
238:
239: for (int count = 0; count < 4; count++) {
240: Thread.sleep(450);
241: interface1.getName();
242: }
243: interface1.getName();
244: called = false;
245: try {
246: interface2.getName();
247: called = true;
248: } catch (Exception ex) {
249: System.out.println("Exception " + ex.getMessage());
250: ex.printStackTrace(System.out);
251: }
252: if (called) {
253: fail("Called the sub object successfully");
254: }
255:
256: // transaction based methods
257: MainInterface1Impl.calledBean = 0;
258: beanInfo1.setTransaction(true);
259: beanInfo2.setTransaction(true);
260: handler = new BeanHandler(beanInfo1, subObject1, beanInfo1
261: .getRole(), permissions, BeanHandler.class
262: .getClassLoader());
263: proxy = (MainInterface1) Proxy.newProxyInstance(
264: BeanHandler.class.getClassLoader(), subObject1
265: .getClass().getInterfaces(), handler);
266:
267: proxy.callVoid();
268:
269: try {
270: proxy.throwException();
271: fail("Failed to throw an exception");
272: } catch (TestProxyException ex) {
273: // worked
274: }
275:
276: // make the test calls on this object
277: if (proxy.getInt() != 1) {
278: fail("Failed to retrieve value 1");
279: }
280: if (!proxy.getString().equals("Test1")) {
281: fail("Failed to retrieve value 1");
282: }
283: testKey = proxy.getAKeyValue();
284: if (!(testKey.getKey1().equals("1"))
285: && !(testKey.getKey2().equals("key1"))) {
286: fail("Failed to retrieve the key");
287: }
288:
289: interface1 = proxy.addSubInterface("key3");
290: if (interface1 != proxy.getSubInterface("key3")) {
291: fail("Failed to retrieve from cache");
292: }
293: if (MainInterface1Impl.calledBean != 0) {
294: fail("Failed the bean got called");
295: }
296:
297: ut.begin();
298: interface2 = proxy.addSubInterface("key4");
299: ut.commit();
300:
301: for (int count = 0; count < 4; count++) {
302: Thread.sleep(450);
303: interface1.getName();
304: }
305: interface1.getName();
306: called = false;
307: try {
308: interface2.getName();
309: called = true;
310: } catch (Exception ex) {
311: System.out.println("Exception " + ex.getMessage());
312: ex.printStackTrace(System.out);
313: }
314: if (called) {
315: fail("Called the sub object successfully");
316: }
317:
318: // create a new proxy
319: handler = new BeanHandler(beanInfo2, subObject1, beanInfo1
320: .getRole(), permissions, BeanHandler.class
321: .getClassLoader());
322: proxy = (MainInterface1) Proxy.newProxyInstance(
323: BeanHandler.class.getClassLoader(), subObject1
324: .getClass().getInterfaces(), handler);
325:
326: MainInterface1Impl.calledBean = 0;
327: ut.begin();
328: interface1 = proxy.getSubInterface("key3");
329: interface1 = proxy.getSubInterface("key3");
330: interface1 = proxy.getSubInterface("key3");
331: ut.commit();
332: if (MainInterface1Impl.calledBean != 3) {
333: fail("Failed to call sub object hitting cache");
334: }
335: interface2 = proxy.addSubInterface("key4");
336:
337: for (int count = 0; count < 4; count++) {
338: Thread.sleep(450);
339: interface1.getName();
340: }
341: interface1.getName();
342: called = false;
343: try {
344: interface2.getName();
345: called = true;
346: } catch (Exception ex) {
347: System.out.println("Exception " + ex.getMessage());
348: ex.printStackTrace(System.out);
349: }
350: if (called) {
351: fail("Called the sub object successfully");
352: }
353:
354: // terminate the cache registry
355: registry.shutdown();
356: }
357: }
|