1 /* 2 * Copyright (C) 2012 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 com.google.common.base.Preconditions.checkState; 20 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES; 21 import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE; 22 import static com.google.common.truth.Truth.assertThat; 23 24 import com.google.common.annotations.GwtCompatible; 25 import com.google.common.annotations.GwtIncompatible; 26 import com.google.common.base.Function; 27 import com.google.common.collect.testing.AnEnum; 28 import com.google.common.collect.testing.Helpers; 29 import com.google.common.collect.testing.MapTestSuiteBuilder; 30 import com.google.common.collect.testing.TestEnumMapGenerator; 31 import com.google.common.collect.testing.features.CollectionSize; 32 import java.util.Map; 33 import java.util.Map.Entry; 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 /** 39 * Tests for {@code ImmutableEnumMap}. 40 * 41 * @author Louis Wasserman 42 */ 43 @GwtCompatible(emulated = true) 44 public class ImmutableEnumMapTest extends TestCase { 45 public static class ImmutableEnumMapGenerator extends TestEnumMapGenerator { 46 @Override create(Entry<AnEnum, String>[] entries)47 protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) { 48 Map<AnEnum, String> map = Maps.newHashMap(); 49 for (Entry<AnEnum, String> entry : entries) { 50 map.put(entry.getKey(), entry.getValue()); 51 } 52 return Maps.immutableEnumMap(map); 53 } 54 } 55 56 @GwtIncompatible // suite suite()57 public static Test suite() { 58 TestSuite suite = new TestSuite(); 59 suite.addTest( 60 MapTestSuiteBuilder.using(new ImmutableEnumMapGenerator()) 61 .named("Maps.immutableEnumMap") 62 .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES) 63 .createTestSuite()); 64 suite.addTestSuite(ImmutableEnumMapTest.class); 65 return suite; 66 } 67 testIteratesOnce()68 public void testIteratesOnce() { 69 Map<AnEnum, AnEnum> map = 70 Maps.asMap( 71 ImmutableSet.of(AnEnum.A), 72 new Function<AnEnum, AnEnum>() { 73 boolean used = false; 74 75 @Override 76 public AnEnum apply(AnEnum ae) { 77 checkState(!used, "should not be applied more than once"); 78 used = true; 79 return ae; 80 } 81 }); 82 ImmutableMap<AnEnum, AnEnum> copy = Maps.immutableEnumMap(map); 83 assertThat(copy.entrySet()).containsExactly(Helpers.mapEntry(AnEnum.A, AnEnum.A)); 84 } 85 testEmptyImmutableEnumMap()86 public void testEmptyImmutableEnumMap() { 87 ImmutableMap<AnEnum, String> map = Maps.immutableEnumMap(ImmutableMap.<AnEnum, String>of()); 88 assertEquals(ImmutableMap.of(), map); 89 } 90 testImmutableEnumMapOrdering()91 public void testImmutableEnumMapOrdering() { 92 ImmutableMap<AnEnum, String> map = 93 Maps.immutableEnumMap(ImmutableMap.of(AnEnum.C, "c", AnEnum.A, "a", AnEnum.E, "e")); 94 95 assertThat(map.entrySet()) 96 .containsExactly( 97 Helpers.mapEntry(AnEnum.A, "a"), 98 Helpers.mapEntry(AnEnum.C, "c"), 99 Helpers.mapEntry(AnEnum.E, "e")) 100 .inOrder(); 101 } 102 } 103