1/*
2 * Copyright (C) 2009 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect.testing.google;
18
19import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22import static java.util.Collections.nCopies;
23
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.collect.testing.features.CollectionFeature;
26import com.google.common.collect.testing.features.CollectionSize;
27
28/**
29 * A generic JUnit test which tests conditional {@code setCount()} operations on
30 * a multiset. Can't be invoked directly; please see
31 * {@link MultisetTestSuiteBuilder}.
32 *
33 * @author Chris Povirk
34 */
35@GwtCompatible
36public class MultisetSetCountConditionallyTester<E> extends
37    AbstractMultisetSetCountTester<E> {
38  @Override void setCountCheckReturnValue(E element, int count) {
39    assertTrue(
40        "setCount() with the correct expected present count should return true",
41        setCount(element, count));
42  }
43
44  @Override void setCountNoCheckReturnValue(E element, int count) {
45    setCount(element, count);
46  }
47
48  private boolean setCount(E element, int count) {
49    return getMultiset().setCount(element, getMultiset().count(element), count);
50  }
51
52  private void assertSetCountNegativeOldCount() {
53    try {
54      getMultiset().setCount(samples.e3, -1, 1);
55      fail("calling setCount() with a negative oldCount should throw "
56          + "IllegalArgumentException");
57    } catch (IllegalArgumentException expected) {
58    }
59  }
60
61  // Negative oldCount.
62
63  @CollectionFeature.Require(SUPPORTS_ADD)
64  public void testSetCountConditional_negativeOldCount_addSupported() {
65    assertSetCountNegativeOldCount();
66  }
67
68  @CollectionFeature.Require(absent = SUPPORTS_ADD)
69  public void testSetCountConditional_negativeOldCount_addUnsupported() {
70    try {
71      assertSetCountNegativeOldCount();
72    } catch (UnsupportedOperationException tolerated) {
73    }
74  }
75
76  // Incorrect expected present count.
77
78  @CollectionFeature.Require(SUPPORTS_ADD)
79  public void testSetCountConditional_oldCountTooLarge() {
80    assertFalse("setCount() with a too-large oldCount should return false",
81        getMultiset().setCount(samples.e0, 2, 3));
82    expectUnchanged();
83  }
84
85  @CollectionSize.Require(absent = ZERO)
86  @CollectionFeature.Require(SUPPORTS_ADD)
87  public void testSetCountConditional_oldCountTooSmallZero() {
88    assertFalse("setCount() with a too-small oldCount should return false",
89        getMultiset().setCount(samples.e0, 0, 2));
90    expectUnchanged();
91  }
92
93  @CollectionSize.Require(SEVERAL)
94  @CollectionFeature.Require(SUPPORTS_ADD)
95  public void testSetCountConditional_oldCountTooSmallNonzero() {
96    initThreeCopies();
97    assertFalse("setCount() with a too-small oldCount should return false",
98        getMultiset().setCount(samples.e0, 1, 5));
99    expectContents(nCopies(3, samples.e0));
100  }
101
102  /*
103   * TODO: test that unmodifiable multisets either throw UOE or return false
104   * when both are valid options. Currently we test the UOE cases and the
105   * return-false cases but not their intersection
106   */
107}
108