001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing; 018 019import static com.google.common.collect.testing.Helpers.copyToList; 020import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE; 021import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS; 022import static com.google.common.testing.SerializableTester.reserialize; 023 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.collect.testing.features.Feature; 026import com.google.common.collect.testing.testers.CollectionSerializationEqualTester; 027import com.google.common.collect.testing.testers.SetAddAllTester; 028import com.google.common.collect.testing.testers.SetAddTester; 029import com.google.common.collect.testing.testers.SetCreationTester; 030import com.google.common.collect.testing.testers.SetEqualsTester; 031import com.google.common.collect.testing.testers.SetHashCodeTester; 032import com.google.common.collect.testing.testers.SetRemoveTester; 033import java.util.ArrayList; 034import java.util.Collection; 035import java.util.HashSet; 036import java.util.List; 037import java.util.Set; 038import junit.framework.TestSuite; 039 040/** 041 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a Set implementation. 042 * 043 * @author George van den Driessche 044 */ 045@GwtIncompatible 046public class SetTestSuiteBuilder<E> 047 extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> { 048 public static <E> SetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) { 049 return new SetTestSuiteBuilder<E>().usingGenerator(generator); 050 } 051 052 @SuppressWarnings("rawtypes") // class literals 053 @Override 054 protected List<Class<? extends AbstractTester>> getTesters() { 055 List<Class<? extends AbstractTester>> testers = copyToList(super.getTesters()); 056 057 testers.add(CollectionSerializationEqualTester.class); 058 testers.add(SetAddAllTester.class); 059 testers.add(SetAddTester.class); 060 testers.add(SetCreationTester.class); 061 testers.add(SetHashCodeTester.class); 062 testers.add(SetEqualsTester.class); 063 testers.add(SetRemoveTester.class); 064 // SetRemoveAllTester doesn't exist because, Sets not permitting 065 // duplicate elements, there are no tests for Set.removeAll() that aren't 066 // covered by CollectionRemoveAllTester. 067 return testers; 068 } 069 070 @Override 071 protected List<TestSuite> createDerivedSuites( 072 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 073 parentBuilder) { 074 List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); 075 076 if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { 077 derivedSuites.add( 078 using(new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator())) 079 .named(getName() + " reserialized") 080 .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) 081 .suppressing(parentBuilder.getSuppressedTests()) 082 .withSetUp(parentBuilder.getSetUp()) 083 .withTearDown(parentBuilder.getTearDown()) 084 .createTestSuite()); 085 } 086 return derivedSuites; 087 } 088 089 private static final class ReserializedSetGenerator<E> implements TestSetGenerator<E> { 090 final OneSizeTestContainerGenerator<Collection<E>, E> gen; 091 092 private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 093 this.gen = gen; 094 } 095 096 @Override 097 public SampleElements<E> samples() { 098 return gen.samples(); 099 } 100 101 @Override 102 public Set<E> create(Object... elements) { 103 return (Set<E>) reserialize(gen.create(elements)); 104 } 105 106 @Override 107 public E[] createArray(int length) { 108 return gen.createArray(length); 109 } 110 111 @Override 112 public Iterable<E> order(List<E> insertionOrder) { 113 return gen.order(insertionOrder); 114 } 115 } 116 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}