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.collect.testing.Helpers;
027import com.google.common.collect.testing.WrongType;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030import java.lang.reflect.Method;
031import java.util.Arrays;
032import java.util.List;
033import org.junit.Ignore;
034
035/**
036 * Tests for {@code Multiset#count}.
037 *
038 * @author Jared Levy
039 */
040@GwtCompatible(emulated = true)
041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
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    try {
067      getMultiset().count(null);
068      fail("Expected NullPointerException");
069    } catch (NullPointerException expected) {
070    }
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  @GwtIncompatible // reflection
090  public static List<Method> getCountDuplicateInitializingMethods() {
091    return Arrays.asList(Helpers.getMethod(MultisetCountTester.class, "testCount_3"));
092  }
093}