Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 162   Methods: 12
NCLOC: 132   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BasicPassivationTest.java 70% 87.1% 91.7% 85.7%
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.passivation;
 9   
 10    import junit.framework.Test;
 11    import junit.framework.TestCase;
 12    import junit.framework.TestSuite;
 13    import org.jboss.cache.AbstractCacheListener;
 14    import org.jboss.cache.CacheImpl;
 15    import org.jboss.cache.CacheListener;
 16    import org.jboss.cache.DefaultCacheFactory;
 17    import org.jboss.cache.Fqn;
 18    import org.jboss.cache.factories.XmlConfigurationParser;
 19    import org.jboss.cache.misc.TestingUtil;
 20   
 21    /**
 22    * @author Ben Wang
 23    * @version $Revision: 1.14 $
 24    */
 25    public class BasicPassivationTest extends TestCase
 26    {
 27    CacheImpl cache_;
 28    int wakeupIntervalMillis_ = 0;
 29    final String ROOT_STR = "/test";
 30    Throwable t1_ex, t2_ex;
 31    final long DURATION = 10000;
 32    boolean isTrue;
 33    final String FQNSTR = "/org/jboss/3";
 34    int activationCount = 0;
 35    int passivationCount = 0;
 36   
 37  3 public BasicPassivationTest(String s)
 38    {
 39  3 super(s);
 40    }
 41   
 42  3 public void setUp() throws Exception
 43    {
 44  3 super.setUp();
 45  3 TestingUtil.recursiveFileRemove("/tmp/JBossCacheFileCacheLoader");// clean up any stale files left around by previous unit tests
 46  3 initCaches();
 47  3 wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
 48  3 log("wakeupInterval is " + wakeupIntervalMillis_);
 49  3 if (wakeupIntervalMillis_ < 0)
 50    {
 51  0 fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
 52    }
 53   
 54  3 t1_ex = t2_ex = null;
 55  3 isTrue = true;
 56    }
 57   
 58  3 void initCaches() throws Exception
 59    {
 60  3 cache_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 61  3 cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-passivation-service.xml"));// read in generic local xml
 62  3 cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 63  3 CacheListener listener = new TestCacheListener();
 64  3 cache_.start();
 65  3 cache_.getNotifier().addCacheListener(listener);
 66    }
 67   
 68  3 public void tearDown() throws Exception
 69    {
 70  3 super.tearDown();
 71  3 cache_.stop();
 72    }
 73   
 74  1 public void testBasic()
 75    {
 76  1 activationCount = 0;
 77  1 passivationCount = 0;
 78  1 Fqn fqn = Fqn.fromString(FQNSTR);
 79  1 try
 80    {
 81  1 cache_.put(fqn, FQNSTR, FQNSTR);
 82    }
 83    catch (Exception e)
 84    {
 85  0 fail("Failed to insert data" + e);
 86  0 e.printStackTrace();
 87    }
 88  1 System.out.println(cache_.toString());
 89  1 TestingUtil.sleepThread(21000);
 90  1 System.out.println(cache_.toString());
 91  1 try
 92    {
 93  1 assertFalse(cache_.exists(FQNSTR, FQNSTR));
 94  1 String val = (String) cache_.get(FQNSTR, FQNSTR);
 95  1 assertNotNull("DataNode should not be empty ", val);
 96    }
 97    catch (Exception e)
 98    {
 99  0 e.printStackTrace();
 100  0 fail("Failed to get" + e);
 101    }
 102   
 103  1 assertEquals("activation count:", 1, activationCount);
 104  1 assertEquals("passivation count:", 1, passivationCount);
 105    }
 106   
 107  1 public void testDualPassivation() throws Exception
 108    {
 109  1 Fqn fqn = Fqn.fromString(FQNSTR);
 110  1 cache_.put(fqn, "key", "value");
 111  1 cache_.evict(fqn);
 112  1 cache_.evict(fqn);
 113  1 assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key"));
 114    }
 115   
 116  1 public void testIntermingledPassivation() throws Exception
 117    {
 118  1 Fqn fqn = Fqn.fromString(FQNSTR);
 119  1 cache_.put(fqn, "key1", "value");
 120  1 cache_.evict(fqn);
 121  1 cache_.put(fqn, "key2", "value");
 122  1 cache_.evict(fqn);
 123  1 assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key1"));
 124   
 125    }
 126   
 127  11 void log(String msg)
 128    {
 129  11 System.out.println("-- " + msg);
 130    }
 131   
 132  1 public static Test suite()
 133    {
 134  1 return new TestSuite(org.jboss.cache.passivation.BasicPassivationTest.class);
 135    }
 136   
 137  0 public static void main(String[] args)
 138    {
 139  0 junit.textui.TestRunner.run(org.jboss.cache.passivation.BasicPassivationTest.suite());
 140    }
 141   
 142    public class TestCacheListener extends AbstractCacheListener
 143    {
 144  12 public void nodeActivated(Fqn fqn, boolean pre)
 145    {
 146  8 if (pre) return;// we are not interested in postActivate event
 147  0 if (!fqn.isChildOrEquals(Fqn.fromString(FQNSTR))) return;// don't care about fqn that doesn't belong to me.
 148   
 149  4 log("nodeActivate(): send postActivate event on fqn: " + fqn);
 150  4 activationCount++;
 151    }
 152   
 153  8 public void nodePassivated(Fqn fqn, boolean pre)
 154    {
 155  4 if (!pre) return;// we are not interested in postPassivate event
 156  0 if (!fqn.isChildOrEquals(Fqn.fromString(FQNSTR))) return;// don't care about fqn that doesn't belong to me.
 157   
 158  4 log("nodePassivate(): send prePassivate event on fqn: " + fqn);
 159  4 passivationCount++;
 160    }
 161    }
 162    }