Home | History | Annotate | Download | only in testers
      1 /*
      2  * Copyright (C) 2007 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.testers;
     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.CollectionSize.ZERO;
     24 
     25 import com.google.common.annotations.GwtCompatible;
     26 import com.google.common.annotations.GwtIncompatible;
     27 import com.google.common.collect.testing.AbstractCollectionTester;
     28 import com.google.common.collect.testing.Helpers;
     29 import com.google.common.collect.testing.features.CollectionFeature;
     30 import com.google.common.collect.testing.features.CollectionSize;
     31 
     32 import java.lang.reflect.Method;
     33 import java.util.ConcurrentModificationException;
     34 import java.util.Iterator;
     35 
     36 /**
     37  * A generic JUnit test which tests {@code add} operations on a collection.
     38  * Can't be invoked directly; please see
     39  * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
     40  *
     41  * @author Chris Povirk
     42  * @author Kevin Bourrillion
     43  */
     44 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
     45 @GwtCompatible(emulated = true)
     46 public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
     47   @CollectionFeature.Require(SUPPORTS_ADD)
     48   public void testAdd_supportedNotPresent() {
     49     assertTrue("add(notPresent) should return true",
     50         collection.add(samples.e3));
     51     expectAdded(samples.e3);
     52   }
     53 
     54   @CollectionFeature.Require(absent = SUPPORTS_ADD)
     55   public void testAdd_unsupportedNotPresent() {
     56     try {
     57       collection.add(samples.e3);
     58       fail("add(notPresent) should throw");
     59     } catch (UnsupportedOperationException expected) {
     60     }
     61     expectUnchanged();
     62     expectMissing(samples.e3);
     63   }
     64 
     65   @CollectionFeature.Require(absent = SUPPORTS_ADD)
     66   @CollectionSize.Require(absent = ZERO)
     67   public void testAdd_unsupportedPresent() {
     68     try {
     69       assertFalse("add(present) should return false or throw",
     70           collection.add(samples.e0));
     71     } catch (UnsupportedOperationException tolerated) {
     72     }
     73     expectUnchanged();
     74   }
     75 
     76   @CollectionFeature.Require(
     77       value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
     78       absent = RESTRICTS_ELEMENTS)
     79   public void testAdd_nullSupported() {
     80     assertTrue("add(null) should return true", collection.add(null));
     81     expectAdded((E) null);
     82   }
     83 
     84   @CollectionFeature.Require(value = SUPPORTS_ADD,
     85       absent = ALLOWS_NULL_VALUES)
     86   public void testAdd_nullUnsupported() {
     87     try {
     88       collection.add(null);
     89       fail("add(null) should throw");
     90     } catch (NullPointerException expected) {
     91     }
     92     expectUnchanged();
     93     expectNullMissingWhenNullUnsupported(
     94         "Should not contain null after unsupported add(null)");
     95   }
     96 
     97   @CollectionFeature.Require({SUPPORTS_ADD,
     98       FAILS_FAST_ON_CONCURRENT_MODIFICATION})
     99   @CollectionSize.Require(absent = ZERO)
    100   public void testAddConcurrentWithIteration() {
    101     try {
    102       Iterator<E> iterator = collection.iterator();
    103       assertTrue(collection.add(samples.e3));
    104       iterator.next();
    105       fail("Expected ConcurrentModificationException");
    106     } catch (ConcurrentModificationException expected) {
    107       // success
    108     }
    109   }
    110 
    111   /**
    112    * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so
    113    * that tests of {@link
    114    * java.util.Collections#checkedCollection(java.util.Collection, Class)} can
    115    * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}
    116    * until <a
    117    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug
    118    * 6409434</a> is fixed. It's unclear whether nulls were to be permitted or
    119    * forbidden, but presumably the eventual fix will be to permit them, as it
    120    * seems more likely that code would depend on that behavior than on the
    121    * other. Thus, we say the bug is in add(), which fails to support null.
    122    */
    123   @GwtIncompatible("reflection")
    124   public static Method getAddNullSupportedMethod() {
    125     return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported");
    126   }
    127 
    128   /**
    129    * Returns the {@link Method} instance for {@link #testAdd_nullSupported()}
    130    * so that tests of {@link java.util.TreeSet} can suppress it with {@code
    131    * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
    132    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug
    133    * 5045147</a> is fixed.
    134    */
    135   @GwtIncompatible("reflection")
    136   public static Method getAddNullUnsupportedMethod() {
    137     return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported");
    138   }
    139 
    140   /**
    141    * Returns the {@link Method} instance for {@link
    142    * #testAdd_unsupportedNotPresent()} so that tests can suppress it with
    143    * {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we figure out
    144    * what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap}
    145    * support for {@code entrySet().add()}</a>.
    146    */
    147   @GwtIncompatible("reflection")
    148   public static Method getAddUnsupportedNotPresentMethod() {
    149     return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent");
    150   }
    151 }
    152