001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing;
018
019import static com.google.common.collect.testing.testers.ListListIteratorTester.getListIteratorFullyModifiableMethod;
020import static com.google.common.collect.testing.testers.ListSubListTester.getSubListOriginalListSetAffectsSubListLargeListMethod;
021import static com.google.common.collect.testing.testers.ListSubListTester.getSubListOriginalListSetAffectsSubListMethod;
022import static com.google.common.collect.testing.testers.ListSubListTester.getSubListSubListRemoveAffectsOriginalLargeListMethod;
023import static java.util.Arrays.asList;
024import static java.util.Collections.emptyList;
025import static java.util.Collections.emptySet;
026import static java.util.Collections.singletonList;
027import static java.util.Collections.unmodifiableList;
028
029import com.google.common.annotations.GwtIncompatible;
030import com.google.common.collect.testing.features.CollectionFeature;
031import com.google.common.collect.testing.features.CollectionSize;
032import com.google.common.collect.testing.features.ListFeature;
033import java.lang.reflect.Method;
034import java.util.AbstractList;
035import java.util.AbstractSequentialList;
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.Collections;
039import java.util.LinkedList;
040import java.util.List;
041import java.util.ListIterator;
042import java.util.Vector;
043import java.util.concurrent.CopyOnWriteArrayList;
044import junit.framework.Test;
045import junit.framework.TestSuite;
046
047/**
048 * Generates a test suite covering the {@link List} implementations in the {@link java.util}
049 * package. Can be subclassed to specify tests that should be suppressed.
050 *
051 * @author Kevin Bourrillion
052 */
053@GwtIncompatible
054public class TestsForListsInJavaUtil {
055  public static Test suite() {
056    return new TestsForListsInJavaUtil().allTests();
057  }
058
059  public Test allTests() {
060    TestSuite suite = new TestSuite("java.util Lists");
061    suite.addTest(testsForEmptyList());
062    suite.addTest(testsForSingletonList());
063    suite.addTest(testsForArraysAsList());
064    suite.addTest(testsForArrayList());
065    suite.addTest(testsForLinkedList());
066    suite.addTest(testsForCopyOnWriteArrayList());
067    suite.addTest(testsForUnmodifiableList());
068    suite.addTest(testsForCheckedList());
069    suite.addTest(testsForAbstractList());
070    suite.addTest(testsForAbstractSequentialList());
071    suite.addTest(testsForVector());
072    return suite;
073  }
074
075  protected Collection<Method> suppressForEmptyList() {
076    return emptySet();
077  }
078
079  protected Collection<Method> suppressForSingletonList() {
080    return emptySet();
081  }
082
083  protected Collection<Method> suppressForArraysAsList() {
084    return emptySet();
085  }
086
087  protected Collection<Method> suppressForArrayList() {
088    return emptySet();
089  }
090
091  protected Collection<Method> suppressForLinkedList() {
092    return emptySet();
093  }
094
095  protected Collection<Method> suppressForCopyOnWriteArrayList() {
096    return asList(
097        getSubListOriginalListSetAffectsSubListMethod(),
098        getSubListOriginalListSetAffectsSubListLargeListMethod(),
099        getSubListSubListRemoveAffectsOriginalLargeListMethod(),
100        getListIteratorFullyModifiableMethod());
101  }
102
103  protected Collection<Method> suppressForUnmodifiableList() {
104    return emptySet();
105  }
106
107  protected Collection<Method> suppressForCheckedList() {
108    return emptySet();
109  }
110
111  protected Collection<Method> suppressForAbstractList() {
112    return emptySet();
113  }
114
115  protected Collection<Method> suppressForAbstractSequentialList() {
116    return emptySet();
117  }
118
119  protected Collection<Method> suppressForVector() {
120    return emptySet();
121  }
122
123  public Test testsForEmptyList() {
124    return ListTestSuiteBuilder.using(
125            new TestStringListGenerator() {
126              @Override
127              public List<String> create(String[] elements) {
128                return emptyList();
129              }
130            })
131        .named("emptyList")
132        .withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO)
133        .suppressing(suppressForEmptyList())
134        .createTestSuite();
135  }
136
137  public Test testsForSingletonList() {
138    return ListTestSuiteBuilder.using(
139            new TestStringListGenerator() {
140              @Override
141              public List<String> create(String[] elements) {
142                return singletonList(elements[0]);
143              }
144            })
145        .named("singletonList")
146        .withFeatures(
147            CollectionFeature.SERIALIZABLE,
148            CollectionFeature.ALLOWS_NULL_VALUES,
149            CollectionSize.ONE)
150        .suppressing(suppressForSingletonList())
151        .createTestSuite();
152  }
153
154  public Test testsForArraysAsList() {
155    return ListTestSuiteBuilder.using(
156            new TestStringListGenerator() {
157              @Override
158              public List<String> create(String[] elements) {
159                return asList(elements.clone());
160              }
161            })
162        .named("Arrays.asList")
163        .withFeatures(
164            ListFeature.SUPPORTS_SET,
165            CollectionFeature.SERIALIZABLE,
166            CollectionFeature.ALLOWS_NULL_VALUES,
167            CollectionSize.ANY)
168        .suppressing(suppressForArraysAsList())
169        .createTestSuite();
170  }
171
172  public Test testsForArrayList() {
173    return ListTestSuiteBuilder.using(
174            new TestStringListGenerator() {
175              @Override
176              public List<String> create(String[] elements) {
177                return new ArrayList<>(MinimalCollection.of(elements));
178              }
179            })
180        .named("ArrayList")
181        .withFeatures(
182            ListFeature.GENERAL_PURPOSE,
183            CollectionFeature.SERIALIZABLE,
184            CollectionFeature.ALLOWS_NULL_VALUES,
185            CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
186            CollectionSize.ANY)
187        .suppressing(suppressForArrayList())
188        .createTestSuite();
189  }
190
191  public Test testsForLinkedList() {
192    return ListTestSuiteBuilder.using(
193            new TestStringListGenerator() {
194              @Override
195              public List<String> create(String[] elements) {
196                return new LinkedList<>(MinimalCollection.of(elements));
197              }
198            })
199        .named("LinkedList")
200        .withFeatures(
201            ListFeature.GENERAL_PURPOSE,
202            CollectionFeature.SERIALIZABLE,
203            CollectionFeature.ALLOWS_NULL_VALUES,
204            CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
205            CollectionSize.ANY)
206        .suppressing(suppressForLinkedList())
207        .createTestSuite();
208  }
209
210  public Test testsForCopyOnWriteArrayList() {
211    return ListTestSuiteBuilder.using(
212            new TestStringListGenerator() {
213              @Override
214              public List<String> create(String[] elements) {
215                return new CopyOnWriteArrayList<>(MinimalCollection.of(elements));
216              }
217            })
218        .named("CopyOnWriteArrayList")
219        .withFeatures(
220            ListFeature.SUPPORTS_ADD_WITH_INDEX,
221            ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
222            ListFeature.SUPPORTS_SET,
223            CollectionFeature.SUPPORTS_ADD,
224            CollectionFeature.SUPPORTS_REMOVE,
225            CollectionFeature.SERIALIZABLE,
226            CollectionFeature.ALLOWS_NULL_VALUES,
227            CollectionSize.ANY)
228        .suppressing(suppressForCopyOnWriteArrayList())
229        .createTestSuite();
230  }
231
232  public Test testsForUnmodifiableList() {
233    return ListTestSuiteBuilder.using(
234            new TestStringListGenerator() {
235              @Override
236              public List<String> create(String[] elements) {
237                List<String> innerList = new ArrayList<>();
238                Collections.addAll(innerList, elements);
239                return unmodifiableList(innerList);
240              }
241            })
242        .named("unmodifiableList/ArrayList")
243        .withFeatures(
244            CollectionFeature.SERIALIZABLE,
245            CollectionFeature.ALLOWS_NULL_VALUES,
246            CollectionSize.ANY)
247        .suppressing(suppressForUnmodifiableList())
248        .createTestSuite();
249  }
250
251  public Test testsForCheckedList() {
252    return ListTestSuiteBuilder.using(
253            new TestStringListGenerator() {
254              @Override
255              public List<String> create(String[] elements) {
256                List<String> innerList = new ArrayList<>();
257                Collections.addAll(innerList, elements);
258                return Collections.checkedList(innerList, String.class);
259              }
260            })
261        .named("checkedList/ArrayList")
262        .withFeatures(
263            ListFeature.GENERAL_PURPOSE,
264            CollectionFeature.SERIALIZABLE,
265            CollectionFeature.RESTRICTS_ELEMENTS,
266            CollectionFeature.ALLOWS_NULL_VALUES,
267            CollectionSize.ANY)
268        .suppressing(suppressForCheckedList())
269        .createTestSuite();
270  }
271
272  public Test testsForAbstractList() {
273    return ListTestSuiteBuilder.using(
274            new TestStringListGenerator() {
275              @Override
276              protected List<String> create(final String[] elements) {
277                return new AbstractList<String>() {
278                  @Override
279                  public int size() {
280                    return elements.length;
281                  }
282
283                  @Override
284                  public String get(int index) {
285                    return elements[index];
286                  }
287                };
288              }
289            })
290        .named("AbstractList")
291        .withFeatures(
292            CollectionFeature.NONE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY)
293        .suppressing(suppressForAbstractList())
294        .createTestSuite();
295  }
296
297  public Test testsForAbstractSequentialList() {
298    return ListTestSuiteBuilder.using(
299            new TestStringListGenerator() {
300              @Override
301              protected List<String> create(final String[] elements) {
302                // For this test we trust ArrayList works
303                final List<String> list = new ArrayList<>();
304                Collections.addAll(list, elements);
305                return new AbstractSequentialList<String>() {
306                  @Override
307                  public int size() {
308                    return list.size();
309                  }
310
311                  @Override
312                  public ListIterator<String> listIterator(int index) {
313                    return list.listIterator(index);
314                  }
315                };
316              }
317            })
318        .named("AbstractSequentialList")
319        .withFeatures(
320            ListFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY)
321        .suppressing(suppressForAbstractSequentialList())
322        .createTestSuite();
323  }
324
325  private Test testsForVector() {
326    return ListTestSuiteBuilder.using(
327            new TestStringListGenerator() {
328              @Override
329              protected List<String> create(String[] elements) {
330                return new Vector<>(MinimalCollection.of(elements));
331              }
332            })
333        .named("Vector")
334        .withFeatures(
335            ListFeature.GENERAL_PURPOSE,
336            CollectionFeature.ALLOWS_NULL_VALUES,
337            CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
338            CollectionFeature.SERIALIZABLE,
339            CollectionSize.ANY)
340        .createTestSuite();
341  }
342}