Java
138 строк · 5.3 Кб
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
17package com.google.common.collect.testing;18
19import com.google.common.annotations.GwtIncompatible;20import com.google.common.collect.testing.features.CollectionSize;21import com.google.common.collect.testing.features.Feature;22import com.google.common.collect.testing.features.FeatureUtil;23import java.lang.reflect.Method;24import java.util.ArrayList;25import java.util.Arrays;26import java.util.List;27import java.util.Set;28import java.util.logging.Logger;29import 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
45public abstract class PerCollectionSizeTestSuiteBuilder<46B extends PerCollectionSizeTestSuiteBuilder<B, G, T, E>,47G extends TestContainerGenerator<T, E>,48T,49E>50extends FeatureSpecificTestSuiteBuilder<B, G> {51private static final Logger logger =52Logger.getLogger(PerCollectionSizeTestSuiteBuilder.class.getName());53
54/** Creates a runnable JUnit test suite based on the criteria already given. */55@Override56public TestSuite createTestSuite() {57checkCanCreate();58
59String name = getName();60// Copy this set, so we can modify it.61Set<Feature<?>> features = Helpers.copyToSet(getFeatures());62@SuppressWarnings("rawtypes") // class literals63List<Class<? extends AbstractTester>> testers = getTesters();64
65logger.fine(" Testing: " + name);66
67// Split out all the specified sizes.68Set<Feature<?>> sizesToTest = Helpers.<Feature<?>>copyToSet(CollectionSize.values());69sizesToTest.retainAll(features);70features.removeAll(sizesToTest);71
72FeatureUtil.addImpliedFeatures(sizesToTest);73sizesToTest.retainAll(74Arrays.asList(CollectionSize.ZERO, CollectionSize.ONE, CollectionSize.SEVERAL));75
76logger.fine(" Sizes: " + formatFeatureSet(sizesToTest));77
78if (sizesToTest.isEmpty()) {79throw new IllegalStateException(80name
81+ ": no CollectionSizes specified (check the argument to "82+ "FeatureSpecificTestSuiteBuilder.withFeatures().)");83}84
85TestSuite suite = new TestSuite(name);86for (Feature<?> collectionSize : sizesToTest) {87String oneSizeName =88Platform.format(89"%s [collection size: %s]", name, collectionSize.toString().toLowerCase());90OneSizeGenerator<T, E> oneSizeGenerator =91new OneSizeGenerator<>(getSubjectGenerator(), (CollectionSize) collectionSize);92Set<Feature<?>> oneSizeFeatures = Helpers.copyToSet(features);93oneSizeFeatures.add(collectionSize);94Set<Method> oneSizeSuppressedTests = getSuppressedTests();95
96OneSizeTestSuiteBuilder<T, E> oneSizeBuilder =97new OneSizeTestSuiteBuilder<T, E>(testers)98.named(oneSizeName)99.usingGenerator(oneSizeGenerator)100.withFeatures(oneSizeFeatures)101.withSetUp(getSetUp())102.withTearDown(getTearDown())103.suppressing(oneSizeSuppressedTests);104TestSuite oneSizeSuite = oneSizeBuilder.createTestSuite();105suite.addTest(oneSizeSuite);106
107for (TestSuite derivedSuite : createDerivedSuites(oneSizeBuilder)) {108oneSizeSuite.addTest(derivedSuite);109}110}111return suite;112}113
114protected List<TestSuite> createDerivedSuites(115FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<T, E>>116parentBuilder) {117return new ArrayList<>();118}119
120/** Builds a test suite for one particular {@link CollectionSize}. */121private static final class OneSizeTestSuiteBuilder<T, E>122extends FeatureSpecificTestSuiteBuilder<123OneSizeTestSuiteBuilder<T, E>, OneSizeGenerator<T, E>> {124@SuppressWarnings("rawtypes") // class literals125private final List<Class<? extends AbstractTester>> testers;126
127@SuppressWarnings("rawtypes") // class literals128public OneSizeTestSuiteBuilder(List<Class<? extends AbstractTester>> testers) {129this.testers = testers;130}131
132@SuppressWarnings("rawtypes") // class literals133@Override134protected List<Class<? extends AbstractTester>> getTesters() {135return testers;136}137}138}
139