• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 static com.google.common.base.Preconditions.checkState;
20 import static com.google.common.collect.testing.Helpers.mapEntry;
21 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
22 import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE;
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.annotations.GwtIncompatible;
27 import com.google.common.base.Equivalence;
28 import com.google.common.base.Function;
29 import com.google.common.collect.testing.AnEnum;
30 import com.google.common.collect.testing.Helpers;
31 import com.google.common.collect.testing.MapTestSuiteBuilder;
32 import com.google.common.collect.testing.TestEnumMapGenerator;
33 import com.google.common.collect.testing.features.CollectionSize;
34 import com.google.common.testing.CollectorTester;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.stream.Collector;
38 import java.util.stream.Stream;
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42 
43 /**
44  * Tests for {@code ImmutableEnumMap}.
45  *
46  * @author Louis Wasserman
47  */
48 @GwtCompatible(emulated = true)
49 public class ImmutableEnumMapTest extends TestCase {
50   public static class ImmutableEnumMapGenerator extends TestEnumMapGenerator {
51     @Override
create(Entry<AnEnum, String>[] entries)52     protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) {
53       Map<AnEnum, String> map = Maps.newHashMap();
54       for (Entry<AnEnum, String> entry : entries) {
55         map.put(entry.getKey(), entry.getValue());
56       }
57       return Maps.immutableEnumMap(map);
58     }
59   }
60 
61   @GwtIncompatible // suite
suite()62   public static Test suite() {
63     TestSuite suite = new TestSuite();
64     suite.addTest(
65         MapTestSuiteBuilder.using(new ImmutableEnumMapGenerator())
66             .named("Maps.immutableEnumMap")
67             .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
68             .createTestSuite());
69     suite.addTestSuite(ImmutableEnumMapTest.class);
70     return suite;
71   }
72 
testIteratesOnce()73   public void testIteratesOnce() {
74     Map<AnEnum, AnEnum> map =
75         Maps.asMap(
76             ImmutableSet.of(AnEnum.A),
77             new Function<AnEnum, AnEnum>() {
78               boolean used = false;
79 
80               @Override
81               public AnEnum apply(AnEnum ae) {
82                 checkState(!used, "should not be applied more than once");
83                 used = true;
84                 return ae;
85               }
86             });
87     ImmutableMap<AnEnum, AnEnum> copy = Maps.immutableEnumMap(map);
88     assertThat(copy.entrySet()).containsExactly(Helpers.mapEntry(AnEnum.A, AnEnum.A));
89   }
90 
testEmptyImmutableEnumMap()91   public void testEmptyImmutableEnumMap() {
92     ImmutableMap<AnEnum, String> map = Maps.immutableEnumMap(ImmutableMap.<AnEnum, String>of());
93     assertEquals(ImmutableMap.of(), map);
94   }
95 
testImmutableEnumMapOrdering()96   public void testImmutableEnumMapOrdering() {
97     ImmutableMap<AnEnum, String> map =
98         Maps.immutableEnumMap(ImmutableMap.of(AnEnum.C, "c", AnEnum.A, "a", AnEnum.E, "e"));
99 
100     assertThat(map.entrySet())
101         .containsExactly(
102             Helpers.mapEntry(AnEnum.A, "a"),
103             Helpers.mapEntry(AnEnum.C, "c"),
104             Helpers.mapEntry(AnEnum.E, "e"))
105         .inOrder();
106   }
107 
testToImmutableEnumMap()108   public void testToImmutableEnumMap() {
109     Collector<Entry<AnEnum, Integer>, ?, ImmutableMap<AnEnum, Integer>> collector =
110         Maps.toImmutableEnumMap(Entry::getKey, Entry::getValue);
111     Equivalence<ImmutableMap<AnEnum, Integer>> equivalence =
112         Equivalence.equals().<Entry<AnEnum, Integer>>pairwise().onResultOf(ImmutableMap::entrySet);
113     CollectorTester.of(collector, equivalence)
114         .expectCollects(
115             ImmutableMap.of(AnEnum.A, 1, AnEnum.C, 2, AnEnum.E, 3),
116             mapEntry(AnEnum.A, 1),
117             mapEntry(AnEnum.C, 2),
118             mapEntry(AnEnum.E, 3));
119   }
120 
testToImmutableMap_exceptionOnDuplicateKey()121   public void testToImmutableMap_exceptionOnDuplicateKey() {
122     Collector<Entry<AnEnum, Integer>, ?, ImmutableMap<AnEnum, Integer>> collector =
123         Maps.toImmutableEnumMap(Entry::getKey, Entry::getValue);
124     try {
125       Stream.of(mapEntry(AnEnum.A, 1), mapEntry(AnEnum.A, 11)).collect(collector);
126       fail("Expected IllegalArgumentException");
127     } catch (IllegalArgumentException expected) {
128     }
129   }
130 
testToImmutableMapMerging()131   public void testToImmutableMapMerging() {
132     Collector<Entry<AnEnum, Integer>, ?, ImmutableMap<AnEnum, Integer>> collector =
133         Maps.toImmutableEnumMap(Entry::getKey, Entry::getValue, Integer::sum);
134     Equivalence<ImmutableMap<AnEnum, Integer>> equivalence =
135         Equivalence.equals().<Entry<AnEnum, Integer>>pairwise().onResultOf(ImmutableMap::entrySet);
136     CollectorTester.of(collector, equivalence)
137         .expectCollects(
138             ImmutableMap.of(AnEnum.A, 1, AnEnum.B, 4, AnEnum.C, 3),
139             mapEntry(AnEnum.A, 1),
140             mapEntry(AnEnum.B, 2),
141             mapEntry(AnEnum.C, 3),
142             mapEntry(AnEnum.B, 2));
143   }
144 }
145