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.SortedMapSubmapTestMapGenerator; 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.SortedMapNavigationTester; 24 25 import junit.framework.TestSuite; 26 27 import java.util.ArrayList; 28 import java.util.Collections; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Map.Entry; 32 import java.util.Set; 33 34 /** 35 * Creates, based on your criteria, a JUnit test suite that exhaustively tests 36 * a SortedMap implementation. 37 */ 38 public class SortedMapTestSuiteBuilder<K, V> extends MapTestSuiteBuilder<K, V> { using( TestSortedMapGenerator<K, V> generator)39 public static <K, V> SortedMapTestSuiteBuilder<K, V> using( 40 TestSortedMapGenerator<K, V> generator) { 41 SortedMapTestSuiteBuilder<K, V> result = new SortedMapTestSuiteBuilder<K, V>(); 42 result.usingGenerator(generator); 43 return result; 44 } 45 getTesters()46 @Override protected List<Class<? extends AbstractTester>> getTesters() { 47 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 48 testers.add(SortedMapNavigationTester.class); 49 return testers; 50 } 51 createTestSuite()52 @Override public TestSuite createTestSuite() { 53 if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) { 54 List<Feature<?>> features = Helpers.copyToList(getFeatures()); 55 features.add(CollectionFeature.KNOWN_ORDER); 56 withFeatures(features); 57 } 58 return super.createTestSuite(); 59 } 60 61 @Override createDerivedSuites(FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> parentBuilder)62 protected List<TestSuite> createDerivedSuites(FeatureSpecificTestSuiteBuilder<?, 63 ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> parentBuilder) { 64 List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder); 65 66 if (!parentBuilder.getFeatures().contains(NoRecurse.SUBMAP)) { 67 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.NO_BOUND, Bound.EXCLUSIVE)); 68 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.NO_BOUND)); 69 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.EXCLUSIVE)); 70 } 71 72 return derivedSuites; 73 } 74 createDerivedKeySetSuite( TestSetGenerator<K> keySetGenerator)75 @Override protected SetTestSuiteBuilder<K> createDerivedKeySetSuite( 76 TestSetGenerator<K> keySetGenerator) { 77 return keySetGenerator instanceof TestSortedSetGenerator 78 ? SortedSetTestSuiteBuilder.using((TestSortedSetGenerator<K>) keySetGenerator) 79 : SetTestSuiteBuilder.using(keySetGenerator); 80 } 81 82 /** 83 * To avoid infinite recursion, test suites with these marker features won't 84 * have derived suites created for them. 85 */ 86 enum NoRecurse implements Feature<Void> { 87 SUBMAP, 88 DESCENDING; 89 90 @Override getImpliedFeatures()91 public Set<Feature<? super Void>> getImpliedFeatures() { 92 return Collections.emptySet(); 93 } 94 } 95 96 /** 97 * Creates a suite whose map has some elements filtered out of view. 98 * 99 * <p>Because the map may be ascending or descending, this test must derive 100 * the relative order of these extreme values rather than relying on their 101 * regular sort ordering. 102 */ createSubmapSuite(final FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> parentBuilder, final Bound from, final Bound to)103 final TestSuite createSubmapSuite(final FeatureSpecificTestSuiteBuilder<?, 104 ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> 105 parentBuilder, final Bound from, final Bound to) { 106 final TestSortedMapGenerator<K, V> delegate 107 = (TestSortedMapGenerator<K, V>) parentBuilder.getSubjectGenerator().getInnerGenerator(); 108 109 List<Feature<?>> features = new ArrayList<Feature<?>>(); 110 features.add(NoRecurse.SUBMAP); 111 features.addAll(parentBuilder.getFeatures()); 112 113 return newBuilderUsing(delegate, to, from) 114 .named(parentBuilder.getName() + " subMap " + from + "-" + to) 115 .withFeatures(features) 116 .suppressing(parentBuilder.getSuppressedTests()) 117 .createTestSuite(); 118 } 119 120 /** Like using() but overrideable by NavigableMapTestSuiteBuilder. */ newBuilderUsing( TestSortedMapGenerator<K, V> delegate, Bound to, Bound from)121 SortedMapTestSuiteBuilder<K, V> newBuilderUsing( 122 TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) { 123 return using(new SortedMapSubmapTestMapGenerator<K, V>(delegate, to, from)); 124 } 125 } 126