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