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 |
| |
11 |
| import java.util.Collection; |
12 |
| import java.util.HashMap; |
13 |
| import java.util.HashSet; |
14 |
| import java.util.Iterator; |
15 |
| import java.util.LinkedHashMap; |
16 |
| import java.util.LinkedHashSet; |
17 |
| import java.util.LinkedList; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class CollectionTest extends TestCase |
27 |
| { |
28 |
| Log log_ = LogFactory.getLog(CollectionTest.class); |
29 |
| PojoCache cache_; |
30 |
| |
31 |
30
| public CollectionTest(String name)
|
32 |
| { |
33 |
30
| super(name);
|
34 |
| } |
35 |
| |
36 |
30
| protected void setUp() throws Exception
|
37 |
| { |
38 |
30
| super.setUp();
|
39 |
30
| log_.info("setUp() ....");
|
40 |
30
| String configFile = "META-INF/local-service.xml";
|
41 |
30
| boolean toStart = false;
|
42 |
30
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
43 |
30
| cache_.start();
|
44 |
| } |
45 |
| |
46 |
30
| protected void tearDown() throws Exception
|
47 |
| { |
48 |
30
| super.tearDown();
|
49 |
30
| cache_.stop();
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
2
| public void testLinkedList() throws Exception
|
58 |
| { |
59 |
2
| LinkedList list = new LinkedList();
|
60 |
2
| LinkedList list1;
|
61 |
2
| list.add("English");
|
62 |
2
| try
|
63 |
| { |
64 |
2
| cache_.attach("/aop/list", list);
|
65 |
2
| list = (LinkedList) cache_.find("/aop/list");
|
66 |
2
| list.add("French");
|
67 |
2
| list1 = (LinkedList) cache_.find("/aop/list");
|
68 |
2
| assertEquals("Size of list ", 2, list1.size());
|
69 |
| } catch (Exception e) |
70 |
| { |
71 |
0
| fail("pubtObject fails");
|
72 |
0
| throw e;
|
73 |
| } |
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
2
| public void testLinkedMap() throws Exception
|
82 |
| { |
83 |
2
| LinkedHashMap map = new LinkedHashMap();
|
84 |
2
| LinkedHashMap map1;
|
85 |
2
| map.put("1", "English");
|
86 |
2
| try
|
87 |
| { |
88 |
2
| cache_.attach("/aop/map", map);
|
89 |
2
| map = (LinkedHashMap) cache_.find("/aop/map");
|
90 |
2
| map.put("2", "French");
|
91 |
2
| map1 = (LinkedHashMap) cache_.find("/aop/map");
|
92 |
2
| assertEquals("Size of map ", 2, map1.size());
|
93 |
| } catch (Exception e) |
94 |
| { |
95 |
0
| fail("pubtObject fails");
|
96 |
0
| throw e;
|
97 |
| } |
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
2
| public void testLinkedSet() throws Exception
|
106 |
| { |
107 |
2
| LinkedHashSet set = new LinkedHashSet();
|
108 |
2
| LinkedHashSet set1;
|
109 |
2
| set.add("English");
|
110 |
2
| Map map;
|
111 |
2
| try
|
112 |
| { |
113 |
2
| cache_.attach("/aop/set", set);
|
114 |
2
| set = (LinkedHashSet) cache_.find("/aop/set");
|
115 |
2
| set.add("French");
|
116 |
2
| set1 = (LinkedHashSet) cache_.find("/aop/set");
|
117 |
2
| assertEquals("Size of set ", 2, set1.size());
|
118 |
| } catch (Exception e) |
119 |
| { |
120 |
0
| fail("pubtObject fails");
|
121 |
0
| throw e;
|
122 |
| } |
123 |
| } |
124 |
| |
125 |
2
| public static Test suite() throws Exception
|
126 |
| { |
127 |
2
| return new TestSuite(CollectionTest.class);
|
128 |
| } |
129 |
| |
130 |
0
| public static void main(String[] args) throws Exception
|
131 |
| { |
132 |
0
| junit.textui.TestRunner.run(suite());
|
133 |
| } |
134 |
| |
135 |
| |
136 |
2
| public void testSize() throws Exception
|
137 |
| { |
138 |
| |
139 |
2
| LinkedList list = new LinkedList();
|
140 |
2
| list.add("java");
|
141 |
2
| try
|
142 |
| { |
143 |
2
| cache_.attach("/language/list", list);
|
144 |
2
| list = (LinkedList) cache_.find("/language/list");
|
145 |
2
| assertEquals("size of collection", 1, list.size());
|
146 |
2
| list.add("c");
|
147 |
2
| list.add("lisp");
|
148 |
2
| assertEquals("size of collection", 3, list.size());
|
149 |
2
| list.remove("lisp");
|
150 |
2
| assertEquals("size of collection", 2, list.size());
|
151 |
2
| list.remove("c");
|
152 |
2
| assertEquals("size of collection", 1, list.size());
|
153 |
2
| list.clear();
|
154 |
2
| assertEquals("size of collection", 0, list.size());
|
155 |
| } |
156 |
| catch (Exception e) |
157 |
| { |
158 |
0
| fail("testSize " + e.getMessage());
|
159 |
0
| throw e;
|
160 |
| } |
161 |
| |
162 |
| } |
163 |
| |
164 |
2
| public void testIsEmpty() throws Exception
|
165 |
| { |
166 |
2
| LinkedList list = new LinkedList();
|
167 |
| |
168 |
2
| try
|
169 |
| { |
170 |
2
| cache_.attach("/language/list", list);
|
171 |
2
| list = (LinkedList) cache_.find("/language/list");
|
172 |
2
| assertTrue("collection is empty", list.isEmpty());
|
173 |
2
| list.add("c");
|
174 |
2
| list.add("lisp");
|
175 |
2
| assertFalse("collection is not empty", list.isEmpty());
|
176 |
2
| list.remove("lisp");
|
177 |
2
| assertFalse("collection is not empty", list.isEmpty());
|
178 |
2
| list.remove("c");
|
179 |
2
| assertTrue("collection is empty", list.isEmpty());
|
180 |
| } |
181 |
| catch (Exception e) |
182 |
| { |
183 |
0
| fail("testIsEmpty " + e.getMessage());
|
184 |
0
| throw e;
|
185 |
| } |
186 |
| } |
187 |
| |
188 |
2
| public void testContains() throws Exception
|
189 |
| { |
190 |
2
| LinkedList list = new LinkedList();
|
191 |
2
| list.add("java");
|
192 |
2
| try
|
193 |
| { |
194 |
2
| cache_.attach("/language/list", list);
|
195 |
2
| list = (LinkedList) cache_.find("/language/list");
|
196 |
2
| assertFalse("collection doesn't contains", list.contains("lisp"));
|
197 |
2
| list.add("c");
|
198 |
2
| list.add("lisp");
|
199 |
2
| assertTrue("collection contains", list.contains("lisp"));
|
200 |
2
| list.remove("lisp");
|
201 |
2
| assertFalse("collection doesn't contain", list.contains("lisp"));
|
202 |
2
| list.remove("c");
|
203 |
2
| list.clear();
|
204 |
2
| assertFalse("collection doesn't contains", list.contains("c"));
|
205 |
| } |
206 |
| catch (Exception e) |
207 |
| { |
208 |
0
| fail("testContains " + e.getMessage());
|
209 |
0
| throw e;
|
210 |
| } |
211 |
| |
212 |
| } |
213 |
| |
214 |
2
| public void testIterator() throws Exception
|
215 |
| { |
216 |
2
| LinkedList list = new LinkedList();
|
217 |
2
| LinkedList list2 = new LinkedList();
|
218 |
2
| list2.add("java");
|
219 |
2
| list2.add("c");
|
220 |
2
| list2.add("lisp");
|
221 |
2
| list2.add("c++");
|
222 |
2
| list2.add("forth");
|
223 |
2
| try
|
224 |
| { |
225 |
2
| cache_.attach("/language/list", list);
|
226 |
2
| list = (LinkedList) cache_.find("/language/list");
|
227 |
2
| list.addAll(list2);
|
228 |
2
| Iterator iter = list.iterator();
|
229 |
2
| while (iter.hasNext())
|
230 |
| { |
231 |
10
| Object o = iter.next();
|
232 |
10
| assertTrue("Iteration test", list2.contains(o));
|
233 |
| } |
234 |
| } |
235 |
| catch (Exception e) |
236 |
| { |
237 |
0
| fail("testIterator " + e.getMessage());
|
238 |
0
| throw e;
|
239 |
| } |
240 |
| } |
241 |
| |
242 |
2
| public void testToArray() throws Exception
|
243 |
| { |
244 |
2
| LinkedList list = new LinkedList();
|
245 |
2
| LinkedList list2 = new LinkedList();
|
246 |
2
| list2.add("java");
|
247 |
2
| list2.add("c");
|
248 |
2
| list2.add("lisp");
|
249 |
2
| list2.add("c++");
|
250 |
2
| list2.add("forth");
|
251 |
2
| list.addAll(list2);
|
252 |
2
| try
|
253 |
| { |
254 |
2
| cache_.attach("/language/list", list);
|
255 |
2
| list = (LinkedList) cache_.find("/language/list");
|
256 |
2
| Object[] values = list.toArray();
|
257 |
| |
258 |
2
| for (int looper = 0; looper < values.length; looper ++)
|
259 |
| { |
260 |
10
| assertTrue("toArray test", list2.contains(values[looper]));
|
261 |
| } |
262 |
| |
263 |
2
| values = new Object[1];
|
264 |
2
| values = list.toArray(values);
|
265 |
2
| for (int looper = 0; looper < values.length; looper ++)
|
266 |
| { |
267 |
10
| assertTrue("toArray test", list2.contains(values[looper]));
|
268 |
| } |
269 |
| |
270 |
2
| values = new Object[10];
|
271 |
2
| values = list.toArray(values);
|
272 |
2
| for (int looper = 0; looper < values.length; looper ++)
|
273 |
| { |
274 |
20
| assertTrue("toArray test", (values[looper] == null || list2.contains(values[looper])));
|
275 |
| } |
276 |
| |
277 |
| } |
278 |
| catch (Exception e) |
279 |
| { |
280 |
0
| fail("testToArray " + e.getMessage());
|
281 |
0
| throw e;
|
282 |
| } |
283 |
| } |
284 |
| |
285 |
2
| public void testAdd() throws Exception
|
286 |
| { |
287 |
2
| LinkedList list = new LinkedList();
|
288 |
2
| try
|
289 |
| { |
290 |
2
| cache_.attach("/language/list", list);
|
291 |
2
| list = (LinkedList) cache_.find("/language/list");
|
292 |
2
| list.add("java");
|
293 |
2
| assertTrue("add test", list.contains("java"));
|
294 |
2
| assertFalse("add test", list.contains("c#"));
|
295 |
| } |
296 |
| catch (Exception e) |
297 |
| { |
298 |
0
| fail("testAdd " + e.getMessage());
|
299 |
0
| throw e;
|
300 |
| } |
301 |
| |
302 |
| } |
303 |
| |
304 |
2
| public void testRemove() throws Exception
|
305 |
| { |
306 |
2
| LinkedList list = new LinkedList();
|
307 |
2
| try
|
308 |
| { |
309 |
2
| cache_.attach("/language/list", list);
|
310 |
2
| list = (LinkedList) cache_.find("/language/list");
|
311 |
2
| list.add("java");
|
312 |
2
| assertTrue(list.remove("java"));
|
313 |
2
| assertFalse("remove test", list.contains("java"));
|
314 |
| } |
315 |
| catch (Exception e) |
316 |
| { |
317 |
0
| fail("testRemove " + e.getMessage());
|
318 |
0
| throw e;
|
319 |
| } |
320 |
| |
321 |
| } |
322 |
| |
323 |
2
| public void testAddAll() throws Exception
|
324 |
| { |
325 |
2
| LinkedList list = new LinkedList();
|
326 |
2
| LinkedList list2 = new LinkedList();
|
327 |
2
| list2.add("java");
|
328 |
2
| list2.add("c");
|
329 |
2
| list2.add("lisp");
|
330 |
2
| list2.add("c++");
|
331 |
2
| list2.add("forth");
|
332 |
| |
333 |
2
| try
|
334 |
| { |
335 |
2
| cache_.attach("/language/list", list);
|
336 |
2
| list = (LinkedList) cache_.find("/language/list");
|
337 |
2
| assertTrue(list.addAll(list2));
|
338 |
2
| Object[] values = list.toArray();
|
339 |
| |
340 |
2
| for (int looper = 0; looper < values.length; looper ++)
|
341 |
| { |
342 |
10
| assertTrue("testAddAll", list2.contains(values[looper]));
|
343 |
| } |
344 |
| } |
345 |
| catch (Exception e) |
346 |
| { |
347 |
0
| fail("testAddAll " + e.getMessage());
|
348 |
0
| throw e;
|
349 |
| } |
350 |
| |
351 |
| } |
352 |
| |
353 |
2
| public void testClear() throws Exception
|
354 |
| { |
355 |
2
| LinkedList list = new LinkedList();
|
356 |
2
| LinkedList list2 = new LinkedList();
|
357 |
2
| list2.add("java");
|
358 |
2
| list2.add("c");
|
359 |
2
| list2.add("lisp");
|
360 |
2
| list2.add("c++");
|
361 |
2
| list2.add("forth");
|
362 |
| |
363 |
2
| try
|
364 |
| { |
365 |
2
| cache_.attach("/language/list", list);
|
366 |
2
| list = (LinkedList) cache_.find("/language/list");
|
367 |
2
| assertTrue(list.addAll(list2));
|
368 |
2
| assertTrue("testClear", list.size() > 0);
|
369 |
2
| list.clear();
|
370 |
2
| assertTrue("testClear", list.size() == 0);
|
371 |
| } |
372 |
| catch (Exception e) |
373 |
| { |
374 |
0
| fail("testClear " + e.getMessage());
|
375 |
0
| throw e;
|
376 |
| } |
377 |
| |
378 |
| } |
379 |
| |
380 |
2
| public void testRetainAll() throws Exception
|
381 |
| { |
382 |
2
| LinkedList list = new LinkedList();
|
383 |
2
| LinkedList list2 = new LinkedList();
|
384 |
2
| list2.add("java");
|
385 |
2
| list2.add("c");
|
386 |
2
| list2.add("lisp");
|
387 |
2
| list2.add("c++");
|
388 |
2
| list2.add("forth");
|
389 |
| |
390 |
2
| try
|
391 |
| { |
392 |
2
| cache_.attach("/language/list", list);
|
393 |
2
| list = (LinkedList) cache_.find("/language/list");
|
394 |
2
| assertTrue(list.addAll(list2));
|
395 |
2
| list2.remove("c");
|
396 |
2
| list2.remove("lisp");
|
397 |
2
| list2.remove("c++");
|
398 |
2
| list2.remove("forth");
|
399 |
2
| assertTrue("testRetainAll", list.retainAll(list2));
|
400 |
| |
401 |
2
| assertTrue("testRetainAll, list size should be 1 but is " + list.size(), list.size() == 1);
|
402 |
2
| assertTrue("testRetainAll", list.contains("java"));
|
403 |
| } |
404 |
| catch (Exception e) |
405 |
| { |
406 |
0
| fail("testRetainAll " + e.getMessage());
|
407 |
0
| throw e;
|
408 |
| } |
409 |
| |
410 |
| |
411 |
| } |
412 |
| |
413 |
2
| public void testRemoveAll() throws Exception
|
414 |
| { |
415 |
2
| LinkedList list = new LinkedList();
|
416 |
2
| LinkedList list2 = new LinkedList();
|
417 |
2
| list2.add("java");
|
418 |
2
| list2.add("c");
|
419 |
2
| list2.add("lisp");
|
420 |
2
| list2.add("c++");
|
421 |
2
| list2.add("forth");
|
422 |
| |
423 |
2
| try
|
424 |
| { |
425 |
2
| cache_.attach("/language/list", list);
|
426 |
2
| list = (LinkedList) cache_.find("/language/list");
|
427 |
2
| assertTrue(list.addAll(list2));
|
428 |
2
| list2.remove("java");
|
429 |
2
| assertTrue("testRemoveAll", list.removeAll(list2));
|
430 |
| |
431 |
2
| assertTrue("testRemoveAll", list.size() == 1);
|
432 |
2
| assertTrue("testRemoveAll", list.contains("java"));
|
433 |
| } |
434 |
| |
435 |
| catch (Exception e) |
436 |
| { |
437 |
0
| fail("testRemoveAll " + e.getMessage());
|
438 |
0
| throw e;
|
439 |
| } |
440 |
| |
441 |
| |
442 |
| } |
443 |
| |
444 |
2
| public void testContainsAll() throws Exception
|
445 |
| { |
446 |
2
| LinkedList list = new LinkedList();
|
447 |
2
| LinkedList list2 = new LinkedList();
|
448 |
2
| list2.add("java");
|
449 |
2
| list2.add("c");
|
450 |
2
| list2.add("lisp");
|
451 |
2
| list2.add("c++");
|
452 |
2
| list2.add("forth");
|
453 |
| |
454 |
2
| try
|
455 |
| { |
456 |
2
| cache_.attach("/language/list", list);
|
457 |
2
| list = (LinkedList) cache_.find("/language/list");
|
458 |
2
| assertTrue(list.addAll(list2));
|
459 |
2
| list2.remove("java");
|
460 |
2
| assertTrue("testContainsAll", list.containsAll(list2));
|
461 |
2
| list.remove("c");
|
462 |
2
| assertFalse("testContainsAll", list.containsAll(list2));
|
463 |
| } |
464 |
| catch (Exception e) |
465 |
| { |
466 |
0
| fail("testContainsAll " + e.getMessage());
|
467 |
0
| throw e;
|
468 |
| } |
469 |
| } |
470 |
| |
471 |
| } |
472 |
| |