1 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 ; 30 import javax.naming.InitialContext ; 31 import junit.framework.*; 32 import java.lang.ClassLoader ; 33 import java.lang.reflect.InvocationHandler ; 34 import java.lang.reflect.Method ; 35 import java.util.Date ; 36 import java.util.HashSet ; 37 import java.util.Set ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 import javax.transaction.UserTransaction ; 41 import java.lang.reflect.Proxy ; 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 76 public class BeanHandlerTest extends TestCase { 77 78 private UserTransaction ut = null; 79 80 81 public BeanHandlerTest(String testName) { 82 super(testName); 83 } 84 85 protected void setUp() throws Exception { 86 } 87 88 protected void tearDown() throws Exception { 89 } 90 91 public static Test suite() { 92 TestSuite suite = new TestSuite(BeanHandlerTest.class); 93 94 return suite; 95 } 96 97 100 public void testBeanHandler() throws Exception { 101 System.out.println("testBeanHandler"); 102 103 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 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 112 userStoreManager); 113 114 RoleManager.getInstance(); 116 117 InterceptorFactory.init(permissions,sessionManager,userStoreManager); 118 119 Set set = new HashSet (); 121 set.add("test"); 122 UserSession user = new UserSession("test1", set); 123 permissions.putSession(new Long (Thread.currentThread().getId()), 124 new ThreadPermissionSession( 125 new Long (Thread.currentThread().getId()),user)); 126 127 NamingDirector.init(threadGroup); 129 130 TransactionDirector transactionDirector = TransactionDirector.init(); 132 133 DBSourceManager.init(); 135 Context context = new InitialContext (); 136 ObjectLockFactory.init(); 137 TransactionManager.init(); 138 ut = (UserTransaction )context.lookup("java:comp/UserTransaction"); 139 140 CacheRegistry registry = CacheRegistry.init(threadGroup); 142 143 Thread.currentThread().setContextClassLoader( 145 BeanHandler.class.getClassLoader()); 146 registry.initCache(); 147 148 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 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 } 176 177 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 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 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 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 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 } 271 272 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 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 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 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 registry.shutdown(); 352 } 353 } 354 | Popular Tags |