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.jspecify.annotations.NullMarked; 037import org.jspecify.annotations.Nullable; 038import org.junit.Ignore; 039 040/** 041 * Testers for {@link com.google.common.collect.ListMultimap#asMap}. 042 * 043 * @author Louis Wasserman 044 * @param <K> The key type of the tested multimap. 045 * @param <V> The value type of the tested multimap. 046 */ 047@GwtCompatible 048@Ignore("test runners must not instantiate and run this directly, only via suites we build") 049// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 050@SuppressWarnings("JUnit4ClassUsedInJUnit3") 051@NullMarked 052public class ListMultimapAsMapTester<K extends @Nullable Object, V extends @Nullable Object> 053 extends AbstractListMultimapTester<K, V> { 054 public void testAsMapValuesImplementList() { 055 for (Collection<V> valueCollection : multimap().asMap().values()) { 056 assertTrue(valueCollection instanceof List); 057 } 058 } 059 060 public void testAsMapGetImplementsList() { 061 for (K key : multimap().keySet()) { 062 assertTrue(multimap().asMap().get(key) instanceof List); 063 } 064 } 065 066 @MapFeature.Require(SUPPORTS_REMOVE) 067 public void testAsMapRemoveImplementsList() { 068 List<K> keys = new ArrayList<>(multimap().keySet()); 069 for (K key : keys) { 070 resetCollection(); 071 assertTrue(multimap().asMap().remove(key) instanceof List); 072 } 073 } 074 075 @CollectionSize.Require(SEVERAL) 076 public void testEquals() { 077 resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3())); 078 Map<K, Collection<V>> expected = Maps.newHashMap(); 079 expected.put(k0(), Lists.newArrayList(v0(), v3())); 080 expected.put(k1(), Lists.newArrayList(v0())); 081 new EqualsTester().addEqualityGroup(expected, multimap().asMap()).testEquals(); 082 } 083 084 @CollectionSize.Require(SEVERAL) 085 public void testEntrySetEquals() { 086 resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3())); 087 Set<Entry<K, Collection<V>>> expected = newHashSet(); 088 expected.add(mapEntry(k0(), (Collection<V>) Lists.newArrayList(v0(), v3()))); 089 expected.add(mapEntry(k1(), (Collection<V>) Lists.newArrayList(v0()))); 090 new EqualsTester().addEqualityGroup(expected, multimap().asMap().entrySet()).testEquals(); 091 } 092 093 @CollectionSize.Require(SEVERAL) 094 @MapFeature.Require(SUPPORTS_REMOVE) 095 /* 096 * ListMultimap.asMap essentially returns a Map<K, List<V>>; we just can't declare it that way. 097 * Thus, calls like asMap().values().remove(someList) are safe because they are comparing a list 098 * to a collection of other lists. 099 */ 100 @SuppressWarnings("CollectionUndefinedEquality") 101 public void testValuesRemove() { 102 resetContainer(mapEntry(k0(), v0()), mapEntry(k1(), v0()), mapEntry(k0(), v3())); 103 assertTrue(multimap().asMap().values().remove(singletonList(v0()))); 104 assertEquals(2, multimap().size()); 105 assertEquals(singletonMap(k0(), Lists.newArrayList(v0(), v3())), multimap().asMap()); 106 } 107}