Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 297   Methods: 18
NCLOC: 225   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LocalTest.java 83.3% 98.5% 94.4% 97.4%
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.passivation;
 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.CacheSPI;
 16    import org.jboss.cache.Fqn;
 17    import org.jboss.cache.notifications.annotation.CacheListener;
 18    import org.jboss.cache.notifications.annotation.NodeActivated;
 19    import org.jboss.cache.notifications.annotation.NodePassivated;
 20    import org.jboss.cache.notifications.event.NodeEvent;
 21    import org.jboss.cache.pojo.PojoCache;
 22    import org.jboss.cache.pojo.PojoCacheFactory;
 23    import org.jboss.cache.pojo.test.Address;
 24    import org.jboss.cache.pojo.test.Link;
 25    import org.jboss.cache.pojo.test.Person;
 26   
 27    import java.util.Map;
 28   
 29    /**
 30    * Basic PojoCache test case.
 31    *
 32    * @author Ben Wang
 33    */
 34   
 35    public class LocalTest extends TestCase
 36    {
 37    Log log = LogFactory.getLog(org.jboss.cache.pojo.passivation.LocalTest.class);
 38    PojoCache cache_;
 39    MyCacheListener listener_;
 40   
 41  6 public LocalTest(String name)
 42    {
 43  6 super(name);
 44    }
 45   
 46  6 protected void setUp() throws Exception
 47    {
 48  6 super.setUp();
 49  6 log.info("setUp() ....");
 50  6 String configFile = "META-INF/pojocache-passivation-service.xml";
 51  6 boolean toStart = false;
 52  6 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 53  6 cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.BatchModeTransactionManagerLookup");
 54   
 55  6 listener_ = new MyCacheListener();
 56  6 cache_.getCache().addCacheListener(listener_);
 57   
 58  6 cache_.start();
 59    }
 60   
 61  6 protected void tearDown() throws Exception
 62    {
 63  6 super.tearDown();
 64  6 cache_.getCache().removeNode(Fqn.ROOT);
 65  6 ((CacheSPI) cache_.getCache()).getCacheLoaderManager().getCacheLoader().remove(Fqn.ROOT);
 66  6 cache_.getCache().removeCacheListener(listener_);
 67  6 cache_.stop();
 68    //Thread.sleep(1000);
 69    }
 70   
 71    // public void testDummy() {}
 72   
 73  7 private Person createPerson(String name, int age)
 74    {
 75  7 Person p = new Person();
 76  7 p.setName(name);
 77  7 p.setAge(age);
 78  7 Address add = new Address();
 79  7 add.setZip(95123);
 80  7 add.setCity("San Jose");
 81  7 p.setAddress(add);
 82  7 return p;
 83    }
 84   
 85  3 private void sanityCheck()
 86    {
 87  3 if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0)
 88    {
 89  0 fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount()
 90    + " passivation - " + listener_.getPassivationCount());
 91    }
 92   
 93  3 listener_.reset();
 94    }
 95   
 96  1 public void testSimple() throws Exception
 97    {
 98  1 log.info("testSimple() ....");
 99  1 String id = "person";
 100  1 Person joe = createPerson("Joe Black", 20);
 101  1 cache_.attach(id, joe);
 102  1 Thread.sleep(9100);// default is 3 seconds so joe should have been passivated.
 103   
 104  1 assertNull("Node should be evicted ", ((CacheSPI) cache_.getCache()).peek(new Fqn(id), false));
 105   
 106  1 assertEquals("age ", 20, joe.getAge());
 107  1 joe.setAge(30);
 108  1 assertEquals("age ", 30, joe.getAge());
 109   
 110  1 sanityCheck();
 111    }
 112   
 113  1 public void testSetAddress() throws Exception
 114    {
 115  1 log.info("testAddress() ....");
 116  1 String id = "person";
 117  1 Person joe = createPerson("Joe Black", 20);
 118  1 cache_.attach(id, joe);
 119  1 Thread.sleep(9100);// default is 3 seconds so joe should have been passivated.
 120   
 121  1 assertNull("Node should be evicted ", ((CacheSPI) cache_.getCache()).peek(new Fqn(id), false));
 122   
 123  1 Address addr = new Address();
 124  1 addr.setCity("Taipei");
 125  1 addr.setZip(106);
 126   
 127  1 joe.setAddress(addr);
 128   
 129  1 sanityCheck();
 130    }
 131   
 132  1 public void testFindAgain() throws Exception
 133    {
 134  1 log.info("testFindAgain() ....");
 135  1 String id = "person";
 136  1 Person joe = createPerson("Joe Black", 20);
 137  1 cache_.attach(id, joe);
 138  1 Thread.sleep(9100);// default is 3 seconds so joe should have been passivated.
 139   
 140  1 assertNull("Node should be evicted ", ((CacheSPI) cache_.getCache()).peek(new Fqn(id), false));
 141   
 142  1 Person p = (Person) cache_.find(id);
 143   
 144  1 assertEquals("age ", 20, joe.getAge());
 145  1 joe.setAge(30);
 146  1 assertEquals("age ", 30, joe.getAge());
 147   
 148  1 assertEquals("age ", p.getAge(), joe.getAge());
 149   
 150  1 assertFalse("Instance not equal (this is known side effect) ", joe == p);
 151   
 152  1 sanityCheck();
 153    }
 154   
 155  1 public void testMultipleReference() throws Exception
 156    {
 157  1 log.info("testMultipleReference() ...");
 158  1 String id1 = "/person/ben";
 159  1 cache_.attach(id1, createPerson("Ben Hogan", 51));
 160  1 Person joe = (Person) cache_.find(id1);
 161  1 String id = "/person/joe";
 162  1 cache_.attach(id, createPerson("Joe Black", 31));
 163  1 Person ben = (Person) cache_.find(id);
 164   
 165  1 Address addr = new Address();
 166  1 addr.setStreet("123 Albert Ave.");
 167  1 addr.setCity("Sunnyvale");
 168  1 addr.setZip(94087);
 169   
 170    // They share the sub-object: address
 171  1 log.info("testMultipleReference(): set Joe address");
 172  1 joe.setAddress(addr);
 173  1 log.info("testMultipleReference(): set Ben address");
 174  1 ben.setAddress(addr);
 175   
 176  1 log.info("testMultipleReference(): verify");
 177  1 Address add1 = (Address) ((Person) cache_.find(id)).getAddress();
 178  1 Address add2 = (Address) ((Person) cache_.find(id)).getAddress();
 179  1 assertEquals(add1.getCity(), add2.getCity());
 180  1 addr.setCity("Santa Clara");
 181  1 assertEquals(add1.getCity(), add2.getCity());
 182   
 183  1 Thread.sleep(9100);// default is 3 seconds so joe should have been passivated.
 184   
 185  1 assertNull("Node should be evicted ", ((CacheSPI) cache_.getCache()).peek(new Fqn(id), false));
 186   
 187  1 assertEquals("City is ", "Santa Clara", add2.getCity());
 188   
 189  1 Thread.sleep(9100);// default is 3 seconds so joe should have been passivated.
 190  1 assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
 191   
 192  1 cache_.detach(id);
 193  1 cache_.detach(id1);
 194    }
 195   
 196  1 public void testRemoveObject1() throws Exception
 197    {
 198  1 log.info("testRemoveObject1() ...");
 199  1 cache_.attach("/person/joe", createPerson("Joe Black", 31));
 200  1 Person joe = (Person) cache_.find("/person/joe");
 201  1 cache_.attach("/person/ben", createPerson("Ben Hogan", 51));
 202  1 Person ben = (Person) cache_.find("/person/ben");
 203   
 204  1 Address addr = new Address();
 205  1 addr.setStreet("123 Albert Ave.");
 206  1 addr.setCity("Sunnyvale");
 207  1 addr.setZip(94087);
 208   
 209    // They share the sub-object: address
 210  1 log.info("testMultipleReference(): set Joe address");
 211  1 joe.setAddress(addr);
 212  1 log.info("testMultipleReference(): set Ben address");
 213  1 ben.setAddress(addr);
 214   
 215  1 Address add1 = (Address) ((Person) cache_.find("/person/joe")).getAddress();
 216  1 Address add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
 217  1 assertEquals(add1.getCity(), add2.getCity());
 218  1 addr.setCity("Santa Clara");
 219  1 assertEquals(add1.getCity(), add2.getCity());
 220   
 221  1 Thread.sleep(9100);
 222    // Remove pojo joe will relocate the address field to ben's
 223  1 cache_.detach("/person/joe");
 224  1 add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
 225  1 assertEquals("City ", "Santa Clara", add2.getCity());
 226    }
 227   
 228  1 public void testCircularReference1() throws Exception
 229    {
 230  1 log.info("testCircularReference1() ...");
 231  1 Link parent = new Link("parent");
 232  1 Link child = new Link("child");
 233  1 parent.setLink(child);
 234  1 child.setLink(parent);
 235  1 cache_.attach("/link/parent", parent);
 236   
 237  1 Thread.sleep(9100);
 238  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 239  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 240    }
 241   
 242   
 243  1 public static Test suite() throws Exception
 244    {
 245  1 return new TestSuite(org.jboss.cache.pojo.passivation.LocalTest.class);
 246    }
 247   
 248   
 249  0 public static void main(String[] args) throws Exception
 250    {
 251  0 junit.textui.TestRunner.run(org.jboss.cache.pojo.passivation.LocalTest.suite());
 252    }
 253   
 254    @CacheListener
 255    public class MyCacheListener
 256    {
 257    int activation = 0;
 258    int passivation = 0;
 259   
 260  3 public int getActivationCount()
 261    {
 262  3 return activation;
 263    }
 264   
 265  3 public int getPassivationCount()
 266    {
 267  3 return passivation;
 268    }
 269   
 270  3 public void reset()
 271    {
 272  3 activation = 0;
 273  3 passivation = 0;
 274    }
 275   
 276  92 @NodeActivated
 277    public void nodeActivated(NodeEvent ne)
 278    {
 279  92 Fqn fqn = ne.getFqn();
 280  92 if (!ne.isPre())
 281    {
 282  32 System.out.println("nodeActivated: " + fqn);
 283  32 activation++;
 284    }
 285    }
 286   
 287  80 @NodePassivated
 288    public void nodePassivated(NodeEvent ne)
 289    {
 290  80 if (!ne.isPre())
 291    {
 292  40 System.out.println("nodePassivated: " + ne.getFqn());
 293  40 passivation++;
 294    }
 295    }
 296    }
 297    }