• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.testing.SampleElements;
22 import com.google.common.collect.testing.features.CollectionFeature;
23 import com.google.common.collect.testing.features.CollectionSize;
24 import com.google.common.collect.testing.features.MapFeature;
25 import com.google.common.collect.testing.google.BiMapTestSuiteBuilder;
26 import com.google.common.collect.testing.google.TestBiMapGenerator;
27 import com.google.common.testing.NullPointerTester;
28 import com.google.common.testing.SerializableTester;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 
38 /**
39  * Tests for {@code EnumHashBiMap}.
40  *
41  * @author Mike Bostock
42  */
43 @GwtCompatible(emulated = true)
44 public class EnumHashBiMapTest extends TestCase {
45   private enum Currency {
46     DOLLAR,
47     FRANC,
48     PESO,
49     POUND,
50     YEN
51   }
52 
53   private enum Country {
54     CANADA,
55     CHILE,
56     JAPAN,
57     SWITZERLAND,
58     UK
59   }
60 
61   public static final class EnumHashBiMapGenerator implements TestBiMapGenerator<Country, String> {
62     @SuppressWarnings("unchecked")
63     @Override
create(Object... entries)64     public BiMap<Country, String> create(Object... entries) {
65       BiMap<Country, String> result = EnumHashBiMap.create(Country.class);
66       for (Object o : entries) {
67         Entry<Country, String> entry = (Entry<Country, String>) o;
68         result.put(entry.getKey(), entry.getValue());
69       }
70       return result;
71     }
72 
73     @Override
samples()74     public SampleElements<Entry<Country, String>> samples() {
75       return new SampleElements<>(
76           Maps.immutableEntry(Country.CANADA, "DOLLAR"),
77           Maps.immutableEntry(Country.CHILE, "PESO"),
78           Maps.immutableEntry(Country.UK, "POUND"),
79           Maps.immutableEntry(Country.JAPAN, "YEN"),
80           Maps.immutableEntry(Country.SWITZERLAND, "FRANC"));
81     }
82 
83     @SuppressWarnings("unchecked")
84     @Override
createArray(int length)85     public Entry<Country, String>[] createArray(int length) {
86       return new Entry[length];
87     }
88 
89     @Override
order(List<Entry<Country, String>> insertionOrder)90     public Iterable<Entry<Country, String>> order(List<Entry<Country, String>> insertionOrder) {
91       return insertionOrder;
92     }
93 
94     @Override
createKeyArray(int length)95     public Country[] createKeyArray(int length) {
96       return new Country[length];
97     }
98 
99     @Override
createValueArray(int length)100     public String[] createValueArray(int length) {
101       return new String[length];
102     }
103   }
104 
105   @GwtIncompatible // suite
suite()106   public static Test suite() {
107     TestSuite suite = new TestSuite();
108     suite.addTest(
109         BiMapTestSuiteBuilder.using(new EnumHashBiMapGenerator())
110             .named("EnumHashBiMap")
111             .withFeatures(
112                 CollectionSize.ANY,
113                 CollectionFeature.SERIALIZABLE,
114                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
115                 MapFeature.ALLOWS_NULL_VALUES,
116                 MapFeature.GENERAL_PURPOSE,
117                 CollectionFeature.KNOWN_ORDER)
118             .createTestSuite());
119     suite.addTestSuite(EnumHashBiMapTest.class);
120     return suite;
121   }
122 
testCreate()123   public void testCreate() {
124     EnumHashBiMap<Currency, String> bimap = EnumHashBiMap.create(Currency.class);
125     assertTrue(bimap.isEmpty());
126     assertEquals("{}", bimap.toString());
127     assertEquals(HashBiMap.create(), bimap);
128     bimap.put(Currency.DOLLAR, "dollar");
129     assertEquals("dollar", bimap.get(Currency.DOLLAR));
130     assertEquals(Currency.DOLLAR, bimap.inverse().get("dollar"));
131   }
132 
testCreateFromMap()133   public void testCreateFromMap() {
134     /* Test with non-empty Map. */
135     Map<Currency, String> map =
136         ImmutableMap.of(
137             Currency.DOLLAR, "dollar",
138             Currency.PESO, "peso",
139             Currency.FRANC, "franc");
140     EnumHashBiMap<Currency, String> bimap = EnumHashBiMap.create(map);
141     assertEquals("dollar", bimap.get(Currency.DOLLAR));
142     assertEquals(Currency.DOLLAR, bimap.inverse().get("dollar"));
143 
144     /* Map must have at least one entry if not an EnumHashBiMap. */
145     try {
146       EnumHashBiMap.create(Collections.<Currency, String>emptyMap());
147       fail("IllegalArgumentException expected");
148     } catch (IllegalArgumentException expected) {
149     }
150 
151     /* Map can be empty if it's an EnumHashBiMap. */
152     Map<Currency, String> emptyBimap = EnumHashBiMap.create(Currency.class);
153     bimap = EnumHashBiMap.create(emptyBimap);
154     assertTrue(bimap.isEmpty());
155 
156     /* Map can be empty if it's an EnumBiMap. */
157     Map<Currency, Country> emptyBimap2 = EnumBiMap.create(Currency.class, Country.class);
158     EnumHashBiMap<Currency, Country> bimap2 = EnumHashBiMap.create(emptyBimap2);
159     assertTrue(bimap2.isEmpty());
160   }
161 
testEnumHashBiMapConstructor()162   public void testEnumHashBiMapConstructor() {
163     /* Test that it copies existing entries. */
164     EnumHashBiMap<Currency, String> bimap1 = EnumHashBiMap.create(Currency.class);
165     bimap1.put(Currency.DOLLAR, "dollar");
166     EnumHashBiMap<Currency, String> bimap2 = EnumHashBiMap.create(bimap1);
167     assertEquals("dollar", bimap2.get(Currency.DOLLAR));
168     assertEquals(bimap1, bimap2);
169     bimap2.inverse().put("franc", Currency.FRANC);
170     assertEquals("franc", bimap2.get(Currency.FRANC));
171     assertNull(bimap1.get(Currency.FRANC));
172     assertFalse(bimap2.equals(bimap1));
173 
174     /* Test that it can be empty. */
175     EnumHashBiMap<Currency, String> emptyBimap = EnumHashBiMap.create(Currency.class);
176     EnumHashBiMap<Currency, String> bimap3 = EnumHashBiMap.create(emptyBimap);
177     assertEquals(bimap3, emptyBimap);
178   }
179 
testEnumBiMapConstructor()180   public void testEnumBiMapConstructor() {
181     /* Test that it copies existing entries. */
182     EnumBiMap<Currency, Country> bimap1 = EnumBiMap.create(Currency.class, Country.class);
183     bimap1.put(Currency.DOLLAR, Country.SWITZERLAND);
184     EnumHashBiMap<Currency, Object> bimap2 = // use supertype
185         EnumHashBiMap.<Currency, Object>create(bimap1);
186     assertEquals(Country.SWITZERLAND, bimap2.get(Currency.DOLLAR));
187     assertEquals(bimap1, bimap2);
188     bimap2.inverse().put("franc", Currency.FRANC);
189     assertEquals("franc", bimap2.get(Currency.FRANC));
190     assertNull(bimap1.get(Currency.FRANC));
191     assertFalse(bimap2.equals(bimap1));
192 
193     /* Test that it can be empty. */
194     EnumBiMap<Currency, Country> emptyBimap = EnumBiMap.create(Currency.class, Country.class);
195     EnumHashBiMap<Currency, Country> bimap3 = // use exact type
196         EnumHashBiMap.create(emptyBimap);
197     assertEquals(bimap3, emptyBimap);
198   }
199 
testKeyType()200   public void testKeyType() {
201     EnumHashBiMap<Currency, String> bimap = EnumHashBiMap.create(Currency.class);
202     assertEquals(Currency.class, bimap.keyType());
203   }
204 
testEntrySet()205   public void testEntrySet() {
206     // Bug 3168290
207     Map<Currency, String> map =
208         ImmutableMap.of(
209             Currency.DOLLAR, "dollar",
210             Currency.PESO, "peso",
211             Currency.FRANC, "franc");
212     EnumHashBiMap<Currency, String> bimap = EnumHashBiMap.create(map);
213 
214     Set<Object> uniqueEntries = Sets.newIdentityHashSet();
215     uniqueEntries.addAll(bimap.entrySet());
216     assertEquals(3, uniqueEntries.size());
217   }
218 
219   @GwtIncompatible // serialize
testSerializable()220   public void testSerializable() {
221     SerializableTester.reserializeAndAssert(EnumHashBiMap.create(Currency.class));
222   }
223 
224   @GwtIncompatible // reflection
testNulls()225   public void testNulls() {
226     new NullPointerTester().testAllPublicStaticMethods(EnumHashBiMap.class);
227     new NullPointerTester().testAllPublicInstanceMethods(EnumHashBiMap.create(Currency.class));
228   }
229 }
230