1 23 24 package com.rift.coad.lib.bean; 26 27 import javax.naming.InitialContext ; 29 import javax.naming.Context ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.Statement ; 33 import javax.sql.DataSource ; 34 import java.util.Set ; 35 import java.util.HashSet ; 36 import javax.transaction.UserTransaction ; 37 import java.sql.Timestamp ; 38 import java.util.ArrayList ; 39 import java.util.Date ; 40 import java.util.Enumeration ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Set ; 45 import javax.transaction.xa.XAException ; 46 import javax.transaction.xa.XAResource ; 47 import javax.transaction.xa.Xid ; 48 import org.apache.log4j.Logger; 49 import org.apache.log4j.BasicConfigurator; 50 51 import junit.framework.*; 53 54 import org.objectweb.jotm.Jotm; 56 57 import com.rift.coad.lib.naming.NamingDirector; 59 import com.rift.coad.lib.naming.ContextManager; 60 import com.rift.coad.lib.db.DBSourceManager; 61 import com.rift.coad.lib.common.ObjectSerializer; 62 import com.rift.coad.lib.cache.CacheEntry; 63 import com.rift.coad.lib.interceptor.InterceptorFactory; 64 import com.rift.coad.lib.security.RoleManager; 65 import com.rift.coad.lib.security.ThreadsPermissionContainer; 66 import com.rift.coad.lib.security.ThreadPermissionSession; 67 import com.rift.coad.lib.security.UserSession; 68 import com.rift.coad.lib.security.user.UserSessionManager; 69 import com.rift.coad.lib.security.user.UserStoreManager; 70 import com.rift.coad.lib.security.SessionManager; 71 import com.rift.coad.lib.security.login.LoginManager; 72 import com.rift.coad.lib.thread.CoadunationThreadGroup; 73 import com.rift.coad.lib.transaction.TransactionDirector; 74 import com.rift.coad.util.lock.ObjectLockFactory; 75 import com.rift.coad.util.transaction.TransactionManager; 76 77 81 public class TransactionBeanCacheTest extends TestCase { 82 83 86 public static class TestCacheEntry implements CacheEntry, java.rmi.Remote { 87 88 private Date touchTime = new Date (); 90 public static int count = 0; 91 92 95 public TestCacheEntry() { 96 97 } 98 99 100 103 public void touch() { 104 touchTime = new Date (); 105 } 106 107 108 114 public boolean isExpired(Date expiryDate) { 115 System.out.println("Touch time : " + touchTime.getTime() + 116 " expiry date : " + expiryDate.getTime()); 117 return touchTime.getTime() < expiryDate.getTime(); 118 } 119 120 121 124 public void cacheRelease() { 125 count++; 126 } 127 } 128 129 130 private TransactionBeanCache instance = null; 132 private UserTransaction ut = null; 133 private boolean addedEntry = false; 134 private boolean caughtException = false; 135 private boolean gotCacheEntry= false; 136 137 138 public TransactionBeanCacheTest(String testName) { 139 super(testName); 140 } 141 142 protected void setUp() throws Exception { 143 } 144 145 protected void tearDown() throws Exception { 146 } 147 148 149 public static Test suite() { 150 TestSuite suite = new TestSuite(TransactionBeanCacheTest.class); 151 152 return suite; 153 } 154 155 158 public void testCache() throws Exception { 159 System.out.println("TransactionBeanCacheTest"); 160 161 ThreadsPermissionContainer permissions = new ThreadsPermissionContainer(); 163 SessionManager.init(permissions); 164 UserStoreManager userStoreManager = new UserStoreManager(); 165 UserSessionManager sessionManager = new UserSessionManager(permissions, 166 userStoreManager); 167 LoginManager.init(sessionManager,userStoreManager); 168 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 170 userStoreManager); 171 172 RoleManager.getInstance(); 174 175 InterceptorFactory.init(permissions,sessionManager,userStoreManager); 176 177 Set set = new HashSet (); 179 set.add("test"); 180 UserSession user = new UserSession("test1", set); 181 permissions.putSession(new Long (Thread.currentThread().getId()), 182 new ThreadPermissionSession( 183 new Long (Thread.currentThread().getId()),user)); 184 185 NamingDirector.init(threadGroup); 187 188 TransactionDirector transactionDirector = TransactionDirector.init(); 190 191 DBSourceManager.init(); 193 Context context = new InitialContext (); 194 ObjectLockFactory.init(); 195 TransactionManager.init(); 196 197 ut = (UserTransaction )context.lookup("java:comp/UserTransaction"); 198 199 200 instance = new TransactionBeanCache(); 202 203 ut.begin(); 204 TestCacheEntry bob = new TestCacheEntry(); 205 instance.addCacheEntry(500,"bob","bob",bob); 206 Thread testThread = new Thread (new Runnable () { 207 public void run() { 208 try { 209 ut.begin(); 210 instance.addCacheEntry(500,"bob","bob",new TestCacheEntry()); 211 addedEntry = true; 212 ut.commit(); 213 } catch (Exception ex) { 214 System.out.println("Failed to get the queue reference : " + 215 ex.getMessage()); 216 ex.printStackTrace(System.out); 217 caughtException = true; 218 try { 219 ut.rollback(); 220 } catch (Exception ex2) { 221 System.out.println( 222 "Failed to roll the transaction back : " + 223 ex2.getMessage()); 224 } 225 } 226 } 227 }); 228 testThread.start(); 229 230 Thread.sleep(500); 231 if (addedEntry) { 232 fail("An entry was added"); 233 } else if (caughtException) { 234 fail("Caught an exception"); 235 } 236 ut.commit(); 237 Thread.sleep(500); 238 if (addedEntry) { 239 fail("An entry was added"); 240 } else if (caughtException == false) { 241 fail("Failed to catch an exception"); 242 } 243 244 245 ut.begin(); 246 try { 247 instance.addCacheEntry(500,"bob","bob",bob); 248 fail("The second add must fail"); 249 } catch (BeanException ex) { 250 System.out.println(ex.getMessage()); 251 } 252 253 254 255 ut.commit(); 256 TestCacheEntry fred = new TestCacheEntry(); 257 ut.begin(); 258 instance.addCacheEntry(500,"fred","fred",fred); 259 try { 260 instance.addCacheEntry(500,"fred","fred",fred); 261 fail("The second add must fail"); 262 } catch (BeanException ex) { 263 System.out.println(ex.getMessage()); 264 } 265 ut.commit(); 266 TestCacheEntry mary = new TestCacheEntry(); 267 ut.begin(); 268 instance.addCacheEntry(500,"mary","mary","mary",mary); 269 ut.commit(); 270 271 ut.begin(); 272 if (bob != instance.getCacheEntry("bob").getCacheEntry()) { 273 fail("bob could not be found"); 274 } 275 instance.getCacheEntry("bob").setCacheEntry(mary); 276 caughtException = false; 277 testThread = new Thread (new Runnable () { 278 public void run() { 279 try { 280 ut.begin(); 281 instance.getCacheEntry("bob"); 282 gotCacheEntry = true; 283 ut.commit(); 284 } catch (Exception ex) { 285 System.out.println("Failed to get the queue reference : " + 286 ex.getMessage()); 287 ex.printStackTrace(System.out); 288 caughtException = true; 289 try { 290 ut.rollback(); 291 } catch (Exception ex2) { 292 System.out.println( 293 "Failed to roll the transaction back : " + 294 ex2.getMessage()); 295 } 296 } 297 } 298 }); 299 testThread.start(); 300 301 Thread.sleep(500); 302 if (gotCacheEntry) { 303 fail("Managed to retrieve the cache entry"); 304 } else if (caughtException) { 305 fail("Caught an exception"); 306 } 307 ut.commit(); 308 Thread.sleep(500); 309 if (!gotCacheEntry) { 310 fail("Failed to retrieve the cache entry"); 311 } else if (caughtException) { 312 fail("Caught an exception"); 313 } 314 315 ut.begin(); 316 if (mary != instance.getCacheEntry("bob").getCacheEntry()) { 317 fail("mary could not be found"); 318 } 319 ut.commit(); 320 ut.begin(); 321 if (mary != instance.getCacheEntry("mary").getBeanHandler()) { 322 fail("mary could not be found"); 323 } 324 instance.getCacheEntry("mary").setBeanHandler(bob); 325 ut.commit(); 326 ut.begin(); 327 if (bob != instance.getCacheEntry("mary").getBeanHandler()) { 328 fail("bob could not be found"); 329 } 330 if (!instance.getCacheEntry("mary").getProxy().equals("mary")) { 331 fail("mary could not be found"); 332 } 333 instance.getCacheEntry("mary").setProxy("mary1"); 334 if (!instance.getCacheEntry("mary").getProxy().equals("mary1")) { 335 fail("mary1 could not be found"); 336 } 337 ut.commit(); 338 339 for (int count = 0; count < 4; count++) { 340 Thread.sleep(500); 341 bob.touch(); 342 mary.touch(); 343 } 344 345 instance.garbageCollect(); 346 347 ut.begin(); 348 if (!instance.contains("bob")) { 349 fail("bob could not be found"); 350 } 351 if (!instance.contains("mary")) { 352 fail("mary could not be found"); 353 } 354 if (instance.contains("fred")) { 355 fail("fred was found"); 356 } 357 ut.commit(); 358 359 ut.begin(); 360 instance.removeCacheEntry("bob"); 361 instance.addCacheEntry(500,"fred","fred",new TestCacheEntry()); 362 if (!instance.contains("fred")) { 363 fail("fred was found"); 364 } 365 if (instance.contains("bob")) { 366 fail("bob was found"); 367 } 368 ut.rollback(); 369 ut.begin(); 370 if (instance.contains("fred")) { 371 fail("fred was found"); 372 } 373 if (!instance.contains("bob")) { 374 fail("bob was not found"); 375 } 376 ut.commit(); 377 instance.clear(); 378 ut.begin(); 379 if (instance.contains("bob")) { 380 fail("bob could still be found"); 381 } 382 383 if (instance.contains("mary")) { 384 fail("bob could still be found"); 385 } 386 try { 387 instance.addCacheEntry(500,"mary","mary","mary",mary); 388 fail("Could add an entry should not be allowed"); 389 } catch (BeanException ex) { 390 } 392 try { 393 instance.addCacheEntry(500,"bob","bob",bob); 394 fail("Could add an entry should not be allowed"); 395 } catch (BeanException ex) { 396 } 398 399 if (TestCacheEntry.count != 4) { 400 fail("Release not called on all classes [" + TestCacheEntry.count 401 + "]"); 402 } 403 ut.rollback(); 404 } 405 406 407 } 408 | Popular Tags |