| 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.annotations.GwtIncompatible; |
| 20 |
import com.google.common.collect.testing.DerivedCollectionGenerators.Bound; |
| 21 |
import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator; |
| 22 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 23 |
import com.google.common.collect.testing.features.Feature; |
| 24 |
import com.google.common.collect.testing.testers.SortedSetNavigationTester; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Collection; |
| 27 |
import java.util.List; |
| 28 |
import junit.framework.TestSuite; |
| 29 |
|
| 30 |
/** |
| 31 |
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a SortedSet |
| 32 |
* implementation. |
| 33 |
*/ |
| 34 |
@GwtIncompatible |
| 35 |
public class SortedSetTestSuiteBuilder<E> extends SetTestSuiteBuilder<E> { |
| 36 |
public static <E> SortedSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) { |
| 37 |
SortedSetTestSuiteBuilder<E> builder = new SortedSetTestSuiteBuilder<>(); |
| 38 |
builder.usingGenerator(generator); |
| 39 |
return builder; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected List<Class<? extends AbstractTester>> getTesters() { |
| 44 |
List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); |
| 45 |
testers.add(SortedSetNavigationTester.class); |
| 46 |
return testers; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public TestSuite createTestSuite() { |
| 51 |
if (!getFeatures().contains(CollectionFeature.KNOWN_ORDER)) { |
| 52 |
List<Feature<?>> features = Helpers.copyToList(getFeatures()); |
| 53 |
features.add(CollectionFeature.KNOWN_ORDER); |
| 54 |
withFeatures(features); |
| 55 |
} |
| 56 |
return super.createTestSuite(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected List<TestSuite> createDerivedSuites( |
| 61 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 62 |
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 the relative order of |
| 78 |
* these extreme values rather than relying on their regular sort ordering. |
| 79 |
*/ |
| 80 |
final TestSuite createSubsetSuite( |
| 81 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 82 |
parentBuilder, |
| 83 |
Bound from, |
| 84 |
Bound to) { |
| 85 |
TestSortedSetGenerator<E> delegate = |
| 86 |
(TestSortedSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator(); |
| 87 |
|
| 88 |
List<Feature<?>> features = new ArrayList<>(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 |
.withSetUp(parentBuilder.getSetUp()) |
| 97 |
.withTearDown(parentBuilder.getTearDown()) |
| 98 |
.createTestSuite(); |
| 99 |
} |
| 100 |
|
| 101 |
/** Like using() but overrideable by NavigableSetTestSuiteBuilder. */ |
| 102 |
SortedSetTestSuiteBuilder<E> newBuilderUsing( |
| 103 |
TestSortedSetGenerator<E> delegate, Bound to, Bound from) { |
| 104 |
return using(new SortedSetSubsetTestSetGenerator<E>(delegate, to, from)); |
| 105 |
} |
| 106 |
} |