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.singletonList;
022import static java.util.Collections.singletonMap;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.Lists;
026import com.google.common.collect.Maps;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import com.google.common.testing.EqualsTester;
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.List;
033import java.util.Map;
034import java.util.Map.Entry;
035import java.util.Set;
036import org.checkerframework.checker.nullness.qual.Nullable;
037import org.junit.Ignore;
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@Ignore("test runners must not instantiate and run this directly, only via suites we build")
048// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
049@SuppressWarnings("JUnit4ClassUsedInJUnit3")
050@ElementTypesAreNonnullByDefault
051public class ListMultimapAsMapTester<K extends @Nullable Object, V extends @Nullable Object>
052    extends AbstractListMultimapTester<K, V> {
053  public void testAsMapValuesImplementList() {
054    for (Collection<V> valueCollection : multimap().asMap().values()) {
055      assertTrue(valueCollection instanceof List);
056    }
057  }
058
059  public void testAsMapGetImplementsList() {
060    for (K key : multimap().keySet()) {
061      assertTrue(multimap().asMap().get(key) instanceof List);
062    }
063  }
064
065  @MapFeature.Require(SUPPORTS_REMOVE)
066  public void testAsMapRemoveImplementsList() {
067    List<K> keys = new ArrayList<>(multimap().keySet());
068    for (K key : keys) {
069      resetCollection();
070      assertTrue(multimap().asMap().remove(key) instanceof List);
071    }
072  }
073
074  @CollectionSize.Require(SEVERAL)
075  public void testEquals() {
076    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
077    Map<K, Collection<V>> expected = Maps.newHashMap();
078    expected.put(k0(), Lists.newArrayList(v0(), v3()));
079    expected.put(k1(), Lists.newArrayList(v0()));
080    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
081  }
082
083  @CollectionSize.Require(SEVERAL)
084  public void testEntrySetEquals() {
085    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
086    Set<Entry<K, Collection<V>>> expected = newHashSet();
087    expected.add(mapEntry(k0(), (Collection<V>) Lists.newArrayList(v0(), v3())));
088    expected.add(mapEntry(k1(), (Collection<V>) Lists.newArrayList(v0())));
089    new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
090  }
091
092  @CollectionSize.Require(SEVERAL)
093  @MapFeature.Require(SUPPORTS_REMOVE)
094  public void testValuesRemove() {
095    resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3()));
096    assertTrue(multimap().asMap().values().remove(singletonList(v0())));
097    assertEquals(2, multimap().size());
098    assertEquals(singletonMap(k0(), Lists.newArrayList(v0(), v3())), multimap().asMap());
099  }
100}