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.junit.Ignore;
036
037/**
038 * Testers for {@link SetMultimap#asMap}.
039 *
040 * @author Louis Wasserman
041 * @param <K> The key type of the tested multimap.
042 * @param <V> The value type of the tested multimap.
043 */
044@GwtCompatible
045@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
046public class SetMultimapAsMapTester<K, V> extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
047  public void testAsMapValuesImplementSet() {
048    for (Collection<V> valueCollection : multimap().asMap().values()) {
049      assertTrue(valueCollection instanceof Set);
050    }
051  }
052
053  public void testAsMapGetImplementsSet() {
054    for (K key : multimap().keySet()) {
055      assertTrue(multimap().asMap().get(key) instanceof Set);
056    }
057  }
058
059  @MapFeature.Require(SUPPORTS_REMOVE)
060  public void testAsMapRemoveImplementsSet() {
061    List<K> keys = new ArrayList<K>(multimap().keySet());
062    for (K key : keys) {
063      resetCollection();
064      assertTrue(multimap().asMap().remove(key) instanceof Set);
065    }
066  }
067
068  @CollectionSize.Require(SEVERAL)
069  public void testEquals() {
070    resetContainer(
071        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
072    Map<K, Collection<V>> expected = Maps.newHashMap();
073    expected.put(k0(), Sets.newHashSet(v0(), v3()));
074    expected.put(k1(), Sets.newHashSet(v0()));
075    new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals();
076  }
077
078  @CollectionSize.Require(SEVERAL)
079  public void testEntrySetEquals() {
080    resetContainer(
081        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
082    Set<Entry<K, Collection<V>>> expected = Sets.newHashSet();
083    expected.add(Helpers.mapEntry(k0(), (Collection<V>) Sets.newHashSet(v0(), v3())));
084    expected.add(Helpers.mapEntry(k1(), (Collection<V>) Sets.newHashSet(v0())));
085    new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals();
086  }
087
088  @CollectionSize.Require(SEVERAL)
089  @MapFeature.Require(SUPPORTS_REMOVE)
090  public void testValuesRemove() {
091    resetContainer(
092        Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3()));
093    assertTrue(multimap().asMap().values().remove(Collections.singleton(v0())));
094    assertEquals(2, multimap().size());
095    assertEquals(Collections.singletonMap(k0(), Sets.newHashSet(v0(), v3())), multimap().asMap());
096  }
097}