• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotations.GwtIncompatible;
21 import com.google.common.collect.ImmutableMultimap.Builder;
22 import com.google.common.collect.testing.SampleElements;
23 import com.google.common.collect.testing.SampleElements.Unhashables;
24 import com.google.common.collect.testing.UnhashableObject;
25 import com.google.common.testing.EqualsTester;
26 import com.google.common.testing.NullPointerTester;
27 import java.util.Arrays;
28 import java.util.Map.Entry;
29 import junit.framework.TestCase;
30 
31 /**
32  * Tests for {@link ImmutableMultimap}.
33  *
34  * @author Jared Levy
35  */
36 @GwtCompatible(emulated = true)
37 public class ImmutableMultimapTest extends TestCase {
38 
testBuilder_withImmutableEntry()39   public void testBuilder_withImmutableEntry() {
40     ImmutableMultimap<String, Integer> multimap =
41         new Builder<String, Integer>().put(Maps.immutableEntry("one", 1)).build();
42     assertEquals(Arrays.asList(1), multimap.get("one"));
43   }
44 
testBuilder_withImmutableEntryAndNullContents()45   public void testBuilder_withImmutableEntryAndNullContents() {
46     Builder<String, Integer> builder = new Builder<>();
47     try {
48       builder.put(Maps.immutableEntry("one", (Integer) null));
49       fail();
50     } catch (NullPointerException expected) {
51     }
52     try {
53       builder.put(Maps.immutableEntry((String) null, 1));
54       fail();
55     } catch (NullPointerException expected) {
56     }
57   }
58 
59   private static class StringHolder {
60     String string;
61   }
62 
testBuilder_withMutableEntry()63   public void testBuilder_withMutableEntry() {
64     ImmutableMultimap.Builder<String, Integer> builder = new Builder<>();
65     final StringHolder holder = new StringHolder();
66     holder.string = "one";
67     Entry<String, Integer> entry =
68         new AbstractMapEntry<String, Integer>() {
69           @Override
70           public String getKey() {
71             return holder.string;
72           }
73 
74           @Override
75           public Integer getValue() {
76             return 1;
77           }
78         };
79 
80     builder.put(entry);
81     holder.string = "two";
82     assertEquals(Arrays.asList(1), builder.build().get("one"));
83   }
84 
85   // TODO: test ImmutableMultimap builder and factory methods
86 
testCopyOf()87   public void testCopyOf() {
88     ImmutableSetMultimap<String, String> setMultimap = ImmutableSetMultimap.of("k1", "v1");
89     ImmutableMultimap<String, String> setMultimapCopy = ImmutableMultimap.copyOf(setMultimap);
90     assertSame(
91         "copyOf(ImmutableSetMultimap) should not create a new instance",
92         setMultimap,
93         setMultimapCopy);
94 
95     ImmutableListMultimap<String, String> listMultimap = ImmutableListMultimap.of("k1", "v1");
96     ImmutableMultimap<String, String> listMultimapCopy = ImmutableMultimap.copyOf(listMultimap);
97     assertSame(
98         "copyOf(ImmutableListMultimap) should not create a new instance",
99         listMultimap,
100         listMultimapCopy);
101   }
102 
testUnhashableSingletonValue()103   public void testUnhashableSingletonValue() {
104     SampleElements<UnhashableObject> unhashables = new Unhashables();
105     Multimap<Integer, UnhashableObject> multimap = ImmutableMultimap.of(0, unhashables.e0());
106     assertEquals(1, multimap.get(0).size());
107     assertTrue(multimap.get(0).contains(unhashables.e0()));
108   }
109 
testUnhashableMixedValues()110   public void testUnhashableMixedValues() {
111     SampleElements<UnhashableObject> unhashables = new Unhashables();
112     Multimap<Integer, Object> multimap =
113         ImmutableMultimap.<Integer, Object>of(
114             0, unhashables.e0(), 2, "hey you", 0, unhashables.e1());
115     assertEquals(2, multimap.get(0).size());
116     assertTrue(multimap.get(0).contains(unhashables.e0()));
117     assertTrue(multimap.get(0).contains(unhashables.e1()));
118     assertTrue(multimap.get(2).contains("hey you"));
119   }
120 
testEquals()121   public void testEquals() {
122     new EqualsTester()
123         .addEqualityGroup(ImmutableMultimap.of(), ImmutableMultimap.of())
124         .addEqualityGroup(ImmutableMultimap.of(1, "a"), ImmutableMultimap.of(1, "a"))
125         .addEqualityGroup(
126             ImmutableMultimap.of(1, "a", 2, "b"), ImmutableMultimap.of(2, "b", 1, "a"))
127         .testEquals();
128   }
129 
130   @GwtIncompatible // reflection
testNulls()131   public void testNulls() throws Exception {
132     NullPointerTester tester = new NullPointerTester();
133     tester.testAllPublicStaticMethods(ImmutableMultimap.class);
134     tester.ignore(ImmutableListMultimap.class.getMethod("get", Object.class));
135     tester.testAllPublicInstanceMethods(ImmutableMultimap.of());
136     tester.testAllPublicInstanceMethods(ImmutableMultimap.of("a", 1));
137   }
138 }
139