| 1 |
/* |
| 2 |
* Copyright (C) 2008 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.features.CollectionFeature.KNOWN_ORDER; |
| 20 |
import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE; |
| 21 |
import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS; |
| 22 |
|
| 23 |
import com.google.common.annotations.GwtIncompatible; |
| 24 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 25 |
import com.google.common.collect.testing.features.Feature; |
| 26 |
import com.google.common.collect.testing.testers.CollectionSerializationEqualTester; |
| 27 |
import com.google.common.collect.testing.testers.ListAddAllAtIndexTester; |
| 28 |
import com.google.common.collect.testing.testers.ListAddAllTester; |
| 29 |
import com.google.common.collect.testing.testers.ListAddAtIndexTester; |
| 30 |
import com.google.common.collect.testing.testers.ListAddTester; |
| 31 |
import com.google.common.collect.testing.testers.ListCreationTester; |
| 32 |
import com.google.common.collect.testing.testers.ListEqualsTester; |
| 33 |
import com.google.common.collect.testing.testers.ListGetTester; |
| 34 |
import com.google.common.collect.testing.testers.ListHashCodeTester; |
| 35 |
import com.google.common.collect.testing.testers.ListIndexOfTester; |
| 36 |
import com.google.common.collect.testing.testers.ListLastIndexOfTester; |
| 37 |
import com.google.common.collect.testing.testers.ListListIteratorTester; |
| 38 |
import com.google.common.collect.testing.testers.ListRemoveAllTester; |
| 39 |
import com.google.common.collect.testing.testers.ListRemoveAtIndexTester; |
| 40 |
import com.google.common.collect.testing.testers.ListRemoveTester; |
| 41 |
import com.google.common.collect.testing.testers.ListReplaceAllTester; |
| 42 |
import com.google.common.collect.testing.testers.ListRetainAllTester; |
| 43 |
import com.google.common.collect.testing.testers.ListSetTester; |
| 44 |
import com.google.common.collect.testing.testers.ListSubListTester; |
| 45 |
import com.google.common.collect.testing.testers.ListToArrayTester; |
| 46 |
import com.google.common.testing.SerializableTester; |
| 47 |
import java.util.ArrayList; |
| 48 |
import java.util.Collection; |
| 49 |
import java.util.HashSet; |
| 50 |
import java.util.List; |
| 51 |
import java.util.Set; |
| 52 |
import junit.framework.TestSuite; |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a List |
| 56 |
* implementation. |
| 57 |
* |
| 58 |
* @author George van den Driessche |
| 59 |
*/ |
| 60 |
@GwtIncompatible |
| 61 |
public final class ListTestSuiteBuilder<E> |
| 62 |
extends AbstractCollectionTestSuiteBuilder<ListTestSuiteBuilder<E>, E> { |
| 63 |
public static <E> ListTestSuiteBuilder<E> using(TestListGenerator<E> generator) { |
| 64 |
return new ListTestSuiteBuilder<E>().usingGenerator(generator); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
protected List<Class<? extends AbstractTester>> getTesters() { |
| 69 |
List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); |
| 70 |
|
| 71 |
testers.add(CollectionSerializationEqualTester.class); |
| 72 |
testers.add(ListAddAllAtIndexTester.class); |
| 73 |
testers.add(ListAddAllTester.class); |
| 74 |
testers.add(ListAddAtIndexTester.class); |
| 75 |
testers.add(ListAddTester.class); |
| 76 |
testers.add(ListCreationTester.class); |
| 77 |
testers.add(ListEqualsTester.class); |
| 78 |
testers.add(ListGetTester.class); |
| 79 |
testers.add(ListHashCodeTester.class); |
| 80 |
testers.add(ListIndexOfTester.class); |
| 81 |
testers.add(ListLastIndexOfTester.class); |
| 82 |
testers.add(ListListIteratorTester.class); |
| 83 |
testers.add(ListRemoveAllTester.class); |
| 84 |
testers.add(ListRemoveAtIndexTester.class); |
| 85 |
testers.add(ListRemoveTester.class); |
| 86 |
testers.add(ListReplaceAllTester.class); |
| 87 |
testers.add(ListRetainAllTester.class); |
| 88 |
testers.add(ListSetTester.class); |
| 89 |
testers.add(ListSubListTester.class); |
| 90 |
testers.add(ListToArrayTester.class); |
| 91 |
return testers; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Specifies {@link CollectionFeature#KNOWN_ORDER} for all list tests, since lists have an |
| 96 |
* iteration ordering corresponding to the insertion order. |
| 97 |
*/ |
| 98 |
@Override |
| 99 |
public TestSuite createTestSuite() { |
| 100 |
withFeatures(KNOWN_ORDER); |
| 101 |
return super.createTestSuite(); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected List<TestSuite> createDerivedSuites( |
| 106 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 107 |
parentBuilder) { |
| 108 |
List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); |
| 109 |
|
| 110 |
if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { |
| 111 |
derivedSuites.add( |
| 112 |
ListTestSuiteBuilder.using( |
| 113 |
new ReserializedListGenerator<E>(parentBuilder.getSubjectGenerator())) |
| 114 |
.named(getName() + " reserialized") |
| 115 |
.withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) |
| 116 |
.suppressing(parentBuilder.getSuppressedTests()) |
| 117 |
.withSetUp(parentBuilder.getSetUp()) |
| 118 |
.withTearDown(parentBuilder.getTearDown()) |
| 119 |
.createTestSuite()); |
| 120 |
} |
| 121 |
return derivedSuites; |
| 122 |
} |
| 123 |
|
| 124 |
static class ReserializedListGenerator<E> implements TestListGenerator<E> { |
| 125 |
final OneSizeTestContainerGenerator<Collection<E>, E> gen; |
| 126 |
|
| 127 |
private ReserializedListGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { |
| 128 |
this.gen = gen; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public SampleElements<E> samples() { |
| 133 |
return gen.samples(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public List<E> create(Object... elements) { |
| 138 |
return (List<E>) SerializableTester.reserialize(gen.create(elements)); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public E[] createArray(int length) { |
| 143 |
return gen.createArray(length); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public Iterable<E> order(List<E> insertionOrder) { |
| 148 |
return gen.order(insertionOrder); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { |
| 153 |
Set<Feature<?>> derivedFeatures = new HashSet<>(features); |
| 154 |
derivedFeatures.remove(SERIALIZABLE); |
| 155 |
derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS); |
| 156 |
return derivedFeatures; |
| 157 |
} |
| 158 |
} |