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> { using(TestSetGenerator<E> generator)47 public static <E> SetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) { 48 return new SetTestSuiteBuilder<E>().usingGenerator(generator); 49 } 50 51 @SuppressWarnings("rawtypes") // class literals 52 @Override getTesters()53 protected List<Class<? extends AbstractTester>> getTesters() { 54 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 55 56 testers.add(CollectionSerializationEqualTester.class); 57 testers.add(SetAddAllTester.class); 58 testers.add(SetAddTester.class); 59 testers.add(SetCreationTester.class); 60 testers.add(SetHashCodeTester.class); 61 testers.add(SetEqualsTester.class); 62 testers.add(SetRemoveTester.class); 63 // SetRemoveAllTester doesn't exist because, Sets not permitting 64 // duplicate elements, there are no tests for Set.removeAll() that aren't 65 // covered by CollectionRemoveAllTester. 66 return testers; 67 } 68 69 @Override createDerivedSuites( FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)70 protected List<TestSuite> createDerivedSuites( 71 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 72 parentBuilder) { 73 List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); 74 75 if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { 76 derivedSuites.add( 77 SetTestSuiteBuilder.using( 78 new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator())) 79 .named(getName() + " reserialized") 80 .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) 81 .suppressing(parentBuilder.getSuppressedTests()) 82 .withSetUp(parentBuilder.getSetUp()) 83 .withTearDown(parentBuilder.getTearDown()) 84 .createTestSuite()); 85 } 86 return derivedSuites; 87 } 88 89 static class ReserializedSetGenerator<E> implements TestSetGenerator<E> { 90 final OneSizeTestContainerGenerator<Collection<E>, E> gen; 91 ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen)92 private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 93 this.gen = gen; 94 } 95 96 @Override samples()97 public SampleElements<E> samples() { 98 return gen.samples(); 99 } 100 101 @Override create(Object... elements)102 public Set<E> create(Object... elements) { 103 return (Set<E>) SerializableTester.reserialize(gen.create(elements)); 104 } 105 106 @Override createArray(int length)107 public E[] createArray(int length) { 108 return gen.createArray(length); 109 } 110 111 @Override order(List<E> insertionOrder)112 public Iterable<E> order(List<E> insertionOrder) { 113 return gen.order(insertionOrder); 114 } 115 } 116 computeReserializedCollectionFeatures(Set<Feature<?>> features)117 private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { 118 Set<Feature<?>> derivedFeatures = new HashSet<>(features); 119 derivedFeatures.remove(SERIALIZABLE); 120 derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS); 121 return derivedFeatures; 122 } 123 } 124