KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > TestListOrderedSet


1 /*
2  * Copyright 2003-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.set;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 /**
28  * Extension of {@link TestSet} for exercising the {@link ListOrderedSet}
29  * implementation.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.7 $ $Date: 2004/06/07 21:42:12 $
33  *
34  * @author Henning P. Schmiedehausen
35  * @author Stephen Colebourne
36  */

37 public class TestListOrderedSet extends AbstractTestSet {
38
39     public TestListOrderedSet(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestListOrderedSet.class);
45     }
46
47     public static void main(String JavaDoc args[]) {
48         String JavaDoc[] testCaseName = { TestListOrderedSet.class.getName()};
49         junit.textui.TestRunner.main(testCaseName);
50     }
51
52     public Set JavaDoc makeEmptySet() {
53         return ListOrderedSet.decorate(new HashSet JavaDoc());
54     }
55
56     protected Set JavaDoc setupSet() {
57         Set JavaDoc set = makeEmptySet();
58
59         for (int i = 0; i < 10; i++) {
60             set.add(Integer.toString(i));
61         }
62         return set;
63     }
64
65     public void testOrdering() {
66         Set JavaDoc set = setupSet();
67         Iterator JavaDoc it = set.iterator();
68
69         for (int i = 0; i < 10; i++) {
70             assertEquals("Sequence is wrong", Integer.toString(i), it.next());
71         }
72
73         for (int i = 0; i < 10; i += 2) {
74             assertTrue("Must be able to remove int", set.remove(Integer.toString(i)));
75         }
76
77         it = set.iterator();
78         for (int i = 1; i < 10; i += 2) {
79             assertEquals("Sequence is wrong after remove ", Integer.toString(i), it.next());
80         }
81
82         for (int i = 0; i < 10; i++) {
83             set.add(Integer.toString(i));
84         }
85
86         assertEquals("Size of set is wrong!", 10, set.size());
87
88         it = set.iterator();
89         for (int i = 1; i < 10; i += 2) {
90             assertEquals("Sequence is wrong", Integer.toString(i), it.next());
91         }
92         for (int i = 0; i < 10; i += 2) {
93             assertEquals("Sequence is wrong", Integer.toString(i), it.next());
94         }
95     }
96     
97     private static final Integer JavaDoc ZERO = new Integer JavaDoc(0);
98     private static final Integer JavaDoc ONE = new Integer JavaDoc(1);
99     private static final Integer JavaDoc TWO = new Integer JavaDoc(2);
100     private static final Integer JavaDoc THREE = new Integer JavaDoc(3);
101     
102     public void testListAddRemove() {
103         ListOrderedSet set = (ListOrderedSet) makeEmptySet();
104         List JavaDoc view = set.asList();
105         set.add(ZERO);
106         set.add(ONE);
107         set.add(TWO);
108         
109         assertEquals(3, set.size());
110         assertSame(ZERO, set.get(0));
111         assertSame(ONE, set.get(1));
112         assertSame(TWO, set.get(2));
113         assertEquals(3, view.size());
114         assertSame(ZERO, view.get(0));
115         assertSame(ONE, view.get(1));
116         assertSame(TWO, view.get(2));
117         
118         assertEquals(0, set.indexOf(ZERO));
119         assertEquals(1, set.indexOf(ONE));
120         assertEquals(2, set.indexOf(TWO));
121         
122         set.remove(1);
123         assertEquals(2, set.size());
124         assertSame(ZERO, set.get(0));
125         assertSame(TWO, set.get(1));
126         assertEquals(2, view.size());
127         assertSame(ZERO, view.get(0));
128         assertSame(TWO, view.get(1));
129     }
130     
131     public void testListAddIndexed() {
132         ListOrderedSet set = (ListOrderedSet) makeEmptySet();
133         List JavaDoc view = set.asList();
134         set.add(ZERO);
135         set.add(TWO);
136         
137         set.add(1, ONE);
138         assertEquals(3, set.size());
139         assertSame(ZERO, set.get(0));
140         assertSame(ONE, set.get(1));
141         assertSame(TWO, set.get(2));
142         
143         set.add(0, ONE);
144         assertEquals(3, set.size());
145         assertSame(ZERO, set.get(0));
146         assertSame(ONE, set.get(1));
147         assertSame(TWO, set.get(2));
148         
149         List JavaDoc list = new ArrayList JavaDoc();
150         list.add(ZERO);
151         list.add(TWO);
152         
153         set.addAll(0, list);
154         assertEquals(3, set.size());
155         assertSame(ZERO, set.get(0));
156         assertSame(ONE, set.get(1));
157         assertSame(TWO, set.get(2));
158         
159         list.add(0, THREE); // list = [3,0,2]
160
set.remove(TWO); // set = [0,1]
161
set.addAll(1, list);
162         assertEquals(4, set.size());
163         assertSame(ZERO, set.get(0));
164         assertSame(THREE, set.get(1));
165         assertSame(TWO, set.get(2));
166         assertSame(ONE, set.get(3));
167     }
168
169     public void testDecorator() {
170         try {
171             ListOrderedSet.decorate((List JavaDoc) null);
172             fail();
173         } catch (IllegalArgumentException JavaDoc ex) {}
174         try {
175             ListOrderedSet.decorate((Set JavaDoc) null);
176             fail();
177         } catch (IllegalArgumentException JavaDoc ex) {}
178         try {
179             ListOrderedSet.decorate(null, null);
180             fail();
181         } catch (IllegalArgumentException JavaDoc ex) {}
182         try {
183             ListOrderedSet.decorate(new HashSet JavaDoc(), null);
184             fail();
185         } catch (IllegalArgumentException JavaDoc ex) {}
186         try {
187             ListOrderedSet.decorate(null, new ArrayList JavaDoc());
188             fail();
189         } catch (IllegalArgumentException JavaDoc ex) {}
190     }
191
192     public String JavaDoc getCompatibilityVersion() {
193         return "3.1";
194     }
195
196 // public void testCreate() throws Exception {
197
// resetEmpty();
198
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.emptyCollection.version3.1.obj");
199
// resetFull();
200
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.fullCollection.version3.1.obj");
201
// }
202

203 }
204
Popular Tags