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;
020import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
021import static java.util.Collections.emptySet;
022
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
025import com.google.common.collect.testing.DerivedCollectionGenerators.SortedMapSubmapTestMapGenerator;
026import com.google.common.collect.testing.features.Feature;
027import com.google.common.collect.testing.testers.SortedMapNavigationTester;
028import java.util.ArrayList;
029import java.util.List;
030import java.util.Map;
031import java.util.Map.Entry;
032import java.util.Set;
033import junit.framework.TestSuite;
034import org.jspecify.annotations.Nullable;
035
036/**
037 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a SortedMap
038 * implementation.
039 */
040@GwtIncompatible
041public class SortedMapTestSuiteBuilder<K, V> extends MapTestSuiteBuilder<K, V> {
042  public static <K, V> SortedMapTestSuiteBuilder<K, V> using(
043      TestSortedMapGenerator<K, V> generator) {
044    SortedMapTestSuiteBuilder<K, V> result = new SortedMapTestSuiteBuilder<>();
045    result.usingGenerator(generator);
046    return result;
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(SortedMapNavigationTester.class);
054    return testers;
055  }
056
057  @Override
058  public TestSuite createTestSuite() {
059    if (!getFeatures().contains(KNOWN_ORDER)) {
060      List<Feature<?>> features = copyToList(getFeatures());
061      features.add(KNOWN_ORDER);
062      withFeatures(features);
063    }
064    return super.createTestSuite();
065  }
066
067  @Override
068  protected List<TestSuite> createDerivedSuites(
069      FeatureSpecificTestSuiteBuilder<
070              ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>>
071          parentBuilder) {
072    List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder);
073
074    if (!parentBuilder.getFeatures().contains(NoRecurse.SUBMAP)) {
075      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE));
076      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND));
077      derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE));
078    }
079
080    return derivedSuites;
081  }
082
083  @Override
084  protected SetTestSuiteBuilder<K> createDerivedKeySetSuite(TestSetGenerator<K> keySetGenerator) {
085    return keySetGenerator instanceof TestSortedSetGenerator
086        ? SortedSetTestSuiteBuilder.using((TestSortedSetGenerator<K>) keySetGenerator)
087        : SetTestSuiteBuilder.using(keySetGenerator);
088  }
089
090  /**
091   * To avoid infinite recursion, test suites with these marker features won't have derived suites
092   * created for them.
093   */
094  enum NoRecurse implements Feature<@Nullable Void> {
095    SUBMAP,
096    DESCENDING;
097
098    @Override
099    public Set<Feature<? super @Nullable Void>> getImpliedFeatures() {
100      return emptySet();
101    }
102  }
103
104  /**
105   * Creates a suite whose map has some elements filtered out of view.
106   *
107   * <p>Because the map may be ascending or descending, this test must derive the relative order of
108   * these extreme values rather than relying on their regular sort ordering.
109   */
110  final TestSuite createSubmapSuite(
111      FeatureSpecificTestSuiteBuilder<
112              ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>>
113          parentBuilder,
114      Bound from,
115      Bound to) {
116    TestSortedMapGenerator<K, V> delegate =
117        (TestSortedMapGenerator<K, V>) parentBuilder.getSubjectGenerator().getInnerGenerator();
118
119    List<Feature<?>> features = new ArrayList<>();
120    features.add(NoRecurse.SUBMAP);
121    features.addAll(parentBuilder.getFeatures());
122
123    return newBuilderUsing(delegate, to, from)
124        .named(parentBuilder.getName() + " subMap " + from + "-" + to)
125        .withFeatures(features)
126        .suppressing(parentBuilder.getSuppressedTests())
127        .withSetUp(parentBuilder.getSetUp())
128        .withTearDown(parentBuilder.getTearDown())
129        .createTestSuite();
130  }
131
132  /** Like using() but overrideable by NavigableMapTestSuiteBuilder. */
133  SortedMapTestSuiteBuilder<K, V> newBuilderUsing(
134      TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) {
135    return using(new SortedMapSubmapTestMapGenerator<K, V>(delegate, to, from));
136  }
137}