001: /*
002: * Copyright 2003-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.HashSet;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * Extension of {@link TestSet} for exercising the {@link ListOrderedSet}
029: * implementation.
030: *
031: * @since Commons Collections 3.0
032: * @version $Revision: 332769 $ $Date: 2005-11-12 12:40:01 +0000 (Sat, 12 Nov 2005) $
033: *
034: * @author Henning P. Schmiedehausen
035: * @author Stephen Colebourne
036: */
037: public class TestListOrderedSet extends AbstractTestSet {
038:
039: public TestListOrderedSet(String testName) {
040: super (testName);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestListOrderedSet.class);
045: }
046:
047: public static void main(String args[]) {
048: String[] testCaseName = { TestListOrderedSet.class.getName() };
049: junit.textui.TestRunner.main(testCaseName);
050: }
051:
052: public Set makeEmptySet() {
053: return ListOrderedSet.decorate(new HashSet());
054: }
055:
056: protected Set setupSet() {
057: Set set = makeEmptySet();
058:
059: for (int i = 0; i < 10; i++) {
060: set.add(Integer.toString(i));
061: }
062: return set;
063: }
064:
065: public void testOrdering() {
066: Set set = setupSet();
067: Iterator it = set.iterator();
068:
069: for (int i = 0; i < 10; i++) {
070: assertEquals("Sequence is wrong", Integer.toString(i), it
071: .next());
072: }
073:
074: for (int i = 0; i < 10; i += 2) {
075: assertTrue("Must be able to remove int", set.remove(Integer
076: .toString(i)));
077: }
078:
079: it = set.iterator();
080: for (int i = 1; i < 10; i += 2) {
081: assertEquals("Sequence is wrong after remove ", Integer
082: .toString(i), it.next());
083: }
084:
085: for (int i = 0; i < 10; i++) {
086: set.add(Integer.toString(i));
087: }
088:
089: assertEquals("Size of set is wrong!", 10, set.size());
090:
091: it = set.iterator();
092: for (int i = 1; i < 10; i += 2) {
093: assertEquals("Sequence is wrong", Integer.toString(i), it
094: .next());
095: }
096: for (int i = 0; i < 10; i += 2) {
097: assertEquals("Sequence is wrong", Integer.toString(i), it
098: .next());
099: }
100: }
101:
102: private static final Integer ZERO = new Integer(0);
103: private static final Integer ONE = new Integer(1);
104: private static final Integer TWO = new Integer(2);
105: private static final Integer THREE = new Integer(3);
106:
107: public void testListAddRemove() {
108: ListOrderedSet set = (ListOrderedSet) makeEmptySet();
109: List view = set.asList();
110: set.add(ZERO);
111: set.add(ONE);
112: set.add(TWO);
113:
114: assertEquals(3, set.size());
115: assertSame(ZERO, set.get(0));
116: assertSame(ONE, set.get(1));
117: assertSame(TWO, set.get(2));
118: assertEquals(3, view.size());
119: assertSame(ZERO, view.get(0));
120: assertSame(ONE, view.get(1));
121: assertSame(TWO, view.get(2));
122:
123: assertEquals(0, set.indexOf(ZERO));
124: assertEquals(1, set.indexOf(ONE));
125: assertEquals(2, set.indexOf(TWO));
126:
127: set.remove(1);
128: assertEquals(2, set.size());
129: assertSame(ZERO, set.get(0));
130: assertSame(TWO, set.get(1));
131: assertEquals(2, view.size());
132: assertSame(ZERO, view.get(0));
133: assertSame(TWO, view.get(1));
134: }
135:
136: public void testListAddIndexed() {
137: ListOrderedSet set = (ListOrderedSet) makeEmptySet();
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 void testListAddReplacing() {
174: ListOrderedSet set = (ListOrderedSet) makeEmptySet();
175: A a = new A();
176: B b = new B();
177: set.add(a);
178: assertEquals(1, set.size());
179: set.add(b); // will match but not replace A as equal
180: assertEquals(1, set.size());
181: assertSame(a, set.getSet().iterator().next());
182: assertSame(a, set.iterator().next());
183: assertSame(a, set.get(0));
184: assertSame(a, set.asList().get(0));
185: }
186:
187: static class A {
188: public boolean equals(Object obj) {
189: return (obj instanceof A || obj instanceof B);
190: }
191:
192: public int hashCode() {
193: return 1;
194: }
195: }
196:
197: static class B {
198: public boolean equals(Object obj) {
199: return (obj instanceof A || obj instanceof B);
200: }
201:
202: public int hashCode() {
203: return 1;
204: }
205: }
206:
207: public void testDecorator() {
208: try {
209: ListOrderedSet.decorate((List) null);
210: fail();
211: } catch (IllegalArgumentException ex) {
212: }
213: try {
214: ListOrderedSet.decorate((Set) null);
215: fail();
216: } catch (IllegalArgumentException ex) {
217: }
218: try {
219: ListOrderedSet.decorate(null, null);
220: fail();
221: } catch (IllegalArgumentException ex) {
222: }
223: try {
224: ListOrderedSet.decorate(new HashSet(), null);
225: fail();
226: } catch (IllegalArgumentException ex) {
227: }
228: try {
229: ListOrderedSet.decorate(null, new ArrayList());
230: fail();
231: } catch (IllegalArgumentException ex) {
232: }
233: }
234:
235: public String getCompatibilityVersion() {
236: return "3.1";
237: }
238:
239: // public void testCreate() throws Exception {
240: // resetEmpty();
241: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.emptyCollection.version3.1.obj");
242: // resetFull();
243: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.fullCollection.version3.1.obj");
244: // }
245:
246: }
|