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