View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
18  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
19  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.Multimap;
23  import com.google.common.collect.testing.Helpers;
24  import com.google.common.collect.testing.features.CollectionSize;
25  import com.google.common.collect.testing.features.MapFeature;
26  import com.google.common.testing.EqualsTester;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map.Entry;
30  
31  /**
32   * Tester for {@code Multimap.equals}.
33   *
34   * @author Louis Wasserman
35   */
36  @GwtCompatible
37  public class MultimapEqualsTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
38    public void testEqualsTrue() {
39      new EqualsTester()
40          .addEqualityGroup(multimap(), getSubjectGenerator().create(getSampleElements().toArray()))
41          .testEquals();
42    }
43  
44    public void testEqualsFalse() {
45      List<Entry<K, V>> targetEntries = new ArrayList<>(getSampleElements());
46      targetEntries.add(Helpers.mapEntry(k0(), v3()));
47      new EqualsTester()
48          .addEqualityGroup(multimap())
49          .addEqualityGroup(getSubjectGenerator().create(targetEntries.toArray()))
50          .testEquals();
51    }
52  
53    @CollectionSize.Require(absent = ZERO)
54    @MapFeature.Require(ALLOWS_NULL_KEYS)
55    public void testEqualsMultimapWithNullKey() {
56      Multimap<K, V> original = multimap();
57      initMultimapWithNullKey();
58      Multimap<K, V> withNull = multimap();
59      new EqualsTester()
60          .addEqualityGroup(original)
61          .addEqualityGroup(
62              withNull, getSubjectGenerator().create((Object[]) createArrayWithNullKey()))
63          .testEquals();
64    }
65  
66    @CollectionSize.Require(absent = ZERO)
67    @MapFeature.Require(ALLOWS_NULL_VALUES)
68    public void testEqualsMultimapWithNullValue() {
69      Multimap<K, V> original = multimap();
70      initMultimapWithNullValue();
71      Multimap<K, V> withNull = multimap();
72      new EqualsTester()
73          .addEqualityGroup(original)
74          .addEqualityGroup(
75              withNull, getSubjectGenerator().create((Object[]) createArrayWithNullValue()))
76          .testEquals();
77    }
78  
79    @CollectionSize.Require(absent = ZERO)
80    public void testNotEqualsEmpty() {
81      new EqualsTester()
82          .addEqualityGroup(multimap())
83          .addEqualityGroup(getSubjectGenerator().create())
84          .testEquals();
85    }
86  
87    public void testHashCodeMatchesAsMap() {
88      assertEquals(multimap().asMap().hashCode(), multimap().hashCode());
89    }
90  }