Coverage Summary for Class: AbstractMultisetSetCountTester (com.google.common.collect.testing.google)

Class Class, % Method, % Line, %
AbstractMultisetSetCountTester 0% (0/1) 0% (0/42) 0% (0/146)


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.ALLOWS_NULL_VALUES; 20 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 21 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 23 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 24 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 25 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 26  27 import com.google.common.annotations.GwtCompatible; 28 import com.google.common.annotations.GwtIncompatible; 29 import com.google.common.collect.Multiset; 30 import com.google.common.collect.Multiset.Entry; 31 import com.google.common.collect.testing.Helpers; 32 import com.google.common.collect.testing.features.CollectionFeature; 33 import com.google.common.collect.testing.features.CollectionSize; 34 import java.lang.reflect.Method; 35 import java.util.Arrays; 36 import java.util.ConcurrentModificationException; 37 import java.util.Iterator; 38 import java.util.List; 39 import org.junit.Ignore; 40  41 /** 42  * Common superclass for {@link MultisetSetCountUnconditionallyTester} and {@link 43  * MultisetSetCountConditionallyTester}. It is used by those testers to test calls to the 44  * unconditional {@code setCount()} method and calls to the conditional {@code setCount()} method 45  * when the expected present count is correct. 46  * 47  * @author Chris Povirk 48  */ 49 @GwtCompatible(emulated = true) 50 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 51 public abstract class AbstractMultisetSetCountTester<E> extends AbstractMultisetTester<E> { 52  /* 53  * TODO: consider adding MultisetFeatures.SUPPORTS_SET_COUNT. Currently we 54  * assume that using setCount() to increase the count is permitted iff add() 55  * is permitted and similarly for decrease/remove(). We assume that a 56  * setCount() no-op is permitted if either add() or remove() is permitted, 57  * though we also allow it to "succeed" if neither is permitted. 58  */ 59  60  private void assertSetCount(E element, int count) { 61  setCountCheckReturnValue(element, count); 62  63  assertEquals( 64  "multiset.count() should return the value passed to setCount()", 65  count, 66  getMultiset().count(element)); 67  68  int size = 0; 69  for (Multiset.Entry<E> entry : getMultiset().entrySet()) { 70  size += entry.getCount(); 71  } 72  assertEquals( 73  "multiset.size() should be the sum of the counts of all entries", 74  size, 75  getMultiset().size()); 76  } 77  78  /** Call the {@code setCount()} method under test, and check its return value. */ 79  abstract void setCountCheckReturnValue(E element, int count); 80  81  /** 82  * Call the {@code setCount()} method under test, but do not check its return value. Callers 83  * should use this method over {@link #setCountCheckReturnValue(Object, int)} when they expect 84  * {@code setCount()} to throw an exception, as checking the return value could produce an 85  * incorrect error message like "setCount() should return the original count" instead of the 86  * message passed to a later invocation of {@code fail()}, like "setCount should throw 87  * UnsupportedOperationException." 88  */ 89  abstract void setCountNoCheckReturnValue(E element, int count); 90  91  private void assertSetCountIncreasingFailure(E element, int count) { 92  try { 93  setCountNoCheckReturnValue(element, count); 94  fail("a call to multiset.setCount() to increase an element's count should throw"); 95  } catch (UnsupportedOperationException expected) { 96  } 97  } 98  99  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 }