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.ALLOWS_NULL_VALUES; 020import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 021import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 023import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 024import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 025import static com.google.common.collect.testing.features.CollectionSize.ZERO; 026 027import com.google.common.annotations.GwtCompatible; 028import com.google.common.annotations.GwtIncompatible; 029import com.google.common.collect.Multiset; 030import com.google.common.collect.Multiset.Entry; 031import com.google.common.collect.testing.Helpers; 032import com.google.common.collect.testing.features.CollectionFeature; 033import com.google.common.collect.testing.features.CollectionSize; 034import java.lang.reflect.Method; 035import java.util.Arrays; 036import java.util.ConcurrentModificationException; 037import java.util.Iterator; 038import java.util.List; 039import org.junit.Ignore; 040 041/** 042 * Common superclass for {@link MultisetSetCountUnconditionallyTester} and {@link 043 * MultisetSetCountConditionallyTester}. It is used by those testers to test calls to the 044 * unconditional {@code setCount()} method and calls to the conditional {@code setCount()} method 045 * when the expected present count is correct. 046 * 047 * @author Chris Povirk 048 */ 049@GwtCompatible(emulated = true) 050@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 051public abstract class AbstractMultisetSetCountTester<E> extends AbstractMultisetTester<E> { 052 /* 053 * TODO: consider adding MultisetFeatures.SUPPORTS_SET_COUNT. Currently we 054 * assume that using setCount() to increase the count is permitted iff add() 055 * is permitted and similarly for decrease/remove(). We assume that a 056 * setCount() no-op is permitted if either add() or remove() is permitted, 057 * though we also allow it to "succeed" if neither is permitted. 058 */ 059 060 private void assertSetCount(E element, int count) { 061 setCountCheckReturnValue(element, count); 062 063 assertEquals( 064 "multiset.count() should return the value passed to setCount()", 065 count, 066 getMultiset().count(element)); 067 068 int size = 0; 069 for (Multiset.Entry<E> entry : getMultiset().entrySet()) { 070 size += entry.getCount(); 071 } 072 assertEquals( 073 "multiset.size() should be the sum of the counts of all entries", 074 size, 075 getMultiset().size()); 076 } 077 078 /** Call the {@code setCount()} method under test, and check its return value. */ 079 abstract void setCountCheckReturnValue(E element, int count); 080 081 /** 082 * Call the {@code setCount()} method under test, but do not check its return value. Callers 083 * should use this method over {@link #setCountCheckReturnValue(Object, int)} when they expect 084 * {@code setCount()} to throw an exception, as checking the return value could produce an 085 * incorrect error message like "setCount() should return the original count" instead of the 086 * message passed to a later invocation of {@code fail()}, like "setCount should throw 087 * UnsupportedOperationException." 088 */ 089 abstract void setCountNoCheckReturnValue(E element, int count); 090 091 private void assertSetCountIncreasingFailure(E element, int count) { 092 try { 093 setCountNoCheckReturnValue(element, count); 094 fail("a call to multiset.setCount() to increase an element's count should throw"); 095 } catch (UnsupportedOperationException expected) { 096 } 097 } 098 099 private void assertSetCountDecreasingFailure(E element, int count) { 100 try { 101 setCountNoCheckReturnValue(element, count); 102 fail("a call to multiset.setCount() to decrease an element's count should throw"); 103 } catch (UnsupportedOperationException expected) { 104 } 105 } 106 107 // Unconditional setCount no-ops. 108 109 private void assertZeroToZero() { 110 assertSetCount(e3(), 0); 111 } 112 113 private void assertOneToOne() { 114 assertSetCount(e0(), 1); 115 } 116 117 private void assertThreeToThree() { 118 initThreeCopies(); 119 assertSetCount(e0(), 3); 120 } 121 122 @CollectionFeature.Require(SUPPORTS_ADD) 123 public void testSetCount_zeroToZero_addSupported() { 124 assertZeroToZero(); 125 } 126 127 @CollectionFeature.Require(SUPPORTS_REMOVE) 128 public void testSetCount_zeroToZero_removeSupported() { 129 assertZeroToZero(); 130 } 131 132 @CollectionFeature.Require(absent = {SUPPORTS_ADD, SUPPORTS_REMOVE}) 133 public void testSetCount_zeroToZero_unsupported() { 134 try { 135 assertZeroToZero(); 136 } catch (UnsupportedOperationException tolerated) { 137 } 138 } 139 140 @CollectionSize.Require(absent = ZERO) 141 @CollectionFeature.Require(SUPPORTS_ADD) 142 public void testSetCount_oneToOne_addSupported() { 143 assertOneToOne(); 144 } 145 146 @CollectionSize.Require(absent = ZERO) 147 @CollectionFeature.Require(SUPPORTS_REMOVE) 148 public void testSetCount_oneToOne_removeSupported() { 149 assertOneToOne(); 150 } 151 152 @CollectionSize.Require(absent = ZERO) 153 @CollectionFeature.Require(absent = {SUPPORTS_ADD, SUPPORTS_REMOVE}) 154 public void testSetCount_oneToOne_unsupported() { 155 try { 156 assertOneToOne(); 157 } catch (UnsupportedOperationException tolerated) { 158 } 159 } 160 161 @CollectionSize.Require(SEVERAL) 162 @CollectionFeature.Require(SUPPORTS_ADD) 163 public void testSetCount_threeToThree_addSupported() { 164 assertThreeToThree(); 165 } 166 167 @CollectionSize.Require(SEVERAL) 168 @CollectionFeature.Require(SUPPORTS_REMOVE) 169 public void testSetCount_threeToThree_removeSupported() { 170 assertThreeToThree(); 171 } 172 173 @CollectionSize.Require(SEVERAL) 174 @CollectionFeature.Require(absent = {SUPPORTS_ADD, SUPPORTS_REMOVE}) 175 public void testSetCount_threeToThree_unsupported() { 176 try { 177 assertThreeToThree(); 178 } catch (UnsupportedOperationException tolerated) { 179 } 180 } 181 182 // Unconditional setCount size increases: 183 184 @CollectionFeature.Require(SUPPORTS_ADD) 185 public void testSetCount_zeroToOne_supported() { 186 assertSetCount(e3(), 1); 187 } 188 189 @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 190 public void testSetCountZeroToOneConcurrentWithIteration() { 191 try { 192 Iterator<E> iterator = collection.iterator(); 193 assertSetCount(e3(), 1); 194 iterator.next(); 195 fail("Expected ConcurrentModificationException"); 196 } catch (ConcurrentModificationException expected) { 197 // success 198 } 199 } 200 201 @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 202 public void testSetCountZeroToOneConcurrentWithEntrySetIteration() { 203 try { 204 Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator(); 205 assertSetCount(e3(), 1); 206 iterator.next(); 207 fail("Expected ConcurrentModificationException"); 208 } catch (ConcurrentModificationException expected) { 209 // success 210 } 211 } 212 213 @CollectionFeature.Require(SUPPORTS_ADD) 214 public void testSetCount_zeroToThree_supported() { 215 assertSetCount(e3(), 3); 216 } 217 218 @CollectionSize.Require(absent = ZERO) 219 @CollectionFeature.Require(SUPPORTS_ADD) 220 public void testSetCount_oneToThree_supported() { 221 assertSetCount(e0(), 3); 222 } 223 224 @CollectionFeature.Require(absent = SUPPORTS_ADD) 225 public void testSetCount_zeroToOne_unsupported() { 226 assertSetCountIncreasingFailure(e3(), 1); 227 } 228 229 @CollectionFeature.Require(absent = SUPPORTS_ADD) 230 public void testSetCount_zeroToThree_unsupported() { 231 assertSetCountIncreasingFailure(e3(), 3); 232 } 233 234 @CollectionSize.Require(absent = ZERO) 235 @CollectionFeature.Require(absent = SUPPORTS_ADD) 236 public void testSetCount_oneToThree_unsupported() { 237 assertSetCountIncreasingFailure(e3(), 3); 238 } 239 240 // Unconditional setCount size decreases: 241 242 @CollectionSize.Require(absent = ZERO) 243 @CollectionFeature.Require(SUPPORTS_REMOVE) 244 public void testSetCount_oneToZero_supported() { 245 assertSetCount(e0(), 0); 246 } 247 248 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 249 @CollectionSize.Require(absent = ZERO) 250 public void testSetCountOneToZeroConcurrentWithIteration() { 251 try { 252 Iterator<E> iterator = collection.iterator(); 253 assertSetCount(e0(), 0); 254 iterator.next(); 255 fail("Expected ConcurrentModificationException"); 256 } catch (ConcurrentModificationException expected) { 257 // success 258 } 259 } 260 261 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 262 @CollectionSize.Require(absent = ZERO) 263 public void testSetCountOneToZeroConcurrentWithEntrySetIteration() { 264 try { 265 Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator(); 266 assertSetCount(e0(), 0); 267 iterator.next(); 268 fail("Expected ConcurrentModificationException"); 269 } catch (ConcurrentModificationException expected) { 270 // success 271 } 272 } 273 274 @CollectionSize.Require(SEVERAL) 275 @CollectionFeature.Require(SUPPORTS_REMOVE) 276 public void testSetCount_threeToZero_supported() { 277 initThreeCopies(); 278 assertSetCount(e0(), 0); 279 } 280 281 @CollectionSize.Require(SEVERAL) 282 @CollectionFeature.Require(SUPPORTS_REMOVE) 283 public void testSetCount_threeToOne_supported() { 284 initThreeCopies(); 285 assertSetCount(e0(), 1); 286 } 287 288 @CollectionSize.Require(absent = ZERO) 289 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 290 public void testSetCount_oneToZero_unsupported() { 291 assertSetCountDecreasingFailure(e0(), 0); 292 } 293 294 @CollectionSize.Require(SEVERAL) 295 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 296 public void testSetCount_threeToZero_unsupported() { 297 initThreeCopies(); 298 assertSetCountDecreasingFailure(e0(), 0); 299 } 300 301 @CollectionSize.Require(SEVERAL) 302 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 303 public void testSetCount_threeToOne_unsupported() { 304 initThreeCopies(); 305 assertSetCountDecreasingFailure(e0(), 1); 306 } 307 308 // setCount with nulls: 309 310 @CollectionSize.Require(absent = ZERO) 311 @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) 312 public void testSetCount_removeNull_nullSupported() { 313 initCollectionWithNullElement(); 314 assertSetCount(null, 0); 315 } 316 317 @CollectionFeature.Require( 318 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 319 absent = RESTRICTS_ELEMENTS) 320 public void testSetCount_addNull_nullSupported() { 321 assertSetCount(null, 1); 322 } 323 324 @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES) 325 public void testSetCount_addNull_nullUnsupported() { 326 try { 327 setCountNoCheckReturnValue(null, 1); 328 fail("adding null with setCount() should throw NullPointerException"); 329 } catch (NullPointerException expected) { 330 } 331 } 332 333 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 334 public void testSetCount_noOpNull_nullSupported() { 335 try { 336 assertSetCount(null, 0); 337 } catch (UnsupportedOperationException tolerated) { 338 } 339 } 340 341 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 342 public void testSetCount_noOpNull_nullUnsupported() { 343 try { 344 assertSetCount(null, 0); 345 } catch (NullPointerException | UnsupportedOperationException tolerated) { 346 } 347 } 348 349 @CollectionSize.Require(absent = ZERO) 350 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 351 public void testSetCount_existingNoNopNull_nullSupported() { 352 initCollectionWithNullElement(); 353 try { 354 assertSetCount(null, 1); 355 } catch (UnsupportedOperationException tolerated) { 356 } 357 } 358 359 // Negative count. 360 361 @CollectionFeature.Require(SUPPORTS_REMOVE) 362 public void testSetCount_negative_removeSupported() { 363 try { 364 setCountNoCheckReturnValue(e3(), -1); 365 fail("calling setCount() with a negative count should throw IllegalArgumentException"); 366 } catch (IllegalArgumentException expected) { 367 } 368 } 369 370 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 371 public void testSetCount_negative_removeUnsupported() { 372 try { 373 setCountNoCheckReturnValue(e3(), -1); 374 fail( 375 "calling setCount() with a negative count should throw " 376 + "IllegalArgumentException or UnsupportedOperationException"); 377 } catch (IllegalArgumentException | UnsupportedOperationException expected) { 378 } 379 } 380 381 // TODO: test adding element of wrong type 382 383 /** 384 * Returns {@link Method} instances for the {@code setCount()} tests that assume multisets support 385 * duplicates so that the test of {@code Multisets.forSet()} can suppress them. 386 */ 387 @GwtIncompatible // reflection 388 public static List<Method> getSetCountDuplicateInitializingMethods() { 389 return Arrays.asList( 390 getMethod("testSetCount_threeToThree_removeSupported"), 391 getMethod("testSetCount_threeToZero_supported"), 392 getMethod("testSetCount_threeToOne_supported")); 393 } 394 395 @GwtIncompatible // reflection 396 private static Method getMethod(String methodName) { 397 return Helpers.getMethod(AbstractMultisetSetCountTester.class, methodName); 398 } 399}