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.RESTRICTS_ELEMENTS;
     21 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD_ALL;
     22 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
     23 import static java.util.Collections.singletonList;
     24 
     25 import com.google.common.collect.testing.AbstractCollectionTester;
     26 import com.google.common.collect.testing.MinimalCollection;
     27 import com.google.common.collect.testing.features.CollectionFeature;
     28 import com.google.common.collect.testing.features.CollectionSize;
     29 
     30 import java.lang.reflect.Method;
     31 import java.util.List;
     32 
     33 /**
     34  * A generic JUnit test which tests addAll operations on a collection. Can't be
     35  * invoked directly; please see
     36  * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
     37  *
     38  * <p>This class is GWT compatible.
     39  *
     40  * @author Chris Povirk
     41  * @author Kevin Bourrillion
     42  */
     43 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
     44 public class CollectionAddAllTester<E> extends AbstractCollectionTester<E> {
     45   @CollectionFeature.Require(SUPPORTS_ADD_ALL)
     46   public void testAddAll_supportedNothing() {
     47     assertFalse("addAll(nothing) should return false",
     48         collection.addAll(emptyCollection()));
     49     expectUnchanged();
     50   }
     51 
     52   @CollectionFeature.Require(absent = SUPPORTS_ADD_ALL)
     53   public void testAddAll_unsupportedNothing() {
     54     try {
     55       assertFalse("addAll(nothing) should return false or throw",
     56           collection.addAll(emptyCollection()));
     57     } catch (UnsupportedOperationException tolerated) {
     58     }
     59     expectUnchanged();
     60   }
     61 
     62   @CollectionFeature.Require(SUPPORTS_ADD_ALL)
     63   public void testAddAll_supportedNonePresent() {
     64     assertTrue("addAll(nonePresent) should return true",
     65         collection.addAll(createDisjointCollection()));
     66     expectAdded(samples.e3, samples.e4);
     67   }
     68 
     69   @CollectionFeature.Require(absent = SUPPORTS_ADD_ALL)
     70   public void testAddAll_unsupportedNonePresent() {
     71     try {
     72       collection.addAll(createDisjointCollection());
     73       fail("addAll(nonePresent) should throw");
     74     } catch (UnsupportedOperationException expected) {
     75     }
     76     expectUnchanged();
     77     expectMissing(samples.e3, samples.e4);
     78   }
     79 
     80   @CollectionFeature.Require(SUPPORTS_ADD_ALL)
     81   @CollectionSize.Require(absent = ZERO)
     82   public void testAddAll_supportedSomePresent() {
     83     assertTrue("addAll(somePresent) should return true",
     84         collection.addAll(MinimalCollection.of(samples.e3, samples.e0)));
     85     assertTrue("should contain " + samples.e3, collection.contains(samples.e3));
     86     assertTrue("should contain " + samples.e0, collection.contains(samples.e0));
     87   }
     88 
     89   @CollectionFeature.Require(absent = SUPPORTS_ADD_ALL)
     90   @CollectionSize.Require(absent = ZERO)
     91   public void testAddAll_unsupportedSomePresent() {
     92     try {
     93       collection.addAll(MinimalCollection.of(samples.e3, samples.e0));
     94       fail("addAll(somePresent) should throw");
     95     } catch (UnsupportedOperationException expected) {
     96     }
     97     expectUnchanged();
     98   }
     99 
    100   @CollectionFeature.Require(absent = SUPPORTS_ADD_ALL)
    101   @CollectionSize.Require(absent = ZERO)
    102   public void testAddAll_unsupportedAllPresent() {
    103     try {
    104       assertFalse("addAll(allPresent) should return false or throw",
    105           collection.addAll(MinimalCollection.of(samples.e0)));
    106     } catch (UnsupportedOperationException tolerated) {
    107     }
    108     expectUnchanged();
    109   }
    110 
    111   @CollectionFeature.Require(value = {SUPPORTS_ADD_ALL,
    112       ALLOWS_NULL_VALUES}, absent = RESTRICTS_ELEMENTS)
    113   public void testAddAll_nullSupported() {
    114     List<E> containsNull = singletonList(null);
    115     assertTrue("addAll(containsNull) should return true", collection
    116         .addAll(containsNull));
    117     /*
    118      * We need (E) to force interpretation of null as the single element of a
    119      * varargs array, not the array itself
    120      */
    121     expectAdded((E) null);
    122   }
    123 
    124   @CollectionFeature.Require(value = SUPPORTS_ADD_ALL,
    125       absent = ALLOWS_NULL_VALUES)
    126   public void testAddAll_nullUnsupported() {
    127     List<E> containsNull = singletonList(null);
    128     try {
    129       collection.addAll(containsNull);
    130       fail("addAll(containsNull) should throw");
    131     } catch (NullPointerException expected) {
    132     }
    133     expectUnchanged();
    134     expectNullMissingWhenNullUnsupported(
    135         "Should not contain null after unsupported addAll(containsNull)");
    136   }
    137 
    138   @CollectionFeature.Require(SUPPORTS_ADD_ALL)
    139   public void testAddAll_nullCollectionReference() {
    140     try {
    141       collection.addAll(null);
    142       fail("addAll(null) should throw NullPointerException");
    143     } catch (NullPointerException expected) {
    144     }
    145   }
    146 
    147   /**
    148    * Returns the {@link Method} instance for {@link
    149    * #testAddAll_nullUnsupported()} so that tests can suppress it with {@code
    150    * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
    151    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun
    152    * bug 5045147</a> is fixed.
    153    */
    154   public static Method getAddAllNullUnsupportedMethod() {
    155     return Platform.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported");
    156   }
    157 }
    158