Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 240   Methods: 14
NCLOC: 186   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ReplicatedObjectGraphTest.java - 99.2% 92.9% 98.6%
coverage coverage
 1    package org.jboss.cache.pojo;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.apache.commons.logging.Log;
 7    import org.apache.commons.logging.LogFactory;
 8    import org.jboss.cache.Fqn;
 9    import org.jboss.cache.config.Configuration.CacheMode;
 10    import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
 11    import org.jboss.cache.pojo.test.Address;
 12    import org.jboss.cache.pojo.test.Person;
 13   
 14    /**
 15    * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
 16    *
 17    * @author Ben Wang
 18    */
 19   
 20    public class ReplicatedObjectGraphTest extends TestCase
 21    {
 22    Log log = LogFactory.getLog(ReplicatedObjectGraphTest.class);
 23    PojoCache cache1;
 24    PojoCache cache2;
 25   
 26  4 public ReplicatedObjectGraphTest(String name)
 27    {
 28  4 super(name);
 29    }
 30   
 31  4 protected void setUp() throws Exception
 32    {
 33  4 super.setUp();
 34  4 log.info("setUp() ....");
 35  4 cache1 = createCache("CacheGroup");
 36  4 cache2 = createCache("CacheGroup");
 37    }
 38   
 39  4 protected void tearDown() throws Exception
 40    {
 41  4 super.tearDown();
 42  4 cache1.getCache().removeNode(Fqn.fromString("/"));
 43  4 cache1.stop();
 44  4 cache2.stop();
 45    }
 46   
 47  8 private PojoCache createCache(String name) throws Exception
 48    {
 49  8 boolean toStart = false;
 50  8 PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 51  8 tree.start();
 52  8 return tree;
 53    }
 54   
 55    // public void testDummy() {}
 56   
 57  9 protected Person createPerson(String name, int age)
 58    {
 59  9 Person p = new Person();
 60  9 p.setName(name);
 61  9 p.setAge(age);
 62  9 return p;
 63    }
 64   
 65  1 private void stage0() throws Exception
 66    {
 67  1 cache1.attach("/person/joe", createPerson("Joe Black", 31));
 68  1 Person joe = (Person) cache1.find("/person/joe");
 69  1 cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
 70  1 Person ben = (Person) cache1.find("/person/ben");
 71   
 72  1 Address addr = new Address();
 73  1 addr.setStreet("123 Albert Ave.");
 74  1 addr.setCity("Sunnyvale");
 75  1 addr.setZip(94087);
 76  1 cache1.attach("/address", addr);
 77   
 78    // They share the sub-object: address
 79  1 joe.setAddress(addr);
 80  1 ben.setAddress(addr);
 81  1 assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
 82  1 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
 83    }
 84   
 85  1 private void stage1() throws Exception
 86    {
 87  1 cache1.attach("/person/joe", createPerson("Joe Black", 31));
 88  1 Person joe = (Person) cache1.find("/person/joe");
 89  1 cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
 90  1 Person ben = (Person) cache1.find("/person/ben");
 91   
 92  1 Address addr = new Address();
 93  1 addr.setStreet("123 Albert Ave.");
 94  1 addr.setCity("Sunnyvale");
 95  1 addr.setZip(94087);
 96   
 97    // They share the sub-object: address
 98  1 joe.setAddress(addr);
 99  1 ben.setAddress(addr);
 100  1 assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
 101  1 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
 102    }
 103   
 104  1 private void stage2(PojoCache cache) throws Exception
 105    {
 106    //
 107  1 cache.detach("/person/joe");
 108  1 Person ben = (Person) cache.find("/person/ben");
 109  1 assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
 110  1 Address addr = ben.getAddress();
 111  1 addr.setCity("Santa Clara");
 112  1 assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity());
 113    }
 114   
 115    /**
 116    * Test whether repeated update on the ref count will change the replicated aop instances
 117    *
 118    * @throws Exception
 119    */
 120  1 public void testCheckReplInstance() throws Exception
 121    {
 122  1 log.info("testCheckReplInstance() ...");
 123  1 stage0();
 124  1 TestingUtil.sleepThread(100);
 125  1 Person joe = (Person) cache1.find("/person/joe");
 126  1 Person ben = (Person) cache1.find("/person/ben");
 127  1 assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
 128    ben.getAddress().getCity());
 129   
 130  1 Address joe1 = (Address) cache2.find("/address");
 131  1 assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity());
 132  1 ben = (Person) cache2.find("/person/ben");
 133  1 cache2.detach("/person/ben");
 134  1 Address joe2 = (Address) cache2.find("/address");
 135  1 assertEquals("Joe's reference should be the same.", joe1, joe2);
 136    }
 137   
 138  1 public void testRefCountCheckRepl() throws Exception
 139    {
 140  1 log.info("testRefCountCheckRepl() ...");
 141  1 stage1();
 142  1 TestingUtil.sleepThread(100);
 143  1 Person joe = (Person) cache1.find("/person/joe");
 144  1 Person ben = (Person) cache1.find("/person/ben");
 145  1 assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
 146    ben.getAddress().getCity());
 147  1 TestingUtil.sleepThread(100);
 148  1 stage2(cache2);
 149  1 assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity());
 150  1 cache2.detach("/person/ben");
 151    }
 152   
 153   
 154  1 public void testdetach1() throws Exception
 155    {
 156  1 log.info("testdetach1() ...");
 157  1 cache1.attach("/person/joe", createPerson("Joe Black", 31));
 158  1 Person joe = (Person) cache1.find("/person/joe");
 159  1 cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
 160  1 Person ben = (Person) cache1.find("/person/ben");
 161   
 162  1 Address addr = new Address();
 163  1 addr.setStreet("123 Albert Ave.");
 164  1 addr.setCity("Sunnyvale");
 165  1 addr.setZip(94087);
 166   
 167    // They share the sub-object: address
 168  1 log.info("testMultipleReference(): set Joe address");
 169  1 joe.setAddress(addr);
 170  1 log.info("testMultipleReference(): set Ben address");
 171  1 ben.setAddress(addr);
 172   
 173  1 Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
 174  1 Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
 175  1 assertEquals(add1.getCity(), add2.getCity());
 176  1 addr.setCity("Santa Clara");
 177  1 assertEquals(add1.getCity(), add2.getCity());
 178   
 179    // Remove pojo joe will relocate the address field to ben's
 180  1 cache2.detach("/person/joe");
 181  1 add2 = ((Person) cache2.find("/person/ben")).getAddress();
 182   
 183  1 assertEquals("City ", "Santa Clara", add2.getCity());
 184    }
 185   
 186  1 public void testdetach2() throws Exception
 187    {
 188  1 log.info("testdetach2() ...");
 189  1 cache1.attach("/person/joe", createPerson("Joe Black", 31));
 190  1 Person joe = (Person) cache1.find("/person/joe");
 191  1 cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
 192  1 Person ben = (Person) cache1.find("/person/ben");
 193  1 cache1.attach("/person/john", createPerson("John Daly", 41));
 194  1 Person john = (Person) cache1.find("/person/john");
 195   
 196  1 Address addr = new Address();
 197  1 addr.setStreet("123 Albert Ave.");
 198  1 addr.setCity("Sunnyvale");
 199  1 addr.setZip(94087);
 200   
 201  1 Address addr1 = new Address();
 202  1 addr1.setStreet("123 Albert Ave.");
 203  1 addr1.setCity("San Jose");
 204  1 addr1.setZip(94087);
 205   
 206    // They share the sub-object: address
 207  1 log.info("testMultipleReference(): set Joe address");
 208  1 joe.setAddress(addr);
 209  1 log.info("testMultipleReference(): set Ben address");
 210  1 ben.setAddress(addr);
 211  1 john.setAddress(addr);
 212   
 213  1 Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
 214  1 Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
 215  1 assertEquals(add1.getCity(), add2.getCity());
 216  1 addr.setCity("Santa Clara");
 217  1 assertEquals(add1.getCity(), add2.getCity());
 218   
 219    // Remove pojo joe will relocate the address field to ben's
 220  1 joe.setAddress(addr1);
 221  1 add2 = ((Person) cache2.find("/person/joe")).getAddress();
 222  1 assertEquals("City ", "San Jose", add2.getCity());
 223  1 add2 = ((Person) cache2.find("/person/ben")).getAddress();
 224  1 assertEquals("City ", "Santa Clara", add2.getCity());
 225  1 add2 = ((Person) cache2.find("/person/john")).getAddress();
 226  1 assertEquals("City ", "Santa Clara", add2.getCity());
 227    }
 228   
 229  1 public static Test suite() throws Exception
 230    {
 231  1 return new TestSuite(ReplicatedObjectGraphTest.class);
 232    }
 233   
 234  0 public static void main(String[] args) throws Exception
 235    {
 236  0 junit.textui.TestRunner.run(suite());
 237    }
 238   
 239    }
 240