Home | History | Annotate | Download | only in collect
      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 static org.junit.contrib.truth.Truth.ASSERT;
     20 
     21 import com.google.common.annotations.GwtCompatible;
     22 import com.google.common.annotations.GwtIncompatible;
     23 import com.google.common.testing.SerializableTester;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import java.util.Collections;
     28 import java.util.Map;
     29 import java.util.Set;
     30 
     31 /**
     32  * Tests for {@code EnumBiMap}.
     33  *
     34  * @author Mike Bostock
     35  * @author Jared Levy
     36  */
     37 @GwtCompatible(emulated = true)
     38 public class EnumBiMapTest extends TestCase {
     39   private enum Currency { DOLLAR, FRANC, PESO }
     40   private enum Country { CANADA, CHILE, SWITZERLAND }
     41 
     42   public void testCreate() {
     43     EnumBiMap<Currency, Country> bimap =
     44         EnumBiMap.create(Currency.class, Country.class);
     45     assertTrue(bimap.isEmpty());
     46     assertEquals("{}", bimap.toString());
     47     assertEquals(HashBiMap.create(), bimap);
     48     bimap.put(Currency.DOLLAR, Country.CANADA);
     49     assertEquals(Country.CANADA, bimap.get(Currency.DOLLAR));
     50     assertEquals(Currency.DOLLAR, bimap.inverse().get(Country.CANADA));
     51   }
     52 
     53   public void testCreateFromMap() {
     54     /* Test with non-empty Map. */
     55     Map<Currency, Country> map = ImmutableMap.of(
     56         Currency.DOLLAR, Country.CANADA,
     57         Currency.PESO, Country.CHILE,
     58         Currency.FRANC, Country.SWITZERLAND);
     59     EnumBiMap<Currency, Country> bimap = EnumBiMap.create(map);
     60     assertEquals(Country.CANADA, bimap.get(Currency.DOLLAR));
     61     assertEquals(Currency.DOLLAR, bimap.inverse().get(Country.CANADA));
     62 
     63     /* Map must have at least one entry if not an EnumBiMap. */
     64     try {
     65       EnumBiMap.create(Collections.<Currency, Country>emptyMap());
     66       fail("IllegalArgumentException expected");
     67     } catch (IllegalArgumentException expected) {}
     68     try {
     69       EnumBiMap.create(
     70           EnumHashBiMap.<Currency, Country>create(Currency.class));
     71       fail("IllegalArgumentException expected");
     72     } catch (IllegalArgumentException expected) {}
     73 
     74     /* Map can be empty if it's an EnumBiMap. */
     75     Map<Currency, Country> emptyBimap =
     76         EnumBiMap.create(Currency.class, Country.class);
     77     bimap = EnumBiMap.create(emptyBimap);
     78     assertTrue(bimap.isEmpty());
     79   }
     80 
     81   public void testEnumBiMapConstructor() {
     82     /* Test that it copies existing entries. */
     83     EnumBiMap<Currency, Country> bimap1 =
     84         EnumBiMap.create(Currency.class, Country.class);
     85     bimap1.put(Currency.DOLLAR, Country.CANADA);
     86     EnumBiMap<Currency, Country> bimap2 =
     87         EnumBiMap.create(bimap1);
     88     assertEquals(Country.CANADA, bimap2.get(Currency.DOLLAR));
     89     assertEquals(bimap1, bimap2);
     90     bimap2.inverse().put(Country.SWITZERLAND, Currency.FRANC);
     91     assertEquals(Country.SWITZERLAND, bimap2.get(Currency.FRANC));
     92     assertNull(bimap1.get(Currency.FRANC));
     93     assertFalse(bimap2.equals(bimap1));
     94 
     95     /* Test that it can be empty. */
     96     EnumBiMap<Currency, Country> emptyBimap =
     97         EnumBiMap.create(Currency.class, Country.class);
     98     EnumBiMap<Currency, Country> bimap3 =
     99         EnumBiMap.create(emptyBimap);
    100     assertEquals(bimap3, emptyBimap);
    101   }
    102 
    103   public void testKeyType() {
    104     EnumBiMap<Currency, Country> bimap =
    105         EnumBiMap.create(Currency.class, Country.class);
    106     assertEquals(Currency.class, bimap.keyType());
    107   }
    108 
    109   public void testValueType() {
    110     EnumBiMap<Currency, Country> bimap =
    111         EnumBiMap.create(Currency.class, Country.class);
    112     assertEquals(Country.class, bimap.valueType());
    113   }
    114 
    115   @GwtIncompatible("SerializationTester")
    116   public void testSerialization() {
    117     Map<Currency, Country> map = ImmutableMap.of(
    118         Currency.DOLLAR, Country.CANADA,
    119         Currency.PESO, Country.CHILE,
    120         Currency.FRANC, Country.SWITZERLAND);
    121     EnumBiMap<Currency, Country> bimap = EnumBiMap.create(map);
    122 
    123     BiMap<Currency, Country> copy =
    124         SerializableTester.reserializeAndAssert(bimap);
    125     assertEquals(bimap.inverse(), copy.inverse());
    126   }
    127 
    128   public void testIterationOrder() {
    129     // The enum orderings are alphabetical, leading to the bimap and its inverse
    130     // having inconsistent iteration orderings.
    131     Map<Currency, Country> map = ImmutableMap.of(
    132         Currency.DOLLAR, Country.CANADA,
    133         Currency.PESO, Country.CHILE,
    134         Currency.FRANC, Country.SWITZERLAND);
    135     EnumBiMap<Currency, Country> bimap = EnumBiMap.create(map);
    136 
    137     // forward map ordered by currency
    138     ASSERT.that(bimap.keySet())
    139         .hasContentsInOrder(Currency.DOLLAR, Currency.FRANC, Currency.PESO);
    140     // forward map ordered by currency (even for country values)
    141     ASSERT.that(bimap.values())
    142         .hasContentsInOrder(Country.CANADA, Country.SWITZERLAND, Country.CHILE);
    143     // backward map ordered by country
    144     ASSERT.that(bimap.inverse().keySet())
    145         .hasContentsInOrder(Country.CANADA, Country.CHILE, Country.SWITZERLAND);
    146     // backward map ordered by country (even for currency values)
    147     ASSERT.that(bimap.inverse().values())
    148         .hasContentsInOrder(Currency.DOLLAR, Currency.PESO, Currency.FRANC);
    149   }
    150 
    151   public void testEntrySet() {
    152     Map<Currency, Country> map = ImmutableMap.of(
    153         Currency.DOLLAR, Country.CANADA,
    154         Currency.PESO, Country.CHILE,
    155         Currency.FRANC, Country.SWITZERLAND);
    156     EnumBiMap<Currency, Country> bimap = EnumBiMap.create(map);
    157     Set<Object> uniqueEntries = Sets.newIdentityHashSet();
    158     uniqueEntries.addAll(bimap.entrySet());
    159     assertEquals(3, uniqueEntries.size());
    160   }
    161 
    162   /* Remaining behavior tested by AbstractBiMapTest. */
    163 }
    164