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 java.util.Arrays.asList;
     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.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   private static enum Color {
     39     BLUE, RED, YELLOW, GREEN, WHITE
     40   }
     41 
     42   public void testClassCreate() {
     43     Multiset<Color> ms = EnumMultiset.create(Color.class);
     44     ms.add(Color.RED);
     45     ms.add(Color.YELLOW);
     46     ms.add(Color.RED);
     47     assertEquals(0, ms.count(Color.BLUE));
     48     assertEquals(1, ms.count(Color.YELLOW));
     49     assertEquals(2, ms.count(Color.RED));
     50   }
     51 
     52   public void testCollectionCreate() {
     53     Multiset<Color> ms = EnumMultiset.create(
     54         asList(Color.RED, Color.YELLOW, Color.RED));
     55     assertEquals(0, ms.count(Color.BLUE));
     56     assertEquals(1, ms.count(Color.YELLOW));
     57     assertEquals(2, ms.count(Color.RED));
     58   }
     59 
     60   public void testIllegalCreate() {
     61     Collection<Color> empty = EnumSet.noneOf(Color.class);
     62     try {
     63       EnumMultiset.create(empty);
     64       fail();
     65     } catch (IllegalArgumentException expected) {}
     66   }
     67 
     68   public void testToString() {
     69     Multiset<Color> ms = EnumMultiset.create(Color.class);
     70     ms.add(Color.BLUE, 3);
     71     ms.add(Color.YELLOW, 1);
     72     ms.add(Color.RED, 2);
     73     assertEquals("[BLUE x 3, RED x 2, YELLOW]", ms.toString());
     74   }
     75 
     76   @GwtIncompatible("SerializableTester")
     77   public void testSerializable() {
     78     Multiset<Color> ms = EnumMultiset.create(
     79         asList(Color.RED, Color.YELLOW, Color.RED));
     80     assertEquals(ms, SerializableTester.reserialize(ms));
     81   }
     82 
     83   public void testEntrySet() {
     84     Multiset<Color> ms = EnumMultiset.create(Color.class);
     85     ms.add(Color.BLUE, 3);
     86     ms.add(Color.YELLOW, 1);
     87     ms.add(Color.RED, 2);
     88 
     89     Set<Object> uniqueEntries = Sets.newIdentityHashSet();
     90     uniqueEntries.addAll(ms.entrySet());
     91     assertEquals(3, uniqueEntries.size());
     92   }
     93 }
     94