KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > collection > CollectionTest


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 JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedHashMap JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * Generic Collection class support testing.
22  *
23  * @author Ben Wang
24  */

25
26 public class CollectionTest extends TestCase
27 {
28    Log log_ = LogFactory.getLog(CollectionTest.class);
29    PojoCache cache_;
30
31    public CollectionTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    protected void setUp() throws Exception JavaDoc
37    {
38       super.setUp();
39       log_.info("setUp() ....");
40       String JavaDoc configFile = "META-INF/local-service.xml";
41       boolean toStart = false;
42       cache_ = PojoCacheFactory.createCache(configFile, toStart);
43       cache_.start();
44    }
45
46    protected void tearDown() throws Exception JavaDoc
47    {
48       super.tearDown();
49       cache_.stop();
50    }
51
52    /**
53     * Testing using LinkedList proxy.
54     *
55     * @throws Exception
56     */

57    public void testLinkedList() throws Exception JavaDoc
58    {
59       LinkedList JavaDoc list = new LinkedList JavaDoc();
60       LinkedList JavaDoc list1;
61       list.add("English");
62       try
63       {
64          cache_.attach("/aop/list", list);
65          list = (LinkedList JavaDoc) cache_.find("/aop/list");
66          list.add("French");
67          list1 = (LinkedList JavaDoc) cache_.find("/aop/list");
68          assertEquals("Size of list ", 2, list1.size());
69       } catch (Exception JavaDoc e)
70       {
71          fail("pubtObject fails");
72          throw e;
73       }
74    }
75
76    /**
77     * Testing using LinkedMap proxy.
78     *
79     * @throws Exception
80     */

81    public void testLinkedMap() throws Exception JavaDoc
82    {
83       LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc();
84       LinkedHashMap JavaDoc map1;
85       map.put("1", "English");
86       try
87       {
88          cache_.attach("/aop/map", map);
89          map = (LinkedHashMap JavaDoc) cache_.find("/aop/map");
90          map.put("2", "French");
91          map1 = (LinkedHashMap JavaDoc) cache_.find("/aop/map");
92          assertEquals("Size of map ", 2, map1.size());
93       } catch (Exception JavaDoc e)
94       {
95          fail("pubtObject fails");
96          throw e;
97       }
98    }
99
100    /**
101     * Testing using LinkedSet proxy.
102     *
103     * @throws Exception
104     */

105    public void testLinkedSet() throws Exception JavaDoc
106    {
107       LinkedHashSet JavaDoc set = new LinkedHashSet JavaDoc();
108       LinkedHashSet JavaDoc set1;
109       set.add("English");
110       Map JavaDoc map;
111       try
112       {
113          cache_.attach("/aop/set", set);
114          set = (LinkedHashSet JavaDoc) cache_.find("/aop/set");
115          set.add("French");
116          set1 = (LinkedHashSet JavaDoc) cache_.find("/aop/set");
117          assertEquals("Size of set ", 2, set1.size());
118       } catch (Exception JavaDoc e)
119       {
120          fail("pubtObject fails");
121          throw e;
122       }
123    }
124
125    public static Test suite() throws Exception JavaDoc
126    {
127       return new TestSuite(CollectionTest.class);
128    }
129
130    public static void main(String JavaDoc[] args) throws Exception JavaDoc
131    {
132       junit.textui.TestRunner.run(suite());
133    }
134
135    // tests for each of the methods in Collection interface
136
public void testSize() throws Exception JavaDoc
137    {
138
139       LinkedList JavaDoc list = new LinkedList JavaDoc();
140       list.add("java");
141       try
142       {
143          cache_.attach("/language/list", list);
144          list = (LinkedList JavaDoc) cache_.find("/language/list");
145          assertEquals("size of collection", 1, list.size());
146          list.add("c");
147          list.add("lisp");
148          assertEquals("size of collection", 3, list.size());
149          list.remove("lisp");
150          assertEquals("size of collection", 2, list.size());
151          list.remove("c");
152          assertEquals("size of collection", 1, list.size());
153          list.clear();
154          assertEquals("size of collection", 0, list.size());
155       }
156       catch (Exception JavaDoc e)
157       {
158          fail("testSize " + e.getMessage());
159          throw e;
160       }
161
162    }
163
164    public void testIsEmpty() throws Exception JavaDoc
165    {
166       LinkedList JavaDoc list = new LinkedList JavaDoc();
167
168       try
169       {
170          cache_.attach("/language/list", list);
171          list = (LinkedList JavaDoc) cache_.find("/language/list");
172          assertTrue("collection is empty", list.isEmpty());
173          list.add("c");
174          list.add("lisp");
175          assertFalse("collection is not empty", list.isEmpty());
176          list.remove("lisp");
177          assertFalse("collection is not empty", list.isEmpty());
178          list.remove("c");
179          assertTrue("collection is empty", list.isEmpty());
180       }
181       catch (Exception JavaDoc e)
182       {
183          fail("testIsEmpty " + e.getMessage());
184          throw e;
185       }
186    }
187
188    public void testContains() throws Exception JavaDoc
189    {
190       LinkedList JavaDoc list = new LinkedList JavaDoc();
191       list.add("java");
192       try
193       {
194          cache_.attach("/language/list", list);
195          list = (LinkedList JavaDoc) cache_.find("/language/list");
196          assertFalse("collection doesn't contains", list.contains("lisp"));
197          list.add("c");
198          list.add("lisp");
199          assertTrue("collection contains", list.contains("lisp"));
200          list.remove("lisp");
201          assertFalse("collection doesn't contain", list.contains("lisp"));
202          list.remove("c");
203          list.clear();
204          assertFalse("collection doesn't contains", list.contains("c"));
205       }
206       catch (Exception JavaDoc e)
207       {
208          fail("testContains " + e.getMessage());
209          throw e;
210       }
211
212    }
213
214    public void testIterator() throws Exception JavaDoc
215    {
216       LinkedList JavaDoc list = new LinkedList JavaDoc();
217       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
218       list2.add("java");
219       list2.add("c");
220       list2.add("lisp");
221       list2.add("c++");
222       list2.add("forth");
223       try
224       {
225          cache_.attach("/language/list", list);
226          list = (LinkedList JavaDoc) cache_.find("/language/list");
227          list.addAll(list2);
228          Iterator JavaDoc iter = list.iterator();
229          while (iter.hasNext())
230          {
231             Object JavaDoc o = iter.next();
232             assertTrue("Iteration test", list2.contains(o));
233          }
234       }
235       catch (Exception JavaDoc e)
236       {
237          fail("testIterator " + e.getMessage());
238          throw e;
239       }
240    }
241
242    public void testToArray() throws Exception JavaDoc
243    {
244       LinkedList JavaDoc list = new LinkedList JavaDoc();
245       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
246       list2.add("java");
247       list2.add("c");
248       list2.add("lisp");
249       list2.add("c++");
250       list2.add("forth");
251       list.addAll(list2);
252       try
253       {
254          cache_.attach("/language/list", list);
255          list = (LinkedList JavaDoc) cache_.find("/language/list");
256          Object JavaDoc[] values = list.toArray();
257
258          for (int looper = 0; looper < values.length; looper ++)
259          {
260             assertTrue("toArray test", list2.contains(values[looper]));
261          }
262
263          values = new Object JavaDoc[1];
264          values = list.toArray(values); // test with too small of an array
265
for (int looper = 0; looper < values.length; looper ++)
266          {
267             assertTrue("toArray test", list2.contains(values[looper]));
268          }
269
270          values = new Object JavaDoc[10];
271          values = list.toArray(values); // test with too large of an array, result should be null padded
272
for (int looper = 0; looper < values.length; looper ++)
273          {
274             assertTrue("toArray test", (values[looper] == null || list2.contains(values[looper])));
275          }
276
277       }
278       catch (Exception JavaDoc e)
279       {
280          fail("testToArray " + e.getMessage());
281          throw e;
282       }
283    }
284
285    public void testAdd() throws Exception JavaDoc
286    {
287       LinkedList JavaDoc list = new LinkedList JavaDoc();
288       try
289       {
290          cache_.attach("/language/list", list);
291          list = (LinkedList JavaDoc) cache_.find("/language/list");
292          list.add("java");
293          assertTrue("add test", list.contains("java"));
294          assertFalse("add test", list.contains("c#"));
295       }
296       catch (Exception JavaDoc e)
297       {
298          fail("testAdd " + e.getMessage());
299          throw e;
300       }
301
302    }
303
304    public void testRemove() throws Exception JavaDoc
305    {
306       LinkedList JavaDoc list = new LinkedList JavaDoc();
307       try
308       {
309          cache_.attach("/language/list", list);
310          list = (LinkedList JavaDoc) cache_.find("/language/list");
311          list.add("java");
312          assertTrue(list.remove("java"));
313          assertFalse("remove test", list.contains("java"));
314       }
315       catch (Exception JavaDoc e)
316       {
317          fail("testRemove " + e.getMessage());
318          throw e;
319       }
320
321    }
322
323    public void testAddAll() throws Exception JavaDoc
324    {
325       LinkedList JavaDoc list = new LinkedList JavaDoc();
326       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
327       list2.add("java");
328       list2.add("c");
329       list2.add("lisp");
330       list2.add("c++");
331       list2.add("forth");
332
333       try
334       {
335          cache_.attach("/language/list", list);
336          list = (LinkedList JavaDoc) cache_.find("/language/list");
337          assertTrue(list.addAll(list2));
338          Object JavaDoc[] values = list.toArray();
339
340          for (int looper = 0; looper < values.length; looper ++)
341          {
342             assertTrue("testAddAll", list2.contains(values[looper]));
343          }
344       }
345       catch (Exception JavaDoc e)
346       {
347          fail("testAddAll " + e.getMessage());
348          throw e;
349       }
350
351    }
352
353    public void testClear() throws Exception JavaDoc
354    {
355       LinkedList JavaDoc list = new LinkedList JavaDoc();
356       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
357       list2.add("java");
358       list2.add("c");
359       list2.add("lisp");
360       list2.add("c++");
361       list2.add("forth");
362
363       try
364       {
365          cache_.attach("/language/list", list);
366          list = (LinkedList JavaDoc) cache_.find("/language/list");
367          assertTrue(list.addAll(list2));
368          assertTrue("testClear", list.size() > 0);
369          list.clear();
370          assertTrue("testClear", list.size() == 0);
371       }
372       catch (Exception JavaDoc e)
373       {
374          fail("testClear " + e.getMessage());
375          throw e;
376       }
377
378    }
379
380    public void testRetainAll() throws Exception JavaDoc
381    {
382       LinkedList JavaDoc list = new LinkedList JavaDoc();
383       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
384       list2.add("java");
385       list2.add("c");
386       list2.add("lisp");
387       list2.add("c++");
388       list2.add("forth");
389
390       try
391       {
392          cache_.attach("/language/list", list);
393          list = (LinkedList JavaDoc) cache_.find("/language/list");
394          assertTrue(list.addAll(list2));
395          list2.remove("c");
396          list2.remove("lisp");
397          list2.remove("c++");
398          list2.remove("forth");
399          assertTrue("testRetainAll", list.retainAll(list2));
400          // should only have java left
401
assertTrue("testRetainAll, list size should be 1 but is " + list.size(), list.size() == 1);
402          assertTrue("testRetainAll", list.contains("java"));
403       }
404       catch (Exception JavaDoc e)
405       {
406          fail("testRetainAll " + e.getMessage());
407          throw e;
408       }
409
410
411    }
412
413    public void testRemoveAll() throws Exception JavaDoc
414    {
415       LinkedList JavaDoc list = new LinkedList JavaDoc();
416       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
417       list2.add("java");
418       list2.add("c");
419       list2.add("lisp");
420       list2.add("c++");
421       list2.add("forth");
422
423       try
424       {
425          cache_.attach("/language/list", list);
426          list = (LinkedList JavaDoc) cache_.find("/language/list");
427          assertTrue(list.addAll(list2));
428          list2.remove("java");
429          assertTrue("testRemoveAll", list.removeAll(list2));
430          // should only have java left
431
assertTrue("testRemoveAll", list.size() == 1);
432          assertTrue("testRemoveAll", list.contains("java"));
433       }
434
435       catch (Exception JavaDoc e)
436       {
437          fail("testRemoveAll " + e.getMessage());
438          throw e;
439       }
440
441
442    }
443
444    public void testContainsAll() throws Exception JavaDoc
445    {
446       LinkedList JavaDoc list = new LinkedList JavaDoc();
447       LinkedList JavaDoc list2 = new LinkedList JavaDoc();
448       list2.add("java");
449       list2.add("c");
450       list2.add("lisp");
451       list2.add("c++");
452       list2.add("forth");
453
454       try
455       {
456          cache_.attach("/language/list", list);
457          list = (LinkedList JavaDoc) cache_.find("/language/list");
458          assertTrue(list.addAll(list2));
459          list2.remove("java");
460          assertTrue("testContainsAll", list.containsAll(list2));
461          list.remove("c");
462          assertFalse("testContainsAll", list.containsAll(list2));
463       }
464       catch (Exception JavaDoc e)
465       {
466          fail("testContainsAll " + e.getMessage());
467          throw e;
468       }
469    }
470    
471 }
472
473
Popular Tags