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.collect.testing.testers.CollectionSerializationEqualTester;
025import com.google.common.collect.testing.testers.SetAddAllTester;
026import com.google.common.collect.testing.testers.SetAddTester;
027import com.google.common.collect.testing.testers.SetCreationTester;
028import com.google.common.collect.testing.testers.SetEqualsTester;
029import com.google.common.collect.testing.testers.SetHashCodeTester;
030import com.google.common.collect.testing.testers.SetRemoveTester;
031import com.google.common.testing.SerializableTester;
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.HashSet;
035import java.util.List;
036import java.util.Set;
037import junit.framework.TestSuite;
038
039/**
040 * Creates, based on your criteria, a JUnit test suite that exhaustively tests a Set implementation.
041 *
042 * @author George van den Driessche
043 */
044@GwtIncompatible
045public class SetTestSuiteBuilder<E>
046    extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> {
047  public static <E> SetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) {
048    return new SetTestSuiteBuilder<E>().usingGenerator(generator);
049  }
050
051  @SuppressWarnings("rawtypes") // class literals
052  @Override
053  protected List<Class<? extends AbstractTester>> getTesters() {
054    List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
055
056    testers.add(CollectionSerializationEqualTester.class);
057    testers.add(SetAddAllTester.class);
058    testers.add(SetAddTester.class);
059    testers.add(SetCreationTester.class);
060    testers.add(SetHashCodeTester.class);
061    testers.add(SetEqualsTester.class);
062    testers.add(SetRemoveTester.class);
063    // SetRemoveAllTester doesn't exist because, Sets not permitting
064    // duplicate elements, there are no tests for Set.removeAll() that aren't
065    // covered by CollectionRemoveAllTester.
066    return testers;
067  }
068
069  @Override
070  protected List<TestSuite> createDerivedSuites(
071      FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
072          parentBuilder) {
073    List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder));
074
075    if (parentBuilder.getFeatures().contains(SERIALIZABLE)) {
076      derivedSuites.add(
077          SetTestSuiteBuilder.using(
078                  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  static 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>) SerializableTester.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}