KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestSetUniqueList


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

16 package org.apache.commons.collections.list;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 /**
29  * JUnit tests.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.6 $ $Date: 2004/06/02 22:07:53 $
33  *
34  * @author Matthew Hawthorne
35  */

36 public class TestSetUniqueList extends AbstractTestList {
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(suite());
40     }
41
42     public static Test suite() {
43         return new TestSuite(TestSetUniqueList.class);
44     }
45
46     public TestSetUniqueList(String JavaDoc testName) {
47         super(testName);
48     }
49
50     //-----------------------------------------------------------------------
51
public List JavaDoc makeEmptyList() {
52         return new SetUniqueList(new ArrayList JavaDoc(), new HashSet JavaDoc());
53     }
54
55     public void testListIteratorSet() {
56         // override to block
57
resetFull();
58         ListIterator JavaDoc it = getList().listIterator();
59         it.next();
60         try {
61             it.set(null);
62             fail();
63         } catch (UnsupportedOperationException JavaDoc ex) {}
64     }
65     
66     public Object JavaDoc[] getFullNonNullElements() {
67         // override to avoid duplicate "One"
68
return new Object JavaDoc[] {
69             new String JavaDoc(""),
70             new String JavaDoc("One"),
71             new Integer JavaDoc(2),
72             "Three",
73             new Integer JavaDoc(4),
74             new Double JavaDoc(5),
75             new Float JavaDoc(6),
76             "Seven",
77             "Eight",
78             new String JavaDoc("Nine"),
79             new Integer JavaDoc(10),
80             new Short JavaDoc((short)11),
81             new Long JavaDoc(12),
82             "Thirteen",
83             "14",
84             "15",
85             new Byte JavaDoc((byte)16)
86         };
87     }
88     
89     public void testListIteratorAdd() {
90         // override to cope with Set behaviour
91
resetEmpty();
92         List JavaDoc list1 = getList();
93         List JavaDoc list2 = getConfirmedList();
94
95         Object JavaDoc[] elements = getOtherElements(); // changed here
96
ListIterator JavaDoc iter1 = list1.listIterator();
97         ListIterator JavaDoc iter2 = list2.listIterator();
98
99         for (int i = 0; i < elements.length; i++) {
100             iter1.add(elements[i]);
101             iter2.add(elements[i]);
102             super.verify(); // changed here
103
}
104
105         resetFull();
106         iter1 = getList().listIterator();
107         iter2 = getConfirmedList().listIterator();
108         for (int i = 0; i < elements.length; i++) {
109             iter1.next();
110             iter2.next();
111             iter1.add(elements[i]);
112             iter2.add(elements[i]);
113             super.verify(); // changed here
114
}
115     }
116     
117     public void testCollectionAddAll() {
118         // override for set behaviour
119
resetEmpty();
120         Object JavaDoc[] elements = getFullElements();
121         boolean r = collection.addAll(Arrays.asList(elements));
122         confirmed.addAll(Arrays.asList(elements));
123         verify();
124         assertTrue("Empty collection should change after addAll", r);
125         for (int i = 0; i < elements.length; i++) {
126             assertTrue("Collection should contain added element",
127                        collection.contains(elements[i]));
128         }
129
130         resetFull();
131         int size = collection.size();
132         elements = getOtherElements();
133         r = collection.addAll(Arrays.asList(elements));
134         confirmed.addAll(Arrays.asList(elements));
135         verify();
136         assertTrue("Full collection should change after addAll", r);
137         for (int i = 0; i < elements.length; i++) {
138             assertTrue("Full collection should contain added element " + i,
139                        collection.contains(elements[i]));
140         }
141         assertEquals("Size should increase after addAll",
142                      size + elements.length, collection.size());
143     }
144     
145     public void testListSetByIndex() {
146         // override for set behaviour
147
resetFull();
148         int size = collection.size();
149         getList().set(0, new Long JavaDoc(1000));
150         assertEquals(size, collection.size());
151
152         getList().set(2, new Long JavaDoc(1000));
153         assertEquals(size - 1, collection.size());
154         assertEquals(new Long JavaDoc(1000), getList().get(1)); // set into 2, but shifted down to 1
155
}
156     
157     boolean extraVerify = true;
158     public void testCollectionIteratorRemove() {
159         try {
160             extraVerify = false;
161             super.testCollectionIteratorRemove();
162         } finally {
163             extraVerify = true;
164         }
165     }
166     
167     public void verify() {
168         super.verify();
169         
170         if (extraVerify) {
171             int size = collection.size();
172             getList().add(new Long JavaDoc(1000));
173             assertEquals(size + 1, collection.size());
174
175             getList().add(new Long JavaDoc(1000));
176             assertEquals(size + 1, collection.size());
177             assertEquals(new Long JavaDoc(1000), getList().get(size));
178         
179             getList().remove(size);
180         }
181     }
182     
183     //-----------------------------------------------------------------------
184
public void testFactory() {
185         Integer JavaDoc[] array = new Integer JavaDoc[] {new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(1)};
186         ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(array));
187         final SetUniqueList lset = SetUniqueList.decorate(list);
188
189         assertEquals("Duplicate element was added.", 2, lset.size());
190         assertEquals(new Integer JavaDoc(1), lset.get(0));
191         assertEquals(new Integer JavaDoc(2), lset.get(1));
192         assertEquals(new Integer JavaDoc(1), list.get(0));
193         assertEquals(new Integer JavaDoc(2), list.get(1));
194     }
195
196     public void testAdd() {
197         final SetUniqueList lset = new SetUniqueList(new ArrayList JavaDoc(), new HashSet JavaDoc());
198
199         // Duplicate element
200
final Object JavaDoc obj = new Integer JavaDoc(1);
201         lset.add(obj);
202         lset.add(obj);
203         assertEquals("Duplicate element was added.", 1, lset.size());
204
205         // Unique element
206
lset.add(new Integer JavaDoc(2));
207         assertEquals("Unique element was not added.", 2, lset.size());
208     }
209
210     public void testAddAll() {
211         final SetUniqueList lset = new SetUniqueList(new ArrayList JavaDoc(), new HashSet JavaDoc());
212
213         lset.addAll(
214             Arrays.asList(new Integer JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(1)}));
215
216         assertEquals("Duplicate element was added.", 1, lset.size());
217     }
218
219     public void testSet() {
220         final SetUniqueList lset = new SetUniqueList(new ArrayList JavaDoc(), new HashSet JavaDoc());
221
222         // Duplicate element
223
final Object JavaDoc obj1 = new Integer JavaDoc(1);
224         final Object JavaDoc obj2 = new Integer JavaDoc(2);
225         final Object JavaDoc obj3 = new Integer JavaDoc(3);
226
227         lset.add(obj1);
228         lset.add(obj2);
229         lset.set(0, obj1);
230         assertEquals(2, lset.size());
231         assertSame(obj1, lset.get(0));
232         assertSame(obj2, lset.get(1));
233
234         lset.clear();
235         lset.add(obj1);
236         lset.add(obj2);
237         lset.set(0, obj2);
238         assertEquals(1, lset.size());
239         assertSame(obj2, lset.get(0));
240
241         lset.clear();
242         lset.add(obj1);
243         lset.add(obj2);
244         lset.set(0, obj3);
245         assertEquals(2, lset.size());
246         assertSame(obj3, lset.get(0));
247         assertSame(obj2, lset.get(1));
248
249         lset.clear();
250         lset.add(obj1);
251         lset.add(obj2);
252         lset.set(1, obj1);
253         assertEquals(1, lset.size());
254         assertSame(obj1, lset.get(0));
255     }
256
257     public void testListIterator() {
258         final SetUniqueList lset = new SetUniqueList(new ArrayList JavaDoc(), new HashSet JavaDoc());
259
260         final Object JavaDoc obj1 = new Integer JavaDoc(1);
261         final Object JavaDoc obj2 = new Integer JavaDoc(2);
262         lset.add(obj1);
263         lset.add(obj2);
264
265         // Attempts to add a duplicate object
266
for (final ListIterator JavaDoc it = lset.listIterator(); it.hasNext();) {
267             it.next();
268
269             if (!it.hasNext()) {
270                 it.add(obj1);
271                 break;
272             }
273         }
274
275         assertEquals("Duplicate element was added", 2, lset.size());
276     }
277
278     public String JavaDoc getCompatibilityVersion() {
279         return "3.1";
280     }
281
282 // public void testCreate() throws Exception {
283
// resetEmpty();
284
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SetUniqueList.emptyCollection.version3.1.obj");
285
// resetFull();
286
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SetUniqueList.fullCollection.version3.1.obj");
287
// }
288

289 }
290
Popular Tags