Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
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  10 public CircularGraphTest(String name)
 35    {
 36  10 super(name);
 37    }
 38   
 39  10 protected void setUp() throws Exception
 40    {
 41  10 super.setUp();
 42  10 log.info("setUp() ....");
 43  10 String configFile = "META-INF/local-service.xml";
 44  10 boolean toStart = false;
 45  10 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 46  10 cache_.start();
 47    }
 48   
 49  10 protected void tearDown() throws Exception
 50    {
 51  10 super.tearDown();
 52  10 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  1 public void testCircularReference1() throws Exception
 71    {
 72  1 log.info("testCircularReference1() ...");
 73  1 Link parent = new Link("parent");
 74  1 Link child = new Link("child");
 75  1 parent.setLink(child);
 76  1 child.setLink(parent);
 77  1 cache_.attach("/link/parent", parent);
 78  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 79  1 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  1 public void testCircularReference2() throws Exception
 88    {
 89  1 log.info("testCircularReference2() ...");
 90  1 Link parent = new Link("parent");
 91  1 Link child = new Link("child");
 92  1 parent.setLink(child);
 93  1 child.setLink(parent);
 94  1 cache_.attach("/link/parent", parent);
 95  1 cache_.attach("/link/child", child);
 96  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 97  1 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  1 public void testCircularReference3() throws Exception
 106    {
 107  1 log.info("testCircularReference3() ...");
 108  1 Link parent = new Link("parent");
 109  1 Link child = new Link("child");
 110  1 parent.setLink(child);
 111  1 child.setLink(parent);
 112  1 cache_.attach("/link/parent", parent);
 113  1 cache_.attach("/link/child", child);
 114  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 115  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 116  1 Link link = (Link) cache_.detach("/link/parent");
 117  1 cache_.detach("/link/child");
 118  1 assertEquals("child", link.getLink().getName());
 119  1 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
 120  1 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  1 public void testCircularReference3a() throws Exception
 129    {
 130  1 log.info("testCircularReference3() ...");
 131  1 Link parent = new Link("parent");
 132  1 Link child = new Link("child");
 133  1 parent.setLink(child);
 134  1 child.setLink(parent);
 135  1 cache_.attach("/link/parent", parent);
 136  1 cache_.attach("/link/child", child);
 137  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 138  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 139  1 cache_.detach("/link/child");
 140  1 Link link = (Link) cache_.detach("/link/parent");
 141  1 assertEquals("child", link.getLink().getName());
 142  1 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
 143  1 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  1 public void testCircularReference4() throws Exception
 152    {
 153  1 log.info("testCircularReference4() ...");
 154  1 Link parent = new Link("parent");
 155  1 Link child = new Link("child");
 156  1 cache_.attach("/link/parent", parent);
 157  1 cache_.attach("/link/child", child);
 158  1 parent.setLink(child);
 159  1 child.setLink(parent);
 160  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 161  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 162  1 Link link = (Link) cache_.detach("/link/parent");
 163  1 assertEquals("child", link.getLink().getName());
 164  1 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  1 public void testCircularReference5() throws Exception
 173    {
 174  1 log.info("testCircularReference5() ...");
 175  1 Link parent = new Link("parent");
 176  1 Link child = new Link("child");
 177  1 cache_.attach("/link/parent", parent);
 178  1 cache_.attach("/link/child", child);
 179  1 parent.setLink(child);
 180  1 child.setLink(parent);
 181  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 182  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 183  1 assertEquals("child", ((Link) cache_.find("/link/child")).getName());
 184  1 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
 185  1 Link link = (Link) cache_.detach("/link/parent");
 186  1 assertEquals("child", link.getLink().getName());
 187  1 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  1 public void testCircularReference6() throws Exception
 196    {
 197  1 log.info("testCircularReference6() ...");
 198  1 Link parent = new Link("parent");
 199  1 Link child = new Link("child");
 200  1 cache_.attach("/link/parent", parent);
 201  1 cache_.attach("/link/child", child);
 202  1 parent.setLink(child);
 203  1 child.setLink(parent);
 204  1 assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
 205  1 assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
 206  1 assertEquals("child", ((Link) cache_.find("/link/child")).getName());
 207  1 assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
 208  1 Link link = (Link) cache_.detach("/link/parent");
 209  1 assertEquals("child", link.getLink().getName());
 210  1 assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))));
 211   
 212    // re-attach
 213  1 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  1 public void testCircularReference7() throws Exception
 222    {
 223  1 log.info("testCircularReference7() ...");
 224  1 Link parent = new Link("parent");
 225  1 Link child = new Link("child");
 226  1 parent.setLink(child);
 227  1 child.setLink(parent);
 228   
 229  1 List<Link> list = new ArrayList<Link>();
 230  1 list.add(parent);
 231   
 232  1 cache_.attach("/list", list);
 233  1 cache_.attach("/alias", list);
 234   
 235  1 List<Link> list1 = (List<Link>) cache_.find("/list");
 236  1 List<Link> list2 = (List<Link>) cache_.find("/alias");
 237   
 238  1 assertEquals("parent", list1.get(0).getName());
 239  1 assertEquals("child", list2.get(0).getLink().getName());
 240    }
 241   
 242    /**
 243    * @throws Exception
 244    */
 245  1 public void testCircularReference8() throws Exception
 246    {
 247  1 log.info("testCircularReference8() ...");
 248  1 Link parent = new Link("parent");
 249  1 Link child = new Link("child");
 250  1 parent.setLink(child);
 251  1 child.setLink(parent);
 252   
 253  1 cache_.attach("parent", parent);
 254  1 parent.setLink(child);// again
 255  1 child.setLink(parent);
 256    }
 257   
 258  1 public void testCircularAndSharedReferences() throws Exception
 259    {
 260  1 log.info("testCircularAndSharedReferences() ...");
 261  1 NodeManager pm_ = new NodeManager();
 262   
 263  1 pm_.setRootNode("root");
 264  1 pm_.addNode("root", "kanto");
 265  1 pm_.addNode("root.kanto", "tokyo");
 266  1 pm_.addNode("root.kanto", "kanagawa");
 267   
 268  1 assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
 269  1 cache_.attach("/propagation", pm_);
 270  1 pm_.addNode("root.kanto.tokyo", "hadanshita");
 271  1 assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
 272   
 273  1 List list = pm_.findNode("root").getChildren();
 274  1 assertEquals("Root should have children of ", 1, list.size());
 275  1 pm_.printNodes();
 276    }
 277   
 278  0 public void XtestObjectIdentity() throws Exception
 279    {
 280    }
 281   
 282  1 public static Test suite() throws Exception
 283    {
 284  1 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    }