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> {
using(TestCollectionGenerator<E> generator)43   public static <E> CollectionTestSuiteBuilder<E> using(TestCollectionGenerator<E> generator) {
44     return new CollectionTestSuiteBuilder<E>().usingGenerator(generator);
45   }
46 
47   @Override
createDerivedSuites( FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)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               .createTestSuite());
61     }
62     return derivedSuites;
63   }
64 
65   static class ReserializedCollectionGenerator<E> implements TestCollectionGenerator<E> {
66     final OneSizeTestContainerGenerator<Collection<E>, E> gen;
67 
ReserializedCollectionGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen)68     private ReserializedCollectionGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) {
69       this.gen = gen;
70     }
71 
72     @Override
samples()73     public SampleElements<E> samples() {
74       return gen.samples();
75     }
76 
77     @Override
create(Object... elements)78     public Collection<E> create(Object... elements) {
79       return SerializableTester.reserialize(gen.create(elements));
80     }
81 
82     @Override
createArray(int length)83     public E[] createArray(int length) {
84       return gen.createArray(length);
85     }
86 
87     @Override
order(List<E> insertionOrder)88     public Iterable<E> order(List<E> insertionOrder) {
89       return gen.order(insertionOrder);
90     }
91   }
92 
computeReserializedCollectionFeatures(Set<Feature<?>> features)93   private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) {
94     Set<Feature<?>> derivedFeatures = new HashSet<>(features);
95     derivedFeatures.remove(SERIALIZABLE);
96     derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS);
97     return derivedFeatures;
98   }
99 }
100