1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.collect.testing.MapInterfaceTest; 21 import java.util.Collection; 22 import java.util.Map; 23 24 /** 25 * Test {@link Multimap#asMap()} for an arbitrary multimap with {@link MapInterfaceTest}. 26 * 27 * @author George van den Driessche 28 * @author Jared Levy 29 */ 30 @GwtCompatible 31 @ElementTypesAreNonnullByDefault 32 public abstract class AbstractMultimapAsMapImplementsMapTest 33 extends MapInterfaceTest<String, Collection<Integer>> { 34 AbstractMultimapAsMapImplementsMapTest( boolean modifiable, boolean allowsNulls, boolean supportsIteratorRemove)35 public AbstractMultimapAsMapImplementsMapTest( 36 boolean modifiable, boolean allowsNulls, boolean supportsIteratorRemove) { 37 super(allowsNulls, allowsNulls, false, modifiable, modifiable, supportsIteratorRemove); 38 } 39 populate(Multimap<String, Integer> multimap)40 protected void populate(Multimap<String, Integer> multimap) { 41 multimap.put("one", 1); 42 multimap.put("two", 2); 43 multimap.put("two", 22); 44 multimap.put("three", 3); 45 multimap.put("three", 33); 46 multimap.put("three", 333); 47 } 48 49 @Override getKeyNotInPopulatedMap()50 protected String getKeyNotInPopulatedMap() throws UnsupportedOperationException { 51 return "zero"; 52 } 53 54 @Override getValueNotInPopulatedMap()55 protected Collection<Integer> getValueNotInPopulatedMap() throws UnsupportedOperationException { 56 return Lists.newArrayList(0); 57 } 58 59 /** 60 * The version of this test supplied by {@link MapInterfaceTest} fails for this particular Map 61 * implementation, because {@code map.get()} returns a view collection that changes in the course 62 * of a call to {@code remove()}. Thus, the expectation doesn't hold that {@code map.remove(x)} 63 * returns the same value which {@code map.get(x)} did immediately beforehand. 64 */ 65 @Override testRemove()66 public void testRemove() { 67 final Map<String, Collection<Integer>> map; 68 final String keyToRemove; 69 try { 70 map = makePopulatedMap(); 71 } catch (UnsupportedOperationException e) { 72 return; 73 } 74 keyToRemove = map.keySet().iterator().next(); 75 if (supportsRemove) { 76 int initialSize = map.size(); 77 map.get(keyToRemove); 78 map.remove(keyToRemove); 79 // This line doesn't hold - see the Javadoc comments above. 80 // assertEquals(expectedValue, oldValue); 81 assertFalse(map.containsKey(keyToRemove)); 82 assertEquals(initialSize - 1, map.size()); 83 } else { 84 try { 85 map.remove(keyToRemove); 86 fail("Expected UnsupportedOperationException."); 87 } catch (UnsupportedOperationException expected) { 88 } 89 } 90 assertInvariants(map); 91 } 92 } 93