Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/SetTestSuiteBuilder.java

raw
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.collect.testing.testers.CollectionSerializationEqualTester;
25 import com.google.common.collect.testing.testers.SetAddAllTester;
26 import com.google.common.collect.testing.testers.SetAddTester;
27 import com.google.common.collect.testing.testers.SetCreationTester;
28 import com.google.common.collect.testing.testers.SetEqualsTester;
29 import com.google.common.collect.testing.testers.SetHashCodeTester;
30 import com.google.common.collect.testing.testers.SetRemoveTester;
31 import com.google.common.testing.SerializableTester;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37 import junit.framework.TestSuite;
38
39 /**
40 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a Set implementation.
41 *
42 * @author George van den Driessche
43 */
44 @GwtIncompatible
45 public class SetTestSuiteBuilder<E>
46 extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> {
47 public static <E> SetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) {
48 return new SetTestSuiteBuilder<E>().usingGenerator(generator);
49 }
50
51 @Override
52 protected List<Class<? extends AbstractTester>> getTesters() {
53 List<Class<? extends AbstractTester>> testers = 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 List<TestSuite> createDerivedSuites(
70 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
71 parentBuilder) {
72 List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder));
73
74 if (parentBuilder.getFeatures().contains(SERIALIZABLE)) {
75 derivedSuites.add(
76 SetTestSuiteBuilder.using(
77 new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator()))
78 .named(getName() + " reserialized")
79 .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures()))
80 .suppressing(parentBuilder.getSuppressedTests())
81 .withSetUp(parentBuilder.getSetUp())
82 .withTearDown(parentBuilder.getTearDown())
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(Set<Feature<?>> features) {
117 Set<Feature<?>> derivedFeatures = new HashSet<>(features);
118 derivedFeatures.remove(SERIALIZABLE);
119 derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS);
120 return derivedFeatures;
121 }
122 }