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 static com.google.common.collect.testing.Helpers.copyToList;
020
021import com.google.common.annotations.GwtIncompatible;
022import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
023import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator;
024import com.google.common.collect.testing.features.CollectionFeature;
025import com.google.common.collect.testing.features.Feature;
026import com.google.common.collect.testing.testers.CollectionAddAllTester;
027import com.google.common.collect.testing.testers.CollectionAddTester;
028import com.google.common.collect.testing.testers.SortedSetNavigationTester;
029import java.lang.reflect.Method;
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.HashSet;
033import java.util.List;
034import java.util.Set;
035import junit.framework.TestSuite;
036
037/**
038 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a SortedSet
039 * implementation.
040 */
041@GwtIncompatible
042public class SortedSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> {
043  public static <E> SortedSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) {
044    SortedSetTestSuiteBuilder<E> builder = new SortedSetTestSuiteBuilder<>();
045    builder.usingGenerator(generator);
046    return builder;
047  }
048
049  @SuppressWarnings("rawtypes") // class literals
050  @Override
051  protected List<Class<? extends AbstractTester>> getTesters() {
052    List<Class<? extends AbstractTester>> testers = copyToList(super.getTesters());
053    testers.add(SortedSetNavigationTester.class);
054    return testers;
055  }
056
057  @Override
058  public TestSuite createTestSuite() {
059    if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) {
060      List<Feature<?>> features = copyToList(getFeatures());
061      features.add(CollectionFeature.KNOWN_ORDER);
062      withFeatures(features);
063    }
064    return super.createTestSuite();
065  }
066
067  @Override
068  protected List<TestSuite> createDerivedSuites(
069      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
070          parentBuilder) {
071    List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder);
072
073    if (!parentBuilder.getFeatures().contains(CollectionFeature.SUBSET_VIEW)) {
074      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE));
075      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND));
076      derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE));
077    }
078
079    return derivedSuites;
080  }
081
082  /**
083   * Creates a suite whose set has some elements filtered out of view.
084   *
085   * <p>Because the set may be ascending or descending, this test must derive the relative order of
086   * these extreme values rather than relying on their regular sort ordering.
087   */
088  final TestSuite createSubsetSuite(
089      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
090          parentBuilder,
091      Bound from,
092      Bound to) {
093    TestSortedSetGenerator<E> delegate =
094        (TestSortedSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator();
095
096    List<Feature<?>> features = new ArrayList<>(parentBuilder.getFeatures());
097    Set<Method> suppressing = new HashSet<>(parentBuilder.getSuppressedTests());
098    features.add(CollectionFeature.SUBSET_VIEW);
099    if (features.remove(CollectionFeature.ALLOWS_NULL_VALUES)) {
100      // the null value might be out of bounds, so we can't always construct a subset with nulls
101      features.add(CollectionFeature.ALLOWS_NULL_QUERIES);
102      // but add null might still be supported if it happens to be within range of the subset
103      suppressing.add(CollectionAddTester.getAddNullUnsupportedMethod());
104      suppressing.add(CollectionAddAllTester.getAddAllNullUnsupportedMethod());
105    }
106
107    return newBuilderUsing(delegate, to, from)
108        .named(parentBuilder.getName() + " subSet " + from + "-" + to)
109        .withFeatures(features)
110        .suppressing(suppressing)
111        .withSetUp(parentBuilder.getSetUp())
112        .withTearDown(parentBuilder.getTearDown())
113        .createTestSuite();
114  }
115
116  /** Like using() but overrideable by NavigableSetTestSuiteBuilder. */
117  SortedSetTestSuiteBuilder<E> newBuilderUsing(
118      TestSortedSetGenerator<E> delegate, Bound to, Bound from) {
119    return using(new SortedSetSubsetTestSetGenerator<E>(delegate, to, from));
120  }
121}