• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.features.CollectionFeature;
24 import com.google.common.collect.testing.features.CollectionSize;
25 import com.google.common.collect.testing.google.MultisetFeature;
26 import com.google.common.collect.testing.google.MultisetTestSuiteBuilder;
27 import com.google.common.collect.testing.google.TestStringMultisetGenerator;
28 import com.google.common.testing.SerializableTester;
29 import java.io.Serializable;
30 import java.util.Arrays;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 
35 /**
36  * Unit test for {@link HashMultiset}.
37  *
38  * @author Kevin Bourrillion
39  * @author Jared Levy
40  */
41 @GwtCompatible(emulated = true)
42 public class HashMultisetTest extends TestCase {
43 
44   @GwtIncompatible // suite
suite()45   public static Test suite() {
46     TestSuite suite = new TestSuite();
47     suite.addTest(
48         MultisetTestSuiteBuilder.using(hashMultisetGenerator())
49             .withFeatures(
50                 CollectionSize.ANY,
51                 CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
52                 CollectionFeature.ALLOWS_NULL_VALUES,
53                 CollectionFeature.SERIALIZABLE,
54                 CollectionFeature.GENERAL_PURPOSE,
55                 MultisetFeature.ENTRIES_ARE_VIEWS)
56             .named("HashMultiset")
57             .createTestSuite());
58     suite.addTestSuite(HashMultisetTest.class);
59     return suite;
60   }
61 
hashMultisetGenerator()62   private static TestStringMultisetGenerator hashMultisetGenerator() {
63     return new TestStringMultisetGenerator() {
64       @Override
65       protected Multiset<String> create(String[] elements) {
66         return HashMultiset.create(asList(elements));
67       }
68     };
69   }
70 
71   public void testCreate() {
72     Multiset<String> multiset = HashMultiset.create();
73     multiset.add("foo", 2);
74     multiset.add("bar");
75     assertEquals(3, multiset.size());
76     assertEquals(2, multiset.count("foo"));
77   }
78 
79   public void testCreateWithSize() {
80     Multiset<String> multiset = HashMultiset.create(50);
81     multiset.add("foo", 2);
82     multiset.add("bar");
83     assertEquals(3, multiset.size());
84     assertEquals(2, multiset.count("foo"));
85   }
86 
87   public void testCreateFromIterable() {
88     Multiset<String> multiset = HashMultiset.create(Arrays.asList("foo", "bar", "foo"));
89     assertEquals(3, multiset.size());
90     assertEquals(2, multiset.count("foo"));
91   }
92 
93   @GwtIncompatible // SerializableTester
94   public void testSerializationContainingSelf() {
95     Multiset<Multiset<?>> multiset = HashMultiset.create();
96     multiset.add(multiset, 2);
97     Multiset<Multiset<?>> copy = SerializableTester.reserialize(multiset);
98     assertEquals(2, copy.size());
99     assertSame(copy, copy.iterator().next());
100   }
101 
102   @GwtIncompatible // Only used by @GwtIncompatible code
103   private static class MultisetHolder implements Serializable {
104     public Multiset<?> member;
105 
106     MultisetHolder(Multiset<?> multiset) {
107       this.member = multiset;
108     }
109 
110     private static final long serialVersionUID = 1L;
111   }
112 
113   @GwtIncompatible // SerializableTester
114   public void testSerializationIndirectSelfReference() {
115     Multiset<MultisetHolder> multiset = HashMultiset.create();
116     MultisetHolder holder = new MultisetHolder(multiset);
117     multiset.add(holder, 2);
118     Multiset<MultisetHolder> copy = SerializableTester.reserialize(multiset);
119     assertEquals(2, copy.size());
120     assertSame(copy, copy.iterator().next().member);
121   }
122 
123   /*
124    * The behavior of toString() and iteration is tested by LinkedHashMultiset,
125    * which shares a lot of code with HashMultiset and has deterministic
126    * iteration order.
127    */
128 }
129