001: /*
002: * Copyright 1999-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;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: /**
025: * Tests base {@link Set} methods and contracts.<P>
026: *
027: * Since {@link Set} doesn't stipulate much new behavior that isn't already
028: * found in {@link Collection}, this class basically just adds tests for
029: * {@link Set#equals()} and {@link Set#hashCode()} along with an updated
030: * {@link #verify()} that ensures elements do not appear more than once in the
031: * set.<P>
032: *
033: * To use, subclass and override the {@link #makeEmptySet()}
034: * method. You may have to override other protected methods if your
035: * set is not modifiable, or if your set restricts what kinds of
036: * elements may be added; see {@link TestCollection} for more details.<P>
037: *
038: * @author Paul Jack
039: * @version $Id: TestSet.java,v 1.2.2.1 2004/05/22 12:14:05 scolebourne Exp $
040: */
041: public abstract class TestSet extends TestCollection {
042:
043: /**
044: * Constructor.
045: *
046: * @param name name for test
047: */
048: public TestSet(String name) {
049:
050: }
051:
052: /**
053: * Makes an empty collection by invoking {@link #makeEmptySet()}.
054: *
055: * @return an empty collection
056: */
057: protected final Collection makeCollection() {
058: return makeEmptySet();
059: }
060:
061: /**
062: * Makes a full collection by invoking {@link #makeFullSet()}.
063: *
064: * @return a full collection
065: */
066: protected final Collection makeFullCollection() {
067: return makeFullSet();
068: }
069:
070: /**
071: * Return the {@link TestCollection#collection} fixture, but cast as a
072: * Set.
073: */
074: protected Set getSet() {
075: return (Set) collection;
076: }
077:
078: /**
079: * Returns an empty {@link HashSet} for use in modification testing.
080: *
081: * @return a confirmed empty collection
082: */
083: protected Collection makeConfirmedCollection() {
084: return new HashSet();
085: }
086:
087: /**
088: * Returns a full {@link HashSet} for use in modification testing.
089: *
090: * @return a confirmed full collection
091: */
092: protected Collection makeConfirmedFullCollection() {
093: HashSet set = new HashSet();
094: set.addAll(Arrays.asList(getFullElements()));
095: return set;
096: }
097:
098: /**
099: * Return the {@link TestCollection#confirmed} fixture, but cast as a
100: * Set.
101: **/
102: protected Set getConfirmedSet() {
103: return (Set) confirmed;
104: }
105:
106: /**
107: * Makes an empty set. The returned set should have no elements.
108: *
109: * @return an empty set
110: */
111: protected abstract Set makeEmptySet();
112:
113: /**
114: * Makes a full set by first creating an empty set and then adding
115: * all the elements returned by {@link #getFullElements()}.
116: *
117: * Override if your set does not support the add operation.
118: *
119: * @return a full set
120: */
121: protected Set makeFullSet() {
122: Set set = makeEmptySet();
123: set.addAll(Arrays.asList(getFullElements()));
124: return set;
125: }
126:
127: /**
128: * Tests {@link Set#equals(Object)}.
129: */
130: public void testSetEquals() {
131: resetEmpty();
132: assertEquals("Empty sets should be equal", getSet(),
133: getConfirmedSet());
134: verify();
135:
136: HashSet set2 = new HashSet();
137: set2.add("foo");
138: assertTrue("Empty set shouldn't equal nonempty set", !getSet()
139: .equals(set2));
140:
141: resetFull();
142: assertEquals("Full sets should be equal", getSet(),
143: getConfirmedSet());
144: verify();
145:
146: set2.clear();
147: set2.addAll(Arrays.asList(getOtherElements()));
148: assertTrue("Sets with different contents shouldn't be equal",
149: !getSet().equals(set2));
150: }
151:
152: /**
153: * Tests {@link Set#hashCode()}.
154: */
155: public void testSetHashCode() {
156: resetEmpty();
157: assertEquals("Empty sets have equal hashCodes", getSet()
158: .hashCode(), getConfirmedSet().hashCode());
159:
160: resetFull();
161: assertEquals("Equal sets have equal hashCodes", getSet()
162: .hashCode(), getConfirmedSet().hashCode());
163: }
164:
165: /**
166: * Provides additional verifications for sets.
167: */
168: protected void verify() {
169: super .verify();
170: assertEquals("Sets should be equal", confirmed, collection);
171: assertEquals("Sets should have equal hashCodes", confirmed
172: .hashCode(), collection.hashCode());
173: HashSet set = new HashSet();
174: Iterator iterator = collection.iterator();
175: while (iterator.hasNext()) {
176: assertTrue(
177: "Set.iterator should only return unique elements",
178: set.add(iterator.next()));
179: }
180: }
181:
182: }
|