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.features.CollectionFeature.SERIALIZABLE; 020import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS; 021 022import com.google.common.annotations.GwtIncompatible; 023import com.google.common.collect.testing.features.Feature; 024import com.google.common.testing.SerializableTester; 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.HashSet; 028import java.util.List; 029import java.util.Set; 030import junit.framework.TestSuite; 031 032/** 033 * Concrete instantiation of {@link AbstractCollectionTestSuiteBuilder} for testing collections that 034 * do not have a more specific tester like {@link ListTestSuiteBuilder} or {@link 035 * SetTestSuiteBuilder}. 036 * 037 * @author Chris Povirk 038 * @author Louis Wasserman 039 */ 040@GwtIncompatible 041public class CollectionTestSuiteBuilder<E> 042 extends AbstractCollectionTestSuiteBuilder<CollectionTestSuiteBuilder<E>, E> { 043 public static <E> CollectionTestSuiteBuilder<E> using(TestCollectionGenerator<E> generator) { 044 return new CollectionTestSuiteBuilder<E>().usingGenerator(generator); 045 } 046 047 @Override 048 protected List<TestSuite> createDerivedSuites( 049 FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 050 parentBuilder) { 051 List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); 052 053 if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { 054 derivedSuites.add( 055 CollectionTestSuiteBuilder.using( 056 new ReserializedCollectionGenerator<E>(parentBuilder.getSubjectGenerator())) 057 .named(getName() + " reserialized") 058 .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) 059 .suppressing(parentBuilder.getSuppressedTests()) 060 .withSetUp(parentBuilder.getSetUp()) 061 .withTearDown(parentBuilder.getTearDown()) 062 .createTestSuite()); 063 } 064 return derivedSuites; 065 } 066 067 static class ReserializedCollectionGenerator<E> implements TestCollectionGenerator<E> { 068 final OneSizeTestContainerGenerator<Collection<E>, E> gen; 069 070 private ReserializedCollectionGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 071 this.gen = gen; 072 } 073 074 @Override 075 public SampleElements<E> samples() { 076 return gen.samples(); 077 } 078 079 @Override 080 public Collection<E> create(Object... elements) { 081 return SerializableTester.reserialize(gen.create(elements)); 082 } 083 084 @Override 085 public E[] createArray(int length) { 086 return gen.createArray(length); 087 } 088 089 @Override 090 public Iterable<E> order(List<E> insertionOrder) { 091 return gen.order(insertionOrder); 092 } 093 } 094 095 private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { 096 Set<Feature<?>> derivedFeatures = new HashSet<>(features); 097 derivedFeatures.remove(SERIALIZABLE); 098 derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS); 099 return derivedFeatures; 100 } 101}