View Javadoc
1   /*
2    * Copyright (C) 2010 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.DerivedCollectionGenerators.Bound;
21  import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator;
22  import com.google.common.collect.testing.features.CollectionFeature;
23  import com.google.common.collect.testing.features.Feature;
24  import com.google.common.collect.testing.testers.SortedSetNavigationTester;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.List;
28  import junit.framework.TestSuite;
29  
30  /**
31   * Creates, based on your criteria, a JUnit test suite that exhaustively tests
32   * a SortedSet implementation.
33   */
34  @GwtIncompatible
35  public class SortedSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> {
36    public static <E> SortedSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) {
37      SortedSetTestSuiteBuilder<E> builder = new SortedSetTestSuiteBuilder<E>();
38      builder.usingGenerator(generator);
39      return builder;
40    }
41  
42    @Override
43    protected List<Class<? extends AbstractTester>> getTesters() {
44      List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
45      testers.add(SortedSetNavigationTester.class);
46      return testers;
47    }
48  
49    @Override
50    public TestSuite createTestSuite() {
51      if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) {
52        List<Feature<?>> features = Helpers.copyToList(getFeatures());
53        features.add(CollectionFeature.KNOWN_ORDER);
54        withFeatures(features);
55      }
56      return super.createTestSuite();
57    }
58  
59    @Override
60    protected List<TestSuite> createDerivedSuites(
61        FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
62            parentBuilder) {
63      List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder);
64  
65      if (!parentBuilder.getFeatures().contains(CollectionFeature.SUBSET_VIEW)) {
66        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE));
67        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND));
68        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE));
69      }
70  
71      return derivedSuites;
72    }
73  
74    /**
75     * Creates a suite whose set has some elements filtered out of view.
76     *
77     * <p>Because the set may be ascending or descending, this test must derive
78     * the relative order of these extreme values rather than relying on their
79     * regular sort ordering.
80     */
81    final TestSuite createSubsetSuite(
82        final FeatureSpecificTestSuiteBuilder<
83                ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
84            parentBuilder,
85        final Bound from,
86        final Bound to) {
87      final TestSortedSetGenerator<E> delegate =
88          (TestSortedSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator();
89  
90      List<Feature<?>> features = new ArrayList<>();
91      features.addAll(parentBuilder.getFeatures());
92      features.remove(CollectionFeature.ALLOWS_NULL_VALUES);
93      features.add(CollectionFeature.SUBSET_VIEW);
94  
95      return newBuilderUsing(delegate, to, from)
96          .named(parentBuilder.getName() + " subSet " + from + "-" + to)
97          .withFeatures(features)
98          .suppressing(parentBuilder.getSuppressedTests())
99          .createTestSuite();
100   }
101 
102   /** Like using() but overrideable by NavigableSetTestSuiteBuilder. */
103   SortedSetTestSuiteBuilder<E> newBuilderUsing(
104       TestSortedSetGenerator<E> delegate, Bound to, Bound from) {
105     return using(new SortedSetSubsetTestSetGenerator<E>(delegate, to, from));
106   }
107 }