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