KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > CachedListAopTest


1 package org.jboss.cache.tests.aop;
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
9 import java.util.*;
10
11 //import org.jboss.test.JBossTestCase;
12

13
14 /**
15  * List interface testing.
16  */

17
18 public class CachedListAopTest extends TestCase
19 {
20    Log log=LogFactory.getLog(CachedListAopTest.class);
21    TreeCacheAopTester tester;
22    List languages;
23
24
25    public CachedListAopTest(String JavaDoc name)
26    {
27       super(name);
28    }
29
30    protected void setUp() throws Exception JavaDoc
31    {
32       super.setUp();
33       log.info("setUp() ....");
34       String JavaDoc configFile = "META-INF/local-service.xml";
35       tester = new TreeCacheAopTester(configFile);
36
37       tester.createPerson("/person/test6", "p6", 50);
38       tester.addLanguage("/person/test6", "German");
39       tester.addLanguage("/person/test6", "English");
40       tester.addLanguage("/person/test6", "French");
41       languages = (List)tester.getLanguages("/person/test6");
42    }
43
44    protected void tearDown() throws Exception JavaDoc
45    {
46       super.tearDown();
47       tester.stop();
48       tester = null;
49    }
50
51    public void testAddAndRemoveIndex() throws Throwable JavaDoc
52    {
53       int size = tester.getLanguagesSize("/person/test6");
54       assertTrue(size == 3);
55       languages.add(1, "Taiwanese");
56       size = tester.getLanguagesSize("/person/test6");
57       assertEquals("Languages size ", 4, size);
58       String JavaDoc language = (String JavaDoc) tester.getLanguage("/person/test6", 1);
59       assertEquals("Language ", (Object JavaDoc)"Taiwanese", (Object JavaDoc)language);
60       languages.remove(1);
61       size = tester.getLanguagesSize("/person/test6");
62       assertEquals("Languages size ", 3, size);
63
64       languages.add("Mandarin");
65       size = tester.getLanguagesSize("/person/test6");
66       assertEquals("Languages size ", 4, size);
67       languages.remove("Mandarin");
68       size = tester.getLanguagesSize("/person/test6");
69       assertEquals("Languages size ", 3, size);
70    }
71
72    public void testAddAllAndClear() throws Throwable JavaDoc
73    {
74       int size = tester.getLanguagesSize("/person/test6");
75       assertTrue(size == 3);
76
77       List list = new ArrayList();
78       list.add("Taiwanese");
79       list.add("Madarin");
80
81       assertTrue("Language is Taiwanese ", list.contains("Taiwanese"));
82
83       languages.addAll(list);
84       size = tester.getLanguagesSize("/person/test6");
85       assertEquals("Languages size ", 5, size);
86
87       languages.removeAll(list);
88       size = tester.getLanguagesSize("/person/test6");
89       assertEquals("Languages size ", 3, size);
90
91       assertEquals("Index of Taiwanese ", 1, languages.indexOf("English"));
92
93       languages.clear();
94       size = tester.getLanguagesSize("/person/test6");
95       assertEquals("Languages size ", 0, size);
96
97       assertTrue("Languages empty ", languages.isEmpty());
98    }
99
100    public void testEquals() throws Throwable JavaDoc {
101       List list = (List)tester.getLanguages("/person/test6");
102       assertTrue("List should be the same ", list.equals(languages));
103       list = new ArrayList();
104       list.add("German");
105       list.add("test");
106       list.add("English");
107       assertFalse("List should not be the same ", languages.equals(list));
108       assertFalse("List should not be the same ", list.equals(languages));
109    }
110
111    public void testIterator() {
112       Iterator it = languages.iterator();
113       int counter = 0;
114       while(it.hasNext()) {
115          counter++;
116          it.next();
117          it.remove();
118       }
119
120       assertEquals("Size should be ", 3, counter);
121       assertEquals("Skills should be empty ", 0, languages.size());
122    }
123
124    public void testListIterator() {
125       ListIterator li = languages.listIterator();
126       assertFalse("LI has no previous element ", li.hasPrevious());
127       assertTrue("LI has next element ", li.hasNext());
128       li.next();
129       assertEquals("Index is ", 1, li.nextIndex());
130       assertEquals("Index is ", 0, li.previousIndex());
131       li.next();
132       li.next(); // the end
133
try {
134          li.next();
135          fail("Should throw an exception here ");
136       }
137       catch (NoSuchElementException ex) {
138          ;
139       }
140       assertEquals("Index is ", 3, li.nextIndex());
141       assertEquals("Index is ", 2, li.previousIndex());
142       li.previous();
143       assertEquals("Index is ", 2, li.nextIndex());
144       assertEquals("Index is ", 1, li.previousIndex());
145       li.previous();
146       li.previous();
147       try {
148          li.previous();
149          fail("Should throw an exception here ");
150       }
151       catch (NoSuchElementException ex) {
152          ;
153       }
154    }
155
156    public static Test suite() throws Exception JavaDoc
157    {
158       return new TestSuite(CachedListAopTest.class);
159    }
160
161    public static void main(String JavaDoc[] args) throws Exception JavaDoc
162    {
163       junit.textui.TestRunner.run(suite());
164    }
165
166 }
167
168
Popular Tags