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