1 23 24 package com.rift.coad.lib.bean; 25 26 import javax.naming.InitialContext ; 28 import javax.naming.Context ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.Statement ; 32 import javax.sql.DataSource ; 33 import java.util.Set ; 34 import java.util.HashSet ; 35 import javax.transaction.UserTransaction ; 36 import java.sql.Timestamp ; 37 import java.util.ArrayList ; 38 import java.util.Date ; 39 import java.util.Enumeration ; 40 import java.util.HashSet ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Set ; 44 import javax.transaction.xa.XAException ; 45 import javax.transaction.xa.XAResource ; 46 import javax.transaction.xa.Xid ; 47 import org.apache.log4j.Logger; 48 import org.apache.log4j.BasicConfigurator; 49 50 import junit.framework.*; 52 53 import org.objectweb.jotm.Jotm; 55 56 import com.rift.coad.lib.naming.NamingDirector; 58 import com.rift.coad.lib.naming.ContextManager; 59 import com.rift.coad.lib.db.DBSourceManager; 60 import com.rift.coad.lib.common.ObjectSerializer; 61 import com.rift.coad.lib.cache.CacheEntry; 62 import com.rift.coad.lib.interceptor.InterceptorFactory; 63 import com.rift.coad.lib.security.RoleManager; 64 import com.rift.coad.lib.security.ThreadsPermissionContainer; 65 import com.rift.coad.lib.security.ThreadPermissionSession; 66 import com.rift.coad.lib.security.UserSession; 67 import com.rift.coad.lib.security.user.UserSessionManager; 68 import com.rift.coad.lib.security.user.UserStoreManager; 69 import com.rift.coad.lib.security.SessionManager; 70 import com.rift.coad.lib.security.login.LoginManager; 71 import com.rift.coad.lib.thread.CoadunationThreadGroup; 72 import com.rift.coad.lib.transaction.TransactionDirector; 73 import com.rift.coad.util.lock.ObjectLockFactory; 74 import com.rift.coad.util.transaction.TransactionManager; 75 76 80 public class TransactionBeanCacheManagerTest extends TestCase { 81 82 85 public static class TestCacheEntry implements CacheEntry, java.rmi.Remote { 86 87 private Date touchTime = new Date (); 89 public static int count = 0; 90 91 94 public TestCacheEntry() { 95 96 } 97 98 99 102 public void touch() { 103 touchTime = new Date (); 104 } 105 106 107 113 public boolean isExpired(Date expiryDate) { 114 System.out.println("Touch time : " + touchTime.getTime() + 115 " expiry date : " + expiryDate.getTime()); 116 return touchTime.getTime() < expiryDate.getTime(); 117 } 118 119 120 123 public void cacheRelease() { 124 count++; 125 } 126 } 127 128 private UserTransaction ut = null; 130 131 public TransactionBeanCacheManagerTest(String testName) { 132 super(testName); 133 } 134 135 protected void setUp() throws Exception { 136 } 137 138 protected void tearDown() throws Exception { 139 } 140 141 public static Test suite() { 142 TestSuite suite = new TestSuite(TransactionBeanCacheManagerTest.class); 143 144 return suite; 145 } 146 147 150 public void testBeanCacheManager() throws Exception { 151 System.out.println("TransactionBeanCacheManager"); 152 153 ThreadsPermissionContainer permissions = new ThreadsPermissionContainer(); 155 SessionManager.init(permissions); 156 UserStoreManager userStoreManager = new UserStoreManager(); 157 UserSessionManager sessionManager = new UserSessionManager(permissions, 158 userStoreManager); 159 LoginManager.init(sessionManager,userStoreManager); 160 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 162 userStoreManager); 163 164 RoleManager.getInstance(); 166 167 InterceptorFactory.init(permissions,sessionManager,userStoreManager); 168 169 Set set = new HashSet (); 171 set.add("test"); 172 UserSession user = new UserSession("test1", set); 173 permissions.putSession(new Long (Thread.currentThread().getId()), 174 new ThreadPermissionSession( 175 new Long (Thread.currentThread().getId()),user)); 176 177 NamingDirector.init(threadGroup); 179 180 TransactionDirector transactionDirector = TransactionDirector.init(); 182 183 DBSourceManager.init(); 185 Context context = new InitialContext (); 186 ObjectLockFactory.init(); 187 TransactionManager.init(); 188 189 ut = (UserTransaction )context.lookup("java:comp/UserTransaction"); 190 191 Object ref = null; 192 TransactionBeanCacheManager instance = new TransactionBeanCacheManager(); 193 194 TransactionBeanCache beanCache1 = instance.getBeanCache("beanCache1"); 195 TransactionBeanCache beanCache2 = instance.getBeanCache("beanCache2"); 196 if (instance.getBeanCache("beanCache1") != beanCache1) { 197 fail("Failed to retrieve the bean cache result object"); 198 } 199 if (!instance.contains("beanCache1")) { 200 fail("The instance does not contain beanCache1"); 201 } 202 if (instance.getBeanCache("beanCache2") != beanCache2) { 203 fail("Failed to retrieve the bean cache 2 result object"); 204 } 205 if (!instance.contains("beanCache2")) { 206 fail("The instance does not contain beanCache2"); 207 } 208 209 ut.begin(); 210 TestCacheEntry bob = new TestCacheEntry(); 211 beanCache1.addCacheEntry(500,"bob","bob",bob); 212 System.out.println("Class : " + beanCache1.getClass().getName()); 213 ut.commit(); 214 ut.begin(); 215 TestCacheEntry fred = new TestCacheEntry(); 216 beanCache1.addCacheEntry(500,"fred","fred",fred); 217 ut.commit(); 218 ut.begin(); 219 TestCacheEntry mary = new TestCacheEntry(); 220 beanCache2.addCacheEntry(500,"mary","mary","mary",mary); 221 ut.commit(); 222 ut.begin(); 223 TestCacheEntry jill = new TestCacheEntry(); 224 beanCache2.addCacheEntry(500,"jill","jill","jill",jill); 225 ut.commit(); 226 227 ut.begin(); 228 if (!beanCache1.contains("bob")) { 229 fail("bob could not be found"); 230 } 231 232 if (!beanCache2.contains("mary")) { 233 fail("mary could not be found"); 234 } 235 if (!beanCache1.contains("fred")) { 236 fail("fred was found"); 237 } 238 if (!beanCache2.contains("jill")) { 239 fail("jill was found"); 240 } 241 ut.commit(); 242 for (int count = 0; count < 4; count++) { 243 Thread.sleep(450); 244 bob.touch(); 245 mary.touch(); 246 } 247 248 instance.garbageCollect(); 249 ut.begin(); 250 if (!beanCache1.contains("bob")) { 251 fail("bob could not be found"); 252 } 253 if (!beanCache2.contains("mary")) { 254 fail("mary could not be found"); 255 } 256 if (beanCache1.contains("fred")) { 257 fail("fred was found"); 258 } 259 if (beanCache2.contains("jill")) { 260 fail("jill was found"); 261 } 262 ut.commit(); 263 264 instance.clear(); 265 266 ut.begin(); 267 if (beanCache1.contains("bob")) { 268 fail("bob could still be found"); 269 } 270 if (beanCache2.contains("mary")) { 271 fail("mary could still be found"); 272 } 273 ut.commit(); 274 try { 275 ut.begin(); 276 beanCache1.addCacheEntry(500,"mary","mary","mary",mary); 277 ut.commit(); 278 fail("Could add an entry should not be allowed"); 279 280 } catch (BeanException ex) { 281 ut.rollback(); 282 } 283 try { 284 ut.begin(); 285 286 beanCache2.addCacheEntry(500,"bob","bob",bob); 287 fail("Could add an entry should not be allowed"); 288 } catch (BeanException ex) { 289 ut.rollback(); 290 } 291 292 try { 293 ut.begin(); 294 beanCache2 = instance.getBeanCache("beanCache2"); 295 ut.commit(); 296 fail("Could add an entry to the bean cache manager."); 297 } catch (BeanException ex) { 298 ut.rollback(); 299 } 300 301 if (TestCacheEntry.count != 4) { 302 fail ("Release not called on all classes"); 303 } 304 } 305 306 307 } 308 | Popular Tags |