HashMultisetTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.annotations.GwtIncompatible;
21import com.google.common.testing.SerializableTester;
22
23import java.io.Serializable;
24import java.util.Arrays;
25
26/**
27 * Unit test for {@link HashMultiset}.
28 *
29 * @author Kevin Bourrillion
30 * @author Jared Levy
31 */
32@GwtCompatible(emulated = true)
33public class HashMultisetTest extends AbstractMultisetTest {
34  @Override protected <E> Multiset<E> create() {
35    return HashMultiset.create();
36  }
37
38  public void testCreate() {
39    Multiset<String> multiset = HashMultiset.create();
40    multiset.add("foo", 2);
41    multiset.add("bar");
42    assertEquals(3, multiset.size());
43    assertEquals(2, multiset.count("foo"));
44  }
45
46  public void testCreateWithSize() {
47    Multiset<String> multiset = HashMultiset.create(50);
48    multiset.add("foo", 2);
49    multiset.add("bar");
50    assertEquals(3, multiset.size());
51    assertEquals(2, multiset.count("foo"));
52  }
53
54  public void testCreateFromIterable() {
55    Multiset<String> multiset
56        = HashMultiset.create(Arrays.asList("foo", "bar", "foo"));
57    assertEquals(3, multiset.size());
58    assertEquals(2, multiset.count("foo"));
59  }
60
61  @GwtIncompatible("SerializableTester")
62  public void testSerializationContainingSelf() {
63    Multiset<Multiset<?>> multiset = HashMultiset.create();
64    multiset.add(multiset, 2);
65    Multiset<Multiset<?>> copy = SerializableTester.reserialize(multiset);
66    assertEquals(2, copy.size());
67    assertSame(copy, copy.iterator().next());
68  }
69
70  @GwtIncompatible("Only used by @GwtIncompatible code")
71  private static class MultisetHolder implements Serializable {
72    public Multiset<?> member;
73    MultisetHolder(Multiset<?> multiset) {
74      this.member = multiset;
75    }
76    private static final long serialVersionUID = 1L;
77  }
78
79  @GwtIncompatible("SerializableTester")
80  public void testSerializationIndirectSelfReference() {
81    Multiset<MultisetHolder> multiset = HashMultiset.create();
82    MultisetHolder holder = new MultisetHolder(multiset);
83    multiset.add(holder, 2);
84    Multiset<MultisetHolder> copy = SerializableTester.reserialize(multiset);
85    assertEquals(2, copy.size());
86    assertSame(copy, copy.iterator().next().member);
87  }
88
89  /*
90   * The behavior of toString() and iteration is tested by LinkedHashMultiset,
91   * which shares a lot of code with HashMultiset and has deterministic
92   * iteration order.
93   */
94
95  /**
96   * This test fails with Java 6, preventing us from running
97   * NullPointerTester on multisets.
98  public void testAnnotations() throws Exception {
99    Method method = HashMultiset.class.getDeclaredMethod(
100        "add", Object.class, int.class);
101    assertTrue(method.getParameterAnnotations()[0].length > 0);
102  }
103  */
104
105  @Override
106  @GwtIncompatible(
107      "http://code.google.com/p/google-web-toolkit/issues/detail?id=3421")
108  public void testEntryAfterRemove() {
109    super.testEntryAfterRemove();
110  }
111
112  @Override
113  @GwtIncompatible(
114      "http://code.google.com/p/google-web-toolkit/issues/detail?id=3421")
115  public void testEntryAfterClear() {
116    super.testEntryAfterClear();
117  }
118
119  @Override
120  @GwtIncompatible(
121      "http://code.google.com/p/google-web-toolkit/issues/detail?id=3421")
122  public void testEntryAfterEntrySetClear() {
123    super.testEntryAfterEntrySetClear();
124  }
125
126  @Override
127  @GwtIncompatible(
128      "http://code.google.com/p/google-web-toolkit/issues/detail?id=3421")
129  public void testEntryAfterEntrySetIteratorRemove() {
130    super.testEntryAfterEntrySetIteratorRemove();
131  }
132
133  @Override
134  @GwtIncompatible(
135      "http://code.google.com/p/google-web-toolkit/issues/detail?id=3421")
136  public void testEntryAfterElementSetIteratorRemove() {
137    super.testEntryAfterElementSetIteratorRemove();
138  }
139}
140