View Javadoc
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 static com.google.common.collect.testing.testers.CollectionSpliteratorTester.getSpliteratorNotImmutableCollectionAllowsAddMethod;
20  import static com.google.common.collect.testing.testers.CollectionSpliteratorTester.getSpliteratorNotImmutableCollectionAllowsRemoveMethod;
21  import static com.google.common.collect.testing.testers.ListListIteratorTester.getListIteratorFullyModifiableMethod;
22  import static com.google.common.collect.testing.testers.ListSubListTester.getSubListOriginalListSetAffectsSubListLargeListMethod;
23  import static com.google.common.collect.testing.testers.ListSubListTester.getSubListOriginalListSetAffectsSubListMethod;
24  import static com.google.common.collect.testing.testers.ListSubListTester.getSubListSubListRemoveAffectsOriginalLargeListMethod;
25  import static java.util.Arrays.asList;
26  
27  import com.google.common.annotations.GwtIncompatible;
28  import com.google.common.collect.testing.features.CollectionFeature;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.ListFeature;
31  import java.lang.reflect.Method;
32  import java.util.AbstractList;
33  import java.util.AbstractSequentialList;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.LinkedList;
39  import java.util.List;
40  import java.util.ListIterator;
41  import java.util.Vector;
42  import java.util.concurrent.CopyOnWriteArrayList;
43  import junit.framework.Test;
44  import junit.framework.TestSuite;
45  
46  /**
47   * Generates a test suite covering the {@link List} implementations in the
48   * {@link java.util} package. Can be subclassed to specify tests that should
49   * be suppressed.
50   *
51   * @author Kevin Bourrillion
52   */
53  @GwtIncompatible
54  public class TestsForListsInJavaUtil {
55    public static Test suite() {
56      return new TestsForListsInJavaUtil().allTests();
57    }
58  
59    public Test allTests() {
60      TestSuite suite = new TestSuite("java.util Lists");
61      suite.addTest(testsForEmptyList());
62      suite.addTest(testsForSingletonList());
63      suite.addTest(testsForArraysAsList());
64      suite.addTest(testsForArrayList());
65      suite.addTest(testsForLinkedList());
66      suite.addTest(testsForCopyOnWriteArrayList());
67      suite.addTest(testsForUnmodifiableList());
68      suite.addTest(testsForCheckedList());
69      suite.addTest(testsForAbstractList());
70      suite.addTest(testsForAbstractSequentialList());
71      suite.addTest(testsForVector());
72      return suite;
73    }
74  
75    protected Collection<Method> suppressForEmptyList() {
76      return Collections.emptySet();
77    }
78  
79    protected Collection<Method> suppressForSingletonList() {
80      return Collections.emptySet();
81    }
82  
83    protected Collection<Method> suppressForArraysAsList() {
84      return Collections.emptySet();
85    }
86  
87    protected Collection<Method> suppressForArrayList() {
88      return Collections.emptySet();
89    }
90  
91    protected Collection<Method> suppressForLinkedList() {
92      return Collections.emptySet();
93    }
94  
95    protected Collection<Method> suppressForCopyOnWriteArrayList() {
96      return asList(
97          getSubListOriginalListSetAffectsSubListMethod(),
98          getSubListOriginalListSetAffectsSubListLargeListMethod(),
99          getSubListSubListRemoveAffectsOriginalLargeListMethod(),
100         getListIteratorFullyModifiableMethod(),
101         getSpliteratorNotImmutableCollectionAllowsAddMethod(),
102         getSpliteratorNotImmutableCollectionAllowsRemoveMethod());
103   }
104 
105   protected Collection<Method> suppressForUnmodifiableList() {
106     return Collections.emptySet();
107   }
108 
109   protected Collection<Method> suppressForCheckedList() {
110     return Collections.emptySet();
111   }
112 
113   protected Collection<Method> suppressForAbstractList() {
114     return Collections.emptySet();
115   }
116 
117   protected Collection<Method> suppressForAbstractSequentialList() {
118     return Collections.emptySet();
119   }
120 
121   protected Collection<Method> suppressForVector() {
122     return Collections.emptySet();
123   }
124 
125   public Test testsForEmptyList() {
126     return ListTestSuiteBuilder.using(
127             new TestStringListGenerator() {
128               @Override
129               public List<String> create(String[] elements) {
130                 return Collections.emptyList();
131               }
132             })
133         .named("emptyList")
134         .withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO)
135         .suppressing(suppressForEmptyList())
136         .createTestSuite();
137   }
138 
139   public Test testsForSingletonList() {
140     return ListTestSuiteBuilder.using(
141             new TestStringListGenerator() {
142               @Override
143               public List<String> create(String[] elements) {
144                 return Collections.singletonList(elements[0]);
145               }
146             })
147         .named("singletonList")
148         .withFeatures(
149             CollectionFeature.SERIALIZABLE,
150             CollectionFeature.ALLOWS_NULL_VALUES,
151             CollectionSize.ONE)
152         .suppressing(suppressForSingletonList())
153         .createTestSuite();
154   }
155 
156   public Test testsForArraysAsList() {
157     return ListTestSuiteBuilder.using(
158             new TestStringListGenerator() {
159               @Override
160               public List<String> create(String[] elements) {
161                 return Arrays.asList(elements.clone());
162               }
163             })
164         .named("Arrays.asList")
165         .withFeatures(
166             ListFeature.SUPPORTS_SET,
167             CollectionFeature.SERIALIZABLE,
168             CollectionFeature.ALLOWS_NULL_VALUES,
169             CollectionSize.ANY)
170         .suppressing(suppressForArraysAsList())
171         .createTestSuite();
172   }
173 
174   public Test testsForArrayList() {
175     return ListTestSuiteBuilder.using(
176             new TestStringListGenerator() {
177               @Override
178               public List<String> create(String[] elements) {
179                 return new ArrayList<>(MinimalCollection.of(elements));
180               }
181             })
182         .named("ArrayList")
183         .withFeatures(
184             ListFeature.GENERAL_PURPOSE,
185             CollectionFeature.SERIALIZABLE,
186             CollectionFeature.ALLOWS_NULL_VALUES,
187             CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
188             CollectionSize.ANY)
189         .suppressing(suppressForArrayList())
190         .createTestSuite();
191   }
192 
193   public Test testsForLinkedList() {
194     return ListTestSuiteBuilder.using(
195             new TestStringListGenerator() {
196               @Override
197               public List<String> create(String[] elements) {
198                 return new LinkedList<>(MinimalCollection.of(elements));
199               }
200             })
201         .named("LinkedList")
202         .withFeatures(
203             ListFeature.GENERAL_PURPOSE,
204             CollectionFeature.SERIALIZABLE,
205             CollectionFeature.ALLOWS_NULL_VALUES,
206             CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
207             CollectionSize.ANY)
208         .suppressing(suppressForLinkedList())
209         .createTestSuite();
210   }
211 
212   public Test testsForCopyOnWriteArrayList() {
213     return ListTestSuiteBuilder.using(
214             new TestStringListGenerator() {
215               @Override
216               public List<String> create(String[] elements) {
217                 return new CopyOnWriteArrayList<>(MinimalCollection.of(elements));
218               }
219             })
220         .named("CopyOnWriteArrayList")
221         .withFeatures(
222             ListFeature.SUPPORTS_ADD_WITH_INDEX,
223             ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
224             ListFeature.SUPPORTS_SET,
225             CollectionFeature.SUPPORTS_ADD,
226             CollectionFeature.SUPPORTS_REMOVE,
227             CollectionFeature.SERIALIZABLE,
228             CollectionFeature.ALLOWS_NULL_VALUES,
229             CollectionSize.ANY)
230         .suppressing(suppressForCopyOnWriteArrayList())
231         .createTestSuite();
232   }
233 
234   public Test testsForUnmodifiableList() {
235     return ListTestSuiteBuilder.using(
236             new TestStringListGenerator() {
237               @Override
238               public List<String> create(String[] elements) {
239                 List<String> innerList = new ArrayList<>();
240                 Collections.addAll(innerList, elements);
241                 return Collections.unmodifiableList(innerList);
242               }
243             })
244         .named("unmodifiableList/ArrayList")
245         .withFeatures(
246             CollectionFeature.SERIALIZABLE,
247             CollectionFeature.ALLOWS_NULL_VALUES,
248             CollectionSize.ANY)
249         .suppressing(suppressForUnmodifiableList())
250         .createTestSuite();
251   }
252 
253   public Test testsForCheckedList() {
254     return ListTestSuiteBuilder.using(
255             new TestStringListGenerator() {
256               @Override
257               public List<String> create(String[] elements) {
258                 List<String> innerList = new ArrayList<>();
259                 Collections.addAll(innerList, elements);
260                 return Collections.checkedList(innerList, String.class);
261               }
262             })
263         .named("checkedList/ArrayList")
264         .withFeatures(
265             ListFeature.GENERAL_PURPOSE,
266             CollectionFeature.SERIALIZABLE,
267             CollectionFeature.RESTRICTS_ELEMENTS,
268             CollectionFeature.ALLOWS_NULL_VALUES,
269             CollectionSize.ANY)
270         .suppressing(suppressForCheckedList())
271         .createTestSuite();
272   }
273 
274   public Test testsForAbstractList() {
275     return ListTestSuiteBuilder.using(
276             new TestStringListGenerator() {
277               @Override
278               protected List<String> create(final String[] elements) {
279                 return new AbstractList<String>() {
280                   @Override
281                   public int size() {
282                     return elements.length;
283                   }
284 
285                   @Override
286                   public String get(int index) {
287                     return elements[index];
288                   }
289                 };
290               }
291             })
292         .named("AbstractList")
293         .withFeatures(
294             CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY)
295         .suppressing(suppressForAbstractList())
296         .createTestSuite();
297   }
298 
299   public Test testsForAbstractSequentialList() {
300     return ListTestSuiteBuilder.using(
301             new TestStringListGenerator() {
302               @Override
303               protected List<String> create(final String[] elements) {
304                 // For this test we trust ArrayList works
305                 final List<String> list = new ArrayList<>();
306                 Collections.addAll(list, elements);
307                 return new AbstractSequentialList<String>() {
308                   @Override
309                   public int size() {
310                     return list.size();
311                   }
312 
313                   @Override
314                   public ListIterator<String> listIterator(int index) {
315                     return list.listIterator(index);
316                   }
317                 };
318               }
319             })
320         .named("AbstractSequentialList")
321         .withFeatures(
322             ListFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY)
323         .suppressing(suppressForAbstractSequentialList())
324         .createTestSuite();
325   }
326 
327   private Test testsForVector() {
328     return ListTestSuiteBuilder.using(
329             new TestStringListGenerator() {
330               @Override
331               protected List<String> create(String[] elements) {
332                 return new Vector<>(MinimalCollection.of(elements));
333               }
334             })
335         .named("Vector")
336         .withFeatures(
337             ListFeature.GENERAL_PURPOSE,
338             CollectionFeature.ALLOWS_NULL_VALUES,
339             CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
340             CollectionFeature.SERIALIZABLE,
341             CollectionSize.ANY)
342         .createTestSuite();
343   }
344 
345 }