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 static com.google.common.collect.testing.Helpers.castOrCopyToList; 20 import static java.util.Collections.reverse; 21 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.testing.DerivedCollectionGenerators.Bound; 24 import com.google.common.collect.testing.DerivedCollectionGenerators.ForwardingTestMapGenerator; 25 import com.google.common.collect.testing.DerivedCollectionGenerators.SortedMapSubmapTestMapGenerator; 26 import com.google.common.collect.testing.features.Feature; 27 import com.google.common.collect.testing.testers.NavigableMapNavigationTester; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Map.Entry; 32 import java.util.NavigableMap; 33 import java.util.SortedMap; 34 import junit.framework.TestSuite; 35 36 /** 37 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a NavigableMap 38 * implementation. 39 */ 40 @GwtIncompatible 41 public class NavigableMapTestSuiteBuilder<K, V> extends SortedMapTestSuiteBuilder<K, V> { using( TestSortedMapGenerator<K, V> generator)42 public static <K, V> NavigableMapTestSuiteBuilder<K, V> using( 43 TestSortedMapGenerator<K, V> generator) { 44 NavigableMapTestSuiteBuilder<K, V> result = new NavigableMapTestSuiteBuilder<>(); 45 result.usingGenerator(generator); 46 return result; 47 } 48 49 @Override getTesters()50 protected List<Class<? extends AbstractTester>> getTesters() { 51 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 52 testers.add(NavigableMapNavigationTester.class); 53 return testers; 54 } 55 56 @Override createDerivedSuites( FeatureSpecificTestSuiteBuilder< ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> parentBuilder)57 protected List<TestSuite> createDerivedSuites( 58 FeatureSpecificTestSuiteBuilder< 59 ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> 60 parentBuilder) { 61 List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder); 62 63 if (!parentBuilder.getFeatures().contains(NoRecurse.DESCENDING)) { 64 derivedSuites.add(createDescendingSuite(parentBuilder)); 65 } 66 67 if (!parentBuilder.getFeatures().contains(NoRecurse.SUBMAP)) { 68 // Other combinations are inherited from SortedMapTestSuiteBuilder. 69 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE)); 70 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND)); 71 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE)); 72 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE)); 73 derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE)); 74 } 75 76 return derivedSuites; 77 } 78 79 @Override createDerivedKeySetSuite( TestSetGenerator<K> keySetGenerator)80 protected NavigableSetTestSuiteBuilder<K> createDerivedKeySetSuite( 81 TestSetGenerator<K> keySetGenerator) { 82 return NavigableSetTestSuiteBuilder.using((TestSortedSetGenerator<K>) keySetGenerator); 83 } 84 85 public static final class NavigableMapSubmapTestMapGenerator<K, V> 86 extends SortedMapSubmapTestMapGenerator<K, V> { NavigableMapSubmapTestMapGenerator( TestSortedMapGenerator<K, V> delegate, Bound to, Bound from)87 public NavigableMapSubmapTestMapGenerator( 88 TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) { 89 super(delegate, to, from); 90 } 91 92 @Override createSubMap(SortedMap<K, V> sortedMap, K firstExclusive, K lastExclusive)93 NavigableMap<K, V> createSubMap(SortedMap<K, V> sortedMap, K firstExclusive, K lastExclusive) { 94 NavigableMap<K, V> map = (NavigableMap<K, V>) sortedMap; 95 if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) { 96 return map.headMap(lastInclusive, true); 97 } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) { 98 return map.tailMap(firstExclusive, false); 99 } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) { 100 return map.subMap(firstExclusive, false, lastExclusive, false); 101 } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) { 102 return map.subMap(firstExclusive, false, lastInclusive, true); 103 } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) { 104 return map.subMap(firstInclusive, true, lastInclusive, true); 105 } else { 106 return (NavigableMap<K, V>) super.createSubMap(map, firstExclusive, lastExclusive); 107 } 108 } 109 } 110 111 @Override newBuilderUsing( TestSortedMapGenerator<K, V> delegate, Bound to, Bound from)112 public NavigableMapTestSuiteBuilder<K, V> newBuilderUsing( 113 TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) { 114 return subSuiteUsing(new NavigableMapSubmapTestMapGenerator<K, V>(delegate, to, from)); 115 } 116 117 /** Create a suite whose maps are descending views of other maps. */ createDescendingSuite( final FeatureSpecificTestSuiteBuilder< ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> parentBuilder)118 private TestSuite createDescendingSuite( 119 final FeatureSpecificTestSuiteBuilder< 120 ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>> 121 parentBuilder) { 122 final TestSortedMapGenerator<K, V> delegate = 123 (TestSortedMapGenerator<K, V>) parentBuilder.getSubjectGenerator().getInnerGenerator(); 124 125 List<Feature<?>> features = new ArrayList<>(); 126 features.add(NoRecurse.DESCENDING); 127 features.addAll(parentBuilder.getFeatures()); 128 129 return subSuiteUsing(new DescendingTestMapGenerator<K, V>(delegate)) 130 .named(parentBuilder.getName() + " descending") 131 .withFeatures(features) 132 .suppressing(parentBuilder.getSuppressedTests()) 133 .createTestSuite(); 134 } 135 subSuiteUsing(TestSortedMapGenerator<K, V> generator)136 NavigableMapTestSuiteBuilder<K, V> subSuiteUsing(TestSortedMapGenerator<K, V> generator) { 137 return using(generator); 138 } 139 140 static class DescendingTestMapGenerator<K, V> extends ForwardingTestMapGenerator<K, V> 141 implements TestSortedMapGenerator<K, V> { DescendingTestMapGenerator(TestSortedMapGenerator<K, V> delegate)142 DescendingTestMapGenerator(TestSortedMapGenerator<K, V> delegate) { 143 super(delegate); 144 } 145 146 @Override create(Object... entries)147 public NavigableMap<K, V> create(Object... entries) { 148 NavigableMap<K, V> map = (NavigableMap<K, V>) delegate.create(entries); 149 return map.descendingMap(); 150 } 151 152 @Override order(List<Entry<K, V>> insertionOrder)153 public Iterable<Entry<K, V>> order(List<Entry<K, V>> insertionOrder) { 154 insertionOrder = castOrCopyToList(delegate.order(insertionOrder)); 155 reverse(insertionOrder); 156 return insertionOrder; 157 } 158 delegate()159 TestSortedMapGenerator<K, V> delegate() { 160 return (TestSortedMapGenerator<K, V>) delegate; 161 } 162 163 @Override belowSamplesLesser()164 public Entry<K, V> belowSamplesLesser() { 165 return delegate().aboveSamplesGreater(); 166 } 167 168 @Override belowSamplesGreater()169 public Entry<K, V> belowSamplesGreater() { 170 return delegate().aboveSamplesLesser(); 171 } 172 173 @Override aboveSamplesLesser()174 public Entry<K, V> aboveSamplesLesser() { 175 return delegate().belowSamplesGreater(); 176 } 177 178 @Override aboveSamplesGreater()179 public Entry<K, V> aboveSamplesGreater() { 180 return delegate().belowSamplesLesser(); 181 } 182 } 183 } 184