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 import org.jboss.aop.proxy.ClassProxy; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.ListIterator ; 18 19 24 25 public class ReplicatedSyncListTest extends TestCase 26 { 27 Log log = LogFactory.getLog(ReplicatedSyncListTest.class); 28 PojoCache cache1; 29 PojoCache cache2; 30 31 public ReplicatedSyncListTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 log.info("setUp() ...."); 40 cache1 = createCache("CacheGroup"); 41 cache2 = createCache("CacheGroup"); 42 } 43 44 protected void tearDown() throws Exception 45 { 46 super.tearDown(); 47 cache1.getCache().removeNode(Fqn.fromString("/")); 48 cache1.stop(); 49 cache2.stop(); 50 } 51 52 private PojoCache createCache(String name) throws Exception 53 { 54 String configFile = "META-INF/replSync-service.xml"; 55 boolean toStart = false; 56 PojoCache cache = PojoCacheFactory.createCache(configFile, toStart); 57 cache.start(); 58 return cache; 59 } 60 61 63 64 protected Person createPerson(String name, int age) 65 { 66 Person p = new Person(); 67 p.setName(name); 68 p.setAge(age); 69 return p; 70 } 71 72 77 public void testAttachDetach() throws Exception 78 { 79 log.info("testAttachDetach() ...."); 80 List list1 = new ArrayList (); 81 Address addr = new Address(); 82 addr.setCity("San Jose"); 83 addr.setZip(95123); 84 list1.add(addr); 85 86 Address addr2 = new Address(); 87 addr2.setCity("Santa Clara"); 88 addr2.setZip(95131); 89 90 Address addr3 = new Address(); 91 addr3.setCity("Sunnyvale"); 92 addr3.setZip(94086); 93 94 cache1.attach("/list", list1); 96 list1 = (List ) cache1.find("/list"); 97 list1.add(addr2); 98 list1 = (List )cache1.detach("/list"); 100 assertEquals("Detached list should still be", 2, list1.size()); 101 list1.add(addr3); 102 cache1.attach("/list", list1); 103 104 List list2 = (List ) cache2.find("/list"); 105 assertTrue("List size should not be 0 ", (list2.size() != 0)); 106 assertEquals("Both list values should be equal ", ((Address) list1.get(0)).getZip(), 107 ((Address) list2.get(0)).getZip()); 108 } 109 110 115 public void testRelationshipWithSharedList1() throws Exception 116 { 117 log.info("testRelationshipWithList() ...."); 118 List list1 = new ArrayList (); 119 Address addr = new Address(); 120 addr.setCity("San Jose"); 121 addr.setZip(95123); 122 list1.add(addr); 123 124 cache1.attach("/list", list1); 126 list1 = (List ) cache1.find("/list"); 128 cache1.attach("/alias", list1); 129 130 List list2 = (List ) cache1.find("/alias"); 131 Address add1 = (Address) list2.get(0); 132 assertNotNull("Address should not be null", add1); 133 assertEquals("Zip ", 95123, add1.getZip()); 134 135 list1 = (List ) cache2.find("/list"); 136 list2 = (List ) cache2.find("/alias"); 137 assertTrue("List size should not be 0 ", (list2.size() != 0)); 138 assertEquals("Both lists should be equal ", list1, list2); 139 assertEquals("Both list values should be equal ", list1.get(0), list2.get(0)); 140 } 141 142 147 public void testRelationshipWithSharedList2() throws Exception 148 { 149 log.info("testRelationshipWithList2() ...."); 150 List list1 = new ArrayList (); 152 Address addr = new Address(); 153 addr.setCity("San Jose"); 154 addr.setZip(95123); 155 list1.add(addr); 156 157 List list2 = new ArrayList (); 158 list2.add(addr); 159 160 cache1.attach("/list1", list1); 161 cache1.attach("/list2", list2); 162 Address add2 = (Address) ((List ) cache2.find("/list2")).get(0); 163 Address add1 = (Address) ((List ) cache2.find("/list1")).get(0); 164 assertEquals("Address should be the same", add1, add2); 165 assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip()); 166 } 167 168 173 public void testRelationshipWithSharedList3() throws Exception 174 { 175 log.info("testRelationshipWithList3() ...."); 176 List list1 = new ArrayList (); 178 Address addr = new Address(); 179 addr.setCity("San Jose"); 180 addr.setZip(95123); 181 list1.add(addr); 182 183 List list2 = new ArrayList (); 184 list2.add(addr); 185 186 cache1.attach("/address", addr); 187 cache1.attach("/list1", list1); 188 Address add1 = (Address) ((List ) cache2.find("/list1")).get(0); 189 Address add2 = (Address) cache2.find("/address"); 190 assertEquals("Address should be the same", add1, add2); 191 assertEquals("Both shared object should be equal ", 95123, add1.getZip()); 192 } 193 194 public void testNullWithSharedList1() throws Exception 195 { 196 log.info("testNullWithSharedList1() ...."); 197 List list1 = new ArrayList (); 198 list1.add("element 0"); 199 list1.add(null); list1.add("element 2"); 201 list1.add(null); 203 cache1.attach("/list", list1); 205 list1 = (List ) cache1.find("/list"); 207 cache1.attach("/alias", list1); 208 209 List list2 = (List ) cache1.find("/alias"); 210 211 list1 = (List ) cache2.find("/list"); 212 list2 = (List ) cache2.find("/alias"); 213 assertTrue("List size should not be 0 ", (list2.size() != 0)); 214 assertEquals("Both listss should be equal ", list1, list2); 215 216 Object a1[] = list1.toArray(); 217 Object a2[] = list2.toArray(); 218 assertTrue("element 1 is null", (a1[1] == null)); 219 assertTrue("element 1 is null", (a2[1] == null)); 220 assertTrue("element 3 is null", (a1[3] == null)); 221 assertTrue("element 3 is null", (a2[3] == null)); 222 223 assertTrue("contains test for null value", list1.contains(null)); 224 assertTrue("contains test for null value", list2.contains(null)); 225 226 assertTrue("index of null ", list2.indexOf(null) == 1); 227 assertTrue("last index of null ", list2.lastIndexOf(null) == 3); 228 229 list1.set(0, null); assertTrue("set first item to null", list2.get(0) == null); 231 list1.set(0, "element 0"); 232 assertTrue("set first item to 'element 0'", list2.get(0).equals("element 0")); 233 234 235 ListIterator listIter = list1.listIterator(); 236 assertTrue("listiter has next", listIter.hasNext()); 237 assertTrue("listiter 1st element is 'element 0'", listIter.next().equals("element 0")); 238 assertTrue("listiter has next", listIter.hasNext()); 239 assertTrue("listiter 2nd element is null", listIter.next() == null); 240 listIter.remove(); 241 242 assertTrue("2nd element should be 'element 2'", list2.get(1).equals("element 2")); 243 244 } 245 246 public void testRecursion1() throws Exception 247 { 248 List list = new ArrayList (); 249 list.add("1"); 250 list.add("2"); 251 cache1.attach("list", list); 252 253 list = (List )cache1.find("list"); 254 list.add(list); 255 256 assertEquals("size ", 3, list.size()); 257 List l2 = (List )list.get(2); 258 assertTrue("Instance of AopProxy", l2 instanceof ClassProxy); 259 } 261 262 public void testRecursion2() throws Exception 263 { 264 List list = new ArrayList (); 265 list.add("1"); 266 list.add("2"); 267 list.add(list); 268 269 cache1.attach("list", list); 270 271 list = (List )cache1.find("list"); 272 list.toString(); 273 } 274 275 public static Test suite() throws Exception 276 { 277 return new TestSuite(ReplicatedSyncListTest.class); 278 } 279 280 public static void main(String [] args) throws Exception 281 { 282 junit.textui.TestRunner.run(suite()); 283 } 284 285 } 286 287 | Popular Tags |