Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 250   Methods: 14
NCLOC: 212   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ReplicatedTxTest.java 33.3% 80.2% 78.6% 77.8%
coverage coverage
 1    package org.jboss.cache.pojo;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.apache.commons.logging.Log;
 7    import org.apache.commons.logging.LogFactory;
 8    import org.jboss.cache.config.Configuration.CacheMode;
 9    import org.jboss.cache.factories.UnitTestCacheFactory;
 10    import org.jboss.cache.pojo.test.Person;
 11    import org.jboss.cache.pojo.test.Student;
 12    import org.jboss.cache.transaction.DummyTransactionManager;
 13   
 14    import javax.naming.Context;
 15    import javax.naming.InitialContext;
 16    import javax.naming.NamingException;
 17    import javax.transaction.NotSupportedException;
 18    import javax.transaction.RollbackException;
 19    import javax.transaction.SystemException;
 20    import javax.transaction.Transaction;
 21    import javax.transaction.UserTransaction;
 22    import java.util.ArrayList;
 23    import java.util.List;
 24    import java.util.Properties;
 25   
 26    /**
 27    */
 28   
 29    public class ReplicatedTxTest extends TestCase
 30    {
 31    Log log = LogFactory.getLog(ReplicatedTxTest.class);
 32    PojoCache cache, cache1;
 33    final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
 34    DummyTransactionManager tx_mgr;
 35    Throwable t1_ex, t2_ex;
 36    long start = 0;
 37   
 38   
 39  6 public ReplicatedTxTest(String name)
 40    {
 41  6 super(name);
 42    }
 43   
 44  6 protected void setUp() throws Exception
 45    {
 46  6 super.setUp();
 47  6 log.info("setUp() ....");
 48  6 boolean toStart = false;
 49  6 cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 50  6 cache.start();
 51  6 cache1 = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 52  6 cache1.start();
 53   
 54  6 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 55   
 56  6 tx_mgr = DummyTransactionManager.getInstance();
 57  6 t1_ex = t2_ex = null;
 58    }
 59   
 60  6 protected void tearDown() throws Exception
 61    {
 62  6 super.tearDown();
 63  6 cache.stop();
 64  6 cache1.stop();
 65   
 66  6 DummyTransactionManager.destroy();
 67    }
 68   
 69    // public void testDummy() {}
 70   
 71  6 UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
 72    {
 73  6 Properties prop = new Properties();
 74  6 prop.put(Context.INITIAL_CONTEXT_FACTORY,
 75    "org.jboss.cache.transaction.DummyContextFactory");
 76  6 return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
 77    }
 78   
 79  6 private Person createPerson(String id, String name, int age)
 80    {
 81  6 Person p = new Person();
 82  6 p.setName(name);
 83  6 p.setAge(age);
 84  6 cache.attach(id, p);
 85  6 return p;
 86    }
 87   
 88  0 private Student createStudent(String id, String name, int age, String grade)
 89    {
 90  0 Student p = new Student();
 91  0 p.setName(name);
 92  0 p.setAge(age);
 93  0 p.setYear(grade);
 94  0 cache.attach(id, p);
 95  0 return p;
 96    }
 97   
 98  2 public void testSimple() throws Exception
 99    {
 100  2 log.info("testSimple() ....");
 101  2 UserTransaction tx = getTransaction();
 102  2 tx.begin();
 103  2 Person p = createPerson("/person/test1", "Harald Gliebe", 32);
 104  2 tx.commit();
 105  2 tx.begin();
 106  2 p.setName("Benoit");
 107  2 tx.commit();
 108  2 Person p1 = (Person) cache1.find("/person/test1");
 109  2 assertEquals("Benoit", p.getName());
 110  2 assertEquals("Benoit", p1.getName());
 111  2 tx.begin();
 112  2 p1.setAge(61);
 113  2 tx.commit();
 114  2 assertEquals(61, p.getAge());
 115  2 assertEquals(61, p1.getAge());
 116    }
 117   
 118  2 public void testModification() throws Exception
 119    {
 120  2 UserTransaction tx = getTransaction();
 121  2 tx.begin();
 122  2 Person p = createPerson("/person/test2", "Harald", 32);
 123  2 p.setName("Harald Gliebe");
 124  2 tx.commit();
 125  2 Person p1 = (Person) cache1.find("/person/test2");
 126  2 tx.begin();
 127  2 p1.setName("Benoit");
 128  2 tx.commit();
 129  2 assertEquals(p.getName(), "Benoit");
 130  2 assertEquals(p1.getName(), "Benoit");
 131  2 tx.begin();
 132  2 p1.setName("Harald");
 133  2 tx.rollback();
 134  2 assertEquals(p.getName(), "Benoit");
 135  2 assertEquals(p1.getName(), "Benoit");
 136    }
 137   
 138  2 public void testConcurrentPuts() throws Exception
 139    {
 140  2 Thread t1 = new Thread()
 141    {
 142    Transaction tx;
 143   
 144  2 public void run()
 145    {
 146  2 try
 147    {
 148  2 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 149  2 UserTransaction tx = getTransaction();
 150  2 tx.begin();
 151  2 lang.add("German");
 152  2 TestingUtil.sleepThread(17000);
 153  2 tx.commit();
 154    }
 155    catch (RollbackException rollback)
 156    {
 157    ;
 158    }
 159    catch (Exception ex)
 160    {
 161  0 t1_ex = ex;
 162    }
 163    }
 164    };
 165   
 166  2 Thread t2 = new Thread()
 167    {
 168    Transaction tx;
 169   
 170  2 public void run()
 171    {
 172  2 UserTransaction tx = null;
 173  2 try
 174    {
 175  2 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
 176  2 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
 177  0 tx = getTransaction();
 178  0 tx.begin();
 179  0 lang.add("English");
 180  0 tx.commit();
 181    }
 182    catch (RollbackException rollback)
 183    {
 184    ;
 185    }
 186    catch (Exception ex)
 187    {
 188  2 try
 189    {
 190  2 tx.rollback();
 191    } catch (SystemException e)
 192    {
 193  0 e.printStackTrace();
 194  0 t2_ex = e;
 195    }
 196    }
 197    }
 198    };
 199   
 200  2 Person p = createPerson("/person/test6", "p6", 50);
 201  2 List<String> lang = new ArrayList<String>();
 202  2 lang.add("German");
 203  2 p.setLanguages(lang);
 204   
 205  2 t1.start();
 206  2 t2.start();
 207   
 208  2 t1.join();
 209  2 t2.join();
 210   
 211    // t2 should rollback due to timeout while t2 should succeed
 212  2 if (t2_ex != null)
 213  0 fail("Thread1 failed: " + t2_ex);
 214  2 if (t1_ex != null)
 215  0 fail("Thread2 failed: " + t1_ex);
 216   
 217  2 int size = ((Person) cache.find("/person/test6")).getLanguages().size();
 218  2 assertEquals("number of languages should be 2, but is " + size + " (" +
 219    ((Person) cache.find("/person/test6")).getLanguages().size() + ")",
 220    2, size);
 221  2 size = ((Person) cache1.find("/person/test6")).getLanguages().size();
 222  2 assertEquals("number of languages should be 2, but is " + size + " (" +
 223    ((Person) cache.find("/person/test6")).getLanguages().size() + ")",
 224    2, size);
 225    }
 226   
 227  0 void log(String s)
 228    {
 229  0 long now;
 230  0 if (start == 0)
 231  0 start = System.currentTimeMillis();
 232  0 now = System.currentTimeMillis();
 233   
 234  0 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
 235    }
 236   
 237   
 238  2 public static Test suite() throws Exception
 239    {
 240  2 return new TestSuite(ReplicatedTxTest.class);
 241    }
 242   
 243   
 244  0 public static void main(String[] args) throws Exception
 245    {
 246  0 junit.textui.TestRunner.run(suite());
 247    }
 248   
 249    }
 250