Home | History | Annotate | Download | only in collect
      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;
     18 
     19 import com.google.common.collect.testing.ListTestSuiteBuilder;
     20 import com.google.common.collect.testing.TestStringListGenerator;
     21 import com.google.common.collect.testing.features.CollectionFeature;
     22 import com.google.common.collect.testing.features.CollectionSize;
     23 import com.google.common.collect.testing.features.ListFeature;
     24 
     25 import junit.framework.Test;
     26 import junit.framework.TestSuite;
     27 
     28 import java.util.Collection;
     29 import java.util.Collections;
     30 import java.util.Iterator;
     31 import java.util.List;
     32 import java.util.ListIterator;
     33 import java.util.RandomAccess;
     34 
     35 /**
     36  * Tests for {@code ForwardingList}.
     37  *
     38  * @author Robert Konigsberg
     39  * @author Louis Wasserman
     40  */
     41 public class ForwardingListTest extends ForwardingTestCase {
     42   static final class StandardImplForwardingList<T> extends ForwardingList<T> {
     43     private final List<T> backingList;
     44 
     45     StandardImplForwardingList(List<T> backingList) {
     46       this.backingList = backingList;
     47     }
     48 
     49     @Override protected List<T> delegate() {
     50       return backingList;
     51     }
     52 
     53     @Override public boolean add(T element) {
     54       return standardAdd(element);
     55     }
     56 
     57     @Override public boolean addAll(Collection<? extends T> collection) {
     58       return standardAddAll(collection);
     59     }
     60 
     61     @Override public void clear() {
     62       standardClear();
     63     }
     64 
     65     @Override public boolean contains(Object object) {
     66       return standardContains(object);
     67     }
     68 
     69     @Override public boolean containsAll(Collection<?> collection) {
     70       return standardContainsAll(collection);
     71     }
     72 
     73     @Override public boolean remove(Object object) {
     74       return standardRemove(object);
     75     }
     76 
     77     @Override public boolean removeAll(Collection<?> collection) {
     78       return standardRemoveAll(collection);
     79     }
     80 
     81     @Override public boolean retainAll(Collection<?> collection) {
     82       return standardRetainAll(collection);
     83     }
     84 
     85     @Override public Object[] toArray() {
     86       return standardToArray();
     87     }
     88 
     89     @Override public <T> T[] toArray(T[] array) {
     90       return standardToArray(array);
     91     }
     92 
     93     @Override public String toString() {
     94       return standardToString();
     95     }
     96 
     97     @Override public boolean addAll(
     98         int index, Collection<? extends T> elements) {
     99       return standardAddAll(index, elements);
    100     }
    101 
    102     @Override public boolean equals(Object object) {
    103       return standardEquals(object);
    104     }
    105 
    106     @Override public int hashCode() {
    107       return standardHashCode();
    108     }
    109 
    110     @Override public int indexOf(Object element) {
    111       return standardIndexOf(element);
    112     }
    113 
    114     @Override public int lastIndexOf(Object element) {
    115       return standardLastIndexOf(element);
    116     }
    117 
    118     @Override public Iterator<T> iterator() {
    119       return listIterator();
    120     }
    121 
    122     @Override public ListIterator<T> listIterator() {
    123       return listIterator(0);
    124     }
    125 
    126     @Override public ListIterator<T> listIterator(int index) {
    127       return standardListIterator(index);
    128     }
    129 
    130     @Override public List<T> subList(int fromIndex, int toIndex) {
    131       return standardSubList(fromIndex, toIndex);
    132     }
    133   }
    134 
    135   private static final List<String> EMPTY_LIST =
    136       Collections.<String>emptyList();
    137 
    138   private List<String> forward;
    139 
    140   public static Test suite(){
    141     TestSuite suite = new TestSuite();
    142 
    143     suite.addTestSuite(ForwardingListTest.class);
    144     suite.addTest(ListTestSuiteBuilder.using(new TestStringListGenerator() {
    145 
    146       @Override protected List<String> create(String[] elements) {
    147         return new StandardImplForwardingList<String>(
    148             Lists.newArrayList(elements));
    149       }
    150     }).named("ForwardingList[ArrayList] with standard implementations")
    151         .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES,
    152             ListFeature.GENERAL_PURPOSE).createTestSuite());
    153     suite.addTest(ListTestSuiteBuilder.using(new TestStringListGenerator() {
    154 
    155       @Override protected List<String> create(String[] elements) {
    156         return new StandardImplForwardingList<String>(
    157             ImmutableList.copyOf(elements));
    158       }
    159     }).named("ForwardingList[ImmutableList] with standard implementations")
    160         .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_QUERIES)
    161         .createTestSuite());
    162 
    163     return suite;
    164   }
    165 
    166   @Override public void setUp() throws Exception {
    167     super.setUp();
    168     /*
    169      * Class parameters must be raw, so we can't create a proxy with generic
    170      * type arguments. The created proxy only records calls and returns null, so
    171      * the type is irrelevant at runtime.
    172      */
    173     @SuppressWarnings("unchecked")
    174     final List<String> list = createProxyInstance(List.class);
    175     forward = new ForwardingList<String>() {
    176       @Override protected List<String> delegate() {
    177         return list;
    178       }
    179     };
    180   }
    181 
    182   public void testAdd_T() {
    183     forward.add("asdf");
    184     assertEquals("[add(Object)]", getCalls());
    185   }
    186 
    187   public void testAdd_int_T() {
    188     forward.add(0, "asdf");
    189     assertEquals("[add(int,Object)]", getCalls());
    190   }
    191 
    192   public void testAddAll_Collection() {
    193     forward.addAll(EMPTY_LIST);
    194     assertEquals("[addAll(Collection)]", getCalls());
    195   }
    196 
    197   public void testAddAll_int_Collection() {
    198     forward.addAll(0, Collections.singleton("asdf"));
    199     assertEquals("[addAll(int,Collection)]", getCalls());
    200   }
    201 
    202   public void testClear() {
    203     forward.clear();
    204     assertEquals("[clear]", getCalls());
    205   }
    206 
    207   public void testContains_Object() {
    208     forward.contains(null);
    209     assertEquals("[contains(Object)]", getCalls());
    210   }
    211 
    212   public void testContainsAll_Collection() {
    213     forward.containsAll(EMPTY_LIST);
    214     assertEquals("[containsAll(Collection)]", getCalls());
    215   }
    216 
    217   public void testGet_int() {
    218     forward.get(0);
    219     assertEquals("[get(int)]", getCalls());
    220   }
    221 
    222   public void testIndexOf_Object() {
    223     forward.indexOf(null);
    224     assertEquals("[indexOf(Object)]", getCalls());
    225   }
    226 
    227   public void testIsEmpty() {
    228     forward.isEmpty();
    229     assertEquals("[isEmpty]", getCalls());
    230   }
    231 
    232   public void testIterator() {
    233     forward.iterator();
    234     assertEquals("[iterator]", getCalls());
    235   }
    236 
    237   public void testLastIndexOf_Object() {
    238     forward.lastIndexOf("asdf");
    239     assertEquals("[lastIndexOf(Object)]", getCalls());
    240   }
    241 
    242   public void testListIterator() {
    243     forward.listIterator();
    244     assertEquals("[listIterator]", getCalls());
    245   }
    246 
    247   public void testListIterator_int() {
    248     forward.listIterator(0);
    249     assertEquals("[listIterator(int)]", getCalls());
    250   }
    251 
    252   public void testRemove_int() {
    253     forward.remove(0);
    254     assertEquals("[remove(int)]", getCalls());
    255   }
    256 
    257   public void testRemove_Object() {
    258     forward.remove(null);
    259     assertEquals("[remove(Object)]", getCalls());
    260   }
    261 
    262   public void testRemoveAll_Collection() {
    263     forward.removeAll(EMPTY_LIST);
    264     assertEquals("[removeAll(Collection)]", getCalls());
    265   }
    266 
    267   public void testRetainAll_Collection() {
    268     forward.retainAll(EMPTY_LIST);
    269     assertEquals("[retainAll(Collection)]", getCalls());
    270   }
    271 
    272   public void testSet_int_T() {
    273     forward.set(0, "asdf");
    274     assertEquals("[set(int,Object)]", getCalls());
    275   }
    276 
    277   public void testSize() {
    278     forward.size();
    279     assertEquals("[size]", getCalls());
    280   }
    281 
    282   public void testSubList_int_int() {
    283     forward.subList(0, 1);
    284     assertEquals("[subList(int,int)]", getCalls());
    285   }
    286 
    287   public void testToArray() {
    288     forward.toArray();
    289     assertEquals("[toArray]", getCalls());
    290   }
    291 
    292   public void testToArray_TArray() {
    293     forward.toArray(new String[0]);
    294     assertEquals("[toArray(Object[])]", getCalls());
    295   }
    296 
    297   public void testEquals_Object() {
    298     forward.equals("asdf");
    299     assertEquals("[equals(Object)]", getCalls());
    300   }
    301 
    302   public void testHashCode() {
    303     forward.hashCode();
    304     assertEquals("[hashCode]", getCalls());
    305   }
    306 
    307   public void testRandomAccess() {
    308     assertFalse(forward instanceof RandomAccess);
    309   }
    310 
    311   public void testToString() {
    312     forward.toString();
    313     assertEquals("[toString]", getCalls());
    314   }
    315 }
    316