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 * Tests {@link Set}.
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 * Gets the {@link AbstractCollectionTest#collection} fixture, but cast as a Set.
49 */
50 @Override
51 public Set<E> getCollection() {
52 return (Set<E>) super.getCollection();
53 }
54
55 /**
56 * Gets the {@link AbstractCollectionTest#confirmed} fixture, but cast as a Set.
57 */
58 @Override
59 public Set<E> getConfirmed() {
60 return (Set<E>) super.getConfirmed();
61 }
62
63 /**
64 * Sets equals method is defined.
65 */
66 @Override
67 public boolean isEqualsCheckable() {
68 return true;
69 }
70
71 /**
72 * Returns an empty Set for use in modification testing.
73 *
74 * @return a confirmed empty collection
75 */
76 @Override
77 public Collection<E> makeConfirmedCollection() {
78 return new HashSet<>();
79 }
80
81 /**
82 * Returns a full Set for use in modification testing.
83 *
84 * @return a confirmed full collection
85 */
86 @Override
87 public Collection<E> makeConfirmedFullCollection() {
88 final Collection<E> set = makeConfirmedCollection();
89 set.addAll(Arrays.asList(getFullElements()));
90 return set;
91 }
92
93 /**
94 * Makes a full set by first creating an empty set and then adding
95 * all the elements returned by {@link #getFullElements()}.
96 *
97 * Override if your set does not support the add operation.
98 *
99 * @return a full set
100 */
101 @Override
102 public Set<E> makeFullCollection() {
103 final Set<E> set = makeObject();
104 set.addAll(Arrays.asList(getFullElements()));
105 return set;
106 }
107
108 /**
109 * Makes an empty set. The returned set should have no elements.
110 *
111 * @return an empty set
112 */
113 @Override
114 public abstract Set<E> makeObject();
115
116 /**
117 * Tests {@link Set#equals(Object)}.
118 */
119 @Test
120 @SuppressWarnings("unchecked")
121 public void testSetEquals() {
122 resetEmpty();
123 assertEquals(getCollection(), getConfirmed(), "Empty sets should be equal");
124 verify();
125
126 final Collection<E> set2 = makeConfirmedCollection();
127 set2.add((E) "foo");
128 assertFalse(getCollection().equals(set2), "Empty set shouldn't equal nonempty set");
129
130 resetFull();
131 assertEquals(getCollection(), getConfirmed(), "Full sets should be equal");
132 verify();
133
134 set2.clear();
135 set2.addAll(Arrays.asList(getOtherElements()));
136 assertFalse(getCollection().equals(set2), "Sets with different contents shouldn't be equal");
137 }
138
139 /**
140 * Tests {@link Set#hashCode()}.
141 */
142 @Test
143 public void testSetHashCode() {
144 resetEmpty();
145 assertEquals(getCollection().hashCode(), getConfirmed().hashCode(),
146 "Empty sets have equal hashCodes");
147
148 resetFull();
149 assertEquals(getCollection().hashCode(), getConfirmed().hashCode(),
150 "Equal sets have equal hashCodes");
151 }
152
153 /**
154 * Provides additional verifications for sets.
155 */
156 @Override
157 public void verify() {
158 super.verify();
159
160 assertEquals(getConfirmed(), getCollection(), "Sets should be equal");
161 assertEquals(getConfirmed().hashCode(), getCollection().hashCode(),
162 "Sets should have equal hashCodes");
163 final Collection<E> set = makeConfirmedCollection();
164 for (final E element : getCollection()) {
165 assertTrue(set.add(element), "Set.iterator should only return unique elements");
166 }
167 }
168
169 }