KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > ListContainerTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25 import junit.framework.TestCase;
26 public class ListContainerTest extends TestCase{
27     protected String JavaDoc name="test";
28     protected Store store;
29     protected ListContainer container;
30     protected LinkedList JavaDoc testList;
31     protected static final int COUNT=10;
32
33     /*
34      * Test method for 'org.apache.activemq.kaha.ListContainer.size()'
35      */

36     public void testSize() throws Exception JavaDoc{
37         container.addAll(testList);
38         assertEquals(container.size(),testList.size());
39     }
40
41     /*
42      * Test method for 'org.apache.activemq.kaha.ListContainer.addFirst(Object)'
43      */

44     public void testAddFirst() throws Exception JavaDoc{
45         container.addAll(testList);
46         assertEquals(container.size(),testList.size());
47         String JavaDoc first="first";
48         container.addFirst(first);
49         assertEquals(first,container.get(0));
50         assertEquals(container.size(),testList.size()+1);
51     }
52
53     /*
54      * Test method for 'org.apache.activemq.kaha.ListContainer.addLast(Object)'
55      */

56     public void testAddLast() throws Exception JavaDoc{
57         container.addAll(testList);
58         assertEquals(container.size(),testList.size());
59         String JavaDoc last="last";
60         container.addLast(last);
61         assertEquals(last,container.get(testList.size()));
62         assertEquals(container.size(),testList.size()+1);
63     }
64
65     /*
66      * Test method for 'org.apache.activemq.kaha.ListContainer.removeFirst()'
67      */

68     public void testRemoveFirst() throws Exception JavaDoc{
69         container.addAll(testList);
70         assertEquals(container.size(),testList.size());
71         assertEquals(testList.get(0),container.removeFirst());
72         assertEquals(container.size(),testList.size()-1);
73         for(int i=1;i<testList.size();i++){
74             assertEquals(testList.get(i),container.get(i-1));
75         }
76     }
77
78     /*
79      * Test method for 'org.apache.activemq.kaha.ListContainer.removeLast()'
80      */

81     public void testRemoveLast() throws Exception JavaDoc{
82         container.addAll(testList);
83         assertEquals(container.size(),testList.size());
84         assertEquals(testList.get(testList.size()-1),container.removeLast());
85         assertEquals(container.size(),testList.size()-1);
86         for(int i=0;i<testList.size()-1;i++){
87             assertEquals(testList.get(i),container.get(i));
88         }
89     }
90
91     /*
92      * Test method for 'java.util.List.iterator()'
93      */

94     public void testIterator() throws Exception JavaDoc{
95         container.addAll(testList);
96         for(Iterator JavaDoc i=testList.iterator(),j=container.iterator();i.hasNext();){
97             assertEquals(i.next(),j.next());
98         }
99         for(Iterator JavaDoc i=container.iterator();i.hasNext();){
100             i.next();
101             i.remove();
102         }
103         assert (container.isEmpty());
104     }
105
106     /*
107      * Test method for 'java.util.List.isEmpty()'
108      */

109     public void testIsEmpty() throws Exception JavaDoc{
110         assertTrue(container.isEmpty());
111     }
112
113     /*
114      * Test method for 'java.util.List.contains(Object)'
115      */

116     public void testContains() throws Exception JavaDoc{
117         container.addAll(testList);
118         for(Iterator JavaDoc i=testList.iterator(),j=container.iterator();i.hasNext();){
119             assertTrue(container.contains(i.next()));
120         }
121     }
122
123     /*
124      * Test method for 'java.util.List.toArray()'
125      */

126     public void testToArray() throws Exception JavaDoc{
127         container.addAll(testList);
128         Object JavaDoc[] a=testList.toArray();
129         Object JavaDoc[] b=container.toArray();
130         assertEquals(a.length,b.length);
131         for(int i=0;i<a.length;i++){
132             assertEquals(a[i],b[i]);
133         }
134     }
135
136     /*
137      * Test method for 'java.util.List.remove(Object)'
138      */

139     public void testRemoveObject() throws Exception JavaDoc{
140         container.addAll(testList);
141         assertEquals(container.size(),testList.size());
142         for(int i=0;i<testList.size();i++){
143             container.remove(testList.get(i));
144         }
145         assertTrue(container.isEmpty());
146     }
147
148     /*
149      * Test method for 'java.util.List.containsAll(Collection<?>)'
150      */

151     public void testContainsAll() throws Exception JavaDoc{
152         container.addAll(testList);
153         assertTrue(container.containsAll(testList));
154     }
155
156     /*
157      * Test method for 'java.util.List.removeAll(Collection<?>)'
158      */

159     public void testRemoveAll() throws Exception JavaDoc{
160         container.addAll(testList);
161         assertEquals(testList.size(),container.size());
162         container.removeAll(testList);
163         assertTrue(container.isEmpty());
164     }
165
166     /*
167      * Test method for 'java.util.List.retainAll(Collection<?>)'
168      */

169     public void testRetainAll() throws Exception JavaDoc{
170         container.addAll(testList);
171         assertEquals(testList.size(),container.size());
172         testList.remove(0);
173         container.retainAll(testList);
174         assertEquals(testList.size(),container.size());
175     }
176
177     /*
178      * Test method for 'java.util.List.clear()'
179      */

180     public void testClear() throws Exception JavaDoc{
181         container.addAll(testList);
182         assertEquals(testList.size(),container.size());
183         container.clear();
184         assertTrue(container.isEmpty());
185     }
186
187     /*
188      * Test method for 'java.util.List.get(int)'
189      */

190     public void testGet() throws Exception JavaDoc{
191         container.addAll(testList);
192         for(int i=0;i<testList.size();i++){
193             assertEquals(container.get(i),testList.get(i));
194         }
195     }
196
197     /*
198      * Test method for 'java.util.List.set(int, E)'
199      */

200     public void testSet() throws Exception JavaDoc{
201         container.addAll(testList);
202     }
203
204     /*
205      * Test method for 'java.util.List.add(int, E)'
206      */

207     public void testAddIntE() throws Exception JavaDoc{
208         container.addAll(testList);
209         assertTrue(container.equals(testList));
210         Object JavaDoc testObj="testObj";
211         int index=0;
212         testList.set(index,testObj);
213         container.set(index,testObj);
214         assertTrue(container.equals(testList));
215         index=testList.size()-1;
216         testList.set(index,testObj);
217         container.set(index,testObj);
218         assertTrue(container.equals(testList));
219     }
220
221     /*
222      * Test method for 'java.util.List.remove(int)'
223      */

224     public void testRemoveInt() throws Exception JavaDoc{
225         container.addAll(testList);
226         assertTrue(container.equals(testList));
227         testList.remove(0);
228         container.remove(0);
229         assertTrue(container.equals(testList));
230         int pos=testList.size()-1;
231         testList.remove(pos);
232         container.remove(pos);
233         assertTrue(container.equals(testList));
234     }
235
236     /*
237      * Test method for 'java.util.List.indexOf(Object)'
238      */

239     public void testIndexOf() throws Exception JavaDoc{
240         container.addAll(testList);
241         assertTrue(container.equals(testList));
242         for(int i=0;i<testList.size();i++){
243             Object JavaDoc o=testList.get(i);
244             assertEquals(i,container.indexOf(o));
245         }
246     }
247
248     /*
249      * Test method for 'java.util.List.listIterator()'
250      */

251     public void testListIterator() throws Exception JavaDoc{
252         container.addAll(testList);
253         ListIterator JavaDoc containerIter=container.listIterator();
254         ListIterator JavaDoc testIter=testList.listIterator();
255         assertTrue(testIter.hasNext());
256         assertTrue(containerIter.hasNext());
257         while(testIter.hasNext()){
258             Object JavaDoc o1=testIter.next();
259             Object JavaDoc o2=containerIter.next();
260             assertEquals(o1,o2);
261             testIter.remove();
262             containerIter.remove();
263         }
264         assertTrue(testList.isEmpty());
265         assertTrue(container.isEmpty());
266     }
267
268     /*
269      * Test method for 'java.util.List.listIterator(int)'
270      */

271     public void testListIteratorInt() throws Exception JavaDoc{
272         container.addAll(testList);
273         int start=testList.size()/2;
274         ListIterator JavaDoc containerIter=container.listIterator(start);
275         ListIterator JavaDoc testIter=testList.listIterator(start);
276         assertTrue(testIter.hasNext());
277         assertTrue(containerIter.hasNext());
278         while(testIter.hasNext()){
279             Object JavaDoc o1=testIter.next();
280             Object JavaDoc o2=containerIter.next();
281             assertEquals(o1,o2);
282         }
283     }
284
285     /*
286      * Test method for 'java.util.List.subList(int, int)'
287      */

288     public void testSubList() throws Exception JavaDoc{
289         container.addAll(testList);
290         int start=testList.size()/2;
291         List JavaDoc l1=testList.subList(start,testList.size());
292         List JavaDoc l2=container.subList(start,testList.size());
293         assertEquals(l1.size(),l2.size());
294         assertEquals(l1,l2);
295     }
296
297     protected Store getStore() throws IOException JavaDoc{
298         return StoreFactory.open(name,"rw");
299     }
300
301     protected void setUp() throws Exception JavaDoc{
302         super.setUp();
303         name = System.getProperty("basedir", ".")+"/target/activemq-data/list-container.db";
304         StoreFactory.delete(name);
305         store=getStore();
306         store.deleteListContainer(name);
307         container=store.getListContainer(name);
308         container.load();
309         testList=new LinkedList JavaDoc();
310         for(int i=0;i<COUNT;i++){
311             String JavaDoc value="value:"+i;
312             testList.add(value);
313         }
314     }
315
316     protected void tearDown() throws Exception JavaDoc{
317         super.tearDown();
318         if(store!=null){
319             store.close();
320         }
321         assertTrue(StoreFactory.delete(name));
322     }
323 }
324
Popular Tags