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.pojo.PojoCache; 9 import org.jboss.cache.pojo.PojoCacheFactory; 10 import org.jboss.cache.pojo.test.Address; 11 import org.jboss.cache.pojo.test.Person; 12 import org.jboss.cache.Fqn; 13 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 23 24 public class ReplicatedSyncSetTest extends TestCase 25 { 26 Log log = LogFactory.getLog(ReplicatedSyncSetTest.class); 27 PojoCache cache1; 28 PojoCache cache2; 29 30 public ReplicatedSyncSetTest(String name) 31 { 32 super(name); 33 } 34 35 protected void setUp() throws Exception 36 { 37 super.setUp(); 38 log.info("setUp() ...."); 39 cache1 = createCache("CacheGroup"); 40 cache2 = createCache("CacheGroup"); 41 } 42 43 protected void tearDown() throws Exception 44 { 45 super.tearDown(); 46 cache1.getCache().removeNode(Fqn.fromString("/")); 47 cache1.stop(); 48 cache2.stop(); 49 } 50 51 private PojoCache createCache(String name) throws Exception 52 { 53 String configFile = "META-INF/replSync-service.xml"; 54 boolean toStart = false; 55 PojoCache cache = PojoCacheFactory.createCache(configFile, toStart); 56 cache.start(); 57 return cache; 58 } 59 60 62 63 protected Person createPerson(String name, int age) 64 { 65 Person p = new Person(); 66 p.setName(name); 67 p.setAge(age); 68 return p; 69 } 70 71 76 public void testAttachDetach() throws Exception 77 { 78 log.info("testAttachDetach() ...."); 79 Set set1 = new HashSet (); 80 Address addr = new Address(); 81 addr.setCity("San Jose"); 82 addr.setZip(95123); 83 set1.add(addr); 84 85 Address addr2 = new Address(); 86 addr2.setCity("Santa Clara"); 87 addr2.setZip(95131); 88 89 Address addr3 = new Address(); 90 addr3.setCity("Sunnyvale"); 91 addr3.setZip(94086); 92 93 cache1.attach("/set", set1); 95 set1 = (Set ) cache1.find("/set"); 96 set1.add(addr2); 97 set1 = (Set )cache1.detach("/set"); 98 assertEquals("Detached set should still be", 2, set1.size()); 99 set1.add(addr3); 100 cache1.attach("/set", set1); 101 102 Set set2 = (Set ) cache2.find("/set"); 103 assertEquals("Set size should be ", 3, set2.size()); 104 } 105 106 public void testRelationshipWithSharedSet1() throws Exception 107 { 108 log.info("testRelationshipWithSet() ...."); 109 Set set1 = new HashSet (); 110 Address addr = new Address(); 111 addr.setCity("San Jose"); 112 addr.setZip(95123); 113 set1.add(addr); 114 115 cache1.attach("/set", set1); 117 set1 = (Set ) cache1.find("/set"); 119 cache1.attach("/alias", set1); 120 121 Set set2 = (Set ) cache1.find("/alias"); 122 Address add1 = (Address) set2.iterator().next(); 123 assertNotNull("Address should not be null", add1); 124 assertEquals("Zip ", 95123, add1.getZip()); 125 126 set1 = (Set ) cache2.find("/set"); 127 set2 = (Set ) cache2.find("/alias"); 128 assertTrue("Set size should not be 0 ", (set2.size() != 0)); 129 assertEquals("Both sets should be equal ", set1, set2); 130 add1 = (Address) set2.iterator().next(); 131 assertNotNull("Address should not be null", add1); 132 assertEquals("Zip ", 95123, add1.getZip()); 133 } 134 135 public void testRelationshipWithSharedSet2() throws Exception 136 { 137 log.info("testRelationshipWithSet2() ...."); 138 Set set1 = new HashSet (); 139 Address addr = new Address(); 140 addr.setCity("San Jose"); 141 addr.setZip(95123); 142 set1.add(addr); 143 144 Set set2 = new HashSet (); 145 set2.add(addr); 146 147 cache1.attach("/set1", set1); 148 cache1.attach("/set2", set2); 149 Address add2 = (Address) ((Set ) cache2.find("/set2")).iterator().next(); 150 Address add1 = (Address) ((Set ) cache2.find("/set1")).iterator().next(); 151 assertEquals("Address should be the same", add1, add2); 152 assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip()); 153 } 154 155 public void testNullWithSharedSet1() throws Exception 156 { 157 log.info("testNullWithSharedSet1() ...."); 158 Set set1 = new HashSet (); 159 set1.add("element 0"); 160 set1.add(null); set1.add("element 2"); 162 assertTrue("contains test for null value", set1.contains(null)); 163 Object a1[] = set1.toArray(); 164 for (int looper = 0; looper < a1.length; looper++) 165 { 166 System.out.println("contained values:" + a1[looper]); 167 } 168 169 cache1.attach("/set", set1); 171 set1 = (Set ) cache1.find("/set"); 173 cache1.attach("/alias", set1); 174 175 Set set2 = (Set ) cache1.find("/alias"); 176 177 set1 = (Set ) cache2.find("/set"); 178 set2 = (Set ) cache2.find("/alias"); 179 assertTrue("Set size should not be 0 ", (set2.size() != 0)); 180 assertEquals("Both sets should be equal ", set1, set2); 181 182 a1 = set1.toArray(); 183 for (int looper = 0; looper < a1.length; looper++) 184 { 185 System.out.println("contained values:" + a1[looper]); 186 } 187 assertTrue("contains test for null value", set1.contains(null)); 188 assertTrue("contains test for null value", set2.contains(null)); 189 190 Iterator iter = set1.iterator(); 191 while (iter.hasNext()) 192 { 193 Object val = iter.next(); 194 if ("element 2".equals(val)) 195 { 196 iter.remove(); } 198 } 199 assertFalse("element 2 is removed", set2.contains("element 2")); 200 } 201 202 203 215 216 public static Test suite() throws Exception 217 { 218 return new TestSuite(ReplicatedSyncSetTest.class); 219 } 220 221 public static void main(String [] args) throws Exception 222 { 223 junit.textui.TestRunner.run(suite()); 224 } 225 226 } 227 228 | Popular Tags |