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