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