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.PojoCacheException; 11 import org.jboss.cache.pojo.test.Address; 12 import org.jboss.cache.pojo.test.Person; 13 import org.jboss.cache.Fqn; 14 import org.jboss.aop.proxy.ClassProxy; 15 16 import java.util.Collection ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 28 29 public class ReplicatedSyncMapTest extends TestCase 30 { 31 Log log = LogFactory.getLog(ReplicatedSyncMapTest.class); 32 PojoCache cache1; 33 PojoCache cache2; 34 35 public ReplicatedSyncMapTest(String name) 36 { 37 super(name); 38 } 39 40 protected void setUp() throws Exception 41 { 42 super.setUp(); 43 log.info("setUp() ...."); 44 cache1 = createCache("CacheGroup"); 45 cache2 = createCache("CacheGroup"); 46 } 47 48 protected void tearDown() throws Exception 49 { 50 super.tearDown(); 51 cache1.getCache().removeNode(Fqn.fromString("/")); 52 cache1.stop(); 53 cache2.stop(); 54 } 55 56 private PojoCache createCache(String name) throws Exception 57 { 58 String configFile = "META-INF/replSync-service.xml"; 59 boolean toStart = false; 60 PojoCache cache = PojoCacheFactory.createCache(configFile, toStart); 61 cache.start(); 62 return cache; 63 } 64 65 67 68 protected Person createPerson(String name, int age) 69 { 70 Person p = new Person(); 71 p.setName(name); 72 p.setAge(age); 73 return p; 74 } 75 76 77 82 public void testAttachDetach() throws Exception 83 { 84 log.info("testAttachDetach() ...."); 85 Map map1 = new HashMap (); 86 Address addr = new Address(); 87 addr.setCity("San Jose"); 88 addr.setZip(95123); 89 map1.put("key1", addr); 90 91 Address addr2 = new Address(); 92 addr2.setCity("Santa Clara"); 93 addr2.setZip(95131); 94 95 Address addr3 = new Address(); 96 addr2.setCity("Sunnyvale"); 97 addr3.setZip(94086); 98 99 cache1.attach("/map", map1); 101 map1 = (Map ) cache1.find("/map"); 102 map1.put("key2", addr2); 103 map1 = (Map )cache1.detach("/map"); 104 assertEquals("Detached map should still be", 2, map1.size()); 105 map1.put("key3", addr3); 106 cache1.attach("/map", map1); 107 108 Map map2 = (Map ) cache2.find("/map"); 109 assertEquals("Map size should be ", 3, map2.size()); 110 } 111 112 public void testRelationshipWithSharedMap1() throws Exception 113 { 114 log.info("testRelationshipWithMap() ...."); 115 Map map1 = new HashMap (); 116 Address addr = new Address(); 117 addr.setCity("San Jose"); 118 addr.setZip(95123); 119 map1.put("key1", addr); 120 121 cache1.attach("/map", map1); 123 map1 = (Map ) cache1.find("/map"); 125 cache1.attach("/alias", map1); 126 127 Map map2 = (Map ) cache1.find("/alias"); 128 Address add1 = (Address) ((Map.Entry ) map2.entrySet().iterator().next()).getValue(); 129 assertNotNull("Address should not be null", add1); 130 assertEquals("Zip ", 95123, add1.getZip()); 131 132 map1 = (Map ) cache2.find("/map"); 133 map2 = (Map ) cache2.find("/alias"); 134 assertTrue("Map size should not be 0 ", (map2.size() != 0)); 135 assertEquals("Both maps should be equal ", map1, map2); 136 add1 = (Address) ((Map.Entry ) map2.entrySet().iterator().next()).getValue(); 137 assertNotNull("Address should not be null", add1); 138 assertEquals("Zip ", 95123, add1.getZip()); 139 } 140 141 public void testNullWithSharedMap1() throws Exception 142 { 143 log.info("testNullWithSharedMap1() ...."); 144 Map map1 = new HashMap (); 145 map1.put("null value test", null); 146 map1.put(null, "null key test"); 147 148 cache1.attach("/map", map1); 150 map1 = (Map ) cache1.find("/map"); 152 cache1.attach("/alias", map1); 153 154 Map map2 = (Map ) cache1.find("/alias"); 155 156 map1 = (Map ) cache2.find("/map"); 157 map2 = (Map ) cache2.find("/alias"); 158 assertTrue("Map size should not be 0 ", (map2.size() != 0)); 159 assertEquals("Both maps should be equal ", map1, map2); 160 161 assertTrue("Get null key returns non-null value", map2.get(null) != null); 162 assertTrue("Get null key returns correct value", map2.get(null).equals("null key test")); 163 164 assertTrue("Get null value returns null value", map2.get("null value test") == null); 165 assertTrue("containsKey test for null value", map2.containsKey("null value test")); 166 assertTrue("containsKey test for null key", map2.containsKey(null)); 167 assertTrue("containsValue test for null value", map2.containsValue(null)); 168 169 assertTrue("values (positive) null test", map2.values().contains(null)); 170 Collection walk = map1.values(); 171 172 assertTrue(walk.remove(null)); 173 assertFalse("values (negative) null test", map2.values().contains(null)); 174 175 assertTrue("values (positive) null test", map2.values().contains("null key test")); 176 assertTrue(walk.remove("null key test")); 177 assertFalse("values (negative) null test", map2.values().contains("null key test")); 178 179 map1.put("null value test", null); 180 map1.put(null, "null key test"); 181 182 183 assertTrue("remove null item test", map1.remove(null).equals("null key test")); 184 assertFalse("null item removed", map2.containsKey(null)); 185 186 187 } 188 189 public void testRelationshipWithSharedMap2() throws Exception 190 { 191 log.info("testRelationshipWithMap2() ...."); 192 Map map1 = new HashMap (); 193 Address addr = new Address(); 194 addr.setCity("San Jose"); 195 addr.setZip(95123); 196 map1.put("key1", addr); 197 198 Map map2 = new HashMap (); 199 map2.put("key2", addr); 200 201 cache2.attach("/map1", map1); 202 cache2.attach("/map2", map2); 203 204 map1 = (Map ) cache2.find("/map1"); 205 map2 = (Map ) cache2.find("/map2"); 206 assertTrue("Map size should not be 0 ", (map2.size() != 0)); 207 assertEquals("Both maps should be equal ", map1.get("key1"), map2.get("key2")); 208 Address add1 = (Address) ((Map.Entry ) map2.entrySet().iterator().next()).getValue(); 209 assertNotNull("Address should not be null", add1); 210 assertEquals("Zip ", 95123, add1.getZip()); 211 } 212 213 public void testKeySetRemoveWithSharedMap1() throws Exception 214 { 215 log.info("testKeySetRemoveWithSharedMap1() ...."); 216 Map map1 = new HashMap (); 217 Address addr = new Address(); 218 addr.setCity("San Jose"); 219 addr.setZip(95123); 220 map1.put("key1_map1", addr); 221 map1.put("key2_map1", null); 222 map1.put(null, "null value test"); 223 Map map2 = new HashMap (); 224 map2.put("key1_map2", addr); 225 map2.put("key2_map2", "round"); 226 227 assertTrue("key1 exists", map1.containsKey("key1_map1")); 228 229 230 cache2.attach("/map1", map1); 231 cache2.attach("/map2", map2); 232 233 map1 = (Map ) cache1.find("/map1"); 234 map2 = (Map ) cache1.find("/map2"); 235 236 assertTrue("key1 exists", map1.containsKey("key1_map1")); 237 assertTrue("null key exists", map1.containsKey(null)); 238 assertTrue("key2 exists", map1.containsKey("key2_map1")); 239 240 Set set = map1.keySet(); 241 Iterator iter = set.iterator(); 242 while (iter.hasNext()) 243 { 244 Object o = iter.next(); 245 System.out.println("testKeySetRemoveWithSharedMap1 iter.next returned: " + o); 246 247 if (o == null || "key1_map1".equals(o)) 248 iter.remove(); 249 } 250 assertTrue("key2 exists", map1.containsKey("key2_map1")); 251 assertFalse("key1 doesn't exist", map1.containsKey("key1_map1")); 252 assertFalse("null key doesn't exist", map1.containsKey(null)); 253 254 map1 = (Map ) cache2.find("/map1"); 255 map2 = (Map ) cache2.find("/map2"); 256 257 assertTrue("key2 exists", map1.containsKey("key2_map1")); 258 assertFalse("key1 doesn't exist", map1.containsKey("key1_map1")); 259 assertFalse("null key doesn't exist", map1.containsKey(null)); 260 261 } 262 263 public void testRecursion1() throws Exception 264 { 265 Map map = new HashMap (); 266 map.put("1", "1"); 267 map.put("2", "2"); 268 cache1.attach("map", map); 269 270 map = (Map )cache1.find("map"); 271 map.put("map", map); 272 273 assertEquals("size ", 3, map.size()); 274 Map m2 = (Map )map.get("map"); 275 assertTrue("Instance of AopProxy", m2 instanceof ClassProxy); 276 } 278 279 public void XtestRecursion2() throws Exception 280 { 281 Map map = new HashMap (); 282 map.put("1", "1"); 283 map.put("2", "2"); 284 map.put("map", map); 285 286 try 287 { 288 cache1.attach("map", map); 289 } catch (PojoCacheException pex) 290 { 291 pex.printStackTrace(); 293 } 294 295 fail("Test should fail since we can't handle recursive map"); 296 } 297 298 public static Test suite() throws Exception 299 { 300 return new TestSuite(ReplicatedSyncMapTest.class); 301 } 302 303 public static void main(String [] args) throws Exception 304 { 305 junit.textui.TestRunner.run(suite()); 306 } 307 308 } 309 310 | Popular Tags |