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.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044public class MultisetCountTester<E> extends AbstractMultisetTester<E> {
045
046  public void testCount_0() {
047    assertEquals("multiset.count(missing) didn't return 0", 0, getMultiset().count(e3()));
048  }
049
050  @CollectionSize.Require(absent = ZERO)
051  public void testCount_1() {
052    assertEquals("multiset.count(present) didn't return 1", 1, getMultiset().count(e0()));
053  }
054
055  @CollectionSize.Require(SEVERAL)
056  public void testCount_3() {
057    initThreeCopies();
058    assertEquals("multiset.count(thriceContained) didn't return 3", 3, getMultiset().count(e0()));
059  }
060
061  @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
062  public void testCount_nullAbsent() {
063    assertEquals("multiset.count(null) didn't return 0", 0, getMultiset().count(null));
064  }
065
066  @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
067  public void testCount_null_forbidden() {
068    try {
069      getMultiset().count(null);
070      fail("Expected NullPointerException");
071    } catch (NullPointerException expected) {
072    }
073  }
074
075  @CollectionSize.Require(absent = ZERO)
076  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
077  public void testCount_nullPresent() {
078    initCollectionWithNullElement();
079    assertEquals(1, getMultiset().count(null));
080  }
081
082  public void testCount_wrongType() {
083    assertEquals(
084        "multiset.count(wrongType) didn't return 0", 0, getMultiset().count(WrongType.VALUE));
085  }
086
087  /**
088   * Returns {@link Method} instances for the read tests that assume multisets support duplicates so
089   * that the test of {@code Multisets.forSet()} can suppress them.
090   */
091  @J2ktIncompatible
092  @GwtIncompatible // reflection
093  public static List<Method> getCountDuplicateInitializingMethods() {
094    return Arrays.asList(Helpers.getMethod(MultisetCountTester.class, "testCount_3"));
095  }
096}