com.google.common.collect.testing.google.MultisetTestSuiteBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guava-testlib Show documentation
Show all versions of guava-testlib Show documentation
Guava testlib is a set of java classes used for more convenient
unit testing - particularly to assist the tests for Guava itself.
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect.testing.google;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;
import com.google.common.collect.Multisets;
import com.google.common.collect.testing.AbstractCollectionTestSuiteBuilder;
import com.google.common.collect.testing.AbstractTester;
import com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.OneSizeTestContainerGenerator;
import com.google.common.collect.testing.SampleElements;
import com.google.common.collect.testing.SetTestSuiteBuilder;
import com.google.common.collect.testing.TestSetGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.Feature;
import com.google.common.collect.testing.testers.CollectionSerializationEqualTester;
import com.google.common.testing.SerializableTester;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.TestSuite;
/**
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a {@code Multiset}
* implementation.
*
* @author Jared Levy
* @author Louis Wasserman
*/
@GwtIncompatible
public class MultisetTestSuiteBuilder
extends AbstractCollectionTestSuiteBuilder, E> {
public static MultisetTestSuiteBuilder using(TestMultisetGenerator generator) {
return new MultisetTestSuiteBuilder().usingGenerator(generator);
}
public enum NoRecurse implements Feature {
NO_ENTRY_SET;
@Override
public Set> getImpliedFeatures() {
return Collections.emptySet();
}
}
@SuppressWarnings("rawtypes") // class literals
@Override
protected List> getTesters() {
List> testers = Helpers.copyToList(super.getTesters());
testers.add(CollectionSerializationEqualTester.class);
testers.add(MultisetAddTester.class);
testers.add(MultisetContainsTester.class);
testers.add(MultisetCountTester.class);
testers.add(MultisetElementSetTester.class);
testers.add(MultisetEqualsTester.class);
testers.add(MultisetForEachEntryTester.class);
testers.add(MultisetReadsTester.class);
testers.add(MultisetSetCountConditionallyTester.class);
testers.add(MultisetSetCountUnconditionallyTester.class);
testers.add(MultisetRemoveTester.class);
testers.add(MultisetEntrySetTester.class);
testers.add(MultisetIteratorTester.class);
testers.add(MultisetSerializationTester.class);
return testers;
}
private static Set> computeEntrySetFeatures(Set> features) {
Set> derivedFeatures = new HashSet<>(features);
derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
derivedFeatures.remove(CollectionFeature.ALLOWS_NULL_VALUES);
derivedFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION);
if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
}
return derivedFeatures;
}
static Set> computeElementSetFeatures(Set> features) {
Set> derivedFeatures = new HashSet<>(features);
derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
}
return derivedFeatures;
}
private static Set> computeReserializedMultisetFeatures(Set> features) {
Set> derivedFeatures = new HashSet<>(features);
derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS);
return derivedFeatures;
}
@Override
protected List createDerivedSuites(
FeatureSpecificTestSuiteBuilder, ? extends OneSizeTestContainerGenerator, E>>
parentBuilder) {
List derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder));
derivedSuites.add(createElementSetTestSuite(parentBuilder));
if (!parentBuilder.getFeatures().contains(NoRecurse.NO_ENTRY_SET)) {
derivedSuites.add(
SetTestSuiteBuilder.using(new EntrySetGenerator(parentBuilder.getSubjectGenerator()))
.named(getName() + ".entrySet")
.withFeatures(computeEntrySetFeatures(parentBuilder.getFeatures()))
.suppressing(parentBuilder.getSuppressedTests())
.withSetUp(parentBuilder.getSetUp())
.withTearDown(parentBuilder.getTearDown())
.createTestSuite());
}
if (parentBuilder.getFeatures().contains(CollectionFeature.SERIALIZABLE)) {
derivedSuites.add(
MultisetTestSuiteBuilder.using(
new ReserializedMultisetGenerator(parentBuilder.getSubjectGenerator()))
.named(getName() + " reserialized")
.withFeatures(computeReserializedMultisetFeatures(parentBuilder.getFeatures()))
.suppressing(parentBuilder.getSuppressedTests())
.withSetUp(parentBuilder.getSetUp())
.withTearDown(parentBuilder.getTearDown())
.createTestSuite());
}
return derivedSuites;
}
TestSuite createElementSetTestSuite(
FeatureSpecificTestSuiteBuilder, ? extends OneSizeTestContainerGenerator, E>>
parentBuilder) {
return SetTestSuiteBuilder.using(
new ElementSetGenerator(parentBuilder.getSubjectGenerator()))
.named(getName() + ".elementSet")
.withFeatures(computeElementSetFeatures(parentBuilder.getFeatures()))
.suppressing(parentBuilder.getSuppressedTests())
.withSetUp(parentBuilder.getSetUp())
.withTearDown(parentBuilder.getTearDown())
.createTestSuite();
}
static class ElementSetGenerator implements TestSetGenerator {
final OneSizeTestContainerGenerator, E> gen;
ElementSetGenerator(OneSizeTestContainerGenerator, E> gen) {
this.gen = gen;
}
@Override
public SampleElements samples() {
return gen.samples();
}
@Override
public Set create(Object... elements) {
Object[] duplicated = new Object[elements.length * 2];
for (int i = 0; i < elements.length; i++) {
duplicated[i] = elements[i];
duplicated[i + elements.length] = elements[i];
}
return ((Multiset) gen.create(duplicated)).elementSet();
}
@Override
public E[] createArray(int length) {
return gen.createArray(length);
}
@Override
public Iterable order(List insertionOrder) {
return gen.order(new ArrayList(new LinkedHashSet(insertionOrder)));
}
}
static class EntrySetGenerator implements TestSetGenerator> {
final OneSizeTestContainerGenerator, E> gen;
private EntrySetGenerator(OneSizeTestContainerGenerator, E> gen) {
this.gen = gen;
}
@Override
public SampleElements> samples() {
SampleElements samples = gen.samples();
return new SampleElements<>(
Multisets.immutableEntry(samples.e0(), 3),
Multisets.immutableEntry(samples.e1(), 4),
Multisets.immutableEntry(samples.e2(), 1),
Multisets.immutableEntry(samples.e3(), 5),
Multisets.immutableEntry(samples.e4(), 2));
}
@Override
public Set> create(Object... entries) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy