001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing;
018
019import com.google.common.annotations.GwtIncompatible;
020import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
021import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator;
022import com.google.common.collect.testing.features.CollectionFeature;
023import com.google.common.collect.testing.features.Feature;
024import com.google.common.collect.testing.testers.SortedSetNavigationTester;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028import junit.framework.TestSuite;
029
030/**
031 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a SortedSet
032 * implementation.
033 */
034@GwtIncompatible
035public class SortedSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> {
036  public static <E> SortedSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) {
037    SortedSetTestSuiteBuilder<E> builder = new SortedSetTestSuiteBuilder<>();
038    builder.usingGenerator(generator);
039    return builder;
040  }
041
042  @SuppressWarnings("rawtypes") // class literals
043  @Override
044  protected List<Class<? extends AbstractTester>> getTesters() {
045    List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
046    testers.add(SortedSetNavigationTester.class);
047    return testers;
048  }
049
050  @Override
051  public TestSuite createTestSuite() {
052    if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) {
053      List<Feature<?>> features = Helpers.copyToList(getFeatures());
054      features.add(CollectionFeature.KNOWN_ORDER);
055      withFeatures(features);
056    }
057    return super.createTestSuite();
058  }
059
060  @Override
061  protected List<TestSuite> createDerivedSuites(
062      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
063          parentBuilder) {
064    List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder);
065
066    if (!parentBuilder.getFeatures().contains(CollectionFeature.SUBSET_VIEW)) {
067      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE));
068      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND));
069      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE));
070    }
071
072    return derivedSuites;
073  }
074
075  /**
076   * Creates a suite whose set has some elements filtered out of view.
077   *
078   * <p>Because the set may be ascending or descending, this test must derive the relative order of
079   * these extreme values rather than relying on their regular sort ordering.
080   */
081  final TestSuite createSubsetSuite(
082      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
083          parentBuilder,
084      Bound from,
085      Bound to) {
086    TestSortedSetGenerator<E> delegate =
087        (TestSortedSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator();
088
089    List<Feature<?>> features = new ArrayList<>(parentBuilder.getFeatures());
090    features.remove(CollectionFeature.ALLOWS_NULL_VALUES);
091    features.add(CollectionFeature.SUBSET_VIEW);
092
093    return newBuilderUsing(delegate, to, from)
094        .named(parentBuilder.getName() + " subSet " + from + "-" + to)
095        .withFeatures(features)
096        .suppressing(parentBuilder.getSuppressedTests())
097        .withSetUp(parentBuilder.getSetUp())
098        .withTearDown(parentBuilder.getTearDown())
099        .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}