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 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 25 26 public class ObjectGraphTest extends TestCase 27 { 28 Log log = LogFactory.getLog(ObjectGraphTest.class); 29 PojoCache cache_; 30 31 public ObjectGraphTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 log.info("setUp() ...."); 40 String configFile = "META-INF/local-service.xml"; 41 boolean toStart = false; 42 cache_ = PojoCacheFactory.createCache(configFile, toStart); 43 cache_.start(); 44 } 45 46 protected void tearDown() throws Exception 47 { 48 super.tearDown(); 49 cache_.stop(); 50 } 51 52 54 protected Person createPerson(String name, int age) 55 { 56 Person p = new Person(); 57 p.setName(name); 58 p.setAge(age); 59 return p; 60 } 61 62 63 public void testListWithMultipleSharedObjccts() throws Exception 64 { 65 log.info("testListWithMultipleSharedObjects() ...."); 66 List list1 = new ArrayList (); 67 Address addr = new Address(); 68 addr.setCity("San Jose"); 69 addr.setZip(95123); 70 list1.add(addr); 71 72 List list2 = new ArrayList (); 73 List list3 = new ArrayList (); 74 list2.add(addr); 75 list3.add(addr); 76 77 cache_.attach("/list1", list1); 78 cache_.attach("/list2", list2); 79 cache_.attach("/list3", list3); 80 81 list3 = (List ) cache_.find("/list3"); 82 assertTrue("List size should not be 0 ", (list3.size() != 0)); 83 assertEquals("Address ", addr.getZip(), ((Address) list3.get(0)).getZip()); 84 addr.setCity("Sunnyvale"); 85 assertEquals("Address ", addr.getCity(), ((Address) list3.get(0)).getCity()); 86 87 cache_.detach("/list1"); 88 cache_.detach("/list2"); 89 cache_.detach("/list3"); 90 } 91 92 public void testRelationshipWithSharedList1() throws Exception 93 { 94 log.info("testRelationshipWithList() ...."); 95 List list1 = new ArrayList (); 96 Address addr = new Address(); 97 addr.setCity("San Jose"); 98 addr.setZip(95123); 99 list1.add(addr); 100 101 cache_.attach("/list", list1); 102 list1 = (List ) cache_.find("/list"); 103 assertEquals("List size should be ", 1, list1.size()); 104 cache_.attach("/alias", list1); 105 106 list1 = (List ) cache_.find("/list"); 107 assertEquals("List size should be ", 1, list1.size()); 108 List list2 = (List ) cache_.find("/alias"); 109 assertEquals("List size should be ", 1, list2.size()); 111 assertEquals("Both lists should be equal ", list1, list2); 112 assertEquals("Both list values should be equal ", list1.get(0), list2.get(0)); 113 } 114 115 public void testRelationshipWithSharedList2() throws Exception 116 { 117 log.info("testRelationshipWithList2() ...."); 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 List list2 = new ArrayList (); 125 list2.add(addr); 126 127 cache_.attach("/list1", list1); 128 cache_.attach("/list2", list2); 129 Address add2 = (Address) ((List ) cache_.find("/list2")).get(0); 130 Address add1 = (Address) ((List ) cache_.find("/list1")).get(0); 131 assertEquals("Address should be the same", add1, add2); 132 assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip()); 133 } 134 135 public void testListWithAttachAndDetach() throws Exception 136 { 137 log.info("testListWithAttachAndDetach() ...."); 138 List list1 = new ArrayList (); 139 Address addr1 = new Address(); 140 addr1.setCity("San Jose"); 141 addr1.setZip(95123); 142 143 Address addr2 = new Address(); 144 addr2.setCity("Sunnyvale"); 145 addr2.setZip(94086); 146 list1.add(addr2); 147 148 cache_.attach("/list", list1); 149 list1 = (List ) cache_.find("/list"); 150 assertEquals("List size should be ", 1, list1.size()); 151 cache_.attach("/alias", list1); 152 153 list1 = (List ) cache_.find("/list"); 154 assertEquals("List size should be ", 1, list1.size()); 155 List list2 = (List ) cache_.find("/alias"); 156 assertEquals("List size should be ", 1, list2.size()); 158 assertEquals("Both lists should be equal ", list1, list2); 159 assertEquals("Both list values should be equal ", list1.get(0), list2.get(0)); 160 } 161 162 public void testRelationshipWithSharedSet1() throws Exception 163 { 164 log.info("testRelationshipWithSet() ...."); 165 Set set1 = new HashSet (); 166 Address addr = new Address(); 167 addr.setCity("San Jose"); 168 addr.setZip(95123); 169 set1.add(addr); 170 171 cache_.attach("/set", set1); 173 set1 = (Set ) cache_.find("/set"); 175 cache_.attach("/alias", set1); 176 177 set1 = (Set ) cache_.find("/set"); 178 Set set2 = (Set ) cache_.find("/alias"); 179 assertTrue("Set size should not be 0 ", (set2.size() != 0)); 180 assertEquals("Both sets should be equal ", set1, set2); 181 Address add1 = (Address) set2.iterator().next(); 182 assertNotNull("Address should not be null", add1); 183 assertEquals("Zip ", 95123, add1.getZip()); 184 } 185 186 public void testRelationshipWithSharedSet2() throws Exception 187 { 188 log.info("testRelationshipWithSet2() ...."); 189 Set set1 = new HashSet (); 190 Address addr = new Address(); 191 addr.setCity("San Jose"); 192 addr.setZip(95123); 193 set1.add(addr); 194 195 Set set2 = new HashSet (); 196 set2.add(addr); 197 198 cache_.attach("/set1", set1); 199 cache_.attach("/set2", set2); 200 201 Address add2 = (Address) ((Set ) cache_.find("/set2")).iterator().next(); 202 Address add1 = (Address) ((Set ) cache_.find("/set1")).iterator().next(); 203 assertEquals("Address should be the same", add1, add2); 204 assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip()); 205 } 206 207 public void testRelationshipWithSharedMap1() throws Exception 208 { 209 log.info("testRelationshipWithMap() ...."); 210 Map map1 = new HashMap (); 211 Address addr = new Address(); 212 addr.setCity("San Jose"); 213 addr.setZip(95123); 214 map1.put("key1", addr); 215 216 cache_.attach("/map", map1); 217 cache_.attach("/alias", map1); 218 219 map1 = (Map ) cache_.find("/map"); 220 Map map2 = (Map ) cache_.find("/alias"); 221 assertTrue("Map size should not be 0 ", (map2.size() != 0)); 222 assertEquals("Both maps should be equal ", map1, map2); 223 Address add1 = (Address) ((Map.Entry ) map2.entrySet().iterator().next()).getValue(); 224 assertNotNull("Address should not be null", add1); 225 assertEquals("Zip ", 95123, add1.getZip()); 226 } 227 228 public void testRelationshipWithSharedMap2() throws Exception 229 { 230 log.info("testRelationshipWithMap2() ...."); 231 Map map1 = new HashMap (); 232 Address addr = new Address(); 233 addr.setCity("San Jose"); 234 addr.setZip(95123); 235 map1.put("key1", addr); 236 237 Map map2 = new HashMap (); 238 map2.put("key2", addr); 239 240 cache_.attach("/map", map1); 242 cache_.attach("/alias", map2); 243 244 map1 = (Map ) cache_.find("/map"); 245 map2 = (Map ) cache_.find("/alias"); 246 assertTrue("Map size should not be 0 ", (map2.size() != 0)); 247 Address add1 = (Address) ((Map.Entry ) map2.entrySet().iterator().next()).getValue(); 248 assertNotNull("Address should not be null", add1); 249 assertEquals("Zip ", 95123, add1.getZip()); 250 } 251 252 253 public void testObjectIdentity() throws Exception 254 { 255 } 256 257 public static Test suite() throws Exception 258 { 259 return new TestSuite(ObjectGraphTest.class); 260 } 261 262 public static void main(String [] args) throws Exception 263 { 264 junit.textui.TestRunner.run(suite()); 265 } 266 267 } 268 269 | Popular Tags |