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.features.CollectionFeature.SUPPORTS_ADD; 021import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.annotations.J2ktIncompatible; 028import com.google.common.collect.testing.Helpers; 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.Collections; 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 // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045public class MultisetElementSetTester<E> extends AbstractMultisetTester<E> { 046 @CollectionFeature.Require(SUPPORTS_ADD) 047 public void testElementSetReflectsAddAbsent() { 048 Set<E> elementSet = getMultiset().elementSet(); 049 assertFalse(elementSet.contains(e3())); 050 getMultiset().add(e3(), 4); 051 assertTrue(elementSet.contains(e3())); 052 } 053 054 @CollectionSize.Require(absent = ZERO) 055 @CollectionFeature.Require(SUPPORTS_REMOVE) 056 public void testElementSetReflectsRemove() { 057 Set<E> elementSet = getMultiset().elementSet(); 058 assertTrue(elementSet.contains(e0())); 059 getMultiset().removeAll(Collections.singleton(e0())); 060 assertFalse(elementSet.contains(e0())); 061 } 062 063 @CollectionSize.Require(absent = ZERO) 064 @CollectionFeature.Require(SUPPORTS_REMOVE) 065 public void testElementSetRemovePropagatesToMultiset() { 066 Set<E> elementSet = getMultiset().elementSet(); 067 int size = getNumElements(); 068 int expectedSize = size - getMultiset().count(e0()); 069 assertTrue(elementSet.remove(e0())); 070 assertFalse(getMultiset().contains(e0())); 071 assertEquals(expectedSize, getMultiset().size()); 072 } 073 074 @CollectionSize.Require(SEVERAL) 075 @CollectionFeature.Require(SUPPORTS_REMOVE) 076 public void testElementSetRemoveDuplicatePropagatesToMultiset() { 077 initThreeCopies(); 078 int size = getNumElements(); 079 int expectedSize = size - getMultiset().count(e0()); 080 Set<E> elementSet = getMultiset().elementSet(); 081 assertTrue(elementSet.remove(e0())); 082 assertEmpty(getMultiset()); 083 assertEquals(expectedSize, getMultiset().size()); 084 } 085 086 @CollectionFeature.Require(SUPPORTS_REMOVE) 087 public void testElementSetRemoveAbsent() { 088 Set<E> elementSet = getMultiset().elementSet(); 089 assertFalse(elementSet.remove(e3())); 090 expectUnchanged(); 091 } 092 093 @CollectionFeature.Require(SUPPORTS_REMOVE) 094 public void testElementSetClear() { 095 getMultiset().elementSet().clear(); 096 assertEmpty(getMultiset()); 097 } 098 099 /** 100 * Returns {@link Method} instances for the read tests that assume multisets support duplicates so 101 * that the test of {@code Multisets.forSet()} can suppress them. 102 */ 103 @J2ktIncompatible 104 @GwtIncompatible // reflection 105 public static List<Method> getElementSetDuplicateInitializingMethods() { 106 return Arrays.asList( 107 Helpers.getMethod( 108 MultisetElementSetTester.class, "testElementSetRemoveDuplicatePropagatesToMultiset")); 109 } 110}