Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 227   Methods: 11
NCLOC: 165   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ReplicatedSyncSetTest.java 100% 95.1% 81.8% 94.2%
coverage coverage
 1    package org.jboss.cache.pojo.collection;
 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.PojoCache;
 12    import org.jboss.cache.pojo.PojoCacheFactory;
 13    import org.jboss.cache.pojo.test.Address;
 14    import org.jboss.cache.pojo.test.Person;
 15   
 16    import java.util.HashSet;
 17    import java.util.Iterator;
 18    import java.util.Set;
 19   
 20    /**
 21    * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
 22    *
 23    * @author Ben Wang
 24    */
 25   
 26    public class ReplicatedSyncSetTest extends TestCase
 27    {
 28    Log log = LogFactory.getLog(ReplicatedSyncSetTest.class);
 29    PojoCache cache1;
 30    PojoCache cache2;
 31   
 32  4 public ReplicatedSyncSetTest(String name)
 33    {
 34  4 super(name);
 35    }
 36   
 37  4 protected void setUp() throws Exception
 38    {
 39  4 super.setUp();
 40  4 log.info("setUp() ....");
 41  4 cache1 = createCache("CacheGroup");
 42  4 cache2 = createCache("CacheGroup");
 43    }
 44   
 45  4 protected void tearDown() throws Exception
 46    {
 47  4 super.tearDown();
 48  4 cache1.getCache().removeNode(Fqn.fromString("/"));
 49  4 cache1.stop();
 50  4 cache2.stop();
 51    }
 52   
 53  8 private PojoCache createCache(String name) throws Exception
 54    {
 55  8 boolean toStart = false;
 56  8 PojoCache cache = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
 57  8 cache.start();
 58  8 return cache;
 59    }
 60   
 61    // public void testDummy() {}
 62   
 63   
 64  0 protected Person createPerson(String name, int age)
 65    {
 66  0 Person p = new Person();
 67  0 p.setName(name);
 68  0 p.setAge(age);
 69  0 return p;
 70    }
 71   
 72    /**
 73    * Test attachment and then detachment and attachment.
 74    *
 75    * @throws Exception
 76    */
 77  1 public void testAttachDetach() throws Exception
 78    {
 79  1 log.info("testAttachDetach() ....");
 80  1 Set set1 = new HashSet();
 81  1 Address addr = new Address();
 82  1 addr.setCity("San Jose");
 83  1 addr.setZip(95123);
 84  1 set1.add(addr);
 85   
 86  1 Address addr2 = new Address();
 87  1 addr2.setCity("Santa Clara");
 88  1 addr2.setZip(95131);
 89   
 90  1 Address addr3 = new Address();
 91  1 addr3.setCity("Sunnyvale");
 92  1 addr3.setZip(94086);
 93   
 94    // Pure list
 95  1 cache1.attach("/set", set1);
 96  1 set1 = (Set) cache1.find("/set");
 97  1 set1.add(addr2);
 98  1 set1 = (Set) cache1.detach("/set");
 99  1 assertEquals("Detached set should still be", 2, set1.size());
 100  1 set1.add(addr3);
 101  1 cache1.attach("/set", set1);
 102   
 103  1 Set set2 = (Set) cache2.find("/set");
 104  1 assertEquals("Set size should be ", 3, set2.size());
 105    }
 106   
 107  1 public void testRelationshipWithSharedSet1() throws Exception
 108    {
 109  1 log.info("testRelationshipWithSet() ....");
 110  1 Set set1 = new HashSet();
 111  1 Address addr = new Address();
 112  1 addr.setCity("San Jose");
 113  1 addr.setZip(95123);
 114  1 set1.add(addr);
 115   
 116    // Pure set
 117  1 cache1.attach("/set", set1);
 118    // We specifically need to use Proxy otherwise it won't work with multiple references
 119  1 set1 = (Set) cache1.find("/set");
 120  1 cache1.attach("/alias", set1);
 121   
 122  1 Set set2 = (Set) cache1.find("/alias");
 123  1 Address add1 = (Address) set2.iterator().next();
 124  1 assertNotNull("Address should not be null", add1);
 125  1 assertEquals("Zip ", 95123, add1.getZip());
 126   
 127  1 set1 = (Set) cache2.find("/set");
 128  1 set2 = (Set) cache2.find("/alias");
 129  1 assertTrue("Set size should not be 0 ", (set2.size() != 0));
 130  1 assertEquals("Both sets should be equal ", set1, set2);
 131  1 add1 = (Address) set2.iterator().next();
 132  1 assertNotNull("Address should not be null", add1);
 133  1 assertEquals("Zip ", 95123, add1.getZip());
 134    }
 135   
 136  1 public void testRelationshipWithSharedSet2() throws Exception
 137    {
 138  1 log.info("testRelationshipWithSet2() ....");
 139  1 Set set1 = new HashSet();
 140  1 Address addr = new Address();
 141  1 addr.setCity("San Jose");
 142  1 addr.setZip(95123);
 143  1 set1.add(addr);
 144   
 145  1 Set set2 = new HashSet();
 146  1 set2.add(addr);
 147   
 148  1 cache1.attach("/set1", set1);
 149  1 cache1.attach("/set2", set2);
 150  1 Address add2 = (Address) ((Set) cache2.find("/set2")).iterator().next();
 151  1 Address add1 = (Address) ((Set) cache2.find("/set1")).iterator().next();
 152  1 assertEquals("Address should be the same", add1, add2);
 153  1 assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
 154    }
 155   
 156  1 public void testNullWithSharedSet1() throws Exception
 157    {
 158  1 log.info("testNullWithSharedSet1() ....");
 159  1 Set set1 = new HashSet();
 160  1 set1.add("element 0");
 161  1 set1.add(null); // element 1
 162  1 set1.add("element 2");
 163  1 assertTrue("contains test for null value", set1.contains(null));
 164  1 Object a1[] = set1.toArray();
 165  1 for (int looper = 0; looper < a1.length; looper++)
 166    {
 167  3 System.out.println("contained values:" + a1[looper]);
 168    }
 169   
 170    // Pure set
 171  1 cache1.attach("/set", set1);
 172    // We specifically need to use Proxy otherwise it won't work with multiple references
 173  1 set1 = (Set) cache1.find("/set");
 174  1 cache1.attach("/alias", set1);
 175   
 176  1 Set set2 = (Set) cache1.find("/alias");
 177   
 178  1 set1 = (Set) cache2.find("/set");
 179  1 set2 = (Set) cache2.find("/alias");
 180  1 assertTrue("Set size should not be 0 ", (set2.size() != 0));
 181  1 assertEquals("Both sets should be equal ", set1, set2);
 182   
 183  1 a1 = set1.toArray();
 184  1 for (int looper = 0; looper < a1.length; looper++)
 185    {
 186  3 System.out.println("contained values:" + a1[looper]);
 187    }
 188  1 assertTrue("contains test for null value", set1.contains(null));
 189  1 assertTrue("contains test for null value", set2.contains(null));
 190   
 191  1 Iterator iter = set1.iterator();
 192  1 while (iter.hasNext())
 193    {
 194  3 Object val = iter.next();
 195  3 if ("element 2".equals(val))
 196    {
 197  1 iter.remove(); // remove element 2
 198    }
 199    }
 200  1 assertFalse("element 2 is removed", set2.contains("element 2"));
 201    }
 202   
 203    /* This test won't pass since the recursive set will fail during replication as well
 204    because of HashSet that calls out to HashMap hashCode. This causes the recursion.
 205    public void testRecursion2() throws Exception
 206    {
 207    Set set = new HashSet();
 208    set.add("1");
 209    set.add("2");
 210    set.add(set);
 211   
 212    cache1.attach("set", set);
 213    }
 214    */
 215   
 216  1 public static Test suite() throws Exception
 217    {
 218  1 return new TestSuite(ReplicatedSyncSetTest.class);
 219    }
 220   
 221  0 public static void main(String[] args) throws Exception
 222    {
 223  0 junit.textui.TestRunner.run(suite());
 224    }
 225   
 226    }
 227