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