• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.collect.testing.features.CollectionFeature;
20 import com.google.common.collect.testing.features.CollectionSize;
21 import com.google.common.collect.testing.features.ListFeature;
22 
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 
26 import java.lang.reflect.Method;
27 import java.util.AbstractList;
28 import java.util.AbstractSequentialList;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.ListIterator;
36 import 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  */
45 public class TestsForListsInJavaUtil {
suite()46   public static Test suite() {
47     return new TestsForListsInJavaUtil().allTests();
48   }
49 
allTests()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 
suppressForEmptyList()65   protected Collection<Method> suppressForEmptyList() {
66     return Collections.emptySet();
67   }
suppressForSingletonList()68   protected Collection<Method> suppressForSingletonList() {
69     return Collections.emptySet();
70   }
suppressForArraysAsList()71   protected Collection<Method> suppressForArraysAsList() {
72     return Collections.emptySet();
73   }
suppressForArrayList()74   protected Collection<Method> suppressForArrayList() {
75     return Collections.emptySet();
76   }
suppressForLinkedList()77   protected Collection<Method> suppressForLinkedList() {
78     return Collections.emptySet();
79   }
suppressForCopyOnWriteArrayList()80   protected Collection<Method> suppressForCopyOnWriteArrayList() {
81     return Collections.emptySet();
82   }
suppressForUnmodifiableList()83   protected Collection<Method> suppressForUnmodifiableList() {
84     return Collections.emptySet();
85   }
suppressForCheckedList()86   protected Collection<Method> suppressForCheckedList() {
87     return Collections.emptySet();
88   }
suppressForAbstractList()89   protected Collection<Method> suppressForAbstractList() {
90     return Collections.emptySet();
91   }
suppressForAbstractSequentialList()92   protected Collection<Method> suppressForAbstractSequentialList() {
93     return Collections.emptySet();
94   }
95 
testsForEmptyList()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.SUPPORTS_ADD_WITH_INDEX,
191             ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
192             ListFeature.SUPPORTS_SET,
193             CollectionFeature.SUPPORTS_ADD,
194             CollectionFeature.SUPPORTS_REMOVE,
195             CollectionFeature.SERIALIZABLE,
196             CollectionFeature.ALLOWS_NULL_VALUES,
197             CollectionSize.ANY)
198         .suppressing(suppressForCopyOnWriteArrayList())
199         .createTestSuite();
200   }
201 
202   public Test testsForUnmodifiableList() {
203     return ListTestSuiteBuilder
204         .using(new TestStringListGenerator() {
205             @Override public List<String> create(String[] elements) {
206               List<String> innerList = new ArrayList<String>();
207               Collections.addAll(innerList, elements);
208               return Collections.unmodifiableList(innerList);
209             }
210           })
211         .named("unmodifiableList/ArrayList")
212         .withFeatures(
213             CollectionFeature.SERIALIZABLE,
214             CollectionFeature.ALLOWS_NULL_VALUES,
215             CollectionSize.ANY)
216         .suppressing(suppressForUnmodifiableList())
217         .createTestSuite();
218   }
219 
220   public Test testsForCheckedList() {
221     return ListTestSuiteBuilder
222         .using(new TestStringListGenerator() {
223             @Override public List<String> create(String[] elements) {
224               List<String> innerList = new ArrayList<String>();
225               Collections.addAll(innerList, elements);
226               return Collections.checkedList(innerList, String.class);
227             }
228           })
229         .named("checkedList/ArrayList")
230         .withFeatures(
231             ListFeature.GENERAL_PURPOSE,
232             CollectionFeature.SERIALIZABLE,
233             CollectionFeature.RESTRICTS_ELEMENTS,
234             CollectionFeature.ALLOWS_NULL_VALUES,
235             CollectionSize.ANY)
236         .suppressing(suppressForCheckedList())
237         .createTestSuite();
238   }
239 
240   public Test testsForAbstractList() {
241     return ListTestSuiteBuilder
242         .using(new TestStringListGenerator () {
243             @Override protected List<String> create(final String[] elements) {
244               return new AbstractList<String>() {
245                 @Override public int size() {
246                   return elements.length;
247                 }
248                 @Override public String get(int index) {
249                   return elements[index];
250                 }
251               };
252             }
253           })
254         .named("AbstractList")
255         .withFeatures(
256             CollectionFeature.NONE,
257             CollectionFeature.ALLOWS_NULL_VALUES,
258             CollectionSize.ANY)
259         .suppressing(suppressForAbstractList())
260         .createTestSuite();
261   }
262 
263   public Test testsForAbstractSequentialList() {
264     return ListTestSuiteBuilder
265         .using(new TestStringListGenerator () {
266             @Override protected List<String> create(final String[] elements) {
267               // For this test we trust ArrayList works
268               final List<String> list = new ArrayList<String>();
269               Collections.addAll(list, elements);
270               return new AbstractSequentialList<String>() {
271                 @Override public int size() {
272                   return list.size();
273                 }
274                 @Override public ListIterator<String> listIterator(int index) {
275                   return list.listIterator(index);
276                 }
277               };
278             }
279           })
280         .named("AbstractSequentialList")
281         .withFeatures(
282             ListFeature.GENERAL_PURPOSE,
283             CollectionFeature.ALLOWS_NULL_VALUES,
284             CollectionSize.ANY)
285         .suppressing(suppressForAbstractSequentialList())
286         .createTestSuite();
287   }
288 }
289