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