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 
     24 import com.google.common.collect.testing.features.CollectionFeature;
     25 import com.google.common.collect.testing.features.CollectionSize;
     26 import com.google.common.collect.testing.features.ListFeature;
     27 
     28 import java.lang.reflect.Method;
     29 
     30 /**
     31  * A generic JUnit test which tests {@code add(int, Object)} operations on a
     32  * list. Can't be invoked directly; please see
     33  * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
     34  *
     35  * <p>This class is GWT compatible.
     36  *
     37  * @author Chris Povirk
     38  */
     39 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
     40 public class ListAddAtIndexTester<E> extends AbstractListTester<E> {
     41   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     42   @CollectionSize.Require(absent = ZERO)
     43   public void testAddAtIndex_supportedPresent() {
     44     getList().add(0, samples.e0);
     45     expectAdded(0, samples.e0);
     46   }
     47 
     48   @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
     49   @CollectionSize.Require(absent = ZERO)
     50   /*
     51    * absent = ZERO isn't required, since unmodList.add() must
     52    * throw regardless, but it keeps the method name accurate.
     53    */
     54   public void testAddAtIndex_unsupportedPresent() {
     55     try {
     56       getList().add(0, samples.e0);
     57       fail("add(n, present) should throw");
     58     } catch (UnsupportedOperationException expected) {
     59     }
     60     expectUnchanged();
     61   }
     62 
     63   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     64   public void testAddAtIndex_supportedNotPresent() {
     65     getList().add(0, samples.e3);
     66     expectAdded(0, samples.e3);
     67   }
     68 
     69   @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
     70   public void testAddAtIndex_unsupportedNotPresent() {
     71     try {
     72       getList().add(0, samples.e3);
     73       fail("add(n, notPresent) should throw");
     74     } catch (UnsupportedOperationException expected) {
     75     }
     76     expectUnchanged();
     77     expectMissing(samples.e3);
     78   }
     79 
     80   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     81   @CollectionSize.Require(absent = {ZERO, ONE})
     82   public void testAddAtIndex_middle() {
     83     getList().add(getNumElements() / 2, samples.e3);
     84     expectAdded(getNumElements() / 2, samples.e3);
     85   }
     86 
     87   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     88   @CollectionSize.Require(absent = ZERO)
     89   public void testAddAtIndex_end() {
     90     getList().add(getNumElements(), samples.e3);
     91     expectAdded(getNumElements(), samples.e3);
     92   }
     93 
     94   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
     95   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
     96   public void testAddAtIndex_nullSupported() {
     97     getList().add(0, null);
     98     expectAdded(0, (E) null);
     99   }
    100 
    101   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    102   @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
    103   public void testAddAtIndex_nullUnsupported() {
    104     try {
    105       getList().add(0, null);
    106       fail("add(n, null) should throw");
    107     } catch (NullPointerException expected) {
    108     }
    109     expectUnchanged();
    110     expectNullMissingWhenNullUnsupported(
    111         "Should not contain null after unsupported add(n, null)");
    112   }
    113 
    114   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    115   public void testAddAtIndex_negative() {
    116     try {
    117       getList().add(-1, samples.e3);
    118       fail("add(-1, e) should throw");
    119     } catch (IndexOutOfBoundsException expected) {
    120     }
    121     expectUnchanged();
    122     expectMissing(samples.e3);
    123   }
    124 
    125   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
    126   public void testAddAtIndex_tooLarge() {
    127     try {
    128       getList().add(getNumElements() + 1, samples.e3);
    129       fail("add(size + 1, e) should throw");
    130     } catch (IndexOutOfBoundsException expected) {
    131     }
    132     expectUnchanged();
    133     expectMissing(samples.e3);
    134   }
    135 
    136   /**
    137    * Returns the {@link Method} instance for
    138    * {@link #testAddAtIndex_nullSupported()} so that tests can suppress it. See
    139    * {@link CollectionAddTester#getAddNullSupportedMethod()} for details.
    140    */
    141   public static Method getAddNullSupportedMethod() {
    142     return Platform.getMethod(
    143         ListAddAtIndexTester.class, "testAddAtIndex_nullSupported");
    144   }
    145 }
    146