Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 221   Methods: 12
NCLOC: 156   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ReplicatedCircularGraphTest.java - 95% 83.3% 93.8%
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;
 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.test.Link;
 19    import org.jboss.cache.pojo.test.NodeManager;
 20    import org.jboss.cache.pojo.test.Person;
 21   
 22    import java.util.ArrayList;
 23    import java.util.List;
 24   
 25    /**
 26    * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
 27    *
 28    * @author Ben Wang
 29    */
 30   
 31    public class ReplicatedCircularGraphTest extends TestCase
 32    {
 33    Log log = LogFactory.getLog(ReplicatedCircularGraphTest.class);
 34    PojoCache cache1;
 35    PojoCache cache2;
 36   
 37  5 public ReplicatedCircularGraphTest(String name)
 38    {
 39  5 super(name);
 40    }
 41   
 42  5 protected void setUp() throws Exception
 43    {
 44  5 super.setUp();
 45  5 log.info("setUp() ....");
 46  5 cache1 = createCache("CacheGroup");
 47  5 cache2 = createCache("CacheGroup");
 48    }
 49   
 50  5 protected void tearDown() throws Exception
 51    {
 52  5 super.tearDown();
 53  5 cache1.getCache().removeNode(Fqn.fromString("/"));
 54  5 cache1.stop();
 55  5 cache2.stop();
 56    }
 57   
 58  10 private PojoCache createCache(String name) throws Exception
 59    {
 60  10 boolean toStart = false;
 61  10 PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 62  10 tree.start();
 63  10 return tree;
 64    }
 65   
 66    // public void testDummy() {}
 67   
 68  0 protected Person createPerson(String name, int age)
 69    {
 70  0 Person p = new Person();
 71  0 p.setName(name);
 72  0 p.setAge(age);
 73  0 return p;
 74    }
 75   
 76  1 public void testCircularReference1() throws Exception
 77    {
 78    // try {Thread.sleep(10000); } catch (Exception e) {};
 79  1 log.info("testCircularReference1() ...");
 80  1 Link parent = new Link("parent");
 81  1 Link child = new Link("child");
 82  1 parent.setLink(child);
 83  1 child.setLink(parent);
 84  1 cache1.attach("/link/parent", parent);
 85  1 TestingUtil.sleepThread(100);
 86  1 assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
 87  1 assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
 88  1 assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
 89  1 assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
 90  1 ((Link) cache2.find("/link/parent")).setLink(null);
 91  1 assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
 92  1 Link link = (Link) cache1.detach("/link/parent");
 93  1 assertNotNull("Link should not be null ", link);
 94  1 System.out.println("Link: " + link);
 95    }
 96   
 97  1 public void testCircularReference2() throws Exception
 98    {
 99    // try {Thread.sleep(10000); } catch (Exception e) {};
 100  1 log.info("testCircularReference2() ...");
 101  1 Link parent = new Link("parent");
 102  1 Link child = new Link("child");
 103  1 cache1.attach("/link/parent", parent);
 104  1 parent.setLink(child);
 105  1 child.setLink(parent);
 106  1 assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
 107  1 assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
 108  1 assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
 109  1 assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
 110  1 ((Link) cache2.find("/link/parent")).setLink(null);
 111  1 assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
 112  1 Link link = (Link) cache1.detach("/link/parent");
 113  1 assertNotNull("Link should not be null ", link);
 114    }
 115   
 116  1 public void testCircularReference3() throws Exception
 117    {
 118    // try {Thread.sleep(10000); } catch (Exception e) {};
 119  1 log.info("testCircularReference3() ...");
 120  1 Link parent = new Link("parent");
 121  1 Link child = new Link("child");
 122  1 cache1.attach("/link/parent", parent);
 123  1 cache1.attach("/link/child", child);
 124  1 TestingUtil.sleepThread(100);
 125  1 parent.setLink(child);
 126  1 child.setLink(parent);
 127   
 128  1 Link p1 = (Link) cache1.find("/link/parent");
 129  1 Link c1 = (Link) cache1.find("/link/child");
 130  1 assertEquals("parent", p1.getName());
 131  1 assertEquals("child", p1.getLink().getName());
 132  1 assertEquals("child", c1.getName());
 133  1 assertEquals("parent", c1.getLink().getName());
 134   
 135  1 Link p2 = (Link) cache1.find("/link/parent");
 136  1 Link c2 = (Link) cache1.find("/link/child");
 137   
 138  1 assertEquals("parent", p2.getName());
 139  1 assertEquals("child", p2.getLink().getName());
 140  1 assertEquals("child", c2.getName());
 141  1 assertEquals("parent", c2.getLink().getName());
 142   
 143  1 p2.setLink(null);
 144  1 assertNull("Child should be null", p2.getLink());
 145  1 Link link = (Link) cache1.detach("/link/parent");
 146  1 assertNotNull("Link should not be null ", link);
 147    }
 148   
 149    /**
 150    * Setting the circular relationship and also as a shared object.
 151    *
 152    * @throws Exception
 153    */
 154  1 public void testCircularReference4() throws Exception
 155    {
 156    // try {Thread.sleep(10000); } catch (Exception e) {};
 157  1 log.info("testCircularReference3() ...");
 158  1 Link parent = new Link("parent");
 159  1 Link child = new Link("child");
 160  1 parent.setLink(child);
 161  1 child.setLink(parent);
 162   
 163  1 List<Link> list = new ArrayList<Link>();
 164  1 list.add(parent);
 165   
 166  1 cache1.attach("/list", list);
 167  1 cache1.attach("/alias", list);
 168   
 169  1 TestingUtil.sleepThread(100);
 170  1 List list1 = (List) cache2.find("/list");
 171  1 List list2 = (List) cache2.find("/alias");
 172   
 173  1 assertEquals("parent", ((Link) list1.get(0)).getName());
 174  1 assertEquals("child", ((Link) list2.get(0)).getLink().getName());
 175    }
 176   
 177  1 public void testCircularAndSharedReferences() throws Exception
 178    {
 179  1 log.info("testCircularAndSharedReferences() ...");
 180  1 NodeManager pm_ = new NodeManager();
 181   
 182  1 pm_.setRootNode("root");
 183  1 pm_.addNode("root", "kanto");
 184  1 pm_.addNode("root.kanto", "tokyo");
 185  1 pm_.addNode("root.kanto", "kanagawa");
 186   
 187  1 cache1.attach("/propagation", pm_);
 188  1 assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
 189  1 pm_.addNode("root.kanto.tokyo", "hadanshita");
 190  1 assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
 191   
 192  1 NodeManager pm2_ = (NodeManager) cache2.find("/propagation");
 193  1 assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN());
 194  1 assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
 195   
 196    /*
 197    System.out.println("\n\n");
 198    System.out.println("---------------------------------------------");
 199    System.out.println("Initial pm state");
 200    System.out.println("---------------------------------------------");
 201    pm_.printNodes();
 202   
 203    System.out.println("\n\n");
 204    System.out.println("---------------------------------------------");
 205    System.out.println("Initial cache content");
 206    System.out.println(cache_.printDetails());
 207    System.out.println("---------------------------------------------");
 208    */
 209    }
 210   
 211  1 public static Test suite() throws Exception
 212    {
 213  1 return new TestSuite(ReplicatedCircularGraphTest.class);
 214    }
 215   
 216  0 public static void main(String[] args) throws Exception
 217    {
 218  0 junit.textui.TestRunner.run(ReplicatedCircularGraphTest.suite());
 219    }
 220   
 221    }