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