View Javadoc
1   /*
2    * Copyright (C) 2008 The Guava Authors
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  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.testing.Helpers;
24  import com.google.common.collect.testing.features.CollectionFeature;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import java.lang.reflect.Method;
27  import java.util.Collection;
28  
29  /**
30   * Tests {@link java.util.Set#hashCode}.
31   *
32   * @author George van den Driessche
33   */
34  @GwtCompatible(emulated = true)
35  public class SetHashCodeTester<E> extends AbstractSetTester<E> {
36    public void testHashCode() {
37      int expectedHashCode = 0;
38      for (E element : getSampleElements()) {
39        expectedHashCode += ((element == null) ? 0 : element.hashCode());
40      }
41      assertEquals(
42          "A Set's hashCode() should be the sum of those of its elements.",
43          expectedHashCode,
44          getSet().hashCode());
45    }
46  
47    @CollectionSize.Require(absent = CollectionSize.ZERO)
48    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
49    public void testHashCode_containingNull() {
50      Collection<E> elements = getSampleElements(getNumElements() - 1);
51      int expectedHashCode = 0;
52      for (E element : elements) {
53        expectedHashCode += ((element == null) ? 0 : element.hashCode());
54      }
55  
56      elements.add(null);
57      collection = getSubjectGenerator().create(elements.toArray());
58      assertEquals(
59          "A Set's hashCode() should be the sum of those of its elements (with "
60              + "a null element counting as having a hash of zero).",
61          expectedHashCode,
62          getSet().hashCode());
63    }
64  
65    /**
66     * Returns the {@link Method} instances for the test methods in this class
67     * which call {@code hashCode()} on the set values so that set tests on
68     * unhashable objects can suppress it with
69     * {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
70     */
71    @GwtIncompatible // reflection
72    public static Method[] getHashCodeMethods() {
73      return new Method[] {
74        Helpers.getMethod(SetHashCodeTester.class, "testHashCode"),
75        Helpers.getMethod(SetHashCodeTester.class, "testHashCode_containingNull")
76      };
77    }
78  }