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.testing.SerializableTester; 22 23 import junit.framework.TestSuite; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.HashSet; 28 import java.util.List; 29 import java.util.Set; 30 31 /** 32 * Concrete instantiation of {@link AbstractCollectionTestSuiteBuilder} for 33 * testing collections that do not have a more specific tester like 34 * {@link ListTestSuiteBuilder} or {@link SetTestSuiteBuilder}. 35 * 36 * @author Chris Povirk 37 * @author Louis Wasserman 38 */ 39 public class CollectionTestSuiteBuilder<E> 40 extends AbstractCollectionTestSuiteBuilder< 41 CollectionTestSuiteBuilder<E>, E> { 42 public static <E> CollectionTestSuiteBuilder<E> using( 43 TestCollectionGenerator<E> generator) { 44 return new CollectionTestSuiteBuilder<E>().usingGenerator(generator); 45 } 46 47 @Override 48 protected 49 List<TestSuite> 50 createDerivedSuites( 51 FeatureSpecificTestSuiteBuilder< 52 ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder) { 53 List<TestSuite> derivedSuites = new ArrayList<TestSuite>( 54 super.createDerivedSuites(parentBuilder)); 55 56 if (parentBuilder.getFeatures().contains(CollectionFeature.SERIALIZABLE)) { 57 derivedSuites.add(CollectionTestSuiteBuilder 58 .using(new ReserializedCollectionGenerator<E>(parentBuilder.getSubjectGenerator())) 59 .named(getName() + " reserialized") 60 .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) 61 .suppressing(parentBuilder.getSuppressedTests()) 62 .createTestSuite()); 63 } 64 return derivedSuites; 65 } 66 67 static class ReserializedCollectionGenerator<E> implements TestCollectionGenerator<E> { 68 final OneSizeTestContainerGenerator<Collection<E>, E> gen; 69 70 private ReserializedCollectionGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 71 this.gen = gen; 72 } 73 74 @Override 75 public SampleElements<E> samples() { 76 return gen.samples(); 77 } 78 79 @Override 80 public Collection<E> create(Object... elements) { 81 return SerializableTester.reserialize(gen.create(elements)); 82 } 83 84 @Override 85 public E[] createArray(int length) { 86 return gen.createArray(length); 87 } 88 89 @Override 90 public Iterable<E> order(List<E> insertionOrder) { 91 return gen.order(insertionOrder); 92 } 93 } 94 95 private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { 96 Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>(); 97 derivedFeatures.addAll(features); 98 derivedFeatures.remove(CollectionFeature.SERIALIZABLE); 99 derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS); 100 return derivedFeatures; 101 } 102 } 103