• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import org.junit.Ignore;
28 
29 /**
30  * A generic JUnit test which tests conditional {@code setCount()} operations on a multiset. Can't
31  * be invoked directly; please see {@link MultisetTestSuiteBuilder}.
32  *
33  * @author Chris Povirk
34  */
35 @GwtCompatible
36 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
37 public class MultisetSetCountConditionallyTester<E> extends AbstractMultisetSetCountTester<E> {
38   @Override
setCountCheckReturnValue(E element, int count)39   void setCountCheckReturnValue(E element, int count) {
40     assertTrue(
41         "setCount() with the correct expected present count should return true",
42         setCount(element, count));
43   }
44 
45   @Override
setCountNoCheckReturnValue(E element, int count)46   void setCountNoCheckReturnValue(E element, int count) {
47     setCount(element, count);
48   }
49 
setCount(E element, int count)50   private boolean setCount(E element, int count) {
51     return getMultiset().setCount(element, getMultiset().count(element), count);
52   }
53 
assertSetCountNegativeOldCount()54   private void assertSetCountNegativeOldCount() {
55     try {
56       getMultiset().setCount(e3(), -1, 1);
57       fail("calling setCount() with a negative oldCount should throw IllegalArgumentException");
58     } catch (IllegalArgumentException expected) {
59     }
60   }
61 
62   // Negative oldCount.
63 
64   @CollectionFeature.Require(SUPPORTS_ADD)
testSetCountConditional_negativeOldCount_addSupported()65   public void testSetCountConditional_negativeOldCount_addSupported() {
66     assertSetCountNegativeOldCount();
67   }
68 
69   @CollectionFeature.Require(absent = SUPPORTS_ADD)
testSetCountConditional_negativeOldCount_addUnsupported()70   public void testSetCountConditional_negativeOldCount_addUnsupported() {
71     try {
72       assertSetCountNegativeOldCount();
73     } catch (UnsupportedOperationException tolerated) {
74     }
75   }
76 
77   // Incorrect expected present count.
78 
79   @CollectionFeature.Require(SUPPORTS_ADD)
testSetCountConditional_oldCountTooLarge()80   public void testSetCountConditional_oldCountTooLarge() {
81     assertFalse(
82         "setCount() with a too-large oldCount should return false",
83         getMultiset().setCount(e0(), 2, 3));
84     expectUnchanged();
85   }
86 
87   @CollectionSize.Require(absent = ZERO)
88   @CollectionFeature.Require(SUPPORTS_ADD)
testSetCountConditional_oldCountTooSmallZero()89   public void testSetCountConditional_oldCountTooSmallZero() {
90     assertFalse(
91         "setCount() with a too-small oldCount should return false",
92         getMultiset().setCount(e0(), 0, 2));
93     expectUnchanged();
94   }
95 
96   @CollectionSize.Require(SEVERAL)
97   @CollectionFeature.Require(SUPPORTS_ADD)
testSetCountConditional_oldCountTooSmallNonzero()98   public void testSetCountConditional_oldCountTooSmallNonzero() {
99     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 }
112