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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.apache.commons.collections4.collection.AbstractCollectionTest;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Abstract test class for {@link Set} methods and contracts.
33   * <p>
34   * Since {@link Set} doesn't stipulate much new behavior that isn't already
35   * found in {@link Collection}, this class basically just adds tests for
36   * {@link Set#equals} and {@link Set#hashCode()} along with an updated
37   * {@link #verify()} that ensures elements do not appear more than once in the
38   * set.
39   * <p>
40   * To use, subclass and override the {@link #makeObject()}
41   * method.  You may have to override other protected methods if your
42   * set is not modifiable, or if your set restricts what kinds of
43   * elements may be added; see {@link AbstractCollectionTest} for more details.
44   */
45  public abstract class AbstractSetTest<E> extends AbstractCollectionTest<E> {
46  
47      /**
48       * JUnit constructor.
49       *
50       * @param name  name for test
51       */
52      public AbstractSetTest(final String name) {
53          super(name);
54      }
55  
56      /**
57       * Gets the {@link AbstractCollectionTest#collection} fixture, but cast as a Set.
58       */
59      @Override
60      public Set<E> getCollection() {
61          return (Set<E>) super.getCollection();
62      }
63  
64      /**
65       * Gets the {@link AbstractCollectionTest#confirmed} fixture, but cast as a Set.
66       */
67      @Override
68      public Set<E> getConfirmed() {
69          return (Set<E>) super.getConfirmed();
70      }
71  
72      /**
73       * Sets equals method is defined.
74       */
75      @Override
76      public boolean isEqualsCheckable() {
77          return true;
78      }
79  
80      /**
81       * Returns an empty Set for use in modification testing.
82       *
83       * @return a confirmed empty collection
84       */
85      @Override
86      public Collection<E> makeConfirmedCollection() {
87          return new HashSet<>();
88      }
89  
90      /**
91       * Returns a full Set for use in modification testing.
92       *
93       * @return a confirmed full collection
94       */
95      @Override
96      public Collection<E> makeConfirmedFullCollection() {
97          final Collection<E> set = makeConfirmedCollection();
98          set.addAll(Arrays.asList(getFullElements()));
99          return set;
100     }
101 
102     /**
103      * Makes a full set by first creating an empty set and then adding
104      * all the elements returned by {@link #getFullElements()}.
105      *
106      * Override if your set does not support the add operation.
107      *
108      * @return a full set
109      */
110     @Override
111     public Set<E> makeFullCollection() {
112         final Set<E> set = makeObject();
113         set.addAll(Arrays.asList(getFullElements()));
114         return set;
115     }
116 
117     /**
118      * Makes an empty set.  The returned set should have no elements.
119      *
120      * @return an empty set
121      */
122     @Override
123     public abstract Set<E> makeObject();
124 
125     /**
126      * Tests {@link Set#equals(Object)}.
127      */
128     @Test
129     @SuppressWarnings("unchecked")
130     public void testSetEquals() {
131         resetEmpty();
132         assertEquals(getCollection(), getConfirmed(), "Empty sets should be equal");
133         verify();
134 
135         final Collection<E> set2 = makeConfirmedCollection();
136         set2.add((E) "foo");
137         assertFalse(getCollection().equals(set2), "Empty set shouldn't equal nonempty set");
138 
139         resetFull();
140         assertEquals(getCollection(), getConfirmed(), "Full sets should be equal");
141         verify();
142 
143         set2.clear();
144         set2.addAll(Arrays.asList(getOtherElements()));
145         assertFalse(getCollection().equals(set2), "Sets with different contents shouldn't be equal");
146     }
147 
148     /**
149      * Tests {@link Set#hashCode()}.
150      */
151     @Test
152     public void testSetHashCode() {
153         resetEmpty();
154         assertEquals(getCollection().hashCode(), getConfirmed().hashCode(),
155                 "Empty sets have equal hashCodes");
156 
157         resetFull();
158         assertEquals(getCollection().hashCode(), getConfirmed().hashCode(),
159                 "Equal sets have equal hashCodes");
160     }
161 
162     /**
163      * Provides additional verifications for sets.
164      */
165     @Override
166     public void verify() {
167         super.verify();
168 
169         assertEquals(getConfirmed(), getCollection(), "Sets should be equal");
170         assertEquals(getConfirmed().hashCode(), getCollection().hashCode(),
171                      "Sets should have equal hashCodes");
172         final Collection<E> set = makeConfirmedCollection();
173         for (final E element : getCollection()) {
174             assertTrue(set.add(element), "Set.iterator should only return unique elements");
175         }
176     }
177 
178 }