View Javadoc
1   /*
2    * Copyright (C) 2013 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.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
20  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.annotations.GwtIncompatible;
26  import com.google.common.collect.testing.Helpers;
27  import com.google.common.collect.testing.WrongType;
28  import com.google.common.collect.testing.features.CollectionFeature;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import java.lang.reflect.Method;
31  import java.util.Arrays;
32  import java.util.List;
33  
34  /**
35   * Tests for {@code Multiset#count}.
36   *
37   * @author Jared Levy
38   */
39  @GwtCompatible(emulated = true)
40  public class MultisetCountTester<E> extends AbstractMultisetTester<E> {
41  
42    public void testCount_0() {
43      assertEquals("multiset.count(missing) didn't return 0", 0, getMultiset().count(e3()));
44    }
45  
46    @CollectionSize.Require(absent = ZERO)
47    public void testCount_1() {
48      assertEquals("multiset.count(present) didn't return 1", 1, getMultiset().count(e0()));
49    }
50  
51    @CollectionSize.Require(SEVERAL)
52    public void testCount_3() {
53      initThreeCopies();
54      assertEquals("multiset.count(thriceContained) didn't return 3", 3, getMultiset().count(e0()));
55    }
56  
57    @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
58    public void testCount_nullAbsent() {
59      assertEquals("multiset.count(null) didn't return 0", 0, getMultiset().count(null));
60    }
61  
62    @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
63    public void testCount_null_forbidden() {
64      try {
65        getMultiset().count(null);
66        fail("Expected NullPointerException");
67      } catch (NullPointerException expected) {
68      }
69    }
70  
71    @CollectionSize.Require(absent = ZERO)
72    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
73    public void testCount_nullPresent() {
74      initCollectionWithNullElement();
75      assertEquals(1, getMultiset().count(null));
76    }
77  
78    public void testCount_wrongType() {
79      assertEquals(
80          "multiset.count(wrongType) didn't return 0", 0, getMultiset().count(WrongType.VALUE));
81    }
82  
83    /**
84     * Returns {@link Method} instances for the read tests that assume multisets
85     * support duplicates so that the test of {@code Multisets.forSet()} can
86     * suppress them.
87     */
88    @GwtIncompatible // reflection
89    public static List<Method> getCountDuplicateInitializingMethods() {
90      return Arrays.asList(Helpers.getMethod(MultisetCountTester.class, "testCount_3"));
91    }
92  }