Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 525   Methods: 22
NCLOC: 432   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LocalTxTest.java 42.9% 86.9% 90.9% 84.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source.
 3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 4    * as indicated by the @author tags. See the copyright.txt file in the
 5    * distribution for a full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 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.CacheSPI;
 30    import org.jboss.cache.Fqn;
 31    import org.jboss.cache.pojo.PojoCache;
 32    import org.jboss.cache.pojo.test.Address;
 33    import org.jboss.cache.pojo.test.Person;
 34    import org.jboss.cache.pojo.util.CacheApiUtil;
 35    import org.jboss.cache.transaction.DummyTransactionManager;
 36   
 37    import javax.naming.Context;
 38    import javax.naming.InitialContext;
 39    import javax.naming.NamingException;
 40    import javax.transaction.NotSupportedException;
 41    import javax.transaction.RollbackException;
 42    import javax.transaction.SystemException;
 43    import javax.transaction.Transaction;
 44    import javax.transaction.UserTransaction;
 45    import java.util.ArrayList;
 46    import java.util.List;
 47    import java.util.Properties;
 48    import java.util.concurrent.CountDownLatch;
 49   
 50    /**
 51    */
 52    public class LocalTxTest extends AbstractOptimisticTestCase
 53    {
 54    Log log = LogFactory.getLog(LocalTxTest.class);
 55    PojoCache cache;
 56    final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
 57    DummyTransactionManager tx_mgr;
 58    Throwable t1_ex, t2_ex;
 59    long start = 0;
 60   
 61   
 62  8 public LocalTxTest(String name)
 63    {
 64  8 super(name);
 65    }
 66   
 67  8 protected void setUp() throws Exception
 68    {
 69  8 super.setUp();
 70  8 log.info("setUp() ....");
 71   
 72  8 cache = createCache();
 73    // cache = createPessimisticCache();
 74   
 75  8 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 76   
 77  8 tx_mgr = DummyTransactionManager.getInstance();
 78  8 t1_ex = t2_ex = null;
 79    }
 80   
 81  8 protected void tearDown()
 82    {
 83  8 super.tearDown();
 84  8 cache.stop();
 85   
 86  8 DummyTransactionManager.destroy();
 87    }
 88   
 89    // public void testDummy() {}
 90   
 91  12 UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
 92    {
 93  12 Properties prop = new Properties();
 94  12 prop.put(Context.INITIAL_CONTEXT_FACTORY,
 95    "org.jboss.cache.transaction.DummyContextFactory");
 96  12 return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
 97    }
 98   
 99  9 private Person createPerson(String id, String name, int age)
 100    {
 101  9 Person p = new Person();
 102  9 p.setName(name);
 103  9 p.setAge(age);
 104  9 cache.attach(id, p);
 105  9 return p;
 106    }
 107   
 108  1 public void testSimple() throws Exception
 109    {
 110  1 log.info("testSimple() ....");
 111  1 UserTransaction tx = getTransaction();
 112  1 tx.begin();
 113  1 Person p = createPerson("/person/test1", "Harald Gliebe", 32);
 114  1 tx.commit();
 115  1 tx.begin();
 116  1 p.setName("Benoit");
 117  1 tx.commit();
 118  1 Person p1 = (Person) cache.find("/person/test1");
 119  1 assertEquals("Benoit", p.getName());
 120  1 assertEquals("Benoit", p1.getName());
 121  1 tx.begin();
 122  1 p1.setAge(41);
 123  1 tx.commit();
 124  1 assertEquals(41, p.getAge());
 125  1 assertEquals(41, p1.getAge());
 126    }
 127   
 128  1 public void testFailure() throws Exception
 129    {
 130  1 log.info("testFailure() ....");
 131  1 UserTransaction tx = getTransaction();
 132  1 tx.begin();
 133  1 Person p = createPerson("/person/test1", "Harald Gliebe", 32);
 134  1 tx.commit();
 135   
 136  1 tx.begin();
 137  1 p = createPerson("/person/test1", "Harald Gliebe", 32);
 138  1 tx.commit();
 139    }
 140   
 141  1 public void testFailure1() throws Exception
 142    {
 143  1 log.info("testFailure1() ....");
 144  1 UserTransaction tx = getTransaction();
 145  1 org.jboss.cache.Fqn f = new org.jboss.cache.Fqn("/person/test2");
 146  1 tx.begin();
 147  1 cache.getCache().put(f, "test", "test");
 148  1 tx.commit();
 149   
 150  1 tx.begin();
 151  1 cache.getCache().removeNode(f);
 152  1 cache.getCache().put(f, "test", "test");
 153  1 tx.commit();
 154    }
 155   
 156  1 public void testFailure2() throws Exception
 157    {
 158  1 Fqn f0 = Fqn.fromString("/person/test");
 159  1 Fqn f1 = new Fqn(f0, "1");
 160  1 Fqn f2 = new Fqn(f0, "2");
 161   
 162  1 cache.getCache().put(f1, "test", "test");
 163  1 cache.getCache().put(f2, "test", "test");
 164   
 165  1 int size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 166  1 assertEquals("Size ", 2, size);
 167   
 168  1 UserTransaction tx = getTransaction();
 169  1 tx.begin();
 170  1 cache.getCache().removeNode(f1);
 171  1 size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 172  1 assertEquals("Size ", 1, size);
 173  1 cache.getCache().put(f1, "test", "test");
 174  1 size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 175  1 assertEquals("Size ", 2, size);
 176  1 tx.commit();
 177    }
 178   
 179  1 public void testModification() throws Exception
 180    {
 181  1 UserTransaction tx = getTransaction();
 182  1 tx.begin();
 183  1 Person p = createPerson("/person/test2", "Harald", 32);
 184  1 p.setName("Harald Gliebe");
 185  1 tx.commit();
 186  1 Person p1 = (Person) cache.find("/person/test2");
 187  1 tx.begin();
 188  1 p1.setName("Benoit");
 189  1 tx.commit();
 190  1 assertEquals(p.getName(), "Benoit");
 191  1 assertEquals(p1.getName(), "Benoit");
 192  1 tx.begin();
 193  1 p1.setName("Harald");
 194  1 tx.rollback();
 195  1 assertEquals(p.getName(), "Benoit");
 196  1 assertEquals(p1.getName(), "Benoit");
 197    }
 198   
 199  1 public void testConcurrentSimplePutsI() throws Exception
 200    {
 201  1 final CountDownLatch latch1 = new CountDownLatch(1);
 202  1 final CountDownLatch latch2 = new CountDownLatch(1);
 203  1 Thread t1 = new Thread("t1")
 204    {
 205    Transaction tx;
 206   
 207  1 public void run()
 208    {
 209  1 try
 210    {
 211  1 Person p = createPerson("/person/test6", "p6", 41);
 212  1 Address addr = new Address();
 213  1 addr.setCity("San Jose");
 214  1 List list = new ArrayList();
 215  1 list.add("English");
 216  1 list.add("French");
 217  1 p.setLanguages(list);
 218  1 UserTransaction tx = getTransaction();
 219  1 tx.begin();
 220  1 p.setAddress(addr);
 221  1 latch1.countDown();
 222  1 latch2.await();
 223  1 tx.commit();
 224    }
 225    catch (RollbackException rollback)
 226    {
 227    ;
 228    }
 229    catch (Exception ex)
 230    {
 231  0 t1_ex = ex;
 232    }
 233    finally
 234    {
 235  1 latch1.countDown();
 236    }
 237    }
 238    };
 239   
 240  1 Thread t2 = new Thread("t2")
 241    {
 242    Transaction tx;
 243   
 244  1 public void run()
 245    {
 246  1 UserTransaction tx = null;
 247  1 try
 248    {
 249  1 latch1.await();
 250  1 Person p = createPerson("/person/test7", "p7", 40);
 251  1 Address addr = new Address();
 252  1 addr.setCity("Santa Clara");
 253  1 tx = getTransaction();
 254  1 tx.begin();
 255  1 p.setAddress(addr);
 256  1 tx.commit();
 257    }
 258    catch (RollbackException rollback)
 259    {
 260    ;
 261    }
 262    catch (Exception ex)
 263    {
 264    // t2_ex = ex;
 265  0 try
 266    {
 267  0 tx.rollback();
 268    }
 269    catch (SystemException e)
 270    {
 271  0 e.printStackTrace();
 272  0 t2_ex = e;
 273    }
 274    }
 275    finally
 276    {
 277  1 latch2.countDown();
 278    }
 279    }
 280    };
 281   
 282  1 Person p = createPerson("/person/test5", "p5", 30);
 283   
 284  1 t1.start();
 285  1 t2.start();
 286   
 287  1 t1.join();
 288  1 t2.join();
 289   
 290    // t2 should rollback due to timeout while t2 should succeed
 291  1 if (t2_ex != null)
 292  0 fail("Thread2 failed: " + t2_ex);
 293  1 if (t1_ex != null)
 294  0 fail("Thread1 failed: " + t1_ex);
 295   
 296    }
 297   
 298  1 public void testConcurrentSimplePutsII() throws Exception
 299    {
 300  1 final CountDownLatch latch1 = new CountDownLatch(1);
 301  1 final CountDownLatch latch2 = new CountDownLatch(1);
 302  1 Thread t1 = new Thread("t1")
 303    {
 304    Transaction tx;
 305   
 306  1 public void run()
 307    {
 308  1 try
 309    {
 310  1 Person p = (Person) cache.find("/person/test6");
 311  1 Address addr = new Address();
 312  1 addr.setCity("San Jose");
 313  1 UserTransaction tx = getTransaction();
 314  1 tx.begin();
 315  1 p.setAddress(addr);
 316  1 latch1.countDown();
 317  1 latch2.await();
 318  1 tx.commit();
 319    }
 320    catch (RollbackException rollback)
 321    {
 322    ;
 323    }
 324    catch (Exception ex)
 325    {
 326  0 t1_ex = ex;
 327    }
 328    finally
 329    {
 330  1 latch1.countDown();
 331    }
 332    }
 333    };
 334   
 335  1 Thread t2 = new Thread("t2")
 336    {
 337    Transaction tx;
 338   
 339  1 public void run()
 340    {
 341  1 UserTransaction tx = null;
 342  1 try
 343    {
 344  1 latch1.await();
 345  1 Person p = (Person) cache.find("/person/test6");
 346  1 Address addr = new Address();
 347  1 addr.setCity("Santa Clara");
 348  1 tx = getTransaction();
 349  1 tx.begin();
 350  1 p.setAddress(addr);
 351  1 tx.commit();
 352    }
 353    catch (RollbackException rollback)
 354    {
 355    ;
 356    }
 357    catch (Exception ex)
 358    {
 359    // t2_ex = ex;
 360  0 try
 361    {
 362  0 tx.rollback();
 363    }
 364    catch (SystemException e)
 365    {
 366  0 e.printStackTrace();
 367  0 t2_ex = e;
 368    }
 369    }
 370    finally
 371    {
 372  1 latch2.countDown();
 373    }
 374    }
 375    };
 376   
 377  1 Person p = createPerson("/person/test6", "p6", 40);
 378   
 379  1 t1.start();
 380  1 t2.start();
 381   
 382  1 t1.join();
 383  1 t2.join();
 384   
 385    // t2 should rollback due to timeout while t2 should succeed
 386  1 if (t2_ex != null)
 387  0 fail("Thread1 failed: " + t2_ex);
 388  1 if (t1_ex != null)
 389  0 fail("Thread2 failed: " + t1_ex);
 390   
 391    // This would fail because we are writing to __JBossInteral__ for every attach now.
 392    // As a result, there is version conflict for optimistic locking and cuasing
 393    // rollback.
 394  1 assertNotSame("City should be different ", "San Jose", p.getAddress().getCity());
 395    }
 396   
 397  1 public void testConcurrentPuts() throws Exception
 398    {
 399  1 final CountDownLatch latch1 = new CountDownLatch(1);
 400  1 final CountDownLatch latch2 = new CountDownLatch(1);
 401  1 Thread t1 = new Thread("t1")
 402    {
 403    Transaction tx;
 404   
 405  1 public void run()
 406    {
 407  1 try
 408    {
 409  1 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 410  1 UserTransaction tx = getTransaction();
 411  1 tx.begin();
 412  1 lang.add("German");
 413  1 latch1.countDown();
 414  1 latch2.await();
 415  1 tx.commit();
 416    }
 417    catch (RollbackException rollback)
 418    {
 419    ;
 420    }
 421    catch (Exception ex)
 422    {
 423  0 t1_ex = ex;
 424    }
 425    finally
 426    {
 427  1 latch1.countDown();
 428    }
 429    }
 430    };
 431   
 432  1 Thread t2 = new Thread("t2")
 433    {
 434    Transaction tx;
 435   
 436  1 public void run()
 437    {
 438  1 UserTransaction tx = null;
 439  1 try
 440    {
 441  1 latch1.await();
 442  1 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 443  1 tx = getTransaction();
 444  1 tx.begin();
 445  1 lang.add("English");
 446  1 tx.commit();
 447    }
 448    catch (RollbackException rollback)
 449    {
 450    ;
 451    }
 452    catch (Exception ex)
 453    {
 454  0 try
 455    {
 456  0 tx.rollback();
 457    }
 458    catch (SystemException e)
 459    {
 460  0 e.printStackTrace();
 461  0 t2_ex = e;
 462    }
 463    }
 464    finally
 465    {
 466  1 latch2.countDown();
 467    }
 468    }
 469    };
 470   
 471    // FIXME - JBCACHE-1034 - This only works with an explicit transaction.
 472  1 UserTransaction tx = getTransaction();
 473  1 tx.begin();
 474  1 Person p = createPerson("/person/test6", "p6", 50);
 475  1 List<String> lang = new ArrayList<String>();
 476  1 lang.add("German");
 477  1 p.setLanguages(lang);
 478  1 tx.commit();
 479   
 480  1 t1.start();
 481  1 t2.start();
 482   
 483  1 t1.join();
 484  1 t2.join();
 485   
 486    // t2 should rollback due to timeout while t2 should succeed
 487  1 if (t2_ex != null)
 488    {
 489  0 t2_ex.printStackTrace(System.err);
 490  0 fail("Thread2 failed: " + t2_ex);
 491    }
 492  1 if (t1_ex != null)
 493    {
 494  0 t1_ex.printStackTrace(System.err);
 495  0 fail("Thread1 failed: " + t1_ex);
 496    }
 497   
 498  1 int size = ((Person) cache.find("/person/test6")).getLanguages().size();
 499  1 assertEquals("number of languages ",
 500    2, size);
 501    }
 502   
 503  0 void log(String s)
 504    {
 505  0 long now;
 506  0 if (start == 0)
 507  0 start = System.currentTimeMillis();
 508  0 now = System.currentTimeMillis();
 509   
 510  0 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
 511    }
 512   
 513   
 514  1 public static Test suite() throws Exception
 515    {
 516  1 return new TestSuite(LocalTxTest.class);
 517    }
 518   
 519   
 520  0 public static void main(String[] args) throws Exception
 521    {
 522  0 junit.textui.TestRunner.run(suite());
 523    }
 524   
 525    }