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.IteratorFeature.MODIFIABLE;
     20 import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
     21 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
     22 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
     23 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
     24 import static com.google.common.collect.testing.testers.Platform.listListIteratorTesterNumIterations;
     25 import static java.util.Collections.singleton;
     26 
     27 import com.google.common.collect.testing.Helpers;
     28 import com.google.common.collect.testing.IteratorFeature;
     29 import com.google.common.collect.testing.ListIteratorTester;
     30 import com.google.common.collect.testing.features.CollectionFeature;
     31 import com.google.common.collect.testing.features.ListFeature;
     32 
     33 import java.lang.reflect.Method;
     34 import java.util.List;
     35 import java.util.ListIterator;
     36 import java.util.Set;
     37 import java.util.concurrent.CopyOnWriteArraySet;
     38 
     39 /**
     40  * A generic JUnit test which tests {@code listIterator} operations on a list.
     41  * Can't be invoked directly; please see
     42  * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
     43  *
     44  * <p>This class is GWT compatible.
     45  *
     46  * @author Chris Povirk
     47  * @author Kevin Bourrillion
     48  */
     49 public class ListListIteratorTester<E> extends AbstractListTester<E> {
     50   // TODO: switch to DerivedIteratorTestSuiteBuilder
     51 
     52   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
     53   @ListFeature.Require(absent = {SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
     54   public void testListIterator_unmodifiable() {
     55     runListIteratorTest(UNMODIFIABLE);
     56   }
     57 
     58   /*
     59    * For now, we don't cope with testing this when the list supports only some
     60    * modification operations.
     61    */
     62   @CollectionFeature.Require(SUPPORTS_REMOVE)
     63   @ListFeature.Require({SUPPORTS_SET, SUPPORTS_ADD_WITH_INDEX})
     64   public void testListIterator_fullyModifiable() {
     65     runListIteratorTest(MODIFIABLE);
     66   }
     67 
     68   private void runListIteratorTest(Set<IteratorFeature> features) {
     69     new ListIteratorTester<E>(
     70         listListIteratorTesterNumIterations(), singleton(samples.e4), features,
     71         Helpers.copyToList(getSampleElements()), 0) {
     72       {
     73         // TODO: don't set this universally
     74         stopTestingWhenAddThrowsException();
     75       }
     76 
     77       @Override protected ListIterator<E> newTargetIterator() {
     78         resetCollection();
     79         return getList().listIterator();
     80       }
     81 
     82       @Override protected void verify(List<E> elements) {
     83         expectContents(elements);
     84       }
     85     }.test();
     86   }
     87 
     88   public void testListIterator_tooLow() {
     89     try {
     90       getList().listIterator(-1);
     91       fail();
     92     } catch (IndexOutOfBoundsException expected) {
     93     }
     94   }
     95 
     96   public void testListIterator_tooHigh() {
     97     try {
     98       getList().listIterator(getNumElements() + 1);
     99       fail();
    100     } catch (IndexOutOfBoundsException expected) {
    101     }
    102   }
    103 
    104   public void testListIterator_atSize() {
    105     getList().listIterator(getNumElements());
    106     // TODO: run the iterator through ListIteratorTester
    107   }
    108 
    109   /**
    110    * Returns the {@link Method} instance for
    111    * {@link #testListIterator_fullyModifiable()} so that tests of
    112    * {@link CopyOnWriteArraySet} can suppress it with
    113    * {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
    114    * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570575">Sun bug
    115    * 6570575</a> is fixed.
    116    */
    117   public static Method getListIteratorFullyModifiableMethod() {
    118     return Platform.getMethod(
    119         ListListIteratorTester.class, "testListIterator_fullyModifiable");
    120   }
    121 
    122   /**
    123    * Returns the {@link Method} instance for
    124    * {@link #testListIterator_unmodifiable()} so that it can be suppressed in
    125    * GWT tests.
    126    */
    127   public static Method getListIteratorUnmodifiableMethod() {
    128     return Platform.getMethod(
    129         ListListIteratorTester.class, "testListIterator_unmodifiable");
    130   }
    131 }
    132