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.Maps;
022import com.google.common.collect.SetMultimap;
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 SetMultimap#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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
048@ElementTypesAreNonnullByDefault
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(
075        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
076    Map<K, Collection<V>> expected = Maps.newHashMap();
077    expected.put(k0(), Sets.newHashSet(v0(), v3()));
078    expected.put(k1(), Sets.newHashSet(v0()));
079    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
080  }
081
082  @CollectionSize.Require(SEVERAL)
083  public void testEntrySetEquals() {
084    resetContainer(
085        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
086    Set<Entry<K, Collection<V>>> expected = Sets.newHashSet();
087    expected.add(Helpers.mapEntry(k0(), (Collection<V>) Sets.newHashSet(v0(), v3())));
088    expected.add(Helpers.mapEntry(k1(), (Collection<V>) Sets.newHashSet(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(
096        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
097    assertTrue(multimap().asMap().values().remove(Collections.singleton(v0())));
098    assertEquals(2, multimap().size());
099    assertEquals(Collections.singletonMap(k0(), Sets.newHashSet(v0(), v3())), multimap().asMap());
100  }
101}