| 1 |
/* |
| 2 |
* Copyright (C) 2008 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.SERIALIZABLE; |
| 20 |
import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS; |
| 21 |
|
| 22 |
import com.google.common.annotations.GwtIncompatible; |
| 23 |
import com.google.common.collect.testing.features.Feature; |
| 24 |
import com.google.common.testing.SerializableTester; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Collection; |
| 27 |
import java.util.HashSet; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Set; |
| 30 |
import junit.framework.TestSuite; |
| 31 |
|
| 32 |
/** |
| 33 |
* Concrete instantiation of {@link AbstractCollectionTestSuiteBuilder} for testing collections that |
| 34 |
* do not have a more specific tester like {@link ListTestSuiteBuilder} or {@link |
| 35 |
* SetTestSuiteBuilder}. |
| 36 |
* |
| 37 |
* @author Chris Povirk |
| 38 |
* @author Louis Wasserman |
| 39 |
*/ |
| 40 |
@GwtIncompatible |
| 41 |
public class CollectionTestSuiteBuilder<E> |
| 42 |
extends AbstractCollectionTestSuiteBuilder<CollectionTestSuiteBuilder<E>, E> { |
| 43 |
public static <E> CollectionTestSuiteBuilder<E> using(TestCollectionGenerator<E> generator) { |
| 44 |
return new CollectionTestSuiteBuilder<E>().usingGenerator(generator); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected List<TestSuite> createDerivedSuites( |
| 49 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 50 |
parentBuilder) { |
| 51 |
List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); |
| 52 |
|
| 53 |
if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { |
| 54 |
derivedSuites.add( |
| 55 |
CollectionTestSuiteBuilder.using( |
| 56 |
new ReserializedCollectionGenerator<E>(parentBuilder.getSubjectGenerator())) |
| 57 |
.named(getName() + " reserialized") |
| 58 |
.withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) |
| 59 |
.suppressing(parentBuilder.getSuppressedTests()) |
| 60 |
.withSetUp(parentBuilder.getSetUp()) |
| 61 |
.withTearDown(parentBuilder.getTearDown()) |
| 62 |
.createTestSuite()); |
| 63 |
} |
| 64 |
return derivedSuites; |
| 65 |
} |
| 66 |
|
| 67 |
static class ReserializedCollectionGenerator<E> implements TestCollectionGenerator<E> { |
| 68 |
final OneSizeTestContainerGenerator<Collection<E>, E> gen; |
| 69 |
|
| 70 |
private ReserializedCollectionGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { |
| 71 |
this.gen = gen; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public SampleElements<E> samples() { |
| 76 |
return gen.samples(); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public Collection<E> create(Object... elements) { |
| 81 |
return SerializableTester.reserialize(gen.create(elements)); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public E[] createArray(int length) { |
| 86 |
return gen.createArray(length); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public Iterable<E> order(List<E> insertionOrder) { |
| 91 |
return gen.order(insertionOrder); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { |
| 96 |
Set<Feature<?>> derivedFeatures = new HashSet<>(features); |
| 97 |
derivedFeatures.remove(SERIALIZABLE); |
| 98 |
derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS); |
| 99 |
return derivedFeatures; |
| 100 |
} |
| 101 |
} |