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.features.CollectionFeature.DESCENDING_VIEW; 20 import static com.google.common.collect.testing.features.CollectionFeature.SUBSET_VIEW; 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.SortedSetSubsetTestSetGenerator; 25 import com.google.common.collect.testing.features.Feature; 26 import com.google.common.collect.testing.testers.NavigableSetNavigationTester; 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.NavigableSet; 32 import java.util.Set; 33 import java.util.SortedSet; 34 import junit.framework.TestSuite; 35 36 /** 37 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a NavigableSet 38 * implementation. 39 */ 40 @GwtIncompatible 41 public final class NavigableSetTestSuiteBuilder<E> extends SortedSetTestSuiteBuilder<E> { using(TestSortedSetGenerator<E> generator)42 public static <E> NavigableSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) { 43 NavigableSetTestSuiteBuilder<E> builder = new NavigableSetTestSuiteBuilder<E>(); 44 builder.usingGenerator(generator); 45 return builder; 46 } 47 48 @Override createDerivedSuites( FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)49 protected List<TestSuite> createDerivedSuites( 50 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 51 parentBuilder) { 52 List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); 53 54 if (!parentBuilder.getFeatures().contains(SUBSET_VIEW)) { 55 // Other combinations are inherited from SortedSetTestSuiteBuilder. 56 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE)); 57 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND)); 58 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE)); 59 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE)); 60 derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE)); 61 } 62 if (!parentBuilder.getFeatures().contains(DESCENDING_VIEW)) { 63 derivedSuites.add(createDescendingSuite(parentBuilder)); 64 } 65 return derivedSuites; 66 } 67 68 public static final class NavigableSetSubsetTestSetGenerator<E> 69 extends SortedSetSubsetTestSetGenerator<E> { NavigableSetSubsetTestSetGenerator( TestSortedSetGenerator<E> delegate, Bound to, Bound from)70 public NavigableSetSubsetTestSetGenerator( 71 TestSortedSetGenerator<E> delegate, Bound to, Bound from) { 72 super(delegate, to, from); 73 } 74 75 @Override createSubSet(SortedSet<E> sortedSet, E firstExclusive, E lastExclusive)76 NavigableSet<E> createSubSet(SortedSet<E> sortedSet, E firstExclusive, E lastExclusive) { 77 NavigableSet<E> set = (NavigableSet<E>) sortedSet; 78 if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) { 79 return set.headSet(lastInclusive, true); 80 } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) { 81 return set.tailSet(firstExclusive, false); 82 } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) { 83 return set.subSet(firstExclusive, false, lastExclusive, false); 84 } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) { 85 return set.subSet(firstExclusive, false, lastInclusive, true); 86 } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) { 87 return set.subSet(firstInclusive, true, lastInclusive, true); 88 } else { 89 return (NavigableSet<E>) super.createSubSet(set, firstExclusive, lastExclusive); 90 } 91 } 92 } 93 94 @Override newBuilderUsing( TestSortedSetGenerator<E> delegate, Bound to, Bound from)95 public NavigableSetTestSuiteBuilder<E> newBuilderUsing( 96 TestSortedSetGenerator<E> delegate, Bound to, Bound from) { 97 return using(new NavigableSetSubsetTestSetGenerator<E>(delegate, to, from)); 98 } 99 100 /** Create a suite whose maps are descending views of other maps. */ createDescendingSuite( final FeatureSpecificTestSuiteBuilder< ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)101 private TestSuite createDescendingSuite( 102 final FeatureSpecificTestSuiteBuilder< 103 ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 104 parentBuilder) { 105 final TestSetGenerator<E> delegate = 106 (TestSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator(); 107 108 List<Feature<?>> features = new ArrayList<>(); 109 features.add(DESCENDING_VIEW); 110 features.addAll(parentBuilder.getFeatures()); 111 112 return NavigableSetTestSuiteBuilder.using( 113 new TestSetGenerator<E>() { 114 115 @Override 116 public SampleElements<E> samples() { 117 return delegate.samples(); 118 } 119 120 @Override 121 public E[] createArray(int length) { 122 return delegate.createArray(length); 123 } 124 125 @Override 126 public Iterable<E> order(List<E> insertionOrder) { 127 List<E> list = new ArrayList<E>(); 128 for (E e : delegate.order(insertionOrder)) { 129 list.add(e); 130 } 131 Collections.reverse(list); 132 return list; 133 } 134 135 @Override 136 public Set<E> create(Object... elements) { 137 NavigableSet<E> navigableSet = (NavigableSet<E>) delegate.create(elements); 138 return navigableSet.descendingSet(); 139 } 140 }) 141 .named(parentBuilder.getName() + " descending") 142 .withFeatures(features) 143 .suppressing(parentBuilder.getSuppressedTests()) 144 .createTestSuite(); 145 } 146 147 @Override 148 protected List<Class<? extends AbstractTester>> getTesters() { 149 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 150 testers.add(NavigableSetNavigationTester.class); 151 return testers; 152 } 153 } 154