aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/collect/ImmutableMultimapTest.java
blob: d02359c7db0db5c02834e2ee12de54854230e66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2008 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.SampleElements.Unhashables;
import com.google.common.collect.testing.UnhashableObject;
import com.google.common.testing.EqualsTester;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Map.Entry;

/**
 * Tests for {@link ImmutableMultimap}.
 *
 * @author Jared Levy
 */
@GwtCompatible(emulated = true)
public class ImmutableMultimapTest extends TestCase {

  public void testBuilder_withImmutableEntry() {
    ImmutableMultimap<String, Integer> multimap = new Builder<String, Integer>()
        .put(Maps.immutableEntry("one", 1))
        .build();
    assertEquals(Arrays.asList(1), multimap.get("one"));
  }

  public void testBuilder_withImmutableEntryAndNullContents() {
    Builder<String, Integer> builder = new Builder<String, Integer>();
    try {
      builder.put(Maps.immutableEntry("one", (Integer) null));
      fail();
    } catch (NullPointerException expected) {
    }
    try {
      builder.put(Maps.immutableEntry((String) null, 1));
      fail();
    } catch (NullPointerException expected) {
    }
  }

  private static class StringHolder {
    String string;
  }

  public void testBuilder_withMutableEntry() {
    ImmutableMultimap.Builder<String, Integer> builder =
        new Builder<String, Integer>();
    final StringHolder holder = new StringHolder();
    holder.string = "one";
    Entry<String, Integer> entry = new AbstractMapEntry<String, Integer>() {
      @Override public String getKey() {
        return holder.string;
      }
      @Override public Integer getValue() {
        return 1;
      }
    };

    builder.put(entry);
    holder.string = "two";
    assertEquals(Arrays.asList(1), builder.build().get("one"));
  }

  // TODO: test ImmutableMultimap builder and factory methods

  public void testCopyOf() {
    ImmutableSetMultimap<String, String> setMultimap
        = ImmutableSetMultimap.of("k1", "v1");
    ImmutableMultimap<String, String> setMultimapCopy
        = ImmutableMultimap.copyOf(setMultimap);
    assertSame("copyOf(ImmutableSetMultimap) should not create a new instance",
        setMultimap, setMultimapCopy);

    ImmutableListMultimap<String, String> listMultimap
        = ImmutableListMultimap.of("k1", "v1");
    ImmutableMultimap<String, String> listMultimapCopy
        = ImmutableMultimap.copyOf(listMultimap);
    assertSame("copyOf(ImmutableListMultimap) should not create a new instance",
        listMultimap, listMultimapCopy);
  }

  public void testUnhashableSingletonValue() {
    SampleElements<UnhashableObject> unhashables = new Unhashables();
    Multimap<Integer, UnhashableObject> multimap = ImmutableMultimap.of(
        0, unhashables.e0);
    assertEquals(1, multimap.get(0).size());
    assertTrue(multimap.get(0).contains(unhashables.e0));
  }

  public void testUnhashableMixedValues() {
    SampleElements<UnhashableObject> unhashables = new Unhashables();
    Multimap<Integer, Object> multimap = ImmutableMultimap.<Integer, Object>of(
        0, unhashables.e0, 2, "hey you", 0, unhashables.e1);
    assertEquals(2, multimap.get(0).size());
    assertTrue(multimap.get(0).contains(unhashables.e0));
    assertTrue(multimap.get(0).contains(unhashables.e1));
    assertTrue(multimap.get(2).contains("hey you"));
  }

  public void testEquals() {
    new EqualsTester()
        .addEqualityGroup(ImmutableMultimap.of(), ImmutableMultimap.of())
        .addEqualityGroup(ImmutableMultimap.of(1, "a"), ImmutableMultimap.of(1, "a"))
        .addEqualityGroup(
            ImmutableMultimap.of(1, "a", 2, "b"),
            ImmutableMultimap.of(2, "b", 1, "a"))
        .testEquals();
  }
}