View Javadoc
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  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
20  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  import static java.util.Collections.nCopies;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.features.CollectionFeature;
26  import 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
36  public class MultisetSetCountConditionallyTester<E> extends AbstractMultisetSetCountTester<E> {
37    @Override
38    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
45    void setCountNoCheckReturnValue(E element, int count) {
46      setCount(element, count);
47    }
48  
49    private boolean setCount(E element, int count) {
50      return getMultiset().setCount(element, getMultiset().count(element), count);
51    }
52  
53    private void assertSetCountNegativeOldCount() {
54      try {
55        getMultiset().setCount(e3(), -1, 1);
56        fail("calling setCount() with a negative oldCount should throw 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(
81          "setCount() with a too-large oldCount should return false",
82          getMultiset().setCount(e0(), 2, 3));
83      expectUnchanged();
84    }
85  
86    @CollectionSize.Require(absent = ZERO)
87    @CollectionFeature.Require(SUPPORTS_ADD)
88    public void testSetCountConditional_oldCountTooSmallZero() {
89      assertFalse(
90          "setCount() with a too-small oldCount should return false",
91          getMultiset().setCount(e0(), 0, 2));
92      expectUnchanged();
93    }
94  
95    @CollectionSize.Require(SEVERAL)
96    @CollectionFeature.Require(SUPPORTS_ADD)
97    public void testSetCountConditional_oldCountTooSmallNonzero() {
98      initThreeCopies();
99      assertFalse(
100         "setCount() with a too-small oldCount should return false",
101         getMultiset().setCount(e0(), 1, 5));
102     expectContents(nCopies(3, e0()));
103   }
104 
105   /*
106    * TODO: test that unmodifiable multisets either throw UOE or return false
107    * when both are valid options. Currently we test the UOE cases and the
108    * return-false cases but not their intersection
109    */
110 }