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