1 /* 2 * Copyright (C) 2007 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 static java.util.Arrays.asList; 20 21 import com.google.common.annotations.GwtCompatible; 22 23 import java.util.Collection; 24 import java.util.Collections; 25 import java.util.Iterator; 26 import java.util.Map; 27 import java.util.Set; 28 29 /** 30 * Tests for {@code SetMultimap} implementations. 31 * 32 * @author Jared Levy 33 */ 34 @GwtCompatible 35 public abstract class AbstractSetMultimapTest extends AbstractMultimapTest { 36 37 public void testDuplicates() { 38 Multimap<String, Integer> multimap = getMultimap(); 39 assertTrue(multimap.put("foo", 1)); 40 assertTrue(multimap.put("foo", 3)); 41 assertTrue(multimap.put("bar", 3)); 42 assertFalse(multimap.put("foo", 1)); 43 assertSize(3); 44 assertTrue(multimap.containsEntry("foo", 1)); 45 assertTrue(multimap.remove("foo", 1)); 46 assertSize(2); 47 assertFalse(multimap.containsEntry("foo", 1)); 48 } 49 50 public void testGetEquals() { 51 Multimap<String, Integer> multimap = getMultimap(); 52 multimap.put("foo", 1); 53 multimap.put("foo", 3); 54 assertEquals(ImmutableSet.of(1, 3), multimap.get("foo")); 55 } 56 57 public void testAsMapEquals() { 58 Multimap<String, Integer> multimap = getMultimap(); 59 multimap.put("foo", 1); 60 multimap.put("foo", nullValue()); 61 multimap.put(nullKey(), 3); 62 Map<String, Collection<Integer>> map = multimap.asMap(); 63 64 Map<String, Collection<Integer>> equalMap = Maps.newHashMap(); 65 equalMap.put("foo", Sets.newHashSet(1, nullValue())); 66 equalMap.put(nullKey(), Sets.newHashSet(3)); 67 assertEquals(map, equalMap); 68 assertEquals(equalMap, map); 69 assertEquals(equalMap.hashCode(), multimap.hashCode()); 70 71 Map<String, Collection<Integer>> unequalMap = Maps.newHashMap(); 72 equalMap.put("foo", Sets.newHashSet(3, nullValue())); 73 equalMap.put(nullKey(), Sets.newHashSet(1)); 74 assertFalse(map.equals(unequalMap)); 75 assertFalse(unequalMap.equals(map)); 76 } 77 78 public void testAsMapEntriesEquals() { 79 Multimap<String, Integer> multimap = getMultimap(); 80 multimap.put("foo", 1); 81 multimap.put("foo", 3); 82 Set<Map.Entry<String, Collection<Integer>>> set 83 = multimap.asMap().entrySet(); 84 85 Iterator<Map.Entry<String, Collection<Integer>>> i = set.iterator(); 86 Map.Entry<String, Collection<Integer>> expected = 87 Maps.immutableEntry( 88 "foo", (Collection<Integer>) ImmutableSet.of(1, 3)); 89 Map.Entry<String, Collection<Integer>> entry = i.next(); 90 assertEquals(expected, entry); 91 assertFalse(i.hasNext()); 92 93 assertTrue(Collections.singleton(expected).equals(set)); 94 assertTrue(set.equals(Collections.singleton(expected))); 95 96 Map.Entry<?, ?>[] array = new Map.Entry<?, ?>[3]; 97 array[1] = Maps.immutableEntry("another", "entry"); 98 assertSame(array, set.toArray(array)); 99 assertEquals(entry, array[0]); 100 assertNull(array[1]); 101 } 102 103 @SuppressWarnings("unchecked") 104 public void testAsMapValues() { 105 Multimap<String, Integer> multimap = create(); 106 Collection<Collection<Integer>> asMapValues = multimap.asMap().values(); 107 multimap.put("foo", 1); 108 multimap.put("foo", 3); 109 110 Collection<?>[] array = new Collection<?>[3]; 111 array[1] = Collections.emptyList(); 112 assertSame(array, asMapValues.toArray(array)); 113 assertEquals(Sets.newHashSet(1, 3), array[0]); 114 assertNull(array[1]); 115 116 multimap.put("bar", 3); 117 assertTrue(asMapValues.containsAll( 118 asList(Sets.newHashSet(1, 3), Sets.newHashSet(3)))); 119 assertFalse(asMapValues.containsAll( 120 asList(Sets.newHashSet(1, 3), Sets.newHashSet(1)))); 121 assertFalse(asMapValues.remove(ImmutableSet.of(1, 2))); 122 assertEquals(3, multimap.size()); 123 assertTrue(asMapValues.remove(ImmutableSet.of(1, 3))); 124 assertEquals(1, multimap.size()); 125 } 126 127 public void testPutReturn() { 128 Multimap<String, Integer> multimap = getMultimap(); 129 assertTrue(multimap.put("foo", 1)); 130 assertFalse(multimap.put("foo", 1)); 131 assertTrue(multimap.put("foo", 3)); 132 assertTrue(multimap.put("bar", 5)); 133 } 134 135 public void testPutAllReturn_existingElements() { 136 Multimap<String, Integer> multimap = create(); 137 assertTrue(multimap.putAll("foo", asList(1, 2, 3))); 138 assertFalse(multimap.put("foo", 1)); 139 assertFalse(multimap.putAll("foo", asList(1, 2, 3))); 140 assertFalse(multimap.putAll("foo", asList(1, 3))); 141 assertTrue(multimap.putAll("foo", asList(1, 2, 4))); 142 143 Multimap<String, Integer> other = create(); 144 other.putAll("foo", asList(1, 2)); 145 assertFalse(multimap.putAll(other)); 146 147 other.putAll("bar", asList(1, 2)); 148 assertTrue(multimap.putAll(other)); 149 assertTrue(other.putAll(multimap)); 150 assertTrue(other.equals(multimap)); 151 } 152 153 @SuppressWarnings("unchecked") 154 public void testEntriesEquals() { 155 Multimap<String, Integer> multimap = getMultimap(); 156 multimap.put("foo", 1); 157 multimap.put("foo", 3); 158 multimap.put("bar", 3); 159 Collection<Map.Entry<String, Integer>> entries = multimap.entries(); 160 161 Set<Map.Entry<String, Integer>> same = Sets.newHashSet( 162 Maps.immutableEntry("foo", 3), 163 Maps.immutableEntry("bar", 3), 164 Maps.immutableEntry("foo", 1)); 165 assertEquals(entries, same); 166 assertEquals(same, entries); 167 assertEquals(entries.hashCode(), same.hashCode()); 168 169 assertFalse(entries.equals(null)); 170 assertFalse(entries.equals("foo")); 171 172 Set<Map.Entry<String, Integer>> different3 = Sets.newHashSet( 173 Maps.immutableEntry("foo", 3), 174 Maps.immutableEntry("bar", 3), 175 Maps.immutableEntry("bar", 1)); 176 assertFalse(entries.equals(different3)); 177 assertFalse(different3.equals(entries)); 178 179 Set<Map.Entry<String, Integer>> different4 = Sets.newHashSet( 180 Maps.immutableEntry("foo", 3), 181 Maps.immutableEntry("bar", 3), 182 Maps.immutableEntry("bar", 1), 183 Maps.immutableEntry("foo", 1)); 184 assertFalse(entries.equals(different4)); 185 assertFalse(different4.equals(entries)); 186 } 187 } 188