Home | History | Annotate | Download | only in testers
      1 /*
      2  * Copyright (C) 2008 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.CollectionSize.ONE;
     21 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
     22 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
     23 import static java.util.Collections.singletonList;
     24 
     25 import com.google.common.annotations.GwtCompatible;
     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 import com.google.common.collect.testing.features.ListFeature;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * A generic JUnit test which tests {@code addAll(int, Collection)} operations
     35  * on a list. Can't be invoked directly; please see
     36  * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
     37  *
     38  * @author Chris Povirk
     39  */
     40 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
     41 @GwtCompatible
     42 public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> {
     43   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     44   @CollectionSize.Require(absent = ZERO)
     45   public void testAddAllAtIndex_supportedAllPresent() {
     46     assertTrue("addAll(n, allPresent) should return true",
     47         getList().addAll(0, MinimalCollection.of(samples.e0)));
     48     expectAdded(0, samples.e0);
     49   }
     50 
     51   @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
     52   @CollectionSize.Require(absent = ZERO)
     53   public void testAddAllAtIndex_unsupportedAllPresent() {
     54     try {
     55       getList().addAll(0, MinimalCollection.of(samples.e0));
     56       fail("addAll(n, allPresent) should throw");
     57     } catch (UnsupportedOperationException expected) {
     58     }
     59     expectUnchanged();
     60   }
     61 
     62   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     63   @CollectionSize.Require(absent = ZERO)
     64   public void testAddAllAtIndex_supportedSomePresent() {
     65     assertTrue("addAll(n, allPresent) should return true",
     66         getList().addAll(0, MinimalCollection.of(samples.e0, samples.e3)));
     67     expectAdded(0, samples.e0, samples.e3);
     68   }
     69 
     70   @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
     71   @CollectionSize.Require(absent = ZERO)
     72   public void testAddAllAtIndex_unsupportedSomePresent() {
     73     try {
     74       getList().addAll(0, MinimalCollection.of(samples.e0, samples.e3));
     75       fail("addAll(n, allPresent) should throw");
     76     } catch (UnsupportedOperationException expected) {
     77     }
     78     expectUnchanged();
     79     expectMissing(samples.e3);
     80   }
     81 
     82   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     83   public void testAddAllAtIndex_supportedNothing() {
     84     assertFalse("addAll(n, nothing) should return false",
     85         getList().addAll(0, emptyCollection()));
     86     expectUnchanged();
     87   }
     88 
     89   @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
     90   public void testAddAllAtIndex_unsupportedNothing() {
     91     try {
     92       assertFalse("addAll(n, nothing) should return false or throw",
     93           getList().addAll(0, emptyCollection()));
     94     } catch (UnsupportedOperationException tolerated) {
     95     }
     96     expectUnchanged();
     97   }
     98 
     99   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    100   public void testAddAllAtIndex_withDuplicates() {
    101     MinimalCollection<E> elementsToAdd
    102         = MinimalCollection.of(samples.e0, samples.e1, samples.e0, samples.e1);
    103     assertTrue("addAll(n, hasDuplicates) should return true",
    104         getList().addAll(0, elementsToAdd));
    105     expectAdded(0, samples.e0, samples.e1, samples.e0, samples.e1);
    106   }
    107 
    108   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    109   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
    110   public void testAddAllAtIndex_nullSupported() {
    111     List<E> containsNull = singletonList(null);
    112     assertTrue("addAll(n, containsNull) should return true",
    113         getList().addAll(0, containsNull));
    114     /*
    115      * We need (E) to force interpretation of null as the single element of a
    116      * varargs array, not the array itself
    117      */
    118     expectAdded(0, (E) null);
    119   }
    120 
    121   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    122   @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
    123   public void testAddAllAtIndex_nullUnsupported() {
    124     List<E> containsNull = singletonList(null);
    125     try {
    126       getList().addAll(0, containsNull);
    127       fail("addAll(n, containsNull) should throw");
    128     } catch (NullPointerException expected) {
    129     }
    130     expectUnchanged();
    131     expectNullMissingWhenNullUnsupported(
    132         "Should not contain null after unsupported addAll(n, containsNull)");
    133   }
    134 
    135   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    136   @CollectionSize.Require(absent = {ZERO, ONE})
    137   public void testAddAllAtIndex_middle() {
    138     assertTrue("addAll(middle, disjoint) should return true",
    139         getList().addAll(getNumElements() / 2, createDisjointCollection()));
    140     expectAdded(getNumElements() / 2, createDisjointCollection());
    141   }
    142 
    143   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    144   @CollectionSize.Require(absent = ZERO)
    145   public void testAddAllAtIndex_end() {
    146     assertTrue("addAll(end, disjoint) should return true",
    147         getList().addAll(getNumElements(), createDisjointCollection()));
    148     expectAdded(getNumElements(), createDisjointCollection());
    149   }
    150 
    151   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    152   public void testAddAllAtIndex_nullCollectionReference() {
    153     try {
    154       getList().addAll(0, null);
    155       fail("addAll(n, null) should throw");
    156     } catch (NullPointerException expected) {
    157     }
    158     expectUnchanged();
    159   }
    160 
    161   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    162   public void testAddAllAtIndex_negative() {
    163     try {
    164       getList().addAll(-1, MinimalCollection.of(samples.e3));
    165       fail("addAll(-1, e) should throw");
    166     } catch (IndexOutOfBoundsException expected) {
    167     }
    168     expectUnchanged();
    169     expectMissing(samples.e3);
    170   }
    171 
    172   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    173   public void testAddAllAtIndex_tooLarge() {
    174     try {
    175       getList().addAll(getNumElements() + 1, MinimalCollection.of(samples.e3));
    176       fail("addAll(size + 1, e) should throw");
    177     } catch (IndexOutOfBoundsException expected) {
    178     }
    179     expectUnchanged();
    180     expectMissing(samples.e3);
    181   }
    182 }
    183