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 com.google.common.collect.testing.features.CollectionFeature; 20 import com.google.common.collect.testing.features.Feature; 21 import com.google.common.collect.testing.testers.CollectionSerializationEqualTester; 22 import com.google.common.collect.testing.testers.SetAddAllTester; 23 import com.google.common.collect.testing.testers.SetAddTester; 24 import com.google.common.collect.testing.testers.SetCreationTester; 25 import com.google.common.collect.testing.testers.SetEqualsTester; 26 import com.google.common.collect.testing.testers.SetHashCodeTester; 27 import com.google.common.collect.testing.testers.SetRemoveTester; 28 import com.google.common.testing.SerializableTester; 29 30 import junit.framework.TestSuite; 31 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 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 */ 44 public class SetTestSuiteBuilder<E> 45 extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> { using( TestSetGenerator<E> generator)46 public static <E> SetTestSuiteBuilder<E> using( 47 TestSetGenerator<E> generator) { 48 return new SetTestSuiteBuilder<E>().usingGenerator(generator); 49 } 50 getTesters()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> createDerivedSuites( FeatureSpecificTestSuiteBuilder< ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder)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 ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen)91 private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 92 this.gen = gen; 93 } 94 95 @Override samples()96 public SampleElements<E> samples() { 97 return gen.samples(); 98 } 99 100 @Override create(Object... elements)101 public Set<E> create(Object... elements) { 102 return (Set<E>) SerializableTester.reserialize(gen.create(elements)); 103 } 104 105 @Override createArray(int length)106 public E[] createArray(int length) { 107 return gen.createArray(length); 108 } 109 110 @Override order(List<E> insertionOrder)111 public Iterable<E> order(List<E> insertionOrder) { 112 return gen.order(insertionOrder); 113 } 114 } 115 computeReserializedCollectionFeatures( Set<Feature<?>> features)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