Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 193   Methods: 9
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ReplicatedTest.java - 96.1% 88.9% 95.3%
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.memory;
 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.Fqn;
 16    import org.jboss.cache.config.Configuration.CacheMode;
 17    import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
 18    import org.jboss.cache.pojo.PojoCache;
 19    import org.jboss.cache.pojo.PojoCacheFactory;
 20    import org.jboss.cache.pojo.TestingUtil;
 21    import org.jboss.cache.pojo.test.Address;
 22    import org.jboss.cache.pojo.test.Person;
 23    import org.jboss.cache.pojo.test.SerializedAddress;
 24   
 25    import java.lang.ref.WeakReference;
 26    import java.util.ArrayList;
 27   
 28    /**
 29    * @author Ben Wang
 30    */
 31   
 32    public class ReplicatedTest extends TestCase
 33    {
 34    Log log_ = LogFactory.getLog(ReplicatedTest.class);
 35    PojoCache cache_;
 36    PojoCache cache1_;
 37   
 38  2 public ReplicatedTest(String name)
 39    {
 40  2 super(name);
 41    }
 42   
 43  2 protected void setUp() throws Exception
 44    {
 45  2 super.setUp();
 46  2 boolean toStart = false;
 47  2 cache_ = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 48  2 cache_.start();
 49  2 cache1_ = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 50  2 cache1_.start();
 51    }
 52   
 53  2 protected void tearDown() throws Exception
 54    {
 55  2 super.tearDown();
 56  2 cache_.stop();
 57  2 cache1_.stop();
 58    }
 59   
 60    // public void testDummy() {}
 61   
 62    /**
 63    * Test replication with classloaders.
 64    *
 65    * @throws Exception
 66    */
 67  1 public void testCLLeakageBasic() throws Exception
 68    {
 69  1 SerializedAddress add = new SerializedAddress();
 70  1 add.setCity("Taipei");
 71   
 72  1 ClassLoader cla = getClassLoader();
 73  1 WeakReference refa = new WeakReference(cla);
 74  1 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla);
 75  1 ClassLoader clb = getClassLoader();
 76  1 WeakReference refb = new WeakReference(clb);
 77  1 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb);
 78   
 79  1 Fqn fqn = new Fqn("/aop");
 80  1 cache_.getCache().put(new Fqn("/aop"), "add", add);
 81   
 82  1 TestingUtil.sleepThread(100);
 83  1 try
 84    {
 85  1 Object ben = cache1_.getCache().get(fqn, "add");
 86  1 assertEquals(add.toString(), ben.toString());
 87  1 ben = null;
 88    }
 89    catch (Exception ex)
 90    {
 91  0 fail("Test fails with exception " + ex);
 92    }
 93   
 94  1 cache_.getCache().remove(fqn, "add");
 95   
 96  1 ClassLoader clc = getClassLoader();
 97  1 cla = null;
 98  1 clb = null;
 99  1 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
 100  1 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
 101  1 System.gc(); // force gc
 102  1 Thread.sleep(1000);
 103  1 assertNull("Classloader should be gced ", refa.get());
 104  1 assertNull("Classloader should be gced ", refb.get());
 105    }
 106   
 107  1 private static void forceOutOfMemoryError() throws Exception
 108    {
 109  1 ArrayList list = new ArrayList();
 110  1 try
 111    {
 112   
 113  1 long i = 0;
 114  1 while (true)
 115    {
 116  289527 list.add("BigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBigBig" + (i++));
 117    }
 118    }
 119    catch (Throwable ignored)
 120    {
 121    }
 122  1 list.clear();
 123  1 list = null;
 124  1 System.gc();
 125  1 Thread.sleep(1000);
 126    }
 127   
 128    /**
 129    * Test replication with classloaders.
 130    *
 131    * @throws Exception
 132    */
 133  1 public void testCLLeakage() throws Exception
 134    {
 135  1 Person p = new Person();
 136  1 p.setName("Ben");
 137  1 Address add = new Address();
 138  1 add.setCity("Taipei");
 139   
 140  1 ClassLoader cla = getClassLoader();
 141  1 WeakReference refa = new WeakReference(cla);
 142  1 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(cla);
 143  1 ClassLoader clb = getClassLoader();
 144  1 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clb);
 145  1 WeakReference refb = new WeakReference(clb);
 146   
 147  1 cache_.attach("/aop", p);
 148   
 149  1 TestingUtil.sleepThread(100);
 150  1 try
 151    {
 152  1 Object ben = cache1_.find("/aop");
 153  1 assertEquals(p.toString(), ben.toString());
 154  1 ben = null;
 155    }
 156    catch (Exception ex)
 157    {
 158  0 fail("Test fails with exception " + ex);
 159    }
 160   
 161  1 cache_.detach("/aop");
 162  1 ClassLoader clc = getClassLoader();
 163  1 cache_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
 164  1 cache1_.getCache().getRegion(new Fqn("/aop"), true).registerContextClassLoader(clc);
 165  1 cla = null;
 166  1 clb = null;
 167  1 forceOutOfMemoryError();
 168   
 169  1 assertNull("Classloader should be gced ", refa.get());
 170  1 assertNull("Classloader should be gced ", refb.get());
 171    }
 172   
 173  6 protected ClassLoader getClassLoader() throws Exception
 174    {
 175  6 String[] includesClasses = {"org.jboss.cache.aop.test.Person",
 176    "org.jboss.cache.aop.test.Address"};
 177  6 String[] excludesClasses = {};
 178  6 ClassLoader cl = Thread.currentThread().getContextClassLoader();
 179  6 return new SelectedClassnameClassLoader(includesClasses, excludesClasses, cl);
 180    }
 181   
 182  1 public static Test suite() throws Exception
 183    {
 184  1 return new TestSuite(ReplicatedTest.class);
 185    }
 186   
 187   
 188  0 public static void main(String[] args) throws Exception
 189    {
 190  0 junit.textui.TestRunner.run(ReplicatedTest.suite());
 191    }
 192   
 193    }