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 
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map.Entry;
31 
32 /**
33  * Tester for {@code Multimap.equals}.
34  *
35  * @author Louis Wasserman
36  */
37 @GwtCompatible
38 public class MultimapEqualsTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
testEqualsTrue()39   public void testEqualsTrue() {
40     new EqualsTester()
41         .addEqualityGroup(multimap(), getSubjectGenerator().create(getSampleElements().toArray()))
42         .testEquals();
43   }
44 
testEqualsFalse()45   public void testEqualsFalse() {
46     List<Entry<K, V>> targetEntries = new ArrayList<Entry<K, V>>(getSampleElements());
47     targetEntries.add(Helpers.mapEntry(sampleKeys().e0, sampleValues().e3));
48     new EqualsTester()
49         .addEqualityGroup(multimap())
50         .addEqualityGroup(getSubjectGenerator().create(targetEntries.toArray()))
51         .testEquals();
52   }
53 
54   @CollectionSize.Require(absent = ZERO)
55   @MapFeature.Require(ALLOWS_NULL_KEYS)
testEqualsMultimapWithNullKey()56   public void testEqualsMultimapWithNullKey() {
57     Multimap<K, V> original = multimap();
58     initMultimapWithNullKey();
59     Multimap<K, V> withNull = multimap();
60     new EqualsTester()
61         .addEqualityGroup(original)
62         .addEqualityGroup(withNull, getSubjectGenerator().create(createArrayWithNullKey()))
63         .testEquals();
64   }
65 
66   @CollectionSize.Require(absent = ZERO)
67   @MapFeature.Require(ALLOWS_NULL_VALUES)
testEqualsMultimapWithNullValue()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(withNull, getSubjectGenerator().create(createArrayWithNullValue()))
75         .testEquals();
76   }
77 
78   @CollectionSize.Require(absent = ZERO)
testNotEqualsEmpty()79   public void testNotEqualsEmpty() {
80     new EqualsTester()
81         .addEqualityGroup(multimap())
82         .addEqualityGroup(getSubjectGenerator().create())
83         .testEquals();
84   }
85 
testHashCodeMatchesAsMap()86   public void testHashCodeMatchesAsMap() {
87     assertEquals(multimap().asMap().hashCode(), multimap().hashCode());
88   }
89 }
90