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.testing.SerializableTester; 22 23 import junit.framework.TestCase; 24 25 import java.util.Collections; 26 import java.util.Map; 27 import java.util.Set; 28 29 /** 30 * Tests for {@code EnumHashBiMap}. 31 * 32 * @author Mike Bostock 33 */ 34 @GwtCompatible(emulated = true) 35 public class EnumHashBiMapTest extends TestCase { 36 private enum Currency { DOLLAR, PESO, FRANC } 37 private enum Country { CANADA, CHILE, SWITZERLAND } 38 39 public void testCreate() { 40 EnumHashBiMap<Currency, String> bimap = 41 EnumHashBiMap.create(Currency.class); 42 assertTrue(bimap.isEmpty()); 43 assertEquals("{}", bimap.toString()); 44 assertEquals(HashBiMap.create(), bimap); 45 bimap.put(Currency.DOLLAR, "dollar"); 46 assertEquals("dollar", bimap.get(Currency.DOLLAR)); 47 assertEquals(Currency.DOLLAR, bimap.inverse().get("dollar")); 48 } 49 50 public void testCreateFromMap() { 51 /* Test with non-empty Map. */ 52 Map<Currency, String> map = ImmutableMap.of( 53 Currency.DOLLAR, "dollar", 54 Currency.PESO, "peso", 55 Currency.FRANC, "franc"); 56 EnumHashBiMap<Currency, String> bimap 57 = EnumHashBiMap.create(map); 58 assertEquals("dollar", bimap.get(Currency.DOLLAR)); 59 assertEquals(Currency.DOLLAR, bimap.inverse().get("dollar")); 60 61 /* Map must have at least one entry if not an EnumHashBiMap. */ 62 try { 63 EnumHashBiMap.create( 64 Collections.<Currency, String>emptyMap()); 65 fail("IllegalArgumentException expected"); 66 } catch (IllegalArgumentException expected) {} 67 68 /* Map can be empty if it's an EnumHashBiMap. */ 69 Map<Currency, String> emptyBimap = EnumHashBiMap.create(Currency.class); 70 bimap = EnumHashBiMap.create(emptyBimap); 71 assertTrue(bimap.isEmpty()); 72 73 /* Map can be empty if it's an EnumBiMap. */ 74 Map<Currency, Country> emptyBimap2 = 75 EnumBiMap.create(Currency.class, Country.class); 76 EnumHashBiMap<Currency, Country> bimap2 77 = EnumHashBiMap.create(emptyBimap2); 78 assertTrue(bimap2.isEmpty()); 79 } 80 81 public void testEnumHashBiMapConstructor() { 82 /* Test that it copies existing entries. */ 83 EnumHashBiMap<Currency, String> bimap1 = 84 EnumHashBiMap.create(Currency.class); 85 bimap1.put(Currency.DOLLAR, "dollar"); 86 EnumHashBiMap<Currency, String> bimap2 = 87 EnumHashBiMap.create(bimap1); 88 assertEquals("dollar", bimap2.get(Currency.DOLLAR)); 89 assertEquals(bimap1, bimap2); 90 bimap2.inverse().put("franc", Currency.FRANC); 91 assertEquals("franc", bimap2.get(Currency.FRANC)); 92 assertNull(bimap1.get(Currency.FRANC)); 93 assertFalse(bimap2.equals(bimap1)); 94 95 /* Test that it can be empty. */ 96 EnumHashBiMap<Currency, String> emptyBimap = 97 EnumHashBiMap.create(Currency.class); 98 EnumHashBiMap<Currency, String> bimap3 = 99 EnumHashBiMap.create(emptyBimap); 100 assertEquals(bimap3, emptyBimap); 101 } 102 103 public void testEnumBiMapConstructor() { 104 /* Test that it copies existing entries. */ 105 EnumBiMap<Currency, Country> bimap1 = 106 EnumBiMap.create(Currency.class, Country.class); 107 bimap1.put(Currency.DOLLAR, Country.SWITZERLAND); 108 EnumHashBiMap<Currency, Object> bimap2 = // use supertype 109 EnumHashBiMap.<Currency, Object>create(bimap1); 110 assertEquals(Country.SWITZERLAND, bimap2.get(Currency.DOLLAR)); 111 assertEquals(bimap1, bimap2); 112 bimap2.inverse().put("franc", Currency.FRANC); 113 assertEquals("franc", bimap2.get(Currency.FRANC)); 114 assertNull(bimap1.get(Currency.FRANC)); 115 assertFalse(bimap2.equals(bimap1)); 116 117 /* Test that it can be empty. */ 118 EnumBiMap<Currency, Country> emptyBimap = 119 EnumBiMap.create(Currency.class, Country.class); 120 EnumHashBiMap<Currency, Country> bimap3 = // use exact type 121 EnumHashBiMap.create(emptyBimap); 122 assertEquals(bimap3, emptyBimap); 123 } 124 125 public void testKeyType() { 126 EnumHashBiMap<Currency, String> bimap = 127 EnumHashBiMap.create(Currency.class); 128 assertEquals(Currency.class, bimap.keyType()); 129 } 130 131 @GwtIncompatible("SerializationTester") 132 public void testSerialization() { 133 Map<Currency, String> map = ImmutableMap.of( 134 Currency.DOLLAR, "dollar", 135 Currency.PESO, "peso", 136 Currency.FRANC, "franc"); 137 EnumHashBiMap<Currency, String> bimap 138 = EnumHashBiMap.create(map); 139 140 BiMap<Currency, String> copy = 141 SerializableTester.reserializeAndAssert(bimap); 142 assertEquals(bimap.inverse(), copy.inverse()); 143 } 144 145 public void testForcePut() { 146 EnumHashBiMap<Currency, String> bimap = 147 EnumHashBiMap.create(Currency.class); 148 bimap.put(Currency.DOLLAR, "dollar"); 149 try { 150 bimap.put(Currency.PESO, "dollar"); 151 } catch (IllegalArgumentException expected) {} 152 bimap.forcePut(Currency.PESO, "dollar"); 153 assertEquals("dollar", bimap.get(Currency.PESO)); 154 assertEquals(Currency.PESO, bimap.inverse().get("dollar")); 155 assertEquals(1, bimap.size()); 156 assertEquals(1, bimap.inverse().size()); 157 } 158 159 public void testEntrySet() { 160 Map<Currency, String> map = ImmutableMap.of( 161 Currency.DOLLAR, "dollar", 162 Currency.PESO, "peso", 163 Currency.FRANC, "franc"); 164 EnumHashBiMap<Currency, String> bimap 165 = EnumHashBiMap.create(map); 166 167 Set<Object> uniqueEntries = Sets.newIdentityHashSet(); 168 uniqueEntries.addAll(bimap.entrySet()); 169 assertEquals(3, uniqueEntries.size()); 170 } 171 /* Remaining behavior tested by AbstractBiMapTest. */ 172 } 173