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