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