KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.collections.collection.AbstractTestCollection;
25
26 /**
27  * Abstract test class for {@link Set} methods and contracts.
28  * <p>
29  * Since {@link Set} doesn't stipulate much new behavior that isn't already
30  * found in {@link Collection}, this class basically just adds tests for
31  * {@link Set#equals} and {@link Set#hashCode()} along with an updated
32  * {@link #verify()} that ensures elements do not appear more than once in the
33  * set.
34  * <p>
35  * To use, subclass and override the {@link #makeEmptySet()}
36  * method. You may have to override other protected methods if your
37  * set is not modifiable, or if your set restricts what kinds of
38  * elements may be added; see {@link AbstractTestCollection} for more details.
39  *
40  * @since Commons Collections 3.0
41  * @version $Revision: 1.5 $ $Date: 2004/05/31 19:09:14 $
42  *
43  * @author Paul Jack
44  */

45 public abstract class AbstractTestSet extends AbstractTestCollection {
46
47     /**
48      * JUnit constructor.
49      *
50      * @param name name for test
51      */

52     public AbstractTestSet(String JavaDoc name) {
53         super(name);
54     }
55
56     //-----------------------------------------------------------------------
57
/**
58      * Provides additional verifications for sets.
59      */

60     public void verify() {
61         super.verify();
62         
63         assertEquals("Sets should be equal", confirmed, collection);
64         assertEquals("Sets should have equal hashCodes",
65                      confirmed.hashCode(), collection.hashCode());
66         Collection JavaDoc set = makeConfirmedCollection();
67         Iterator JavaDoc iterator = collection.iterator();
68         while (iterator.hasNext()) {
69             assertTrue("Set.iterator should only return unique elements",
70                        set.add(iterator.next()));
71         }
72     }
73
74     //-----------------------------------------------------------------------
75
/**
76      * Set equals method is defined.
77      */

78     public boolean isEqualsCheckable() {
79         return true;
80     }
81
82     /**
83      * Returns an empty Set for use in modification testing.
84      *
85      * @return a confirmed empty collection
86      */

87     public Collection JavaDoc makeConfirmedCollection() {
88         return new HashSet JavaDoc();
89     }
90
91     /**
92      * Returns a full Set for use in modification testing.
93      *
94      * @return a confirmed full collection
95      */

96     public Collection JavaDoc makeConfirmedFullCollection() {
97         Collection JavaDoc set = makeConfirmedCollection();
98         set.addAll(Arrays.asList(getFullElements()));
99         return set;
100     }
101
102     /**
103      * Makes an empty set. The returned set should have no elements.
104      *
105      * @return an empty set
106      */

107     public abstract Set JavaDoc makeEmptySet();
108
109     /**
110      * Makes a full set by first creating an empty set and then adding
111      * all the elements returned by {@link #getFullElements()}.
112      *
113      * Override if your set does not support the add operation.
114      *
115      * @return a full set
116      */

117     public Set JavaDoc makeFullSet() {
118         Set JavaDoc set = makeEmptySet();
119         set.addAll(Arrays.asList(getFullElements()));
120         return set;
121     }
122
123     /**
124      * Makes an empty collection by invoking {@link #makeEmptySet()}.
125      *
126      * @return an empty collection
127      */

128     public final Collection JavaDoc makeCollection() {
129         return makeEmptySet();
130     }
131
132     /**
133      * Makes a full collection by invoking {@link #makeFullSet()}.
134      *
135      * @return a full collection
136      */

137     public final Collection JavaDoc makeFullCollection() {
138         return makeFullSet();
139     }
140
141     //-----------------------------------------------------------------------
142
/**
143      * Return the {@link AbstractTestCollection#collection} fixture, but cast as a Set.
144      */

145     public Set JavaDoc getSet() {
146         return (Set JavaDoc)collection;
147     }
148
149     /**
150      * Return the {@link AbstractTestCollection#confirmed} fixture, but cast as a Set.
151      */

152     public Set JavaDoc getConfirmedSet() {
153         return (Set JavaDoc)confirmed;
154     }
155
156     //-----------------------------------------------------------------------
157
/**
158      * Tests {@link Set#equals(Object)}.
159      */

160     public void testSetEquals() {
161         resetEmpty();
162         assertEquals("Empty sets should be equal",
163                      getSet(), getConfirmedSet());
164         verify();
165
166         Collection JavaDoc set2 = makeConfirmedCollection();
167         set2.add("foo");
168         assertTrue("Empty set shouldn't equal nonempty set",
169                    !getSet().equals(set2));
170
171         resetFull();
172         assertEquals("Full sets should be equal", getSet(), getConfirmedSet());
173         verify();
174
175         set2.clear();
176         set2.addAll(Arrays.asList(getOtherElements()));
177         assertTrue("Sets with different contents shouldn't be equal",
178                    !getSet().equals(set2));
179     }
180
181     /**
182      * Tests {@link Set#hashCode()}.
183      */

184     public void testSetHashCode() {
185         resetEmpty();
186         assertEquals("Empty sets have equal hashCodes",
187                      getSet().hashCode(), getConfirmedSet().hashCode());
188
189         resetFull();
190         assertEquals("Equal sets have equal hashCodes",
191                      getSet().hashCode(), getConfirmedSet().hashCode());
192     }
193
194 }
195
Popular Tags