Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2009 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;
     18 
     19 import com.google.common.collect.testing.features.CollectionFeature;
     20 import com.google.common.collect.testing.features.CollectionSize;
     21 import com.google.common.collect.testing.features.ListFeature;
     22 
     23 import junit.framework.Test;
     24 import junit.framework.TestSuite;
     25 
     26 import java.lang.reflect.Method;
     27 import java.util.AbstractList;
     28 import java.util.AbstractSequentialList;
     29 import java.util.ArrayList;
     30 import java.util.Arrays;
     31 import java.util.Collection;
     32 import java.util.Collections;
     33 import java.util.LinkedList;
     34 import java.util.List;
     35 import java.util.ListIterator;
     36 import java.util.concurrent.CopyOnWriteArrayList;
     37 
     38 /**
     39  * Generates a test suite covering the {@link List} implementations in the
     40  * {@link java.util} package. Can be subclassed to specify tests that should
     41  * be suppressed.
     42  *
     43  * @author Kevin Bourrillion
     44  */
     45 public class TestsForListsInJavaUtil {
     46   public static Test suite() {
     47     return new TestsForListsInJavaUtil().allTests();
     48   }
     49 
     50   public Test allTests() {
     51     TestSuite suite = new TestSuite("java.util Lists");
     52     suite.addTest(testsForEmptyList());
     53     suite.addTest(testsForSingletonList());
     54     suite.addTest(testsForArraysAsList());
     55     suite.addTest(testsForArrayList());
     56     suite.addTest(testsForLinkedList());
     57     suite.addTest(testsForCopyOnWriteArrayList());
     58     suite.addTest(testsForUnmodifiableList());
     59     suite.addTest(testsForCheckedList());
     60     suite.addTest(testsForAbstractList());
     61     suite.addTest(testsForAbstractSequentialList());
     62     return suite;
     63   }
     64 
     65   protected Collection<Method> suppressForEmptyList() {
     66     return Collections.emptySet();
     67   }
     68   protected Collection<Method> suppressForSingletonList() {
     69     return Collections.emptySet();
     70   }
     71   protected Collection<Method> suppressForArraysAsList() {
     72     return Collections.emptySet();
     73   }
     74   protected Collection<Method> suppressForArrayList() {
     75     return Collections.emptySet();
     76   }
     77   protected Collection<Method> suppressForLinkedList() {
     78     return Collections.emptySet();
     79   }
     80   protected Collection<Method> suppressForCopyOnWriteArrayList() {
     81     return Collections.emptySet();
     82   }
     83   protected Collection<Method> suppressForUnmodifiableList() {
     84     return Collections.emptySet();
     85   }
     86   protected Collection<Method> suppressForCheckedList() {
     87     return Collections.emptySet();
     88   }
     89   protected Collection<Method> suppressForAbstractList() {
     90     return Collections.emptySet();
     91   }
     92   protected Collection<Method> suppressForAbstractSequentialList() {
     93     return Collections.emptySet();
     94   }
     95 
     96   public Test testsForEmptyList() {
     97     return ListTestSuiteBuilder
     98         .using(new TestStringListGenerator() {
     99             @Override public List<String> create(String[] elements) {
    100               return Collections.emptyList();
    101             }
    102           })
    103         .named("emptyList")
    104         .withFeatures(
    105             CollectionFeature.NONE,
    106             CollectionSize.ZERO)
    107         .suppressing(suppressForEmptyList())
    108         .createTestSuite();
    109   }
    110 
    111   public Test testsForSingletonList() {
    112     return ListTestSuiteBuilder
    113         .using(new TestStringListGenerator() {
    114             @Override public List<String> create(String[] elements) {
    115               return Collections.singletonList(elements[0]);
    116             }
    117           })
    118         .named("singletonList")
    119         .withFeatures(
    120             CollectionFeature.NONE,
    121             CollectionFeature.ALLOWS_NULL_VALUES,
    122             CollectionSize.ONE)
    123         .suppressing(suppressForSingletonList())
    124         .createTestSuite();
    125   }
    126 
    127   public Test testsForArraysAsList() {
    128     return ListTestSuiteBuilder
    129         .using(new TestStringListGenerator() {
    130             @Override public List<String> create(String[] elements) {
    131               return Arrays.asList(elements.clone());
    132             }
    133           })
    134         .named("Arrays.asList")
    135         .withFeatures(
    136             ListFeature.SUPPORTS_SET,
    137             CollectionFeature.ALLOWS_NULL_VALUES,
    138             CollectionSize.ANY)
    139         .suppressing(suppressForArraysAsList())
    140         .createTestSuite();
    141   }
    142 
    143   public Test testsForArrayList() {
    144     return ListTestSuiteBuilder
    145         .using(new TestStringListGenerator() {
    146             @Override public List<String> create(String[] elements) {
    147               return new ArrayList<String>(MinimalCollection.of(elements));
    148             }
    149           })
    150         .named("ArrayList")
    151         .withFeatures(
    152             ListFeature.GENERAL_PURPOSE,
    153             CollectionFeature.ALLOWS_NULL_VALUES,
    154             CollectionSize.ANY)
    155         .suppressing(suppressForArrayList())
    156         .createTestSuite();
    157   }
    158 
    159   public Test testsForLinkedList() {
    160     return ListTestSuiteBuilder
    161         .using(new TestStringListGenerator() {
    162             @Override public List<String> create(String[] elements) {
    163               return new LinkedList<String>(MinimalCollection.of(elements));
    164             }
    165           })
    166         .named("LinkedList")
    167         .withFeatures(
    168             ListFeature.GENERAL_PURPOSE,
    169             CollectionFeature.ALLOWS_NULL_VALUES,
    170             CollectionSize.ANY)
    171         .suppressing(suppressForLinkedList())
    172         .createTestSuite();
    173   }
    174 
    175   public Test testsForCopyOnWriteArrayList() {
    176     return ListTestSuiteBuilder
    177         .using(new TestStringListGenerator() {
    178             @Override public List<String> create(String[] elements) {
    179               return new CopyOnWriteArrayList<String>(
    180                   MinimalCollection.of(elements));
    181             }
    182           })
    183         .named("CopyOnWriteArrayList")
    184         .withFeatures(
    185             ListFeature.GENERAL_PURPOSE,
    186             CollectionFeature.ALLOWS_NULL_VALUES,
    187             CollectionSize.ANY)
    188         .suppressing(suppressForCopyOnWriteArrayList())
    189         .createTestSuite();
    190   }
    191 
    192   public Test testsForUnmodifiableList() {
    193     return ListTestSuiteBuilder
    194         .using(new TestStringListGenerator() {
    195             @Override public List<String> create(String[] elements) {
    196               List<String> innerList = new ArrayList<String>();
    197               Collections.addAll(innerList, elements);
    198               return Collections.unmodifiableList(innerList);
    199             }
    200           })
    201         .named("unmodifiableList/ArrayList")
    202         .withFeatures(
    203             CollectionFeature.NONE,
    204             CollectionFeature.ALLOWS_NULL_VALUES,
    205             CollectionSize.ANY)
    206         .suppressing(suppressForUnmodifiableList())
    207         .createTestSuite();
    208   }
    209 
    210   public Test testsForCheckedList() {
    211     return ListTestSuiteBuilder
    212         .using(new TestStringListGenerator() {
    213             @Override public List<String> create(String[] elements) {
    214               List<String> innerList = new ArrayList<String>();
    215               Collections.addAll(innerList, elements);
    216               return Collections.checkedList(innerList, String.class);
    217             }
    218           })
    219         .named("checkedList/ArrayList")
    220         .withFeatures(
    221             ListFeature.GENERAL_PURPOSE,
    222             CollectionFeature.RESTRICTS_ELEMENTS,
    223             CollectionFeature.ALLOWS_NULL_VALUES,
    224             CollectionSize.ANY)
    225         .suppressing(suppressForCheckedList())
    226         .createTestSuite();
    227   }
    228 
    229   public Test testsForAbstractList() {
    230     return ListTestSuiteBuilder
    231         .using(new TestStringListGenerator () {
    232             @Override protected List<String> create(final String[] elements) {
    233               return new AbstractList<String>() {
    234                 @Override public int size() {
    235                   return elements.length;
    236                 }
    237                 @Override public String get(int index) {
    238                   return elements[index];
    239                 }
    240               };
    241             }
    242           })
    243         .named("AbstractList")
    244         .withFeatures(
    245             CollectionFeature.NONE,
    246             CollectionFeature.ALLOWS_NULL_VALUES,
    247             CollectionSize.ANY)
    248         .suppressing(suppressForAbstractList())
    249         .createTestSuite();
    250   }
    251 
    252   public Test testsForAbstractSequentialList() {
    253     return ListTestSuiteBuilder
    254         .using(new TestStringListGenerator () {
    255             @Override protected List<String> create(final String[] elements) {
    256               // For this test we trust ArrayList works
    257               final List<String> list = new ArrayList<String>();
    258               Collections.addAll(list, elements);
    259               return new AbstractSequentialList<String>() {
    260                 @Override public int size() {
    261                   return list.size();
    262                 }
    263                 @Override public ListIterator<String> listIterator(int index) {
    264                   return list.listIterator(index);
    265                 }
    266               };
    267             }
    268           })
    269         .named("AbstractSequentialList")
    270         .withFeatures(
    271             ListFeature.GENERAL_PURPOSE,
    272             CollectionFeature.ALLOWS_NULL_VALUES,
    273             CollectionSize.ANY)
    274         .suppressing(suppressForAbstractSequentialList())
    275         .createTestSuite();
    276   }
    277 }
    278