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