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 com.google.errorprone.annotations.CanIgnoreReturnValue; 028import org.junit.Ignore; 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 037@Ignore("test runners must not instantiate and run this directly, only via suites we build") 038// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 039@SuppressWarnings("JUnit4ClassUsedInJUnit3") 040public class MultisetSetCountConditionallyTester<E> extends AbstractMultisetSetCountTester<E> { 041 @Override 042 void setCountCheckReturnValue(E element, int count) { 043 assertTrue( 044 "setCount() with the correct expected present count should return true", 045 setCount(element, count)); 046 } 047 048 @Override 049 void setCountNoCheckReturnValue(E element, int count) { 050 setCount(element, count); 051 } 052 053 @CanIgnoreReturnValue 054 private boolean setCount(E element, int count) { 055 return getMultiset().setCount(element, getMultiset().count(element), count); 056 } 057 058 private void assertSetCountNegativeOldCount() { 059 try { 060 getMultiset().setCount(e3(), -1, 1); 061 fail("calling setCount() with a negative oldCount should throw IllegalArgumentException"); 062 } catch (IllegalArgumentException expected) { 063 } 064 } 065 066 // Negative oldCount. 067 068 @CollectionFeature.Require(SUPPORTS_ADD) 069 public void testSetCountConditional_negativeOldCount_addSupported() { 070 assertSetCountNegativeOldCount(); 071 } 072 073 @CollectionFeature.Require(absent = SUPPORTS_ADD) 074 public void testSetCountConditional_negativeOldCount_addUnsupported() { 075 try { 076 assertSetCountNegativeOldCount(); 077 } catch (UnsupportedOperationException tolerated) { 078 } 079 } 080 081 // Incorrect expected present count. 082 083 @CollectionFeature.Require(SUPPORTS_ADD) 084 public void testSetCountConditional_oldCountTooLarge() { 085 assertFalse( 086 "setCount() with a too-large oldCount should return false", 087 getMultiset().setCount(e0(), 2, 3)); 088 expectUnchanged(); 089 } 090 091 @CollectionSize.Require(absent = ZERO) 092 @CollectionFeature.Require(SUPPORTS_ADD) 093 public void testSetCountConditional_oldCountTooSmallZero() { 094 assertFalse( 095 "setCount() with a too-small oldCount should return false", 096 getMultiset().setCount(e0(), 0, 2)); 097 expectUnchanged(); 098 } 099 100 @CollectionSize.Require(SEVERAL) 101 @CollectionFeature.Require(SUPPORTS_ADD) 102 public void testSetCountConditional_oldCountTooSmallNonzero() { 103 initThreeCopies(); 104 assertFalse( 105 "setCount() with a too-small oldCount should return false", 106 getMultiset().setCount(e0(), 1, 5)); 107 expectContents(nCopies(3, e0())); 108 } 109 110 /* 111 * TODO: test that unmodifiable multisets either throw UOE or return false 112 * when both are valid options. Currently we test the UOE cases and the 113 * return-false cases but not their intersection 114 */ 115}