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 com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import com.google.common.collect.testing.features.CollectionFeature; 22 import com.google.common.collect.testing.features.CollectionSize; 23 import com.google.common.collect.testing.features.MapFeature; 24 import com.google.common.collect.testing.google.SetMultimapTestSuiteBuilder; 25 import com.google.common.collect.testing.google.TestStringSetMultimapGenerator; 26 import java.util.Map.Entry; 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 31 /** 32 * Unit tests for {@link HashMultimap}. 33 * 34 * @author Jared Levy 35 */ 36 @GwtCompatible(emulated = true) 37 public class HashMultimapTest extends TestCase { 38 39 @GwtIncompatible // suite suite()40 public static Test suite() { 41 TestSuite suite = new TestSuite(); 42 suite.addTest( 43 SetMultimapTestSuiteBuilder.using( 44 new TestStringSetMultimapGenerator() { 45 @Override 46 protected SetMultimap<String, String> create(Entry<String, String>[] entries) { 47 SetMultimap<String, String> multimap = HashMultimap.create(); 48 for (Entry<String, String> entry : entries) { 49 multimap.put(entry.getKey(), entry.getValue()); 50 } 51 return multimap; 52 } 53 }) 54 .named("HashMultimap") 55 .withFeatures( 56 MapFeature.ALLOWS_NULL_KEYS, 57 MapFeature.ALLOWS_NULL_VALUES, 58 MapFeature.ALLOWS_ANY_NULL_QUERIES, 59 MapFeature.GENERAL_PURPOSE, 60 MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, 61 CollectionFeature.SUPPORTS_ITERATOR_REMOVE, 62 CollectionFeature.SERIALIZABLE, 63 CollectionSize.ANY) 64 .createTestSuite()); 65 suite.addTestSuite(HashMultimapTest.class); 66 return suite; 67 } 68 69 /* 70 * The behavior of toString() is tested by TreeMultimap, which shares a 71 * lot of code with HashMultimap and has deterministic iteration order. 72 */ testCreate()73 public void testCreate() { 74 HashMultimap<String, Integer> multimap = HashMultimap.create(); 75 multimap.put("foo", 1); 76 multimap.put("bar", 2); 77 multimap.put("foo", 3); 78 assertEquals(ImmutableSet.of(1, 3), multimap.get("foo")); 79 assertEquals(2, multimap.expectedValuesPerKey); 80 } 81 testCreateFromMultimap()82 public void testCreateFromMultimap() { 83 HashMultimap<String, Integer> multimap = HashMultimap.create(); 84 multimap.put("foo", 1); 85 multimap.put("bar", 2); 86 multimap.put("foo", 3); 87 HashMultimap<String, Integer> copy = HashMultimap.create(multimap); 88 assertEquals(multimap, copy); 89 assertEquals(2, copy.expectedValuesPerKey); 90 } 91 testCreateFromSizes()92 public void testCreateFromSizes() { 93 HashMultimap<String, Integer> multimap = HashMultimap.create(20, 15); 94 multimap.put("foo", 1); 95 multimap.put("bar", 2); 96 multimap.put("foo", 3); 97 assertEquals(ImmutableSet.of(1, 3), multimap.get("foo")); 98 assertEquals(15, multimap.expectedValuesPerKey); 99 } 100 testCreateFromIllegalSizes()101 public void testCreateFromIllegalSizes() { 102 try { 103 HashMultimap.create(-20, 15); 104 fail(); 105 } catch (IllegalArgumentException expected) { 106 } 107 108 try { 109 HashMultimap.create(20, -15); 110 fail(); 111 } catch (IllegalArgumentException expected) { 112 } 113 } 114 testEmptyMultimapsEqual()115 public void testEmptyMultimapsEqual() { 116 Multimap<String, Integer> setMultimap = HashMultimap.create(); 117 Multimap<String, Integer> listMultimap = ArrayListMultimap.create(); 118 assertTrue(setMultimap.equals(listMultimap)); 119 assertTrue(listMultimap.equals(setMultimap)); 120 } 121 } 122