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