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.features.CollectionSize.SEVERAL;
018import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
019
020import com.google.common.annotations.GwtCompatible;
021import com.google.common.collect.Lists;
022import com.google.common.collect.Maps;
023import com.google.common.collect.Sets;
024import com.google.common.collect.testing.Helpers;
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.Collections;
031import java.util.List;
032import java.util.Map;
033import java.util.Map.Entry;
034import java.util.Set;
035import org.checkerframework.checker.nullness.qual.Nullable;
036import org.junit.Ignore;
037
038/**
039 * Testers for {@link com.google.common.collect.ListMultimap#asMap}.
040 *
041 * @author Louis Wasserman
042 * @param <K> The key type of the tested multimap.
043 * @param <V> The value type of the tested multimap.
044 */
045@GwtCompatible
046@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
047@ElementTypesAreNonnullByDefault
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(
074        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
075    Map<K, Collection<V>> expected = Maps.newHashMap();
076    expected.put(k0(), Lists.newArrayList(v0(), v3()));
077    expected.put(k1(), Lists.newArrayList(v0()));
078    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
079  }
080
081  @CollectionSize.Require(SEVERAL)
082  public void testEntrySetEquals() {
083    resetContainer(
084        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
085    Set<Entry<K, Collection<V>>> expected = Sets.newHashSet();
086    expected.add(Helpers.mapEntry(k0(), (Collection<V>) Lists.newArrayList(v0(), v3())));
087    expected.add(Helpers.mapEntry(k1(), (Collection<V>) Lists.newArrayList(v0())));
088    new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
089  }
090
091  @CollectionSize.Require(SEVERAL)
092  @MapFeature.Require(SUPPORTS_REMOVE)
093  public void testValuesRemove() {
094    resetContainer(
095        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
096    assertTrue(multimap().asMap().values().remove(Collections.singletonList(v0())));
097    assertEquals(2, multimap().size());
098    assertEquals(
099        Collections.singletonMap(k0(), Lists.newArrayList(v0(), v3())), multimap().asMap());
100  }
101}