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<E>(); 038 builder.usingGenerator(generator); 039 return builder; 040 } 041 042 @Override 043 protected List<Class<? extends AbstractTester>> getTesters() { 044 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 045 testers.add(SortedSetNavigationTester.class); 046 return testers; 047 } 048 049 @Override 050 public TestSuite createTestSuite() { 051 if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) { 052 List<Feature<?>> features = Helpers.copyToList(getFeatures()); 053 features.add(CollectionFeature.KNOWN_ORDER); 054 withFeatures(features); 055 } 056 return super.createTestSuite(); 057 } 058 059 @Override 060 protected List<TestSuite> createDerivedSuites( 061 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 062 parentBuilder) { 063 List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder); 064 065 if (!parentBuilder.getFeatures().contains(CollectionFeature.SUBSET_VIEW)) { 066 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE)); 067 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND)); 068 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE)); 069 } 070 071 return derivedSuites; 072 } 073 074 /** 075 * Creates a suite whose set has some elements filtered out of view. 076 * 077 * <p>Because the set may be ascending or descending, this test must derive the relative order of 078 * these extreme values rather than relying on their regular sort ordering. 079 */ 080 final TestSuite createSubsetSuite( 081 final FeatureSpecificTestSuiteBuilder< 082 ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 083 parentBuilder, 084 final Bound from, 085 final Bound to) { 086 final 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}