001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Set;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Extension of {@link TestSet} for exercising the {@link ListOrderedSet}
028: * implementation.
029: *
030: * @since Commons Collections 3.1
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Henning P. Schmiedehausen
034: * @author Stephen Colebourne
035: */
036: public class TestListOrderedSet2 extends AbstractTestSet {
037:
038: public TestListOrderedSet2(String testName) {
039: super (testName);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestListOrderedSet2.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestListOrderedSet2.class.getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: public Set makeEmptySet() {
052: return new ListOrderedSet();
053: }
054:
055: protected Set setupSet() {
056: Set set = makeEmptySet();
057:
058: for (int i = 0; i < 10; i++) {
059: set.add(Integer.toString(i));
060: }
061: return set;
062: }
063:
064: public void testOrdering() {
065: Set set = setupSet();
066: Iterator it = set.iterator();
067:
068: for (int i = 0; i < 10; i++) {
069: assertEquals("Sequence is wrong", Integer.toString(i), it
070: .next());
071: }
072:
073: for (int i = 0; i < 10; i += 2) {
074: assertTrue("Must be able to remove int", set.remove(Integer
075: .toString(i)));
076: }
077:
078: it = set.iterator();
079: for (int i = 1; i < 10; i += 2) {
080: assertEquals("Sequence is wrong after remove ", Integer
081: .toString(i), it.next());
082: }
083:
084: for (int i = 0; i < 10; i++) {
085: set.add(Integer.toString(i));
086: }
087:
088: assertEquals("Size of set is wrong!", 10, set.size());
089:
090: it = set.iterator();
091: for (int i = 1; i < 10; i += 2) {
092: assertEquals("Sequence is wrong", Integer.toString(i), it
093: .next());
094: }
095: for (int i = 0; i < 10; i += 2) {
096: assertEquals("Sequence is wrong", Integer.toString(i), it
097: .next());
098: }
099: }
100:
101: private static final Integer ZERO = new Integer(0);
102: private static final Integer ONE = new Integer(1);
103: private static final Integer TWO = new Integer(2);
104: private static final Integer THREE = new Integer(3);
105:
106: public void testListAddRemove() {
107: ListOrderedSet set = (ListOrderedSet) makeEmptySet();
108: List view = set.asList();
109: set.add(ZERO);
110: set.add(ONE);
111: set.add(TWO);
112:
113: assertEquals(3, set.size());
114: assertSame(ZERO, set.get(0));
115: assertSame(ONE, set.get(1));
116: assertSame(TWO, set.get(2));
117: assertEquals(3, view.size());
118: assertSame(ZERO, view.get(0));
119: assertSame(ONE, view.get(1));
120: assertSame(TWO, view.get(2));
121:
122: assertEquals(0, set.indexOf(ZERO));
123: assertEquals(1, set.indexOf(ONE));
124: assertEquals(2, set.indexOf(TWO));
125:
126: set.remove(1);
127: assertEquals(2, set.size());
128: assertSame(ZERO, set.get(0));
129: assertSame(TWO, set.get(1));
130: assertEquals(2, view.size());
131: assertSame(ZERO, view.get(0));
132: assertSame(TWO, view.get(1));
133: }
134:
135: public void testListAddIndexed() {
136: ListOrderedSet set = (ListOrderedSet) makeEmptySet();
137: List view = set.asList();
138: set.add(ZERO);
139: set.add(TWO);
140:
141: set.add(1, ONE);
142: assertEquals(3, set.size());
143: assertSame(ZERO, set.get(0));
144: assertSame(ONE, set.get(1));
145: assertSame(TWO, set.get(2));
146:
147: set.add(0, ONE);
148: assertEquals(3, set.size());
149: assertSame(ZERO, set.get(0));
150: assertSame(ONE, set.get(1));
151: assertSame(TWO, set.get(2));
152:
153: List list = new ArrayList();
154: list.add(ZERO);
155: list.add(TWO);
156:
157: set.addAll(0, list);
158: assertEquals(3, set.size());
159: assertSame(ZERO, set.get(0));
160: assertSame(ONE, set.get(1));
161: assertSame(TWO, set.get(2));
162:
163: list.add(0, THREE); // list = [3,0,2]
164: set.remove(TWO); // set = [0,1]
165: set.addAll(1, list);
166: assertEquals(4, set.size());
167: assertSame(ZERO, set.get(0));
168: assertSame(THREE, set.get(1));
169: assertSame(TWO, set.get(2));
170: assertSame(ONE, set.get(3));
171: }
172:
173: public String getCompatibilityVersion() {
174: return "3.1";
175: }
176:
177: // public void testCreate() throws Exception {
178: // resetEmpty();
179: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.emptyCollection.version3.1.obj");
180: // resetFull();
181: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.fullCollection.version3.1.obj");
182: // }
183:
184: }
|