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