001/*
002 * Copyright (C) 2016 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.assertEqualIgnoringOrder;
020import static com.google.common.collect.testing.Helpers.getMethod;
021import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
022import static java.util.Arrays.asList;
023import static java.util.Collections.singletonList;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.common.collect.Multiset.Entry;
029import com.google.common.collect.Multisets;
030import com.google.common.collect.testing.features.CollectionFeature;
031import java.lang.reflect.Method;
032import java.util.ArrayList;
033import java.util.List;
034import org.junit.Ignore;
035
036/**
037 * Tests for {@code Multiset#forEachEntry}.
038 *
039 * @author Louis Wasserman
040 */
041@GwtCompatible(emulated = true)
042@Ignore("test runners must not instantiate and run this directly, only via suites we build")
043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
044@SuppressWarnings("JUnit4ClassUsedInJUnit3")
045public class MultisetForEachEntryTester<E> extends AbstractMultisetTester<E> {
046  public void testForEachEntry() {
047    List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
048    List<Entry<E>> actual = new ArrayList<>();
049    getMultiset()
050        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
051    assertEqualIgnoringOrder(expected, actual);
052  }
053
054  @CollectionFeature.Require(KNOWN_ORDER)
055  public void testForEachEntryOrdered() {
056    List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet());
057    List<Entry<E>> actual = new ArrayList<>();
058    getMultiset()
059        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
060    assertEquals(expected, actual);
061  }
062
063  public void testForEachEntryDuplicates() {
064    initThreeCopies();
065    List<Entry<E>> expected = singletonList(Multisets.immutableEntry(e0(), 3));
066    List<Entry<E>> actual = new ArrayList<>();
067    getMultiset()
068        .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count)));
069    assertEquals(expected, actual);
070  }
071
072  /**
073   * Returns {@link Method} instances for the remove tests that assume multisets support duplicates
074   * so that the test of {@code Multisets.forSet()} can suppress them.
075   */
076  @J2ktIncompatible
077  @GwtIncompatible // reflection
078  public static List<Method> getForEachEntryDuplicateInitializingMethods() {
079    return asList(getMethod(MultisetForEachEntryTester.class, "testForEachEntryDuplicates"));
080  }
081}