Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 482   Methods: 22
NCLOC: 390   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LocalTxTest.java 42.9% 86.6% 90.9% 84.5%
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.TestingUtil;
 33    import org.jboss.cache.pojo.test.Address;
 34    import org.jboss.cache.pojo.test.Person;
 35    import org.jboss.cache.pojo.util.CacheApiUtil;
 36    import org.jboss.cache.transaction.DummyTransactionManager;
 37   
 38    import javax.naming.Context;
 39    import javax.naming.InitialContext;
 40    import javax.naming.NamingException;
 41    import javax.transaction.NotSupportedException;
 42    import javax.transaction.RollbackException;
 43    import javax.transaction.SystemException;
 44    import javax.transaction.Transaction;
 45    import javax.transaction.UserTransaction;
 46    import java.util.ArrayList;
 47    import java.util.List;
 48    import java.util.Properties;
 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  16 public LocalTxTest(String name)
 63    {
 64  16 super(name);
 65    }
 66   
 67  16 protected void setUp() throws Exception
 68    {
 69  16 super.setUp();
 70  16 log.info("setUp() ....");
 71   
 72  16 cache = createCache();
 73    // cache = createPessimisticCache();
 74   
 75  16 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 76   
 77  16 tx_mgr = DummyTransactionManager.getInstance();
 78  16 t1_ex = t2_ex = null;
 79    }
 80   
 81  16 protected void tearDown()
 82    {
 83  16 super.tearDown();
 84  16 cache.stop();
 85   
 86  16 DummyTransactionManager.destroy();
 87    }
 88   
 89    // public void testDummy() {}
 90   
 91  22 UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
 92    {
 93  22 Properties prop = new Properties();
 94  22 prop.put(Context.INITIAL_CONTEXT_FACTORY,
 95    "org.jboss.cache.transaction.DummyContextFactory");
 96  22 return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
 97    }
 98   
 99  18 private Person createPerson(String id, String name, int age)
 100    {
 101  18 Person p = new Person();
 102  18 p.setName(name);
 103  18 p.setAge(age);
 104  18 cache.attach(id, p);
 105  18 return p;
 106    }
 107   
 108  2 public void testSimple() throws Exception
 109    {
 110  2 log.info("testSimple() ....");
 111  2 UserTransaction tx = getTransaction();
 112  2 tx.begin();
 113  2 Person p = createPerson("/person/test1", "Harald Gliebe", 32);
 114  2 tx.commit();
 115  2 tx.begin();
 116  2 p.setName("Benoit");
 117  2 tx.commit();
 118  2 Person p1 = (Person) cache.find("/person/test1");
 119  2 assertEquals("Benoit", p.getName());
 120  2 assertEquals("Benoit", p1.getName());
 121  2 tx.begin();
 122  2 p1.setAge(41);
 123  2 tx.commit();
 124  2 assertEquals(41, p.getAge());
 125  2 assertEquals(41, p1.getAge());
 126    }
 127   
 128  2 public void testFailure() throws Exception
 129    {
 130  2 log.info("testFailure() ....");
 131  2 UserTransaction tx = getTransaction();
 132  2 tx.begin();
 133  2 Person p = createPerson("/person/test1", "Harald Gliebe", 32);
 134  2 tx.commit();
 135   
 136  2 tx.begin();
 137  2 p = createPerson("/person/test1", "Harald Gliebe", 32);
 138  2 tx.commit();
 139    }
 140   
 141  2 public void testFailure1() throws Exception
 142    {
 143  2 log.info("testFailure1() ....");
 144  2 UserTransaction tx = getTransaction();
 145  2 org.jboss.cache.Fqn f = new org.jboss.cache.Fqn("/person/test2");
 146  2 tx.begin();
 147  2 cache.getCache().put(f, "test", "test");
 148  2 tx.commit();
 149   
 150  2 tx.begin();
 151  2 cache.getCache().removeNode(f);
 152  2 cache.getCache().put(f, "test", "test");
 153  2 tx.commit();
 154    }
 155   
 156  2 public void testFailure2() throws Exception
 157    {
 158  2 Fqn f0 = Fqn.fromString("/person/test");
 159  2 Fqn f1 = new Fqn(f0, "1");
 160  2 Fqn f2 = new Fqn(f0, "2");
 161   
 162  2 cache.getCache().put(f1, "test", "test");
 163  2 cache.getCache().put(f2, "test", "test");
 164   
 165  2 int size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 166  2 assertEquals("Size ", 2, size);
 167   
 168  2 UserTransaction tx = getTransaction();
 169  2 tx.begin();
 170  2 cache.getCache().removeNode(f1);
 171  2 size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 172  2 assertEquals("Size ", 1, size);
 173  2 cache.getCache().put(f1, "test", "test");
 174  2 size = CacheApiUtil.getNodeChildren((CacheSPI) cache.getCache(), f0).size();
 175  2 assertEquals("Size ", 2, size);
 176  2 tx.commit();
 177    }
 178   
 179  2 public void testModification() throws Exception
 180    {
 181  2 UserTransaction tx = getTransaction();
 182  2 tx.begin();
 183  2 Person p = createPerson("/person/test2", "Harald", 32);
 184  2 p.setName("Harald Gliebe");
 185  2 tx.commit();
 186  2 Person p1 = (Person) cache.find("/person/test2");
 187  2 tx.begin();
 188  2 p1.setName("Benoit");
 189  2 tx.commit();
 190  2 assertEquals(p.getName(), "Benoit");
 191  2 assertEquals(p1.getName(), "Benoit");
 192  2 tx.begin();
 193  2 p1.setName("Harald");
 194  2 tx.rollback();
 195  2 assertEquals(p.getName(), "Benoit");
 196  2 assertEquals(p1.getName(), "Benoit");
 197    }
 198   
 199  2 public void testConcurrentSimplePutsI() throws Exception
 200    {
 201  2 Thread t1 = new Thread("t1")
 202    {
 203    Transaction tx;
 204   
 205  2 public void run()
 206    {
 207  2 try
 208    {
 209  2 Person p = createPerson("/person/test6", "p6", 41);
 210  2 Address addr = new Address();
 211  2 addr.setCity("San Jose");
 212  2 List list = new ArrayList();
 213  2 list.add("English");
 214  2 list.add("French");
 215  2 p.setLanguages(list);
 216  2 UserTransaction tx = getTransaction();
 217  2 tx.begin();
 218  2 p.setAddress(addr);
 219  2 TestingUtil.sleepThread(17000);
 220  2 tx.commit();
 221    }
 222    catch (RollbackException rollback)
 223    {
 224    ;
 225    }
 226    catch (Exception ex)
 227    {
 228  0 t1_ex = ex;
 229    }
 230    }
 231    };
 232   
 233  2 Thread t2 = new Thread("t2")
 234    {
 235    Transaction tx;
 236   
 237  2 public void run()
 238    {
 239  2 UserTransaction tx = null;
 240  2 try
 241    {
 242  2 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
 243  2 Person p = createPerson("/person/test7", "p7", 40);
 244  2 Address addr = new Address();
 245  2 addr.setCity("Santa Clara");
 246  2 tx = getTransaction();
 247  2 tx.begin();
 248  2 p.setAddress(addr);
 249  2 tx.commit();
 250    }
 251    catch (RollbackException rollback)
 252    {
 253    ;
 254    }
 255    catch (Exception ex)
 256    {
 257    // t2_ex = ex;
 258  0 try
 259    {
 260  0 tx.rollback();
 261    }
 262    catch (SystemException e)
 263    {
 264  0 e.printStackTrace();
 265  0 t2_ex = e;
 266    }
 267    }
 268    }
 269    };
 270   
 271  2 Person p = createPerson("/person/test5", "p5", 30);
 272   
 273  2 t1.start();
 274  2 t2.start();
 275   
 276  2 t1.join();
 277  2 t2.join();
 278   
 279    // t2 should rollback due to timeout while t2 should succeed
 280  2 if (t2_ex != null)
 281  0 fail("Thread2 failed: " + t2_ex);
 282  2 if (t1_ex != null)
 283  0 fail("Thread1 failed: " + t1_ex);
 284   
 285    }
 286   
 287  2 public void testConcurrentSimplePutsII() throws Exception
 288    {
 289  2 Thread t1 = new Thread("t1")
 290    {
 291    Transaction tx;
 292   
 293  2 public void run()
 294    {
 295  2 try
 296    {
 297  2 Person p = (Person) cache.find("/person/test6");
 298  2 Address addr = new Address();
 299  2 addr.setCity("San Jose");
 300  2 UserTransaction tx = getTransaction();
 301  2 tx.begin();
 302  2 p.setAddress(addr);
 303  2 TestingUtil.sleepThread(17000);
 304  2 tx.commit();
 305    }
 306    catch (RollbackException rollback)
 307    {
 308    ;
 309    }
 310    catch (Exception ex)
 311    {
 312  0 t1_ex = ex;
 313    }
 314    }
 315    };
 316   
 317  2 Thread t2 = new Thread("t2")
 318    {
 319    Transaction tx;
 320   
 321  2 public void run()
 322    {
 323  2 UserTransaction tx = null;
 324  2 try
 325    {
 326  2 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
 327  2 Person p = (Person) cache.find("/person/test6");
 328  2 Address addr = new Address();
 329  2 addr.setCity("Santa Clara");
 330  2 tx = getTransaction();
 331  2 tx.begin();
 332  2 p.setAddress(addr);
 333  2 tx.commit();
 334    }
 335    catch (RollbackException rollback)
 336    {
 337    ;
 338    }
 339    catch (Exception ex)
 340    {
 341    // t2_ex = ex;
 342  0 try
 343    {
 344  0 tx.rollback();
 345    }
 346    catch (SystemException e)
 347    {
 348  0 e.printStackTrace();
 349  0 t2_ex = e;
 350    }
 351    }
 352    }
 353    };
 354   
 355  2 Person p = createPerson("/person/test6", "p6", 40);
 356   
 357  2 t1.start();
 358  2 t2.start();
 359   
 360  2 t1.join();
 361  2 t2.join();
 362   
 363    // t2 should rollback due to timeout while t2 should succeed
 364  2 if (t2_ex != null)
 365  0 fail("Thread1 failed: " + t2_ex);
 366  2 if (t1_ex != null)
 367  0 fail("Thread2 failed: " + t1_ex);
 368   
 369    // This would fail because we are writing to __JBossInteral__ for every attach now.
 370    // As a result, there is version conflict for optimistic locking and cuasing
 371    // rollback.
 372  2 assertNotSame("City should be different ", "San Jose", p.getAddress().getCity());
 373    }
 374   
 375  2 public void testConcurrentPuts() throws Exception
 376    {
 377  2 Thread t1 = new Thread("t1")
 378    {
 379    Transaction tx;
 380   
 381  2 public void run()
 382    {
 383  2 try
 384    {
 385  2 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 386  2 UserTransaction tx = getTransaction();
 387  2 tx.begin();
 388  2 lang.add("German");
 389  2 TestingUtil.sleepThread(17000);
 390  2 tx.commit();
 391    }
 392    catch (RollbackException rollback)
 393    {
 394    ;
 395    }
 396    catch (Exception ex)
 397    {
 398  0 t1_ex = ex;
 399    }
 400    }
 401    };
 402   
 403  2 Thread t2 = new Thread("t2")
 404    {
 405    Transaction tx;
 406   
 407  2 public void run()
 408    {
 409  2 UserTransaction tx = null;
 410  2 try
 411    {
 412  2 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
 413  2 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 414  2 tx = getTransaction();
 415  2 tx.begin();
 416  2 lang.add("English");
 417  2 tx.commit();
 418    }
 419    catch (RollbackException rollback)
 420    {
 421    ;
 422    }
 423    catch (Exception ex)
 424    {
 425  0 try
 426    {
 427  0 tx.rollback();
 428    }
 429    catch (SystemException e)
 430    {
 431  0 e.printStackTrace();
 432  0 t2_ex = e;
 433    }
 434    }
 435    }
 436    };
 437   
 438  2 Person p = createPerson("/person/test6", "p6", 50);
 439  2 List<String> lang = new ArrayList<String>();
 440  2 lang.add("German");
 441  2 p.setLanguages(lang);
 442   
 443  2 t1.start();
 444  2 t2.start();
 445   
 446  2 t1.join();
 447  2 t2.join();
 448   
 449    // t2 should rollback due to timeout while t2 should succeed
 450  2 if (t2_ex != null)
 451  0 fail("Thread1 failed: " + t2_ex);
 452  2 if (t1_ex != null)
 453  0 fail("Thread2 failed: " + t1_ex);
 454   
 455  2 int size = ((Person) cache.find("/person/test6")).getLanguages().size();
 456  2 assertEquals("number of languages ",
 457    2, size);
 458    }
 459   
 460  0 void log(String s)
 461    {
 462  0 long now;
 463  0 if (start == 0)
 464  0 start = System.currentTimeMillis();
 465  0 now = System.currentTimeMillis();
 466   
 467  0 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
 468    }
 469   
 470   
 471  2 public static Test suite() throws Exception
 472    {
 473  2 return new TestSuite(LocalTxTest.class);
 474    }
 475   
 476   
 477  0 public static void main(String[] args) throws Exception
 478    {
 479  0 junit.textui.TestRunner.run(suite());
 480    }
 481   
 482    }