1 7 8 package org.jboss.cache.pojo; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.test.Address; 16 import org.jboss.cache.pojo.test.Person; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.naming.Context ; 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 import javax.transaction.NotSupportedException ; 23 import javax.transaction.RollbackException ; 24 import javax.transaction.SystemException ; 25 import javax.transaction.Transaction ; 26 import javax.transaction.UserTransaction ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Properties ; 30 31 36 37 public class NewReplicatedTxTest extends TestCase 38 { 39 Log log = LogFactory.getLog(NewReplicatedTxTest.class); 40 PojoCache cache_; 41 PojoCache cache1_; 42 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 43 DummyTransactionManager tx_mgr; 44 Throwable t1_ex, t2_ex; 45 long start = 0; 46 47 48 public NewReplicatedTxTest(String name) 49 { 50 super(name); 51 } 52 53 protected void setUp() throws Exception 54 { 55 super.setUp(); 56 log.info("setUp() ...."); 57 super.setUp(); 58 Properties prop = new Properties (); 59 prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 60 boolean toStart = false; 61 cache_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 62 cache1_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart); 63 cache_.start(); 64 cache1_.start(); 65 66 tx_mgr = DummyTransactionManager.getInstance(); 67 t1_ex = t2_ex = null; 68 } 69 70 protected void tearDown() throws Exception 71 { 72 super.tearDown(); 73 cache_.stop(); 74 cache1_.stop(); 75 DummyTransactionManager.destroy(); 76 } 77 78 UserTransaction getTransaction() throws SystemException , NotSupportedException , NamingException 79 { 80 Properties prop = new Properties (); 81 prop.put(Context.INITIAL_CONTEXT_FACTORY, 82 "org.jboss.cache.transaction.DummyContextFactory"); 83 return (UserTransaction ) new InitialContext (prop).lookup("UserTransaction"); 84 } 85 86 public void testSimpleTxWithRollback() throws Exception 87 { 88 log.info("testSimpleTxWithRollback() ...."); 89 UserTransaction tx = getTransaction(); 90 91 Person joe = new Person(); 92 joe.setName("Joe"); 93 Address add = new Address(); 94 add.setZip(104); 95 add.setCity("Taipei"); 96 joe.setAddress(add); 97 98 tx.begin(); 99 cache_.attach("/person/joe", joe); 100 tx.commit(); 101 Person p = (Person) cache1_.find("/person/joe"); 102 assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip()); 103 104 Person ben = new Person(); 106 ben.setName("Ben"); 107 add = new Address(); 108 add.setZip(104); 109 add.setCity("Taipei"); 110 joe.setAddress(add); 111 tx.begin(); 112 cache_.attach("/person/ben", ben); 113 tx.rollback(); 114 assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip()); 115 } 116 117 public void testCollectionTxWithRollback() throws Exception 118 { 119 log.info("testCollectionTxWithRollback() ...."); 120 UserTransaction tx = getTransaction(); 121 122 Person joe = new Person(); 123 joe.setName("Joe"); 124 Address add = new Address(); 125 add.setZip(104); 126 add.setCity("Taipei"); 127 joe.setAddress(add); 128 ArrayList <String > lang = new ArrayList <String >(); 129 lang.add("English"); 130 lang.add("Taiwanese"); 131 lang.add("Mandirin"); 132 joe.setLanguages(lang); 133 134 tx.begin(); 135 cache_.attach("/person/joe", joe); 136 tx.commit(); 137 Person p = (Person) cache1_.find("/person/joe"); 138 assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip()); 139 140 Person ben = new Person(); 142 ben.setName("Ben"); 143 add = new Address(); 144 add.setZip(104); 145 add.setCity("Taipei"); 146 ben.setAddress(add); 147 tx.begin(); 148 cache_.attach("/person/ben", ben); 149 tx.rollback(); 150 assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip()); 151 assertEquals("Langue 1 should be: ", "English", joe.getLanguages().get(0)); 152 } 153 154 155 public void testConcurrentPutsWithRollback() throws Exception 156 { 157 Thread t1 = new Thread () 158 { 159 public void run() 160 { 161 Person p = new Person(); 162 p.setName("Ben"); 163 Address add = new Address(); 164 add.setCity("Taipei"); 165 p.setAddress(add); 166 List <String > lang = new ArrayList <String >(); 167 lang.add("English"); 168 lang.add("Taiwanese"); 169 lang.add("Japanese"); 170 p.setLanguages(lang); 171 try 172 { 173 cache_.attach("/test/ben", p); 174 } 175 catch (Exception ex) 176 { 177 try 178 { 179 throw new RuntimeException ("PojoCache is not rolling back properly for Collection yet."); 180 } 182 catch (Exception ex1) 183 { 184 t1_ex = ex1; 185 } 186 } 187 } 188 }; 189 190 Thread t2 = new Thread () 191 { 192 Transaction tx; 193 194 public void run() 195 { 196 try 197 { 198 UserTransaction tx = getTransaction(); 199 tx.begin(); 200 Person p = new Person(); 201 p.setName("Ben"); 202 Address add = new Address(); 203 add.setCity("Taipei"); 204 p.setAddress(add); 205 cache1_.attach("/test/ben", p); 206 TestingUtil.sleepThread(1000); 207 tx.commit(); 208 } 209 catch (RollbackException rollback) 210 { 211 } 212 catch (Exception ex) 213 { 214 t2_ex = ex; 215 } 216 } 217 }; 218 219 t1.start(); 220 t2.start(); 221 222 t1.join(); 223 t2.join(); 224 225 if (t1_ex != null) 227 { 228 fail("Thread1 failed: " + t1_ex); 229 } 230 if (t2_ex != null) 231 { 232 fail("Thread2 failed: " + t2_ex); 233 } 234 } 235 236 public void testConcurrentTxPutsWithRollback() throws Exception 237 { 238 Thread t1 = new Thread () 239 { 240 Transaction tx; 241 242 public void run() 243 { 244 UserTransaction tx = null; 245 Person p = new Person(); 246 p.setName("Ben"); 247 Address add = new Address(); 248 add.setCity("Taipei"); 249 p.setAddress(add); 250 List <String > lang = new ArrayList <String >(); 251 lang.add("English"); 252 lang.add("Taiwanese"); 253 lang.add("Japanese"); 254 p.setLanguages(lang); 255 try 256 { 257 tx = getTransaction(); 258 tx.begin(); 259 cache_.attach("/test/ben", p); 260 TestingUtil.sleepThread(500); 261 tx.commit(); 262 } 263 catch (RollbackException rollback) 264 { 265 try 266 { 267 tx = getTransaction(); 268 tx.begin(); 269 cache_.attach("/test/ben", p); 271 tx.commit(); 272 } 273 catch (Exception ex) 274 { 275 t1_ex = ex; 276 ex.printStackTrace(); 277 } 278 } 279 catch (Exception ex) 280 { 281 t1_ex = ex; 282 ex.printStackTrace(); 283 } 284 } 285 }; 286 287 Thread t2 = new Thread () 288 { 289 Transaction tx; 290 291 public void run() 292 { 293 try 294 { 295 UserTransaction tx = getTransaction(); 296 tx.begin(); 297 Person p = new Person(); 298 p.setName("Ben"); 299 Address add = new Address(); 300 add.setCity("Taipei"); 301 p.setAddress(add); 302 cache1_.attach("/test/ben", p); 303 TestingUtil.sleepThread(1000); 304 tx.commit(); 305 } 306 catch (RollbackException rollback) 307 { 308 } 309 catch (Exception ex) 310 { 311 t2_ex = ex; 312 } 313 } 314 }; 315 316 t1.start(); 317 t2.start(); 318 319 t1.join(); 320 t2.join(); 321 322 if (t1_ex != null) 324 { 325 fail("Thread1 failed: " + t1_ex); 326 } 327 if (t2_ex != null) 328 { 329 fail("Thread2 failed: " + t2_ex); 330 } 331 } 332 333 public void testConcurrentTxPutsWithRollbackField() throws Exception 334 { 335 Person p = new Person(); 336 p.setName("Ben"); 337 Address add = new Address(); 338 add.setCity("Taipei"); 339 p.setAddress(add); 340 List <String > lang = new ArrayList <String >(); 341 lang.add("1"); 342 p.setLanguages(lang); 343 cache_.attach("/test/ben", p); 344 345 Thread t1 = new Thread () 346 { 347 Transaction tx; 348 349 public void run() 350 { 351 UserTransaction tx = null; 352 List <String > lang = null; 353 try 354 { 355 Person ben = (Person) cache_.find("/test/ben"); 356 lang = ben.getLanguages(); 357 358 tx = getTransaction(); 359 tx.begin(); 360 lang.add("2"); 361 lang.add("3"); 362 lang.remove(0); 363 TestingUtil.sleepThread(1000); 364 tx.commit(); 365 } 366 catch (RollbackException rollback) 367 { 368 try 369 { 370 tx = getTransaction(); 371 tx.begin(); 372 lang.add("2"); 373 lang.add("3"); 374 lang.remove(0); 375 tx.commit(); 376 } 377 catch (Exception ex) 378 { 379 t1_ex = ex; 380 } 381 } 382 catch (Exception ex) 383 { 384 t1_ex = ex; 385 } 386 } 387 }; 388 389 Thread t2 = new Thread () 390 { 391 Transaction tx; 392 393 public void run() 394 { 395 List <String > lang = null; 396 UserTransaction tx = null; 397 try 398 { 399 Person ben = (Person) cache1_.find("/test/ben"); 400 lang = ben.getLanguages(); 401 402 tx = getTransaction(); 403 tx.begin(); 404 lang.add("2"); 405 TestingUtil.sleepThread(1000); 406 tx.commit(); 407 } 408 catch (RollbackException rollback) 409 { 410 TestingUtil.sleepThread(1000); 411 try 412 { 413 tx = getTransaction(); 414 tx.begin(); 415 lang.add("2"); 416 tx.commit(); 417 } 418 catch (Exception e) 419 { 420 e.printStackTrace(); 421 } 422 } 423 catch (Exception ex) 424 { 425 t2_ex = ex; 426 } 427 } 428 }; 429 430 t1.start(); 431 t2.start(); 432 433 t1.join(); 434 t2.join(); 435 436 if (t1_ex != null) 438 { 439 fail("Thread1 failed: " + t1_ex); 440 } 441 if (t2_ex != null) 442 { 443 fail("Thread2 failed: " + t2_ex); 444 } 445 } 446 447 public static Test suite() throws Exception 448 { 449 return new TestSuite(NewReplicatedTxTest.class); 450 } 451 452 453 public static void main(String [] args) throws Exception 454 { 455 junit.textui.TestRunner.run(NewReplicatedTxTest.suite()); 456 } 457 458 } 459 | Popular Tags |