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 org.junit.Ignore;
028
029/**
030 * A generic JUnit test which tests conditional {@code setCount()} operations on a multiset. Can't
031 * be invoked directly; please see {@link MultisetTestSuiteBuilder}.
032 *
033 * @author Chris Povirk
034 */
035@GwtCompatible
036@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
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  private boolean setCount(E element, int count) {
051    return getMultiset().setCount(element, getMultiset().count(element), count);
052  }
053
054  private void assertSetCountNegativeOldCount() {
055    try {
056      getMultiset().setCount(e3(), -1, 1);
057      fail("calling setCount() with a negative oldCount should throw IllegalArgumentException");
058    } catch (IllegalArgumentException expected) {
059    }
060  }
061
062  // Negative oldCount.
063
064  @CollectionFeature.Require(SUPPORTS_ADD)
065  public void testSetCountConditional_negativeOldCount_addSupported() {
066    assertSetCountNegativeOldCount();
067  }
068
069  @CollectionFeature.Require(absent = SUPPORTS_ADD)
070  public void testSetCountConditional_negativeOldCount_addUnsupported() {
071    try {
072      assertSetCountNegativeOldCount();
073    } catch (UnsupportedOperationException tolerated) {
074    }
075  }
076
077  // Incorrect expected present count.
078
079  @CollectionFeature.Require(SUPPORTS_ADD)
080  public void testSetCountConditional_oldCountTooLarge() {
081    assertFalse(
082        "setCount() with a too-large oldCount should return false",
083        getMultiset().setCount(e0(), 2, 3));
084    expectUnchanged();
085  }
086
087  @CollectionSize.Require(absent = ZERO)
088  @CollectionFeature.Require(SUPPORTS_ADD)
089  public void testSetCountConditional_oldCountTooSmallZero() {
090    assertFalse(
091        "setCount() with a too-small oldCount should return false",
092        getMultiset().setCount(e0(), 0, 2));
093    expectUnchanged();
094  }
095
096  @CollectionSize.Require(SEVERAL)
097  @CollectionFeature.Require(SUPPORTS_ADD)
098  public void testSetCountConditional_oldCountTooSmallNonzero() {
099    initThreeCopies();
100    assertFalse(
101        "setCount() with a too-small oldCount should return false",
102        getMultiset().setCount(e0(), 1, 5));
103    expectContents(nCopies(3, e0()));
104  }
105
106  /*
107   * TODO: test that unmodifiable multisets either throw UOE or return false
108   * when both are valid options. Currently we test the UOE cases and the
109   * return-false cases but not their intersection
110   */
111}