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;
023import static org.junit.Assert.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.features.CollectionFeature;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.errorprone.annotations.CanIgnoreReturnValue;
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
037public class MultisetSetCountConditionallyTester<E> extends AbstractMultisetSetCountTester<E> {
038  @Override
039  void setCountCheckReturnValue(E element, int count) {
040    assertTrue(
041        "setCount() with the correct expected present count should return true",
042        setCount(element, count));
043  }
044
045  @Override
046  void setCountNoCheckReturnValue(E element, int count) {
047    setCount(element, count);
048  }
049
050  @CanIgnoreReturnValue
051  private boolean setCount(E element, int count) {
052    return getMultiset().setCount(element, getMultiset().count(element), count);
053  }
054
055  // Negative oldCount.
056
057  @CollectionFeature.Require(SUPPORTS_ADD)
058  public void testSetCountConditional_negativeOldCount_addSupported() {
059    assertThrows(IllegalArgumentException.class, () -> getMultiset().setCount(e3(), -1, 1));
060  }
061
062  @CollectionFeature.Require(absent = SUPPORTS_ADD)
063  public void testSetCountConditional_negativeOldCount_addUnsupported() {
064    RuntimeException expected =
065        assertThrows(RuntimeException.class, () -> getMultiset().setCount(e3(), -1, 1));
066    if (!(expected instanceof IllegalArgumentException
067        || expected instanceof UnsupportedOperationException)) {
068      throw expected;
069    }
070  }
071
072  // Incorrect expected present count.
073
074  @CollectionFeature.Require(SUPPORTS_ADD)
075  public void testSetCountConditional_oldCountTooLarge() {
076    assertFalse(
077        "setCount() with a too-large oldCount should return false",
078        getMultiset().setCount(e0(), 2, 3));
079    expectUnchanged();
080  }
081
082  @CollectionSize.Require(absent = ZERO)
083  @CollectionFeature.Require(SUPPORTS_ADD)
084  public void testSetCountConditional_oldCountTooSmallZero() {
085    assertFalse(
086        "setCount() with a too-small oldCount should return false",
087        getMultiset().setCount(e0(), 0, 2));
088    expectUnchanged();
089  }
090
091  @CollectionSize.Require(SEVERAL)
092  @CollectionFeature.Require(SUPPORTS_ADD)
093  public void testSetCountConditional_oldCountTooSmallNonzero() {
094    initThreeCopies();
095    assertFalse(
096        "setCount() with a too-small oldCount should return false",
097        getMultiset().setCount(e0(), 1, 5));
098    expectContents(nCopies(3, e0()));
099  }
100
101  /*
102   * TODO: test that unmodifiable multisets either throw UOE or return false when both are valid
103   * options. Currently we test the UOE cases and the return-false cases but not their intersection
104   */
105}