1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import java.util.Map.Entry;
32
33
34
35
36
37
38 @GwtCompatible(emulated = true)
39 public class HashMultimapTest extends TestCase {
40
41 @GwtIncompatible("suite")
42 public static Test suite() {
43 TestSuite suite = new TestSuite();
44 suite.addTest(SetMultimapTestSuiteBuilder.using(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
71
72
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
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
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
101 public void testCreateFromIllegalSizes() {
102 try {
103 HashMultimap.create(-20, 15);
104 fail();
105 } catch (IllegalArgumentException expected) {}
106
107 try {
108 HashMultimap.create(20, -15);
109 fail();
110 } catch (IllegalArgumentException expected) {}
111 }
112
113 public void testEmptyMultimapsEqual() {
114 Multimap<String, Integer> setMultimap = HashMultimap.create();
115 Multimap<String, Integer> listMultimap = ArrayListMultimap.create();
116 assertTrue(setMultimap.equals(listMultimap));
117 assertTrue(listMultimap.equals(setMultimap));
118 }
119 }