View Javadoc
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.collect.testing.AnEnum;
23  import com.google.common.collect.testing.google.TestEnumMultisetGenerator;
24  
25  import junit.framework.TestCase;
26  
27  import java.util.Collection;
28  import java.util.EnumSet;
29  import java.util.Set;
30  
31  /**
32   * Tests for an {@link EnumMultiset}.
33   *
34   * @author Jared Levy
35   */
36  @GwtCompatible(emulated = true)
37  public class EnumMultisetTest extends TestCase {
38  
39    private static TestEnumMultisetGenerator enumMultisetGenerator() {
40      return new TestEnumMultisetGenerator() {
41        @Override protected Multiset<AnEnum> create(AnEnum[] elements) {
42          return (elements.length == 0)
43              ? EnumMultiset.create(AnEnum.class)
44              : EnumMultiset.create(asList(elements));
45        }
46      };
47    }
48  
49    private enum Color {
50      BLUE, RED, YELLOW, GREEN, WHITE
51    }
52  
53    private enum Gender {
54      MALE, FEMALE
55    }
56  
57    public void testClassCreate() {
58      Multiset<Color> ms = EnumMultiset.create(Color.class);
59      ms.add(Color.RED);
60      ms.add(Color.YELLOW);
61      ms.add(Color.RED);
62      assertEquals(0, ms.count(Color.BLUE));
63      assertEquals(1, ms.count(Color.YELLOW));
64      assertEquals(2, ms.count(Color.RED));
65    }
66  
67    public void testCollectionCreate() {
68      Multiset<Color> ms = EnumMultiset.create(
69          asList(Color.RED, Color.YELLOW, Color.RED));
70      assertEquals(0, ms.count(Color.BLUE));
71      assertEquals(1, ms.count(Color.YELLOW));
72      assertEquals(2, ms.count(Color.RED));
73    }
74  
75    public void testIllegalCreate() {
76      Collection<Color> empty = EnumSet.noneOf(Color.class);
77      try {
78        EnumMultiset.create(empty);
79        fail();
80      } catch (IllegalArgumentException expected) {}
81    }
82    
83    public void testCreateEmptyWithClass() {
84      Multiset<Color> ms = EnumMultiset.create(ImmutableList.<Color>of(), Color.class);
85      ms.add(Color.RED);
86    }
87    
88    public void testCreateEmptyWithoutClassFails() {
89      try {
90        EnumMultiset.create(ImmutableList.<Color> of());
91        fail("Expected IllegalArgumentException");
92      } catch (IllegalArgumentException expected) {
93      }
94    }
95  
96    public void testToString() {
97      Multiset<Color> ms = EnumMultiset.create(Color.class);
98      ms.add(Color.BLUE, 3);
99      ms.add(Color.YELLOW, 1);
100     ms.add(Color.RED, 2);
101     assertEquals("[BLUE x 3, RED x 2, YELLOW]", ms.toString());
102   }
103 
104   public void testEntrySet() {
105     Multiset<Color> ms = EnumMultiset.create(Color.class);
106     ms.add(Color.BLUE, 3);
107     ms.add(Color.YELLOW, 1);
108     ms.add(Color.RED, 2);
109 
110     Set<Object> uniqueEntries = Sets.newIdentityHashSet();
111     uniqueEntries.addAll(ms.entrySet());
112     assertEquals(3, uniqueEntries.size());
113   }
114 
115   // Wrapper of EnumMultiset factory methods, because we need to skip create(Class).
116   // create(Enum1.class) is equal to create(Enum2.class) but testEquals() expects otherwise.
117   // For the same reason, we need to skip create(Iterable, Class).
118   private static class EnumMultisetFactory {
119     public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) {
120       return EnumMultiset.create(elements);
121     }
122   }
123 }
124