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 com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
025import static java.util.Arrays.asList;
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;
035import org.junit.Ignore;
036
037/**
038 * Tests for {@code Multiset#count}.
039 *
040 * @author Jared Levy
041 */
042@GwtCompatible(emulated = true)
043@Ignore("test runners must not instantiate and run this directly, only via suites we build")
044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045@SuppressWarnings("JUnit4ClassUsedInJUnit3")
046public class MultisetCountTester<E> extends AbstractMultisetTester<E> {
047
048  public void testCount_0() {
049    assertEquals("multiset.count(missing) didn't return 0", 0, getMultiset().count(e3()));
050  }
051
052  @CollectionSize.Require(absent = ZERO)
053  public void testCount_1() {
054    assertEquals("multiset.count(present) didn't return 1", 1, getMultiset().count(e0()));
055  }
056
057  @CollectionSize.Require(SEVERAL)
058  public void testCount_3() {
059    initThreeCopies();
060    assertEquals("multiset.count(thriceContained) didn't return 3", 3, getMultiset().count(e0()));
061  }
062
063  @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
064  public void testCount_nullAbsent() {
065    assertEquals("multiset.count(null) didn't return 0", 0, getMultiset().count(null));
066  }
067
068  @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
069  public void testCount_null_forbidden() {
070    assertThrows(NullPointerException.class, () -> getMultiset().count(null));
071  }
072
073  @CollectionSize.Require(absent = ZERO)
074  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
075  public void testCount_nullPresent() {
076    initCollectionWithNullElement();
077    assertEquals(1, getMultiset().count(null));
078  }
079
080  public void testCount_wrongType() {
081    assertEquals(
082        "multiset.count(wrongType) didn't return 0", 0, getMultiset().count(WrongType.VALUE));
083  }
084
085  /**
086   * Returns {@link Method} instances for the read tests that assume multisets support duplicates so
087   * that the test of {@code Multisets.forSet()} can suppress them.
088   */
089  @J2ktIncompatible
090  @GwtIncompatible // reflection
091  public static List<Method> getCountDuplicateInitializingMethods() {
092    return asList(getMethod(MultisetCountTester.class, "testCount_3"));
093  }
094}