Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect.testing.google;
 
 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
 
 
 import java.util.List;

Common superclass for MultisetSetCountUnconditionallyTester and MultisetSetCountConditionallyTester. It is used by those testers to test calls to the unconditional setCount() method and calls to the conditional setCount() method when the expected present count is correct.

Author(s):
Chris Povirk
 
 @GwtCompatible(emulated = true)
 public abstract class AbstractMultisetSetCountTester<E>
     extends AbstractMultisetTester<E> {
   /*
    * TODO: consider adding MultisetFeatures.SUPPORTS_SET_COUNT. Currently we
    * assume that using setCount() to increase the count is permitted iff add()
    * is permitted and similarly for decrease/remove(). We assume that a
    * setCount() no-op is permitted if either add() or remove() is permitted,
    * though we also allow it to "succeed" if neither is permitted.
    */
 
   private void assertSetCount(E elementint count) {
     setCountCheckReturnValue(elementcount);
 
     assertEquals(
         "multiset.count() should return the value passed to setCount()",
         countgetMultiset().count(element));
 
     int size = 0;
     for (Multiset.Entry<E> entry : getMultiset().entrySet()) {
       size += entry.getCount();
     }
     assertEquals(
         "multiset.size() should be the sum of the counts of all entries",
         sizegetMultiset().size());
   }

  
Call the setCount() method under test, and check its return value.
 
   abstract void setCountCheckReturnValue(E elementint count);

  
Call the setCount() method under test, but do not check its return value. Callers should use this method over setCountCheckReturnValue(java.lang.Object,int) when they expect setCount() to throw an exception, as checking the return value could produce an incorrect error message like "setCount() should return the original count" instead of the message passed to a later invocation of fail(), like "setCount should throw UnsupportedOperationException."
 
   abstract void setCountNoCheckReturnValue(E elementint count);
 
   private void assertSetCountIncreasingFailure(E elementint count) {
     try {
       setCountNoCheckReturnValue(elementcount);
       fail("a call to multiset.setCount() to increase an element's count "
           + "should throw");
     } catch (UnsupportedOperationException expected) {
    }
  }
  private void assertSetCountDecreasingFailure(E elementint count) {
    try {
      setCountNoCheckReturnValue(elementcount);
      fail("a call to multiset.setCount() to decrease an element's count "
          + "should throw");
    } catch (UnsupportedOperationException expected) {
    }
  }
  // Unconditional setCount no-ops.
  private void assertZeroToZero() {
  }
  private void assertOneToOne() {
  }
  private void assertThreeToThree() {
  }
  }
  }
    try {
      assertZeroToZero();
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  @CollectionSize.Require(absent = )
  }
  @CollectionSize.Require(absent = )
  }
  @CollectionSize.Require(absent = )
    try {
      assertOneToOne();
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  }
  }
    try {
      assertThreeToThree();
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  // Unconditional setCount size increases:
  }
    try {
      Iterator<E> iterator = .iterator();
      assertSetCount(., 1);
      iterator.next();
      fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException expected) {
      // success
    }
  }
    try {
      Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
      assertSetCount(., 1);
      iterator.next();
      fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException expected) {
      // success
    }
  }
  }
  @CollectionSize.Require(absent = )
  }
  }
  }
  @CollectionSize.Require(absent = )
  }
  // Unconditional setCount size decreases:
  @CollectionSize.Require(absent = )
  }
  @CollectionSize.Require(absent = )
    try {
      Iterator<E> iterator = .iterator();
      assertSetCount(., 0);
      iterator.next();
      fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException expected) {
      // success
    }
  }
  @CollectionSize.Require(absent = )
    try {
      Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
      assertSetCount(., 0);
      iterator.next();
      fail("Expected ConcurrentModificationException");
    } catch (ConcurrentModificationException expected) {
      // success
    }
  }
  }
  }
  @CollectionSize.Require(absent = )
  }
  }
  }
  // setCount with nulls:
  @CollectionSize.Require(absent = )
    assertSetCount(null, 0);
  }
      absent = )
    assertSetCount(null, 1);
  }
    try {
      setCountNoCheckReturnValue(null, 1);
      fail("adding null with setCount() should throw NullPointerException");
    } catch (NullPointerException expected) {
    }
  }
    try {
      assertSetCount(null, 0);
    } catch (UnsupportedOperationException tolerated) {
    }
  }
    try {
      assertSetCount(null, 0);
    } catch (NullPointerException tolerated) {
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  @CollectionSize.Require(absent = )
    try {
      assertSetCount(null, 1);
    } catch (UnsupportedOperationException tolerated) {
    }
  }
  // Negative count.
    try {
      fail("calling setCount() with a negative count should throw "
          + "IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
  }
    try {
      fail("calling setCount() with a negative count should throw "
          + "IllegalArgumentException or UnsupportedOperationException");
    } catch (IllegalArgumentException expected) {
    } catch (UnsupportedOperationException expected) {
    }
  }
  // TODO: test adding element of wrong type

  
Returns java.lang.reflect.Method instances for the setCount() tests that assume multisets support duplicates so that the test of Multisets.forSet() can suppress them.
  @GwtIncompatible("reflection")
    return Arrays.asList(
        getMethod("testSetCount_threeToThree_removeSupported"),
        getMethod("testSetCount_threeToZero_supported"),
        getMethod("testSetCount_threeToOne_supported"));
  }
  @GwtIncompatible("reflection")
  private static Method getMethod(String methodName) {
    return Helpers.getMethod(AbstractMultisetSetCountTester.classmethodName);
  }
New to GrepCode? Check out our FAQ X