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.collect.testing.DerivedCollectionGenerators.Bound; 20 import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator; 21 import com.google.common.collect.testing.features.CollectionFeature; 22 import com.google.common.collect.testing.features.Feature; 23 import com.google.common.collect.testing.testers.SortedSetNavigationTester; 24 25 import junit.framework.TestSuite; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.List; 30 31 /** 32 * Creates, based on your criteria, a JUnit test suite that exhaustively tests 33 * a SortedSet implementation. 34 */ 35 public class SortedSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> { using( TestSortedSetGenerator<E> generator)36 public static <E> SortedSetTestSuiteBuilder<E> using( 37 TestSortedSetGenerator<E> generator) { 38 SortedSetTestSuiteBuilder<E> builder = 39 new SortedSetTestSuiteBuilder<E>(); 40 builder.usingGenerator(generator); 41 return builder; 42 } 43 getTesters()44 @Override protected List<Class<? extends AbstractTester>> getTesters() { 45 List<Class<? extends AbstractTester>> testers = 46 Helpers.copyToList(super.getTesters()); 47 testers.add(SortedSetNavigationTester.class); 48 return testers; 49 } 50 createTestSuite()51 @Override public TestSuite createTestSuite() { 52 if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) { 53 List<Feature<?>> features = Helpers.copyToList(getFeatures()); 54 features.add(CollectionFeature.KNOWN_ORDER); 55 withFeatures(features); 56 } 57 return super.createTestSuite(); 58 } 59 60 @Override createDerivedSuites(FeatureSpecificTestSuiteBuilder< ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)61 protected List<TestSuite> createDerivedSuites(FeatureSpecificTestSuiteBuilder< 62 ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 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 */ createSubsetSuite(final FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder, final Bound from, final Bound to)81 final TestSuite createSubsetSuite(final FeatureSpecificTestSuiteBuilder<?, 82 ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 83 parentBuilder, final Bound from, final Bound to) { 84 final TestSortedSetGenerator<E> delegate 85 = (TestSortedSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator(); 86 87 List<Feature<?>> features = new ArrayList<Feature<?>>(); 88 features.addAll(parentBuilder.getFeatures()); 89 features.remove(CollectionFeature.ALLOWS_NULL_VALUES); 90 features.add(CollectionFeature.SUBSET_VIEW); 91 92 return newBuilderUsing(delegate, to, from) 93 .named(parentBuilder.getName() + " subSet " + from + "-" + to) 94 .withFeatures(features) 95 .suppressing(parentBuilder.getSuppressedTests()) 96 .createTestSuite(); 97 } 98 99 /** Like using() but overrideable by NavigableSetTestSuiteBuilder. */ newBuilderUsing( TestSortedSetGenerator<E> delegate, Bound to, Bound from)100 SortedSetTestSuiteBuilder<E> newBuilderUsing( 101 TestSortedSetGenerator<E> delegate, Bound to, Bound from) { 102 return using(new SortedSetSubsetTestSetGenerator<E>(delegate, to, from)); 103 } 104 } 105