KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

179 }
180
Popular Tags