Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 292   Methods: 17
NCLOC: 203   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CircularGraphTest.java - 96.3% 82.4% 94.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.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.pojo.test.Link;
 17    import org.jboss.cache.pojo.test.NodeManager;
 18    import org.jboss.cache.pojo.test.Person;
 19   
 20    import java.util.ArrayList;
 21    import java.util.List;
 22   
 23    /**
 24    * Test object graph handling in PojoCache, e.g., circular reference, multiple reference, link, etc.
 25    *
 26    * @author Ben Wang
 27    */
 28   
 29    public class CircularGraphTest extends TestCase
 30    {
 31    Log log = LogFactory.getLog(CircularGraphTest.class);
 32    PojoCache cache_;
 33   
 34  20 public CircularGraphTest(String name)
 35    {
 36  20 super(name);
 37    }
 38   
 39  20 protected void setUp() throws Exception
 40    {
 41  20 super.setUp();
 42  20 log.info("setUp() ....");
 43  20 String configFile = "META-INF/local-service.xml";
 44  20 boolean toStart = false;
 45  20 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 46  20 cache_.start();
 47    }
 48   
 49  20 protected void tearDown() throws Exception
 50    {
 51  20 super.tearDown();
 52  20 cache_.stop();
 53    }
 54   
 55    // public void testDummy() {}
 56   
 57  0 protected Person createPerson(String name, int age)
 58    {
 59  0 Person p = new Person();
 60  0 p.setName(name);
 61  0 p.setAge(age);
 62  0 return p;
 63    }
 64   
 65    /**
 66    * Pure parent child relationsip
 67    *
 68    * @throws Exception
 69    */
 70  2 public void testCircularReference1() throws Exception
 71    {
 72  2 log.info("testCircularReference1() ...");
 73  2 Link parent = new Link("parent");
 74  2 Link child = new Link("child");
 75  2 parent.setLink(child);
 76  2 child.setLink(parent);
 77  2 cache_.attach("/link/parent", parent);
 78  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 79  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 80    }
 81   
 82    /**
 83    * Pure parent child relationsip. Attach both parent and child.
 84    *
 85    * @throws Exception
 86    */
 87  2 public void testCircularReference2() throws Exception
 88    {
 89  2 log.info("testCircularReference2() ...");
 90  2 Link parent = new Link("parent");
 91  2 Link child = new Link("child");
 92  2 parent.setLink(child);
 93  2 child.setLink(parent);
 94  2 cache_.attach("/link/parent", parent);
 95  2 cache_.attach("/link/child", child);
 96  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 97  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 98    }
 99   
 100    /**
 101    * Pure parent child relationsip with detach
 102    *
 103    * @throws Exception
 104    */
 105  2 public void testCircularReference3() throws Exception
 106    {
 107  2 log.info("testCircularReference3() ...");
 108  2 Link parent = new Link("parent");
 109  2 Link child = new Link("child");
 110  2 parent.setLink(child);
 111  2 child.setLink(parent);
 112  2 cache_.attach("/link/parent", parent);
 113  2 cache_.attach("/link/child", child);
 114  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 115  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 116  2 Link link = (Link) cache_.detach("/link/parent");
 117  2 cache_.detach("/link/child");
 118  2 assertEquals("child", link.getLink().getName());
 119  2 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
 120  2 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
 121    }
 122   
 123    /**
 124    * Pure parent child relationsip with detach reversed
 125    *
 126    * @throws Exception
 127    */
 128  2 public void testCircularReference3a() throws Exception
 129    {
 130  2 log.info("testCircularReference3() ...");
 131  2 Link parent = new Link("parent");
 132  2 Link child = new Link("child");
 133  2 parent.setLink(child);
 134  2 child.setLink(parent);
 135  2 cache_.attach("/link/parent", parent);
 136  2 cache_.attach("/link/child", child);
 137  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 138  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 139  2 cache_.detach("/link/child");
 140  2 Link link = (Link) cache_.detach("/link/parent");
 141  2 assertEquals("child", link.getLink().getName());
 142  2 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
 143  2 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
 144    }
 145   
 146    /**
 147    * cache managed first before put in the relationsip.
 148    *
 149    * @throws Exception
 150    */
 151  2 public void testCircularReference4() throws Exception
 152    {
 153  2 log.info("testCircularReference4() ...");
 154  2 Link parent = new Link("parent");
 155  2 Link child = new Link("child");
 156  2 cache_.attach("/link/parent", parent);
 157  2 cache_.attach("/link/child", child);
 158  2 parent.setLink(child);
 159  2 child.setLink(parent);
 160  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 161  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 162  2 Link link = (Link) cache_.detach("/link/parent");
 163  2 assertEquals("child", link.getLink().getName());
 164  2 assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
 165    }
 166   
 167    /**
 168    * Put first before settting the relationship
 169    *
 170    * @throws Exception
 171    */
 172  2 public void testCircularReference5() throws Exception
 173    {
 174  2 log.info("testCircularReference5() ...");
 175  2 Link parent = new Link("parent");
 176  2 Link child = new Link("child");
 177  2 cache_.attach("/link/parent", parent);
 178  2 cache_.attach("/link/child", child);
 179  2 parent.setLink(child);
 180  2 child.setLink(parent);
 181  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 182  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 183  2 assertEquals("child", ((Link) cache_.find("/link/child")).getName());
 184  2 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
 185  2 Link link = (Link) cache_.detach("/link/parent");
 186  2 assertEquals("child", link.getLink().getName());
 187  2 assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
 188    }
 189   
 190    /**
 191    * Put first before settting the relationship
 192    *
 193    * @throws Exception
 194    */
 195  2 public void testCircularReference6() throws Exception
 196    {
 197  2 log.info("testCircularReference6() ...");
 198  2 Link parent = new Link("parent");
 199  2 Link child = new Link("child");
 200  2 cache_.attach("/link/parent", parent);
 201  2 cache_.attach("/link/child", child);
 202  2 parent.setLink(child);
 203  2 child.setLink(parent);
 204  2 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 205  2 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 206  2 assertEquals("child", ((Link) cache_.find("/link/child")).getName());
 207  2 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
 208  2 Link link = (Link) cache_.detach("/link/parent");
 209  2 assertEquals("child", link.getLink().getName());
 210  2 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))));
 211   
 212    // re-attach
 213  2 cache_.attach("/link/parent", parent);
 214    }
 215   
 216    /**
 217    * Setting the circular relationship and also as a shared object.
 218    *
 219    * @throws Exception
 220    */
 221  2 public void testCircularReference7() throws Exception
 222    {
 223  2 log.info("testCircularReference7() ...");
 224  2 Link parent = new Link("parent");
 225  2 Link child = new Link("child");
 226  2 parent.setLink(child);
 227  2 child.setLink(parent);
 228   
 229  2 List<Link> list = new ArrayList<Link>();
 230  2 list.add(parent);
 231   
 232  2 cache_.attach("/list", list);
 233  2 cache_.attach("/alias", list);
 234   
 235  2 List<Link> list1 = (List<Link>) cache_.find("/list");
 236  2 List<Link> list2 = (List<Link>) cache_.find("/alias");
 237   
 238  2 assertEquals("parent", list1.get(0).getName());
 239  2 assertEquals("child", list2.get(0).getLink().getName());
 240    }
 241   
 242    /**
 243    * @throws Exception
 244    */
 245  2 public void testCircularReference8() throws Exception
 246    {
 247  2 log.info("testCircularReference8() ...");
 248  2 Link parent = new Link("parent");
 249  2 Link child = new Link("child");
 250  2 parent.setLink(child);
 251  2 child.setLink(parent);
 252   
 253  2 cache_.attach("parent", parent);
 254  2 parent.setLink(child);// again
 255  2 child.setLink(parent);
 256    }
 257   
 258  2 public void testCircularAndSharedReferences() throws Exception
 259    {
 260  2 log.info("testCircularAndSharedReferences() ...");
 261  2 NodeManager pm_ = new NodeManager();
 262   
 263  2 pm_.setRootNode("root");
 264  2 pm_.addNode("root", "kanto");
 265  2 pm_.addNode("root.kanto", "tokyo");
 266  2 pm_.addNode("root.kanto", "kanagawa");
 267   
 268  2 assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
 269  2 cache_.attach("/propagation", pm_);
 270  2 pm_.addNode("root.kanto.tokyo", "hadanshita");
 271  2 assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
 272   
 273  2 List list = pm_.findNode("root").getChildren();
 274  2 assertEquals("Root should have children of ", 1, list.size());
 275  2 pm_.printNodes();
 276    }
 277   
 278  0 public void XtestObjectIdentity() throws Exception
 279    {
 280    }
 281   
 282  2 public static Test suite() throws Exception
 283    {
 284  2 return new TestSuite(CircularGraphTest.class);
 285    }
 286   
 287  0 public static void main(String[] args) throws Exception
 288    {
 289  0 junit.textui.TestRunner.run(CircularGraphTest.suite());
 290    }
 291   
 292    }