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