View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.set;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.apache.commons.collections4.collection.AbstractCollectionTest;
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 #makeObject()}
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 AbstractCollectionTest} for more details.
39   *
40   * @since 3.0
41   */
42  public abstract class AbstractSetTest<E> extends AbstractCollectionTest<E> {
43  
44      /**
45       * JUnit constructor.
46       *
47       * @param name  name for test
48       */
49      public AbstractSetTest(final String name) {
50          super(name);
51      }
52  
53      //-----------------------------------------------------------------------
54      /**
55       * Provides additional verifications for sets.
56       */
57      @Override
58      public void verify() {
59          super.verify();
60  
61          assertEquals("Sets should be equal", getConfirmed(), getCollection());
62          assertEquals("Sets should have equal hashCodes",
63                       getConfirmed().hashCode(), getCollection().hashCode());
64          final Collection<E> set = makeConfirmedCollection();
65          for (final E element : getCollection()) {
66              assertTrue("Set.iterator should only return unique elements", set.add(element));
67          }
68      }
69  
70      //-----------------------------------------------------------------------
71      /**
72       * Set equals method is defined.
73       */
74      @Override
75      public boolean isEqualsCheckable() {
76          return true;
77      }
78  
79      /**
80       * Returns an empty Set for use in modification testing.
81       *
82       * @return a confirmed empty collection
83       */
84      @Override
85      public Collection<E> makeConfirmedCollection() {
86          return new HashSet<>();
87      }
88  
89      /**
90       * Returns a full Set for use in modification testing.
91       *
92       * @return a confirmed full collection
93       */
94      @Override
95      public Collection<E> makeConfirmedFullCollection() {
96          final Collection<E> set = makeConfirmedCollection();
97          set.addAll(Arrays.asList(getFullElements()));
98          return set;
99      }
100 
101     /**
102      * Makes an empty set.  The returned set should have no elements.
103      *
104      * @return an empty set
105      */
106     @Override
107     public abstract Set<E> makeObject();
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     @Override
118     public Set<E> makeFullCollection() {
119         final Set<E> set = makeObject();
120         set.addAll(Arrays.asList(getFullElements()));
121         return set;
122     }
123 
124     //-----------------------------------------------------------------------
125     /**
126      * Return the {@link AbstractCollectionTest#collection} fixture, but cast as a Set.
127      */
128     @Override
129     public Set<E> getCollection() {
130         return (Set<E>) super.getCollection();
131     }
132 
133     /**
134      * Return the {@link AbstractCollectionTest#confirmed} fixture, but cast as a Set.
135      */
136     @Override
137     public Set<E> getConfirmed() {
138         return (Set<E>) super.getConfirmed();
139     }
140 
141     //-----------------------------------------------------------------------
142     /**
143      * Tests {@link Set#equals(Object)}.
144      */
145     @SuppressWarnings("unchecked")
146     public void testSetEquals() {
147         resetEmpty();
148         assertEquals("Empty sets should be equal", getCollection(), getConfirmed());
149         verify();
150 
151         final Collection<E> set2 = makeConfirmedCollection();
152         set2.add((E) "foo");
153         assertTrue("Empty set shouldn't equal nonempty set", !getCollection().equals(set2));
154 
155         resetFull();
156         assertEquals("Full sets should be equal", getCollection(), getConfirmed());
157         verify();
158 
159         set2.clear();
160         set2.addAll(Arrays.asList(getOtherElements()));
161         assertTrue("Sets with different contents shouldn't be equal", !getCollection().equals(set2));
162     }
163 
164     /**
165      * Tests {@link Set#hashCode()}.
166      */
167     public void testSetHashCode() {
168         resetEmpty();
169         assertEquals("Empty sets have equal hashCodes",
170                 getCollection().hashCode(), getConfirmed().hashCode());
171 
172         resetFull();
173         assertEquals("Equal sets have equal hashCodes",
174                 getCollection().hashCode(), getConfirmed().hashCode());
175     }
176 
177 }