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; 023 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.collect.testing.features.CollectionFeature; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.ListFeature; 028import java.lang.reflect.Method; 029import java.util.AbstractList; 030import java.util.AbstractSequentialList; 031import java.util.ArrayList; 032import java.util.Arrays; 033import java.util.Collection; 034import java.util.Collections; 035import java.util.LinkedList; 036import java.util.List; 037import java.util.ListIterator; 038import java.util.Vector; 039import java.util.concurrent.CopyOnWriteArrayList; 040import junit.framework.Test; 041import junit.framework.TestSuite; 042 043/** 044 * Generates a test suite covering the {@link List} implementations in the {@link java.util} 045 * package. Can be subclassed to specify tests that should be suppressed. 046 * 047 * @author Kevin Bourrillion 048 */ 049@GwtIncompatible 050public class TestsForListsInJavaUtil { 051 public static Test suite() { 052 return new TestsForListsInJavaUtil().allTests(); 053 } 054 055 public Test allTests() { 056 TestSuite suite = new TestSuite("java.util Lists"); 057 suite.addTest(testsForEmptyList()); 058 suite.addTest(testsForSingletonList()); 059 suite.addTest(testsForArraysAsList()); 060 suite.addTest(testsForArrayList()); 061 suite.addTest(testsForLinkedList()); 062 suite.addTest(testsForCopyOnWriteArrayList()); 063 suite.addTest(testsForUnmodifiableList()); 064 suite.addTest(testsForCheckedList()); 065 suite.addTest(testsForAbstractList()); 066 suite.addTest(testsForAbstractSequentialList()); 067 suite.addTest(testsForVector()); 068 return suite; 069 } 070 071 protected Collection<Method> suppressForEmptyList() { 072 return Collections.emptySet(); 073 } 074 075 protected Collection<Method> suppressForSingletonList() { 076 return Collections.emptySet(); 077 } 078 079 protected Collection<Method> suppressForArraysAsList() { 080 return Collections.emptySet(); 081 } 082 083 protected Collection<Method> suppressForArrayList() { 084 return Collections.emptySet(); 085 } 086 087 protected Collection<Method> suppressForLinkedList() { 088 return Collections.emptySet(); 089 } 090 091 protected Collection<Method> suppressForCopyOnWriteArrayList() { 092 return Arrays.asList( 093 getSubListOriginalListSetAffectsSubListMethod(), 094 getSubListOriginalListSetAffectsSubListLargeListMethod(), 095 getSubListSubListRemoveAffectsOriginalLargeListMethod(), 096 getListIteratorFullyModifiableMethod()); 097 } 098 099 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}