001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.collect.testing.google;
016
017import static com.google.common.collect.testing.Helpers.mapEntry;
018import static com.google.common.collect.testing.features.CollectionSize.ZERO;
019import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.Multimap;
024import com.google.common.collect.testing.features.CollectionSize;
025import com.google.common.collect.testing.features.MapFeature;
026import com.google.common.testing.EqualsTester;
027import java.util.ArrayList;
028import java.util.List;
029import java.util.Map.Entry;
030import org.jspecify.annotations.NullMarked;
031import org.jspecify.annotations.Nullable;
032import org.junit.Ignore;
033
034/**
035 * Tester for {@code Multimap.equals}.
036 *
037 * @author Louis Wasserman
038 */
039@GwtCompatible
040@Ignore("test runners must not instantiate and run this directly, only via suites we build")
041// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042@SuppressWarnings("JUnit4ClassUsedInJUnit3")
043@NullMarked
044public class MultimapEqualsTester<K extends @Nullable Object, V extends @Nullable Object>
045    extends AbstractMultimapTester<K, V, Multimap<K, V>> {
046  public void testEqualsTrue() {
047    new EqualsTester()
048        .addEqualityGroup(multimap(), getSubjectGenerator().create(getSampleElements().toArray()))
049        .testEquals();
050  }
051
052  public void testEqualsFalse() {
053    List<Entry<K, V>> targetEntries = new ArrayList<>(getSampleElements());
054    targetEntries.add(mapEntry(k0(), v3()));
055    new EqualsTester()
056        .addEqualityGroup(multimap())
057        .addEqualityGroup(getSubjectGenerator().create(targetEntries.toArray()))
058        .testEquals();
059  }
060
061  @CollectionSize.Require(absent = ZERO)
062  @MapFeature.Require(ALLOWS_NULL_KEYS)
063  public void testEqualsMultimapWithNullKey() {
064    Multimap<K, V> original = multimap();
065    initMultimapWithNullKey();
066    Multimap<K, V> withNull = multimap();
067    new EqualsTester()
068        .addEqualityGroup(original)
069        .addEqualityGroup(
070            withNull, getSubjectGenerator().create((Object[]) createArrayWithNullKey()))
071        .testEquals();
072  }
073
074  @CollectionSize.Require(absent = ZERO)
075  @MapFeature.Require(ALLOWS_NULL_VALUES)
076  public void testEqualsMultimapWithNullValue() {
077    Multimap<K, V> original = multimap();
078    initMultimapWithNullValue();
079    Multimap<K, V> withNull = multimap();
080    new EqualsTester()
081        .addEqualityGroup(original)
082        .addEqualityGroup(
083            withNull, getSubjectGenerator().create((Object[]) createArrayWithNullValue()))
084        .testEquals();
085  }
086
087  @CollectionSize.Require(absent = ZERO)
088  public void testNotEqualsEmpty() {
089    new EqualsTester()
090        .addEqualityGroup(multimap())
091        .addEqualityGroup(getSubjectGenerator().create())
092        .testEquals();
093  }
094
095  public void testHashCodeMatchesAsMap() {
096    assertEquals(multimap().asMap().hashCode(), multimap().hashCode());
097  }
098}