TestsForListsInJavaUtil.java revision 3c77433663281544363151bf284b0240dfd22a42
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
17package com.google.common.collect.testing;
18
19import com.google.common.collect.testing.features.CollectionFeature;
20import com.google.common.collect.testing.features.CollectionSize;
21import com.google.common.collect.testing.features.ListFeature;
22
23import junit.framework.Test;
24import junit.framework.TestSuite;
25
26import java.lang.reflect.Method;
27import java.util.AbstractList;
28import java.util.AbstractSequentialList;
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Collection;
32import java.util.Collections;
33import java.util.LinkedList;
34import java.util.List;
35import java.util.ListIterator;
36import 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 */
45public 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.SERIALIZABLE,
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.SERIALIZABLE,
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.SERIALIZABLE,
138            CollectionFeature.ALLOWS_NULL_VALUES,
139            CollectionSize.ANY)
140        .suppressing(suppressForArraysAsList())
141        .createTestSuite();
142  }
143
144  public Test testsForArrayList() {
145    return ListTestSuiteBuilder
146        .using(new TestStringListGenerator() {
147            @Override public List<String> create(String[] elements) {
148              return new ArrayList<String>(MinimalCollection.of(elements));
149            }
150          })
151        .named("ArrayList")
152        .withFeatures(
153            ListFeature.GENERAL_PURPOSE,
154            CollectionFeature.SERIALIZABLE,
155            CollectionFeature.ALLOWS_NULL_VALUES,
156            CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
157            CollectionSize.ANY)
158        .suppressing(suppressForArrayList())
159        .createTestSuite();
160  }
161
162  public Test testsForLinkedList() {
163    return ListTestSuiteBuilder
164        .using(new TestStringListGenerator() {
165            @Override public List<String> create(String[] elements) {
166              return new LinkedList<String>(MinimalCollection.of(elements));
167            }
168          })
169        .named("LinkedList")
170        .withFeatures(
171            ListFeature.GENERAL_PURPOSE,
172            CollectionFeature.SERIALIZABLE,
173            CollectionFeature.ALLOWS_NULL_VALUES,
174            CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
175            CollectionSize.ANY)
176        .suppressing(suppressForLinkedList())
177        .createTestSuite();
178  }
179
180  public Test testsForCopyOnWriteArrayList() {
181    return ListTestSuiteBuilder
182        .using(new TestStringListGenerator() {
183            @Override public List<String> create(String[] elements) {
184              return new CopyOnWriteArrayList<String>(
185                  MinimalCollection.of(elements));
186            }
187          })
188        .named("CopyOnWriteArrayList")
189        .withFeatures(
190            ListFeature.GENERAL_PURPOSE,
191            CollectionFeature.SERIALIZABLE,
192            CollectionFeature.ALLOWS_NULL_VALUES,
193            CollectionSize.ANY)
194        .suppressing(suppressForCopyOnWriteArrayList())
195        .createTestSuite();
196  }
197
198  public Test testsForUnmodifiableList() {
199    return ListTestSuiteBuilder
200        .using(new TestStringListGenerator() {
201            @Override public List<String> create(String[] elements) {
202              List<String> innerList = new ArrayList<String>();
203              Collections.addAll(innerList, elements);
204              return Collections.unmodifiableList(innerList);
205            }
206          })
207        .named("unmodifiableList/ArrayList")
208        .withFeatures(
209            CollectionFeature.SERIALIZABLE,
210            CollectionFeature.ALLOWS_NULL_VALUES,
211            CollectionSize.ANY)
212        .suppressing(suppressForUnmodifiableList())
213        .createTestSuite();
214  }
215
216  public Test testsForCheckedList() {
217    return ListTestSuiteBuilder
218        .using(new TestStringListGenerator() {
219            @Override public List<String> create(String[] elements) {
220              List<String> innerList = new ArrayList<String>();
221              Collections.addAll(innerList, elements);
222              return Collections.checkedList(innerList, String.class);
223            }
224          })
225        .named("checkedList/ArrayList")
226        .withFeatures(
227            ListFeature.GENERAL_PURPOSE,
228            CollectionFeature.SERIALIZABLE,
229            CollectionFeature.RESTRICTS_ELEMENTS,
230            CollectionFeature.ALLOWS_NULL_VALUES,
231            CollectionSize.ANY)
232        .suppressing(suppressForCheckedList())
233        .createTestSuite();
234  }
235
236  public Test testsForAbstractList() {
237    return ListTestSuiteBuilder
238        .using(new TestStringListGenerator () {
239            @Override protected List<String> create(final String[] elements) {
240              return new AbstractList<String>() {
241                @Override public int size() {
242                  return elements.length;
243                }
244                @Override public String get(int index) {
245                  return elements[index];
246                }
247              };
248            }
249          })
250        .named("AbstractList")
251        .withFeatures(
252            CollectionFeature.NONE,
253            CollectionFeature.ALLOWS_NULL_VALUES,
254            CollectionSize.ANY)
255        .suppressing(suppressForAbstractList())
256        .createTestSuite();
257  }
258
259  public Test testsForAbstractSequentialList() {
260    return ListTestSuiteBuilder
261        .using(new TestStringListGenerator () {
262            @Override protected List<String> create(final String[] elements) {
263              // For this test we trust ArrayList works
264              final List<String> list = new ArrayList<String>();
265              Collections.addAll(list, elements);
266              return new AbstractSequentialList<String>() {
267                @Override public int size() {
268                  return list.size();
269                }
270                @Override public ListIterator<String> listIterator(int index) {
271                  return list.listIterator(index);
272                }
273              };
274            }
275          })
276        .named("AbstractSequentialList")
277        .withFeatures(
278            ListFeature.GENERAL_PURPOSE,
279            CollectionFeature.ALLOWS_NULL_VALUES,
280            CollectionSize.ANY)
281        .suppressing(suppressForAbstractSequentialList())
282        .createTestSuite();
283  }
284}
285