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 // We are testing LinkedList / testing our tests on LinkedList. 192 @SuppressWarnings("JdkObsolete") 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 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(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(String[] elements) { 304 // For this test we trust ArrayList works 305 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 // We are testing Vector / testing our tests on Vector. 328 @SuppressWarnings("JdkObsolete") 329 private Test testsForVector() { 330 return ListTestSuiteBuilder.using( 331 new TestStringListGenerator() { 332 @Override 333 protected List<String> create(String[] elements) { 334 return new Vector<>(MinimalCollection.of(elements)); 335 } 336 }) 337 .named("Vector") 338 .withFeatures( 339 ListFeature.GENERAL_PURPOSE, 340 CollectionFeature.ALLOWS_NULL_VALUES, 341 CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, 342 CollectionFeature.SERIALIZABLE, 343 CollectionSize.ANY) 344 .createTestSuite(); 345 } 346}