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