SetTestSuiteBuilder.java revision 3c77433663281544363151bf284b0240dfd22a42
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
17package com.google.common.collect.testing;
18
19import com.google.common.collect.testing.features.CollectionFeature;
20import com.google.common.collect.testing.features.Feature;
21import com.google.common.collect.testing.testers.CollectionSerializationEqualTester;
22import com.google.common.collect.testing.testers.SetAddAllTester;
23import com.google.common.collect.testing.testers.SetAddTester;
24import com.google.common.collect.testing.testers.SetCreationTester;
25import com.google.common.collect.testing.testers.SetEqualsTester;
26import com.google.common.collect.testing.testers.SetHashCodeTester;
27import com.google.common.collect.testing.testers.SetRemoveTester;
28import com.google.common.testing.SerializableTester;
29
30import junit.framework.TestSuite;
31
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
37
38/**
39 * Creates, based on your criteria, a JUnit test suite that exhaustively tests
40 * a Set implementation.
41 *
42 * @author George van den Driessche
43 */
44public class SetTestSuiteBuilder<E>
45    extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> {
46  public static <E> SetTestSuiteBuilder<E> using(
47      TestSetGenerator<E> generator) {
48    return new SetTestSuiteBuilder<E>().usingGenerator(generator);
49  }
50
51  @Override protected List<Class<? extends AbstractTester>> getTesters() {
52    List<Class<? extends AbstractTester>> testers
53        = Helpers.copyToList(super.getTesters());
54
55    testers.add(CollectionSerializationEqualTester.class);
56    testers.add(SetAddAllTester.class);
57    testers.add(SetAddTester.class);
58    testers.add(SetCreationTester.class);
59    testers.add(SetHashCodeTester.class);
60    testers.add(SetEqualsTester.class);
61    testers.add(SetRemoveTester.class);
62    // SetRemoveAllTester doesn't exist because, Sets not permitting
63    // duplicate elements, there are no tests for Set.removeAll() that aren't
64    // covered by CollectionRemoveAllTester.
65    return testers;
66  }
67
68  @Override
69  protected
70      List<TestSuite>
71      createDerivedSuites(
72          FeatureSpecificTestSuiteBuilder<
73              ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder) {
74    List<TestSuite> derivedSuites = new ArrayList<TestSuite>(
75        super.createDerivedSuites(parentBuilder));
76
77    if (parentBuilder.getFeatures().contains(CollectionFeature.SERIALIZABLE)) {
78      derivedSuites.add(SetTestSuiteBuilder
79          .using(new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator()))
80          .named(getName() + " reserialized")
81          .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures()))
82          .suppressing(parentBuilder.getSuppressedTests())
83          .createTestSuite());
84    }
85    return derivedSuites;
86  }
87
88  static class ReserializedSetGenerator<E> implements TestSetGenerator<E>{
89    final OneSizeTestContainerGenerator<Collection<E>, E> gen;
90
91    private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) {
92      this.gen = gen;
93    }
94
95    @Override
96    public SampleElements<E> samples() {
97      return gen.samples();
98    }
99
100    @Override
101    public Set<E> create(Object... elements) {
102      return (Set<E>) SerializableTester.reserialize(gen.create(elements));
103    }
104
105    @Override
106    public E[] createArray(int length) {
107      return gen.createArray(length);
108    }
109
110    @Override
111    public Iterable<E> order(List<E> insertionOrder) {
112      return gen.order(insertionOrder);
113    }
114  }
115
116  private static Set<Feature<?>> computeReserializedCollectionFeatures(
117      Set<Feature<?>> features) {
118    Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>();
119    derivedFeatures.addAll(features);
120    derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
121    derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS);
122    return derivedFeatures;
123  }
124}
125