Debian Codegraph @

android/guava-testlib/src/com/google/common/collect/testing/TestsForListsInJavaUtil.java

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