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 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import java.io.Serializable; 35 import java.util.Arrays; 36 37 /** 38 * Unit test for {@link HashMultiset}. 39 * 40 * @author Kevin Bourrillion 41 * @author Jared Levy 42 */ 43 @GwtCompatible(emulated = true) 44 public class HashMultisetTest extends TestCase { 45 46 @GwtIncompatible("suite") suite()47 public static Test suite() { 48 TestSuite suite = new TestSuite(); 49 suite.addTest(MultisetTestSuiteBuilder.using(hashMultisetGenerator()) 50 .withFeatures(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 protected Multiset<String> create(String[] elements) { 65 return HashMultiset.create(asList(elements)); 66 } 67 }; 68 } 69 70 public void testCreate() { 71 Multiset<String> multiset = HashMultiset.create(); 72 multiset.add("foo", 2); 73 multiset.add("bar"); 74 assertEquals(3, multiset.size()); 75 assertEquals(2, multiset.count("foo")); 76 } 77 78 public void testCreateWithSize() { 79 Multiset<String> multiset = HashMultiset.create(50); 80 multiset.add("foo", 2); 81 multiset.add("bar"); 82 assertEquals(3, multiset.size()); 83 assertEquals(2, multiset.count("foo")); 84 } 85 86 public void testCreateFromIterable() { 87 Multiset<String> multiset 88 = 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 MultisetHolder(Multiset<?> multiset) { 106 this.member = multiset; 107 } 108 private static final long serialVersionUID = 1L; 109 } 110 111 @GwtIncompatible("SerializableTester") 112 public void testSerializationIndirectSelfReference() { 113 Multiset<MultisetHolder> multiset = HashMultiset.create(); 114 MultisetHolder holder = new MultisetHolder(multiset); 115 multiset.add(holder, 2); 116 Multiset<MultisetHolder> copy = SerializableTester.reserialize(multiset); 117 assertEquals(2, copy.size()); 118 assertSame(copy, copy.iterator().next().member); 119 } 120 121 /* 122 * The behavior of toString() and iteration is tested by LinkedHashMultiset, 123 * which shares a lot of code with HashMultiset and has deterministic 124 * iteration order. 125 */ 126 } 127