Home | History | Annotate | Download | only in collect
      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 
     25 import junit.framework.TestCase;
     26 
     27 import java.util.Arrays;
     28 import java.util.Map.Entry;
     29 
     30 /**
     31  * Tests for {@link ImmutableMultimap}.
     32  *
     33  * @author Jared Levy
     34  */
     35 @GwtCompatible
     36 public class ImmutableMultimapTest extends TestCase {
     37 
     38   public void testBuilder_withImmutableEntry() {
     39     ImmutableMultimap<String, Integer> multimap = new Builder<String, Integer>()
     40         .put(Maps.immutableEntry("one", 1))
     41         .build();
     42     assertEquals(Arrays.asList(1), multimap.get("one"));
     43   }
     44 
     45   public void testBuilder_withImmutableEntryAndNullContents() {
     46     Builder<String, Integer> builder = new Builder<String, Integer>();
     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 
     63   public void testBuilder_withMutableEntry() {
     64     ImmutableMultimap.Builder<String, Integer> builder =
     65         new Builder<String, Integer>();
     66     final StringHolder holder = new StringHolder();
     67     holder.string = "one";
     68     Entry<String, Integer> entry = new AbstractMapEntry<String, Integer>() {
     69       @Override public String getKey() {
     70         return holder.string;
     71       }
     72       @Override public Integer getValue() {
     73         return 1;
     74       }
     75     };
     76 
     77     builder.put(entry);
     78     holder.string = "two";
     79     assertEquals(Arrays.asList(1), builder.build().get("one"));
     80   }
     81 
     82   // TODO: test ImmutableMultimap builder and factory methods
     83 
     84   public void testCopyOf() {
     85     ImmutableSetMultimap<String, String> setMultimap
     86         = ImmutableSetMultimap.of("k1", "v1");
     87     ImmutableMultimap<String, String> setMultimapCopy
     88         = ImmutableMultimap.copyOf(setMultimap);
     89     assertSame("copyOf(ImmutableSetMultimap) should not create a new instance",
     90         setMultimap, setMultimapCopy);
     91 
     92     ImmutableListMultimap<String, String> listMultimap
     93         = ImmutableListMultimap.of("k1", "v1");
     94     ImmutableMultimap<String, String> listMultimapCopy
     95         = ImmutableMultimap.copyOf(listMultimap);
     96     assertSame("copyOf(ImmutableListMultimap) should not create a new instance",
     97         listMultimap, listMultimapCopy);
     98   }
     99 
    100   public void testUnhashableSingletonValue() {
    101     SampleElements<UnhashableObject> unhashables = new Unhashables();
    102     Multimap<Integer, UnhashableObject> multimap = ImmutableMultimap.of(
    103         0, unhashables.e0);
    104     assertEquals(1, multimap.get(0).size());
    105     assertTrue(multimap.get(0).contains(unhashables.e0));
    106   }
    107 
    108   public void testUnhashableMixedValues() {
    109     SampleElements<UnhashableObject> unhashables = new Unhashables();
    110     Multimap<Integer, Object> multimap = ImmutableMultimap.<Integer, Object>of(
    111         0, unhashables.e0, 2, "hey you", 0, unhashables.e1);
    112     assertEquals(2, multimap.get(0).size());
    113     assertTrue(multimap.get(0).contains(unhashables.e0));
    114     assertTrue(multimap.get(0).contains(unhashables.e1));
    115     assertTrue(multimap.get(2).contains("hey you"));
    116   }
    117 }
    118