Java

Форк
0
/
PerCollectionSizeTestSuiteBuilder.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

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
    @SuppressWarnings("rawtypes") // class literals
63
    List<Class<? extends AbstractTester>> testers = getTesters();
64

65
    logger.fine(" Testing: " + name);
66

67
    // Split out all the specified sizes.
68
    Set<Feature<?>> sizesToTest = Helpers.<Feature<?>>copyToSet(CollectionSize.values());
69
    sizesToTest.retainAll(features);
70
    features.removeAll(sizesToTest);
71

72
    FeatureUtil.addImpliedFeatures(sizesToTest);
73
    sizesToTest.retainAll(
74
        Arrays.asList(CollectionSize.ZERO, CollectionSize.ONE, CollectionSize.SEVERAL));
75

76
    logger.fine("   Sizes: " + formatFeatureSet(sizesToTest));
77

78
    if (sizesToTest.isEmpty()) {
79
      throw new IllegalStateException(
80
          name
81
              + ": no CollectionSizes specified (check the argument to "
82
              + "FeatureSpecificTestSuiteBuilder.withFeatures().)");
83
    }
84

85
    TestSuite suite = new TestSuite(name);
86
    for (Feature<?> collectionSize : sizesToTest) {
87
      String oneSizeName =
88
          Platform.format(
89
              "%s [collection size: %s]", name, collectionSize.toString().toLowerCase());
90
      OneSizeGenerator<T, E> oneSizeGenerator =
91
          new OneSizeGenerator<>(getSubjectGenerator(), (CollectionSize) collectionSize);
92
      Set<Feature<?>> oneSizeFeatures = Helpers.copyToSet(features);
93
      oneSizeFeatures.add(collectionSize);
94
      Set<Method> oneSizeSuppressedTests = getSuppressedTests();
95

96
      OneSizeTestSuiteBuilder<T, E> oneSizeBuilder =
97
          new OneSizeTestSuiteBuilder<T, E>(testers)
98
              .named(oneSizeName)
99
              .usingGenerator(oneSizeGenerator)
100
              .withFeatures(oneSizeFeatures)
101
              .withSetUp(getSetUp())
102
              .withTearDown(getTearDown())
103
              .suppressing(oneSizeSuppressedTests);
104
      TestSuite oneSizeSuite = oneSizeBuilder.createTestSuite();
105
      suite.addTest(oneSizeSuite);
106

107
      for (TestSuite derivedSuite : createDerivedSuites(oneSizeBuilder)) {
108
        oneSizeSuite.addTest(derivedSuite);
109
      }
110
    }
111
    return suite;
112
  }
113

114
  protected List<TestSuite> createDerivedSuites(
115
      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<T, E>>
116
          parentBuilder) {
117
    return new ArrayList<>();
118
  }
119

120
  /** Builds a test suite for one particular {@link CollectionSize}. */
121
  private static final class OneSizeTestSuiteBuilder<T, E>
122
      extends FeatureSpecificTestSuiteBuilder<
123
          OneSizeTestSuiteBuilder<T, E>, OneSizeGenerator<T, E>> {
124
    @SuppressWarnings("rawtypes") // class literals
125
    private final List<Class<? extends AbstractTester>> testers;
126

127
    @SuppressWarnings("rawtypes") // class literals
128
    public OneSizeTestSuiteBuilder(List<Class<? extends AbstractTester>> testers) {
129
      this.testers = testers;
130
    }
131

132
    @SuppressWarnings("rawtypes") // class literals
133
    @Override
134
    protected List<Class<? extends AbstractTester>> getTesters() {
135
      return testers;
136
    }
137
  }
138
}
139

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.