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.Sets.newHashSet;
018import static com.google.common.collect.testing.Helpers.mapEntry;
019import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
020import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
021import static java.util.Collections.singleton;
022import static java.util.Collections.singletonMap;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.SetMultimap;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.common.collect.testing.features.MapFeature;
028import com.google.common.testing.EqualsTester;
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.HashSet;
033import java.util.List;
034import java.util.Map;
035import java.util.Map.Entry;
036import java.util.Set;
037import org.jspecify.annotations.NullMarked;
038import org.jspecify.annotations.Nullable;
039
040/**
041 * Testers for {@link SetMultimap#asMap}.
042 *
043 * @author Louis Wasserman
044 * @param <K> The key type of the tested multimap.
045 * @param <V> The value type of the tested multimap.
046 */
047@GwtCompatible
048@NullMarked
049public class SetMultimapAsMapTester<K extends @Nullable Object, V extends @Nullable Object>
050    extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
051  public void testAsMapValuesImplementSet() {
052    for (Collection<V> valueCollection : multimap().asMap().values()) {
053      assertTrue(valueCollection instanceof Set);
054    }
055  }
056
057  public void testAsMapGetImplementsSet() {
058    for (K key : multimap().keySet()) {
059      assertTrue(multimap().asMap().get(key) instanceof Set);
060    }
061  }
062
063  @MapFeature.Require(SUPPORTS_REMOVE)
064  public void testAsMapRemoveImplementsSet() {
065    List<K> keys = new ArrayList<>(multimap().keySet());
066    for (K key : keys) {
067      resetCollection();
068      assertTrue(multimap().asMap().remove(key) instanceof Set);
069    }
070  }
071
072  @CollectionSize.Require(SEVERAL)
073  public void testEquals() {
074    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
075    Map<K, Collection<V>> expected = new HashMap<>();
076    expected.put(k0(), newHashSet(v0(), v3()));
077    expected.put(k1(), newHashSet(v0()));
078    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
079  }
080
081  @CollectionSize.Require(SEVERAL)
082  public void testEntrySetEquals() {
083    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
084    Set<Entry<K, Collection<V>>> expected = new HashSet<>();
085    expected.add(mapEntry(k0(), newHashSet(v0(), v3())));
086    expected.add(mapEntry(k1(), newHashSet(v0())));
087    new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
088  }
089
090  @CollectionSize.Require(SEVERAL)
091  @MapFeature.Require(SUPPORTS_REMOVE)
092  /*
093   * SetMultimap.asMap essentially returns a Map<K, Set<V>>; we just can't declare it that way.
094   * Thus, calls like asMap().values().remove(someSet) are safe because they are comparing a set to
095   * a collection of other sets.
096   */
097  @SuppressWarnings("CollectionUndefinedEquality")
098  public void testValuesRemove() {
099    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
100    assertTrue(multimap().asMap().values().remove(singleton(v0())));
101    assertEquals(2, multimap().size());
102    assertEquals(singletonMap(k0(), newHashSet(v0(), v3())), multimap().asMap());
103  }
104}