001/*
002 * Copyright (C) 2009 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.SUPPORTS_ADD;
020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static java.util.Collections.nCopies;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.features.CollectionFeature;
026import com.google.common.collect.testing.features.CollectionSize;
027import com.google.errorprone.annotations.CanIgnoreReturnValue;
028import org.junit.Ignore;
029
030/**
031 * A generic JUnit test which tests conditional {@code setCount()} operations on a multiset. Can't
032 * be invoked directly; please see {@link MultisetTestSuiteBuilder}.
033 *
034 * @author Chris Povirk
035 */
036@GwtCompatible
037@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
038public class MultisetSetCountConditionallyTester<E> extends AbstractMultisetSetCountTester<E> {
039  @Override
040  void setCountCheckReturnValue(E element, int count) {
041    assertTrue(
042        "setCount() with the correct expected present count should return true",
043        setCount(element, count));
044  }
045
046  @Override
047  void setCountNoCheckReturnValue(E element, int count) {
048    setCount(element, count);
049  }
050
051  @CanIgnoreReturnValue
052  private boolean setCount(E element, int count) {
053    return getMultiset().setCount(element, getMultiset().count(element), count);
054  }
055
056  private void assertSetCountNegativeOldCount() {
057    try {
058      getMultiset().setCount(e3(), -1, 1);
059      fail("calling setCount() with a negative oldCount should throw IllegalArgumentException");
060    } catch (IllegalArgumentException expected) {
061    }
062  }
063
064  // Negative oldCount.
065
066  @CollectionFeature.Require(SUPPORTS_ADD)
067  public void testSetCountConditional_negativeOldCount_addSupported() {
068    assertSetCountNegativeOldCount();
069  }
070
071  @CollectionFeature.Require(absent = SUPPORTS_ADD)
072  public void testSetCountConditional_negativeOldCount_addUnsupported() {
073    try {
074      assertSetCountNegativeOldCount();
075    } catch (UnsupportedOperationException tolerated) {
076    }
077  }
078
079  // Incorrect expected present count.
080
081  @CollectionFeature.Require(SUPPORTS_ADD)
082  public void testSetCountConditional_oldCountTooLarge() {
083    assertFalse(
084        "setCount() with a too-large oldCount should return false",
085        getMultiset().setCount(e0(), 2, 3));
086    expectUnchanged();
087  }
088
089  @CollectionSize.Require(absent = ZERO)
090  @CollectionFeature.Require(SUPPORTS_ADD)
091  public void testSetCountConditional_oldCountTooSmallZero() {
092    assertFalse(
093        "setCount() with a too-small oldCount should return false",
094        getMultiset().setCount(e0(), 0, 2));
095    expectUnchanged();
096  }
097
098  @CollectionSize.Require(SEVERAL)
099  @CollectionFeature.Require(SUPPORTS_ADD)
100  public void testSetCountConditional_oldCountTooSmallNonzero() {
101    initThreeCopies();
102    assertFalse(
103        "setCount() with a too-small oldCount should return false",
104        getMultiset().setCount(e0(), 1, 5));
105    expectContents(nCopies(3, e0()));
106  }
107
108  /*
109   * TODO: test that unmodifiable multisets either throw UOE or return false
110   * when both are valid options. Currently we test the UOE cases and the
111   * return-false cases but not their intersection
112   */
113}