001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.google;
018
019import static com.google.common.collect.testing.Helpers.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
021import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024import static java.util.Arrays.asList;
025import static org.junit.Assert.assertThrows;
026
027import com.google.common.annotations.GwtCompatible;
028import com.google.common.annotations.GwtIncompatible;
029import com.google.common.annotations.J2ktIncompatible;
030import com.google.common.collect.testing.WrongType;
031import com.google.common.collect.testing.features.CollectionFeature;
032import com.google.common.collect.testing.features.CollectionSize;
033import java.lang.reflect.Method;
034import java.util.List;
035
036/**
037 * Tests for {@code Multiset#count}.
038 *
039 * @author Jared Levy
040 */
041@GwtCompatible
042public class MultisetCountTester<E> extends AbstractMultisetTester<E> {
043
044  public void testCount_0() {
045    assertEquals("multiset.count(missing) didn't return 0", 0, getMultiset().count(e3()));
046  }
047
048  @CollectionSize.Require(absent = ZERO)
049  public void testCount_1() {
050    assertEquals("multiset.count(present) didn't return 1", 1, getMultiset().count(e0()));
051  }
052
053  @CollectionSize.Require(SEVERAL)
054  public void testCount_3() {
055    initThreeCopies();
056    assertEquals("multiset.count(thriceContained) didn't return 3", 3, getMultiset().count(e0()));
057  }
058
059  @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
060  public void testCount_nullAbsent() {
061    assertEquals("multiset.count(null) didn't return 0", 0, getMultiset().count(null));
062  }
063
064  @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
065  public void testCount_null_forbidden() {
066    assertThrows(NullPointerException.class, () -> getMultiset().count(null));
067  }
068
069  @CollectionSize.Require(absent = ZERO)
070  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
071  public void testCount_nullPresent() {
072    initCollectionWithNullElement();
073    assertEquals(1, getMultiset().count(null));
074  }
075
076  public void testCount_wrongType() {
077    assertEquals(
078        "multiset.count(wrongType) didn't return 0", 0, getMultiset().count(WrongType.VALUE));
079  }
080
081  /**
082   * Returns {@link Method} instances for the read tests that assume multisets support duplicates so
083   * that the test of {@code Multisets.forSet()} can suppress them.
084   */
085  @J2ktIncompatible
086  @GwtIncompatible // reflection
087  public static List<Method> getCountDuplicateInitializingMethods() {
088    return asList(getMethod(MultisetCountTester.class, "testCount_3"));
089  }
090}