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