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;
022
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.collect.testing.features.Feature;
025import com.google.common.collect.testing.testers.CollectionSerializationEqualTester;
026import com.google.common.collect.testing.testers.SetAddAllTester;
027import com.google.common.collect.testing.testers.SetAddTester;
028import com.google.common.collect.testing.testers.SetCreationTester;
029import com.google.common.collect.testing.testers.SetEqualsTester;
030import com.google.common.collect.testing.testers.SetHashCodeTester;
031import com.google.common.collect.testing.testers.SetRemoveTester;
032import com.google.common.testing.SerializableTester;
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          SetTestSuiteBuilder.using(
079                  new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator()))
080              .named(getName() + " reserialized")
081              .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures()))
082              .suppressing(parentBuilder.getSuppressedTests())
083              .withSetUp(parentBuilder.getSetUp())
084              .withTearDown(parentBuilder.getTearDown())
085              .createTestSuite());
086    }
087    return derivedSuites;
088  }
089
090  static class ReserializedSetGenerator<E> implements TestSetGenerator<E> {
091    final OneSizeTestContainerGenerator<Collection<E>, E> gen;
092
093    private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) {
094      this.gen = gen;
095    }
096
097    @Override
098    public SampleElements<E> samples() {
099      return gen.samples();
100    }
101
102    @Override
103    public Set<E> create(Object... elements) {
104      return (Set<E>) SerializableTester.reserialize(gen.create(elements));
105    }
106
107    @Override
108    public E[] createArray(int length) {
109      return gen.createArray(length);
110    }
111
112    @Override
113    public Iterable<E> order(List<E> insertionOrder) {
114      return gen.order(insertionOrder);
115    }
116  }
117
118  private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) {
119    Set<Feature<?>> derivedFeatures = new HashSet<>(features);
120    derivedFeatures.remove(SERIALIZABLE);
121    derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS);
122    return derivedFeatures;
123  }
124}