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