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.Helpers.assertEmpty; 020import static com.google.common.collect.testing.Helpers.getMethod; 021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 023import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 024import static com.google.common.collect.testing.features.CollectionSize.ZERO; 025import static java.util.Arrays.asList; 026import static java.util.Collections.singleton; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.annotations.GwtIncompatible; 030import com.google.common.annotations.J2ktIncompatible; 031import com.google.common.collect.testing.features.CollectionFeature; 032import com.google.common.collect.testing.features.CollectionSize; 033import java.lang.reflect.Method; 034import java.util.List; 035import java.util.Set; 036import org.junit.Ignore; 037 038/** 039 * Tests for {@code Multiset.elementSet()} not covered by the derived {@code SetTestSuiteBuilder}. 040 * 041 * @author Louis Wasserman 042 */ 043@GwtCompatible 044@Ignore("test runners must not instantiate and run this directly, only via suites we build") 045// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 046@SuppressWarnings("JUnit4ClassUsedInJUnit3") 047public class MultisetElementSetTester<E> extends AbstractMultisetTester<E> { 048 @CollectionFeature.Require(SUPPORTS_ADD) 049 public void testElementSetReflectsAddAbsent() { 050 Set<E> elementSet = getMultiset().elementSet(); 051 assertFalse(elementSet.contains(e3())); 052 getMultiset().add(e3(), 4); 053 assertTrue(elementSet.contains(e3())); 054 } 055 056 @CollectionSize.Require(absent = ZERO) 057 @CollectionFeature.Require(SUPPORTS_REMOVE) 058 public void testElementSetReflectsRemove() { 059 Set<E> elementSet = getMultiset().elementSet(); 060 assertTrue(elementSet.contains(e0())); 061 getMultiset().removeAll(singleton(e0())); 062 assertFalse(elementSet.contains(e0())); 063 } 064 065 @CollectionSize.Require(absent = ZERO) 066 @CollectionFeature.Require(SUPPORTS_REMOVE) 067 public void testElementSetRemovePropagatesToMultiset() { 068 Set<E> elementSet = getMultiset().elementSet(); 069 int size = getNumElements(); 070 int expectedSize = size - getMultiset().count(e0()); 071 assertTrue(elementSet.remove(e0())); 072 assertFalse(getMultiset().contains(e0())); 073 assertEquals(expectedSize, getMultiset().size()); 074 } 075 076 @CollectionSize.Require(SEVERAL) 077 @CollectionFeature.Require(SUPPORTS_REMOVE) 078 public void testElementSetRemoveDuplicatePropagatesToMultiset() { 079 initThreeCopies(); 080 int size = getNumElements(); 081 int expectedSize = size - getMultiset().count(e0()); 082 Set<E> elementSet = getMultiset().elementSet(); 083 assertTrue(elementSet.remove(e0())); 084 assertEmpty(getMultiset()); 085 assertEquals(expectedSize, getMultiset().size()); 086 } 087 088 @CollectionFeature.Require(SUPPORTS_REMOVE) 089 public void testElementSetRemoveAbsent() { 090 Set<E> elementSet = getMultiset().elementSet(); 091 assertFalse(elementSet.remove(e3())); 092 expectUnchanged(); 093 } 094 095 @CollectionFeature.Require(SUPPORTS_REMOVE) 096 public void testElementSetClear() { 097 getMultiset().elementSet().clear(); 098 assertEmpty(getMultiset()); 099 } 100 101 /** 102 * Returns {@link Method} instances for the read tests that assume multisets support duplicates so 103 * that the test of {@code Multisets.forSet()} can suppress them. 104 */ 105 @J2ktIncompatible 106 @GwtIncompatible // reflection 107 public static List<Method> getElementSetDuplicateInitializingMethods() { 108 return asList( 109 getMethod( 110 MultisetElementSetTester.class, "testElementSetRemoveDuplicatePropagatesToMultiset")); 111 } 112}