| 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 com.google.common.annotations.GwtIncompatible; |
| 20 |
import com.google.common.collect.testing.features.CollectionSize; |
| 21 |
import com.google.common.collect.testing.features.Feature; |
| 22 |
import com.google.common.collect.testing.features.FeatureUtil; |
| 23 |
import java.lang.reflect.Method; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Arrays; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Set; |
| 28 |
import java.util.logging.Logger; |
| 29 |
import junit.framework.TestSuite; |
| 30 |
|
| 31 |
/** |
| 32 |
* This builder creates a composite test suite, containing a separate test suite for each {@link |
| 33 |
* CollectionSize} present in the features specified by {@link #withFeatures(Feature...)}. |
| 34 |
* |
| 35 |
* @param <B> The concrete type of this builder (the 'self-type'). All the Builder methods of this |
| 36 |
* class (such as {@link #named(String)}) return this type, so that Builder methods of more |
| 37 |
* derived classes can be chained onto them without casting. |
| 38 |
* @param <G> The type of the generator to be passed to testers in the generated test suite. An |
| 39 |
* instance of G should somehow provide an instance of the class under test, plus any other |
| 40 |
* information required to parameterize the test. |
| 41 |
* @see FeatureSpecificTestSuiteBuilder |
| 42 |
* @author George van den Driessche |
| 43 |
*/ |
| 44 |
@GwtIncompatible |
| 45 |
public abstract class PerCollectionSizeTestSuiteBuilder< |
| 46 |
B extends PerCollectionSizeTestSuiteBuilder<B, G, T, E>, |
| 47 |
G extends TestContainerGenerator<T, E>, |
| 48 |
T, |
| 49 |
E> |
| 50 |
extends FeatureSpecificTestSuiteBuilder<B, G> { |
| 51 |
private static final Logger logger = |
| 52 |
Logger.getLogger(PerCollectionSizeTestSuiteBuilder.class.getName()); |
| 53 |
|
| 54 |
/** Creates a runnable JUnit test suite based on the criteria already given. */ |
| 55 |
@Override |
| 56 |
public TestSuite createTestSuite() { |
| 57 |
checkCanCreate(); |
| 58 |
|
| 59 |
String name = getName(); |
| 60 |
// Copy this set, so we can modify it. |
| 61 |
Set<Feature<?>> features = Helpers.copyToSet(getFeatures()); |
| 62 |
List<Class<? extends AbstractTester>> testers = getTesters(); |
| 63 |
|
| 64 |
logger.fine(" Testing: " + name); |
| 65 |
|
| 66 |
// Split out all the specified sizes. |
| 67 |
Set<Feature<?>> sizesToTest = Helpers.<Feature<?>>copyToSet(CollectionSize.values()); |
| 68 |
sizesToTest.retainAll(features); |
| 69 |
features.removeAll(sizesToTest); |
| 70 |
|
| 71 |
FeatureUtil.addImpliedFeatures(sizesToTest); |
| 72 |
sizesToTest.retainAll( |
| 73 |
Arrays.asList(CollectionSize.ZERO, CollectionSize.ONE, CollectionSize.SEVERAL)); |
| 74 |
|
| 75 |
logger.fine(" Sizes: " + formatFeatureSet(sizesToTest)); |
| 76 |
|
| 77 |
if (sizesToTest.isEmpty()) { |
| 78 |
throw new IllegalStateException( |
| 79 |
name |
| 80 |
+ ": no CollectionSizes specified (check the argument to " |
| 81 |
+ "FeatureSpecificTestSuiteBuilder.withFeatures().)"); |
| 82 |
} |
| 83 |
|
| 84 |
TestSuite suite = new TestSuite(name); |
| 85 |
for (Feature<?> collectionSize : sizesToTest) { |
| 86 |
String oneSizeName = |
| 87 |
Platform.format( |
| 88 |
"%s [collection size: %s]", name, collectionSize.toString().toLowerCase()); |
| 89 |
OneSizeGenerator<T, E> oneSizeGenerator = |
| 90 |
new OneSizeGenerator<>(getSubjectGenerator(), (CollectionSize) collectionSize); |
| 91 |
Set<Feature<?>> oneSizeFeatures = Helpers.copyToSet(features); |
| 92 |
oneSizeFeatures.add(collectionSize); |
| 93 |
Set<Method> oneSizeSuppressedTests = getSuppressedTests(); |
| 94 |
|
| 95 |
OneSizeTestSuiteBuilder<T, E> oneSizeBuilder = |
| 96 |
new OneSizeTestSuiteBuilder<T, E>(testers) |
| 97 |
.named(oneSizeName) |
| 98 |
.usingGenerator(oneSizeGenerator) |
| 99 |
.withFeatures(oneSizeFeatures) |
| 100 |
.withSetUp(getSetUp()) |
| 101 |
.withTearDown(getTearDown()) |
| 102 |
.suppressing(oneSizeSuppressedTests); |
| 103 |
TestSuite oneSizeSuite = oneSizeBuilder.createTestSuite(); |
| 104 |
suite.addTest(oneSizeSuite); |
| 105 |
|
| 106 |
for (TestSuite derivedSuite : createDerivedSuites(oneSizeBuilder)) { |
| 107 |
oneSizeSuite.addTest(derivedSuite); |
| 108 |
} |
| 109 |
} |
| 110 |
return suite; |
| 111 |
} |
| 112 |
|
| 113 |
protected List<TestSuite> createDerivedSuites( |
| 114 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<T, E>> |
| 115 |
parentBuilder) { |
| 116 |
return new ArrayList<>(); |
| 117 |
} |
| 118 |
|
| 119 |
/** Builds a test suite for one particular {@link CollectionSize}. */ |
| 120 |
private static final class OneSizeTestSuiteBuilder<T, E> |
| 121 |
extends FeatureSpecificTestSuiteBuilder< |
| 122 |
OneSizeTestSuiteBuilder<T, E>, OneSizeGenerator<T, E>> { |
| 123 |
private final List<Class<? extends AbstractTester>> testers; |
| 124 |
|
| 125 |
public OneSizeTestSuiteBuilder(List<Class<? extends AbstractTester>> testers) { |
| 126 |
this.testers = testers; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
protected List<Class<? extends AbstractTester>> getTesters() { |
| 131 |
return testers; |
| 132 |
} |
| 133 |
} |
| 134 |
} |