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