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