Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 430   Methods: 21
NCLOC: 330   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SyncReplTest.java 50% 98.1% 95.2% 90.7%
coverage coverage
 1    /*
 2    *
 3    * JBoss, the OpenSource J2EE webOS
 4    *
 5    * Distributable under LGPL license.
 6    * See terms of license at gnu.org.
 7    */
 8   
 9    package org.jboss.cache.marshall;
 10   
 11   
 12    import junit.framework.Test;
 13    import junit.framework.TestSuite;
 14    import org.jboss.cache.CacheSPI;
 15    import org.jboss.cache.DefaultCacheFactory;
 16    import org.jboss.cache.Fqn;
 17    import org.jboss.cache.Region;
 18    import org.jboss.cache.config.Configuration.CacheMode;
 19    import org.jboss.cache.factories.UnitTestCacheFactory;
 20    import org.jboss.cache.marshall.data.Address;
 21    import org.jboss.cache.marshall.data.Person;
 22    import org.jboss.cache.misc.TestingUtil;
 23    import org.jboss.cache.transaction.DummyTransactionManager;
 24   
 25    import javax.transaction.NotSupportedException;
 26    import javax.transaction.SystemException;
 27    import javax.transaction.Transaction;
 28    import java.lang.reflect.Method;
 29    import java.util.HashMap;
 30   
 31    /**
 32    * Test case for marshalling using Sync mode.
 33    *
 34    * @author Ben Wang
 35    * @version $Revision: 1.17 $
 36    */
 37    public class SyncReplTest extends RegionBasedMarshallingTestBase
 38    {
 39    CacheSPI cache1, cache2;
 40    String props = null;
 41    Person ben_;
 42    Address addr_;
 43    Throwable ex_;
 44    private Fqn aop = Fqn.fromString("/aop");
 45   
 46  13 public void setUp() throws Exception
 47    {
 48  13 super.setUp();
 49   
 50  13 log("creating cache1");
 51  13 cache1 = createCache("TestCache");
 52   
 53  13 log("creating cache2");
 54   
 55  13 cache2 = createCache("TestCache");
 56  13 addr_ = new Address();
 57  13 addr_.setCity("San Jose");
 58  13 ben_ = new Person();
 59  13 ben_.setName("Ben");
 60  13 ben_.setAddress(addr_);
 61   
 62    // Pause to give caches time to see each other
 63  13 TestingUtil.blockUntilViewsReceived(new CacheSPI[]{cache1, cache2}, 60000);
 64    }
 65   
 66  26 private CacheSPI createCache(String name) throws Exception
 67    {
 68  26 CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), false);
 69  26 cache.getConfiguration().setClusterName(name);
 70    // Use marshaller
 71  26 cache.getConfiguration().setUseRegionBasedMarshalling(true);
 72  26 cache.create();
 73  26 cache.start();
 74  26 return cache;
 75    }
 76   
 77  13 public void tearDown() throws Exception
 78    {
 79  13 super.tearDown();
 80  13 cache1.removeNode(Fqn.ROOT);
 81  13 if (cache1 != null)
 82    {
 83  13 log("stopping cache1");
 84  13 cache1.stop();
 85    }
 86   
 87  13 if (cache2 != null)
 88    {
 89  13 log("stopping cache2");
 90  13 cache2.stop();
 91    }
 92    }
 93   
 94  1 public void testPlainPut() throws Exception
 95    {
 96  1 cache1.put(aop, "person", ben_);
 97  1 Person ben2 = (Person) cache2.get(aop, "person");
 98  1 assertNotNull("Person from 2nd cache should not be null ", ben2);
 99  1 assertEquals(ben_.toString(), ben2.toString());
 100    }
 101   
 102  1 public void testCCE() throws Exception
 103    {
 104  1 ClassLoader cl = getClassLoader();
 105  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 106  1 r1.registerContextClassLoader(cl);
 107   
 108  1 cl = getClassLoader();
 109  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 110  1 r2.registerContextClassLoader(cl);
 111   
 112  1 cache1.put(aop, "person", ben_);
 113  1 try
 114    {
 115  1 Person ben2 = (Person) cache2.get(aop, "person");
 116    }
 117    catch (ClassCastException ex)
 118    {
 119    // That's ok.
 120  1 return;
 121    }
 122  0 fail("Should have thrown an exception");
 123    }
 124   
 125  1 public void testPut() throws Exception
 126    {
 127  1 ClassLoader cla = getClassLoader();
 128  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 129  1 r1.registerContextClassLoader(cla);
 130  1 ClassLoader clb = getClassLoader();
 131  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 132  1 r2.registerContextClassLoader(clb);
 133   
 134  1 cache1.put(aop, "person", ben_);
 135   
 136  1 Object ben2;
 137    // Can't cast it to Person. CCE will resutl.
 138  1 ben2 = cache2.get(aop, "person");
 139  1 assertEquals(ben_.toString(), ben2.toString());
 140    }
 141   
 142  1 public void testCLSet() throws Exception
 143    {
 144  1 ClassLoader cla = getClassLoader();
 145  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 146  1 r1.registerContextClassLoader(cla);
 147  1 ClassLoader clb = getClassLoader();
 148  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 149  1 r2.registerContextClassLoader(clb);
 150   
 151  1 cache1.put(aop, "person", ben_);
 152   
 153  1 Object ben2;
 154    // Can't cast it to Person. CCE will resutl.
 155  1 ben2 = cache2.get(aop, "person");
 156  1 assertEquals(ben_.toString(), ben2.toString());
 157   
 158  1 Class claz = clb.loadClass(ADDRESS_CLASSNAME);
 159  1 Object add = claz.newInstance();
 160    {
 161  1 Class[] types = {String.class};
 162  1 Method setValue = claz.getMethod("setCity", types);
 163  1 Object[] margs = {"Sunnyvale"};
 164  1 setValue.invoke(add, margs);
 165    }
 166   
 167    {
 168  1 Class clasz1 = clb.loadClass(PERSON_CLASSNAME);
 169  1 Class[] types = {claz};
 170  1 Method setValue = clasz1.getMethod("setAddress", types);
 171  1 Object[] margs = {add};
 172  1 setValue.invoke(ben2, margs);
 173    }
 174    }
 175   
 176    /**
 177    * Test replication with classloaders.
 178    *
 179    * @throws Exception
 180    */
 181  1 public void testCLSet2() throws Exception
 182    {
 183  1 ClassLoader cla = getClassLoader();
 184  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 185  1 r1.registerContextClassLoader(cla);
 186  1 ClassLoader clb = getClassLoader();
 187  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 188  1 r2.registerContextClassLoader(clb);
 189   
 190  1 cache1.put(aop, "person", ben_);
 191   
 192  1 Object ben2;
 193    // Can't cast it to Person. CCE will resutl.
 194  1 ben2 = cache2.get(aop, "person");
 195  1 assertEquals(ben_.toString(), ben2.toString());
 196   
 197  1 Class claz = clb.loadClass(ADDRESS_CLASSNAME);
 198  1 Object add = claz.newInstance();
 199    {
 200  1 Class[] types = {String.class};
 201  1 Method setValue = claz.getMethod("setCity", types);
 202  1 Object[] margs = {"Sunnyvale"};
 203  1 setValue.invoke(add, margs);
 204    }
 205   
 206    {
 207  1 Class clasz1 = clb.loadClass(PERSON_CLASSNAME);
 208  1 Class[] types = {claz};
 209  1 Method setValue = clasz1.getMethod("setAddress", types);
 210  1 Object[] margs = {add};
 211  1 setValue.invoke(ben2, margs);
 212    }
 213   
 214    // Set it back to the cache
 215    // Can't cast it to Person. CCE will resutl.
 216  1 cache2.put(aop, "person", ben2);
 217  1 Object ben3 = cache1.get(aop, "person");
 218  1 assertEquals(ben2.toString(), ben3.toString());
 219   
 220    }
 221   
 222  1 public void testPuts() throws Exception
 223    {
 224  1 ClassLoader cl = getClassLoader();
 225  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 226  1 r1.registerContextClassLoader(cl);
 227   
 228    // Create an empty Person loaded by this classloader
 229  1 Object scopedBen1 = getPersonFromClassloader(cl);
 230   
 231  1 cl = getClassLoader();
 232  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 233  1 r2.registerContextClassLoader(cl);
 234   
 235    // Create another empty Person loaded by this classloader
 236  1 Object scopedBen2 = getPersonFromClassloader(cl);
 237   
 238  1 cache1.put(Fqn.fromString("/aop/1"), "person", ben_);
 239  1 cache1.put(Fqn.fromString("/aop/2"), "person", scopedBen1);
 240   
 241  1 Object ben2 = null;
 242  1 try
 243    {
 244    // Can't cast it to Person. CCE will resutl.
 245  1 ben2 = cache2.get(Fqn.fromString("/aop/1"), "person");
 246  1 assertEquals(ben_.toString(), ben2.toString());
 247   
 248  1 ben2 = cache2.get(Fqn.fromString("/aop/2"), "person");
 249  1 assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
 250  1 assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
 251  1 assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
 252    }
 253    catch (Exception ex)
 254    {
 255  0 fail("Test fails with exception " + ex);
 256    }
 257   
 258    }
 259   
 260  1 public void testMethodCall() throws Exception
 261    {
 262  1 ClassLoader cl = getClassLoader();
 263  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 264  1 r1.registerContextClassLoader(cl);
 265  1 cl = getClassLoader();
 266  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 267  1 r2.registerContextClassLoader(cl);
 268   
 269  1 cache1.put(Fqn.fromString("/aop/1"), "person", ben_);
 270  1 cache1.remove(Fqn.fromString("/aop/1"), "person");
 271  1 HashMap map = new HashMap();
 272  1 map.put("1", "1");
 273  1 map.put("2", "2");
 274  1 cache1.put(Fqn.fromString("/aop/2"), map);
 275  1 cache1.removeNode(Fqn.fromString("/aop/2"));
 276   
 277  1 TestingUtil.sleepThread(1000);
 278    }
 279   
 280  1 public void testTxMethodCall() throws Exception
 281    {
 282  1 ClassLoader cl = getClassLoader();
 283  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 284  1 r1.registerContextClassLoader(cl);
 285  1 cl = getClassLoader();
 286  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 287  1 r2.registerContextClassLoader(cl);
 288   
 289  1 Transaction tx = beginTransaction();
 290  1 cache1.put(Fqn.fromString("/aop/1"), "person", ben_);
 291  1 cache1.remove(Fqn.fromString("/aop/1"), "person");
 292  1 HashMap map = new HashMap();
 293  1 map.put("1", "1");
 294  1 map.put("2", "2");
 295  1 cache1.put(Fqn.fromString("/aop/2"), map);
 296  1 cache1.removeNode(Fqn.fromString("/aop/2"));
 297  1 tx.commit();
 298   
 299  1 TestingUtil.sleepThread(1000);
 300    }
 301   
 302  1 public void testTxPut() throws Exception
 303    {
 304  1 Transaction tx = beginTransaction();
 305  1 cache1.put(aop, "person", ben_);
 306  1 cache1.put(aop, "person1", ben_);
 307  1 cache1.removeNode(aop);
 308  1 cache1.put(aop, "person", ben_);
 309  1 tx.commit();
 310  1 Person ben2 = (Person) cache2.get(aop, "person");
 311  1 assertNotNull("Person from 2nd cache should not be null ", ben2);
 312  1 assertEquals(ben_.toString(), ben2.toString());
 313    }
 314   
 315  1 public void testTxRollback() throws Exception
 316    {
 317  1 Transaction tx = beginTransaction();
 318  1 cache1.put(aop, "person", ben_);
 319  1 cache1.put(aop, "person1", ben_);
 320  1 tx.rollback();
 321  1 Person ben2 = (Person) cache2.get(aop, "person");
 322  1 assertNull("Person from 2nd cache should be null ", ben2);
 323    }
 324   
 325  1 public void testTxCLSet2() throws Exception
 326    {
 327  1 ClassLoader cla = getClassLoader();
 328  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 329  1 r1.registerContextClassLoader(cla);
 330  1 ClassLoader clb = getClassLoader();
 331  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 332  1 r2.registerContextClassLoader(clb);
 333   
 334  1 Transaction tx = beginTransaction();
 335  1 cache1.put(aop, "person", ben_);
 336  1 tx.commit();
 337   
 338  1 Object ben2;
 339    // Can't cast it to Person. CCE will resutl.
 340  1 ben2 = cache2.get(aop, "person");
 341  1 assertEquals(ben_.toString(), ben2.toString());
 342   
 343  1 Class claz = clb.loadClass(ADDRESS_CLASSNAME);
 344  1 Object add = claz.newInstance();
 345    {
 346  1 Class[] types = {String.class};
 347  1 Method setValue = claz.getMethod("setCity", types);
 348  1 Object[] margs = {"Sunnyvale"};
 349  1 setValue.invoke(add, margs);
 350    }
 351   
 352    {
 353  1 Class clasz1 = clb.loadClass(PERSON_CLASSNAME);
 354  1 Class[] types = {claz};
 355  1 Method setValue = clasz1.getMethod("setAddress", types);
 356  1 Object[] margs = {add};
 357  1 setValue.invoke(ben2, margs);
 358    }
 359   
 360    // Set it back to the cache
 361    // Can't cast it to Person. CCE will resutl.
 362  1 cache2.put(aop, "person", ben2);
 363  1 Object ben3 = cache1.get(aop, "person");
 364  1 assertEquals(ben2.toString(), ben3.toString());
 365    }
 366   
 367  1 public void testStateTransfer() throws Exception
 368    {
 369    // Need to test out if app is not registered with beforehand??
 370    }
 371   
 372  1 public void testCustomFqn() throws Exception
 373    {
 374  1 FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
 375  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 376  1 r1.registerContextClassLoader(cl1);
 377  1 FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
 378  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 379  1 r2.registerContextClassLoader(cl2);
 380   
 381  1 Class clazz = cl1.loadFoo();
 382  1 Object custom1 = clazz.newInstance();
 383   
 384  1 clazz = cl2.loadFoo();
 385  1 Object custom2 = clazz.newInstance();
 386   
 387  1 Fqn base = aop;
 388  1 cache1.put(new Fqn(base, custom1), "key", "value");
 389   
 390  1 try
 391    {
 392  1 Object val = cache2.get(new Fqn(base, custom2), "key");
 393  1 assertEquals("value", val);
 394    }
 395    catch (Exception ex)
 396    {
 397  0 fail("Test fails with exception " + ex);
 398    }
 399    }
 400   
 401  4 Transaction beginTransaction() throws SystemException, NotSupportedException
 402    {
 403  4 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 404  4 mgr.begin();
 405  4 return mgr.getTransaction();
 406    }
 407   
 408  2 protected Object getPersonFromClassloader(ClassLoader cl) throws Exception
 409    {
 410  2 Class clazz = cl.loadClass(PERSON_CLASSNAME);
 411  2 return clazz.newInstance();
 412    }
 413   
 414  52 void log(String msg)
 415    {
 416  52 System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
 417    }
 418   
 419   
 420  1 public static Test suite()
 421    {
 422  1 return new TestSuite(SyncReplTest.class);
 423    }
 424   
 425  0 public static void main(String[] args)
 426    {
 427  0 junit.textui.TestRunner.run(suite());
 428    }
 429   
 430    }