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.collect.testing.WrongType;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import com.google.common.collect.testing.features.CollectionSize;
28  
29  /**
30   * Tests for {@code Multiset#count}.
31   *
32   * @author Jared Levy
33   */
34  @GwtCompatible(emulated = true)
35  public class MultisetCountTester<E> extends AbstractMultisetTester<E> {
36  
37    public void testCount_0() {
38      assertEquals("multiset.count(missing) didn't return 0",
39          0, getMultiset().count(samples.e3));
40    }
41  
42    @CollectionSize.Require(absent = ZERO)
43    public void testCount_1() {
44      assertEquals("multiset.count(present) didn't return 1",
45          1, getMultiset().count(samples.e0));
46    }
47  
48    @CollectionSize.Require(SEVERAL)
49    public void testCount_3() {
50      initThreeCopies();
51      assertEquals("multiset.count(thriceContained) didn't return 3",
52          3, getMultiset().count(samples.e0));
53    }
54  
55    @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
56    public void testCount_nullAbsent() {
57      assertEquals("multiset.count(null) didn't return 0",
58          0, getMultiset().count(null));
59    }
60  
61    @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
62    public void testCount_null_forbidden() {
63      try {
64        getMultiset().count(null);
65        fail("Expected NullPointerException");
66      } catch (NullPointerException expected) {}
67    }
68    
69    @CollectionSize.Require(absent = ZERO)
70    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
71    public void testCount_nullPresent() {
72      initCollectionWithNullElement();
73      assertEquals(1, getMultiset().count(null));
74    }
75  
76    public void testCount_wrongType() {
77      assertEquals("multiset.count(wrongType) didn't return 0",
78          0, getMultiset().count(WrongType.VALUE));
79    }
80  }
81