Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 345   Methods: 14
NCLOC: 263   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AsyncReplTest.java 50% 93.9% 92.9% 89%
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.apache.commons.logging.LogFactory;
 15    import org.jboss.cache.CacheSPI;
 16    import org.jboss.cache.DefaultCacheFactory;
 17    import org.jboss.cache.Fqn;
 18    import org.jboss.cache.Region;
 19    import org.jboss.cache.config.Configuration.CacheMode;
 20    import org.jboss.cache.factories.UnitTestCacheFactory;
 21    import org.jboss.cache.marshall.data.Address;
 22    import org.jboss.cache.marshall.data.Person;
 23    import org.jboss.cache.misc.TestingUtil;
 24    import org.jboss.cache.transaction.DummyTransactionManager;
 25   
 26    import javax.transaction.NotSupportedException;
 27    import javax.transaction.SystemException;
 28    import javax.transaction.Transaction;
 29    import java.lang.reflect.Method;
 30   
 31    /**
 32    * Test marshalling for async mode.
 33    *
 34    * @author Ben Wang
 35    * @version $Revision: 1.16 $
 36    */
 37    public class AsyncReplTest 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  6 public void setUp() throws Exception
 47    {
 48  6 super.setUp();
 49   
 50  6 log("creating cache1");
 51  6 cache1 = createCache("TestCache");
 52   
 53  6 log("creating cache2");
 54   
 55  6 cache2 = createCache("TestCache");
 56   
 57  6 addr_ = new Address();
 58  6 addr_.setCity("San Jose");
 59  6 ben_ = new Person();
 60  6 ben_.setName("Ben");
 61  6 ben_.setAddress(addr_);
 62   
 63    // Pause to give caches time to see each other
 64  6 TestingUtil.blockUntilViewsReceived(new CacheSPI[]{cache1, cache2}, 60000);
 65    }
 66   
 67  12 private CacheSPI createCache(String name) throws Exception
 68    {
 69  12 CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_ASYNC), false);
 70  12 cache.getConfiguration().setClusterName(name);
 71    // Use marshaller
 72  12 cache.getConfiguration().setUseRegionBasedMarshalling(true);
 73  12 cache.create();
 74  12 cache.start();
 75  12 return cache;
 76    }
 77   
 78  6 public void tearDown() throws Exception
 79    {
 80  6 super.tearDown();
 81  6 cache1.removeNode(Fqn.ROOT);
 82  6 if (cache1 != null)
 83    {
 84  6 log("stopping cache1");
 85  6 cache1.stop();
 86    }
 87   
 88  6 if (cache2 != null)
 89    {
 90  6 log("stopping cache2");
 91  6 cache2.stop();
 92    }
 93    }
 94   
 95    /**
 96    * Test replication with classloaders.
 97    *
 98    * @throws Exception
 99    */
 100  1 public void testCLSet2() throws Exception
 101    {
 102  1 ClassLoader cla = getClassLoader();
 103   
 104  1 Region existing = cache1.getRegion(aop, false);
 105  1 if (existing == null)
 106    {
 107  1 existing = cache1.getRegion(aop, true);
 108    }
 109  1 existing.registerContextClassLoader(cla);
 110   
 111   
 112  1 ClassLoader clb = getClassLoader();
 113  1 existing = cache2.getRegion(aop, false);
 114  1 if (existing == null)
 115    {
 116  1 existing = cache2.getRegion(aop, true);
 117    }
 118  1 existing.registerContextClassLoader(clb);
 119   
 120  1 cache1.put(aop, "person", ben_);
 121  1 cache1.put(new Fqn("alias"), "person", ben_);
 122   
 123  1 TestingUtil.sleepThread(1000);
 124  1 Object ben2 = null;
 125  1 try
 126    {
 127    // Can't cast it to Person. CCE will resutl.
 128  1 ben2 = cache2.get(aop, "person");
 129  1 assertNotNull(ben2);
 130  1 assertEquals(ben_.toString(), ben2.toString());
 131    }
 132    catch (Exception ex)
 133    {
 134  0 LogFactory.getLog("TEST").debug("Ex:", ex);
 135  0 fail("Test fails with exception " + ex);
 136    }
 137   
 138  1 Class claz = clb.loadClass(ADDRESS_CLASSNAME);
 139  1 Object add = claz.newInstance();
 140    {
 141  1 Class[] types = {String.class};
 142  1 Method setValue = claz.getMethod("setCity", types);
 143  1 Object[] margs = {"Sunnyvale"};
 144  1 setValue.invoke(add, margs);
 145    }
 146   
 147    {
 148  1 Class clasz1 = clb.loadClass(PERSON_CLASSNAME);
 149  1 Class[] types = {claz};
 150  1 Method setValue = clasz1.getMethod("setAddress", types);
 151  1 Object[] margs = {add};
 152  1 setValue.invoke(ben2, margs);
 153    }
 154   
 155    // Set it back to the cache
 156  1 try
 157    {
 158    // Can't cast it to Person. CCE will resutl.
 159  1 cache2.put(aop, "person", ben2);
 160  1 TestingUtil.sleepThread(1000);
 161  1 Object ben3 = cache1.get(aop, "person");
 162  1 assertEquals(ben2.toString(), ben3.toString());
 163    }
 164    catch (Exception ex)
 165    {
 166  0 ex.printStackTrace();
 167  0 fail("Test fails with exception " + ex);
 168    }
 169   
 170    }
 171   
 172  1 public void testPuts() throws Exception
 173    {
 174  1 ClassLoader cl = getClassLoader();
 175  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 176  1 r1.registerContextClassLoader(cl);
 177    // Create an empty Person loaded by this classloader
 178  1 Object scopedBen1 = getPersonFromClassloader(cl);
 179   
 180  1 cl = getClassLoader();
 181  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 182  1 r2.registerContextClassLoader(cl);
 183    // Create another empty Person loaded by this classloader
 184  1 Object scopedBen2 = getPersonFromClassloader(cl);
 185   
 186  1 cache1.put(Fqn.fromString("/aop/1"), "person", ben_);
 187  1 cache1.put(Fqn.fromString("/aop/2"), "person", scopedBen1);
 188  1 TestingUtil.sleepThread(1000);
 189   
 190  1 Object ben2 = null;
 191  1 try
 192    {
 193    // Can't cast it to Person. CCE will resutl.
 194  1 ben2 = cache2.get(Fqn.fromString("/aop/1"), "person");
 195  1 assertEquals(ben_.toString(), ben2.toString());
 196   
 197  1 ben2 = cache2.get(Fqn.fromString("/aop/2"), "person");
 198  1 assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
 199  1 assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
 200  1 assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
 201    }
 202    catch (Exception ex)
 203    {
 204  0 fail("Test fails with exception " + ex);
 205    }
 206   
 207    }
 208   
 209  1 public void testTxPut() throws Exception
 210    {
 211  1 Transaction tx = beginTransaction();
 212  1 cache1.put(aop, "person", ben_);
 213  1 cache1.put(aop, "person1", ben_);
 214  1 tx.commit();
 215  1 TestingUtil.sleepThread(1000);
 216  1 Person ben2 = (Person) cache2.get(aop, "person");
 217  1 assertNotNull("Person from 2nd cache should not be null ", ben2);
 218  1 assertEquals(ben_.toString(), ben2.toString());
 219    }
 220   
 221  1 public void testTxCLSet2() throws Exception
 222    {
 223  1 ClassLoader cla = getClassLoader();
 224  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 225  1 r1.registerContextClassLoader(cla);
 226  1 ClassLoader clb = getClassLoader();
 227  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 228  1 r2.registerContextClassLoader(clb);
 229   
 230  1 Transaction tx = beginTransaction();
 231  1 cache1.put(aop, "person", ben_);
 232  1 tx.commit();
 233  1 TestingUtil.sleepThread(1000);
 234   
 235  1 Object ben2 = null;
 236  1 try
 237    {
 238    // Can't cast it to Person. CCE will resutl.
 239  1 ben2 = cache2.get(aop, "person");
 240  1 assertEquals(ben_.toString(), ben2.toString());
 241    }
 242    catch (Exception ex)
 243    {
 244  0 fail("Test fails with exception " + ex);
 245    }
 246   
 247  1 Class claz = clb.loadClass(ADDRESS_CLASSNAME);
 248  1 Object add = claz.newInstance();
 249    {
 250  1 Class[] types = {String.class};
 251  1 Method setValue = claz.getMethod("setCity", types);
 252  1 Object[] margs = {"Sunnyvale"};
 253  1 setValue.invoke(add, margs);
 254    }
 255   
 256    {
 257  1 Class clasz1 = clb.loadClass(PERSON_CLASSNAME);
 258  1 Class[] types = {claz};
 259  1 Method setValue = clasz1.getMethod("setAddress", types);
 260  1 Object[] margs = {add};
 261  1 setValue.invoke(ben2, margs);
 262    }
 263   
 264    // Set it back to the cache
 265  1 try
 266    {
 267    // Can't cast it to Person. CCE will resutl.
 268  1 cache2.put(aop, "person", ben2);
 269  1 TestingUtil.sleepThread(1000);
 270  1 Object ben3 = cache1.get(aop, "person");
 271  1 assertEquals(ben2.toString(), ben3.toString());
 272    }
 273    catch (Exception ex)
 274    {
 275  0 fail("Test fails with exception " + ex);
 276    }
 277   
 278    }
 279   
 280  1 public void testStateTransfer() throws Exception
 281    {
 282    // Need to test out if app is not registered with beforehand??
 283    }
 284   
 285  1 public void testCustomFqn() throws Exception
 286    {
 287  1 FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
 288  1 Region r1 = cache1.getRegion(aop, false) == null ? cache1.getRegion(aop, true) : cache1.getRegion(aop, false);
 289  1 r1.registerContextClassLoader(cl1);
 290   
 291  1 FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
 292  1 Region r2 = cache2.getRegion(aop, false) == null ? cache2.getRegion(aop, true) : cache2.getRegion(aop, false);
 293  1 r2.registerContextClassLoader(cl2);
 294   
 295  1 Class clazz = cl1.loadFoo();
 296  1 Object custom1 = clazz.newInstance();
 297   
 298  1 clazz = cl2.loadFoo();
 299  1 Object custom2 = clazz.newInstance();
 300   
 301  1 Fqn base = Fqn.fromString("/aop");
 302  1 cache1.put(new Fqn(base, custom1), "key", "value");
 303  1 TestingUtil.sleepThread(1000);
 304   
 305  1 try
 306    {
 307  1 Object val = cache2.get(new Fqn(base, custom2), "key");
 308  1 assertEquals("value", val);
 309    }
 310    catch (Exception ex)
 311    {
 312  0 fail("Test fails with exception " + ex);
 313    }
 314    }
 315   
 316  2 Transaction beginTransaction() throws SystemException, NotSupportedException
 317    {
 318  2 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 319  2 mgr.begin();
 320  2 return mgr.getTransaction();
 321    }
 322   
 323  2 protected Object getPersonFromClassloader(ClassLoader cl) throws Exception
 324    {
 325  2 Class clazz = cl.loadClass(PERSON_CLASSNAME);
 326  2 return clazz.newInstance();
 327    }
 328   
 329  24 void log(String msg)
 330    {
 331  24 System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
 332    }
 333   
 334   
 335  1 public static Test suite()
 336    {
 337  1 return new TestSuite(AsyncReplTest.class);
 338    }
 339   
 340  0 public static void main(String[] args)
 341    {
 342  0 junit.textui.TestRunner.run(suite());
 343    }
 344   
 345    }