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 java.util.Arrays.asList; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.testing.AnEnum; 24 import com.google.common.collect.testing.features.CollectionFeature; 25 import com.google.common.collect.testing.features.CollectionSize; 26 import com.google.common.collect.testing.google.MultisetFeature; 27 import com.google.common.collect.testing.google.MultisetTestSuiteBuilder; 28 import com.google.common.collect.testing.google.TestEnumMultisetGenerator; 29 import com.google.common.testing.ClassSanityTester; 30 import com.google.common.testing.NullPointerTester; 31 import com.google.common.testing.SerializableTester; 32 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestSuite; 36 37 import java.util.Collection; 38 import java.util.EnumSet; 39 import java.util.Set; 40 41 /** 42 * Tests for an {@link EnumMultiset}. 43 * 44 * @author Jared Levy 45 */ 46 @GwtCompatible(emulated = true) 47 public class EnumMultisetTest extends TestCase { 48 49 @GwtIncompatible("suite") 50 public static Test suite() { 51 TestSuite suite = new TestSuite(); 52 suite.addTest(MultisetTestSuiteBuilder.using(enumMultisetGenerator()) 53 .withFeatures(CollectionSize.ANY, 54 CollectionFeature.KNOWN_ORDER, 55 CollectionFeature.GENERAL_PURPOSE, 56 CollectionFeature.SUPPORTS_ITERATOR_REMOVE, 57 CollectionFeature.ALLOWS_NULL_QUERIES, 58 MultisetFeature.ENTRIES_ARE_VIEWS) 59 .named("EnumMultiset") 60 .createTestSuite()); 61 suite.addTestSuite(EnumMultisetTest.class); 62 return suite; 63 } 64 65 private static TestEnumMultisetGenerator enumMultisetGenerator() { 66 return new TestEnumMultisetGenerator() { 67 @Override protected Multiset<AnEnum> create(AnEnum[] elements) { 68 return (elements.length == 0) 69 ? EnumMultiset.create(AnEnum.class) 70 : EnumMultiset.create(asList(elements)); 71 } 72 }; 73 } 74 75 private enum Color { 76 BLUE, RED, YELLOW, GREEN, WHITE 77 } 78 79 private enum Gender { 80 MALE, FEMALE 81 } 82 83 public void testClassCreate() { 84 Multiset<Color> ms = EnumMultiset.create(Color.class); 85 ms.add(Color.RED); 86 ms.add(Color.YELLOW); 87 ms.add(Color.RED); 88 assertEquals(0, ms.count(Color.BLUE)); 89 assertEquals(1, ms.count(Color.YELLOW)); 90 assertEquals(2, ms.count(Color.RED)); 91 } 92 93 public void testCollectionCreate() { 94 Multiset<Color> ms = EnumMultiset.create( 95 asList(Color.RED, Color.YELLOW, Color.RED)); 96 assertEquals(0, ms.count(Color.BLUE)); 97 assertEquals(1, ms.count(Color.YELLOW)); 98 assertEquals(2, ms.count(Color.RED)); 99 } 100 101 public void testIllegalCreate() { 102 Collection<Color> empty = EnumSet.noneOf(Color.class); 103 try { 104 EnumMultiset.create(empty); 105 fail(); 106 } catch (IllegalArgumentException expected) {} 107 } 108 109 public void testCreateEmptyWithClass() { 110 Multiset<Color> ms = EnumMultiset.create(ImmutableList.<Color>of(), Color.class); 111 ms.add(Color.RED); 112 } 113 114 public void testCreateEmptyWithoutClassFails() { 115 try { 116 EnumMultiset.create(ImmutableList.<Color> of()); 117 fail("Expected IllegalArgumentException"); 118 } catch (IllegalArgumentException expected) { 119 } 120 } 121 122 public void testToString() { 123 Multiset<Color> ms = EnumMultiset.create(Color.class); 124 ms.add(Color.BLUE, 3); 125 ms.add(Color.YELLOW, 1); 126 ms.add(Color.RED, 2); 127 assertEquals("[BLUE x 3, RED x 2, YELLOW]", ms.toString()); 128 } 129 130 @GwtIncompatible("SerializableTester") 131 public void testSerializable() { 132 Multiset<Color> ms = EnumMultiset.create( 133 asList(Color.RED, Color.YELLOW, Color.RED)); 134 assertEquals(ms, SerializableTester.reserialize(ms)); 135 } 136 137 public void testEntrySet() { 138 Multiset<Color> ms = EnumMultiset.create(Color.class); 139 ms.add(Color.BLUE, 3); 140 ms.add(Color.YELLOW, 1); 141 ms.add(Color.RED, 2); 142 143 Set<Object> uniqueEntries = Sets.newIdentityHashSet(); 144 uniqueEntries.addAll(ms.entrySet()); 145 assertEquals(3, uniqueEntries.size()); 146 } 147 148 // Wrapper of EnumMultiset factory methods, because we need to skip create(Class). 149 // create(Enum1.class) is equal to create(Enum2.class) but testEquals() expects otherwise. 150 // For the same reason, we need to skip create(Iterable, Class). 151 private static class EnumMultisetFactory { 152 public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) { 153 return EnumMultiset.create(elements); 154 } 155 } 156 157 @GwtIncompatible("reflection") 158 public void testEquals() throws Exception { 159 new ClassSanityTester() 160 .setDistinctValues(Class.class, Color.class, Gender.class) 161 .setDistinctValues(Enum.class, Color.BLUE, Color.RED) 162 .forAllPublicStaticMethods(EnumMultisetFactory.class) 163 .testEquals(); 164 } 165 166 @GwtIncompatible("reflection") 167 public void testNulls() throws Exception { 168 new NullPointerTester() 169 .setDefault(Class.class, Color.class) 170 .setDefault(Iterable.class, EnumSet.allOf(Color.class)) 171 .testAllPublicStaticMethods(EnumMultiset.class); 172 } 173 } 174