All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.testing.MapTestSuiteBuilder Maven / Gradle / Ivy

Go to download

Guava testlib is a set of java classes used for more convenient unit testing - particularly to assist the tests for Guava itself.

There is a newer version: 17.0
Show newest version
/*
 * 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;

import static com.google.common.collect.testing.DerivedCollectionGenerators.keySetGenerator;

import com.google.common.collect.testing.DerivedCollectionGenerators.MapEntrySetGenerator;
import com.google.common.collect.testing.DerivedCollectionGenerators.MapValueCollectionGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.Feature;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.collect.testing.testers.MapClearTester;
import com.google.common.collect.testing.testers.MapContainsKeyTester;
import com.google.common.collect.testing.testers.MapContainsValueTester;
import com.google.common.collect.testing.testers.MapCreationTester;
import com.google.common.collect.testing.testers.MapEntrySetTester;
import com.google.common.collect.testing.testers.MapEqualsTester;
import com.google.common.collect.testing.testers.MapGetTester;
import com.google.common.collect.testing.testers.MapHashCodeTester;
import com.google.common.collect.testing.testers.MapIsEmptyTester;
import com.google.common.collect.testing.testers.MapPutAllTester;
import com.google.common.collect.testing.testers.MapPutTester;
import com.google.common.collect.testing.testers.MapRemoveTester;
import com.google.common.collect.testing.testers.MapSerializationTester;
import com.google.common.collect.testing.testers.MapSizeTester;
import com.google.common.collect.testing.testers.MapToStringTester;
import com.google.common.testing.SerializableTester;

import junit.framework.TestSuite;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Creates, based on your criteria, a JUnit test suite that exhaustively tests
 * a Map implementation.
 *
 * @author George van den Driessche
 */
public class MapTestSuiteBuilder
    extends PerCollectionSizeTestSuiteBuilder<
        MapTestSuiteBuilder,
        TestMapGenerator, Map, Map.Entry> {
  public static  MapTestSuiteBuilder using(
      TestMapGenerator generator) {
    return new MapTestSuiteBuilder().usingGenerator(generator);
  }

  @SuppressWarnings("unchecked") // Class parameters must be raw.
  @Override protected List> getTesters() {
    return Arrays.>asList(
        MapClearTester.class,
        MapContainsKeyTester.class,
        MapContainsValueTester.class,
        MapCreationTester.class,
        MapEntrySetTester.class,
        MapEqualsTester.class,
        MapGetTester.class,
        MapHashCodeTester.class,
        MapIsEmptyTester.class,
        MapPutTester.class,
        MapPutAllTester.class,
        MapRemoveTester.class,
        MapSerializationTester.class,
        MapSizeTester.class,
        MapToStringTester.class
    );
  }

  @Override
  protected List createDerivedSuites(
      FeatureSpecificTestSuiteBuilder<
          ?,
          ? extends OneSizeTestContainerGenerator, Map.Entry>>
      parentBuilder) {
    // TODO: Once invariant support is added, supply invariants to each of the
    // derived suites, to check that mutations to the derived collections are
    // reflected in the underlying map.

    List derivedSuites = super.createDerivedSuites(parentBuilder);

    if (parentBuilder.getFeatures().contains(CollectionFeature.SERIALIZABLE)) {
      derivedSuites.add(MapTestSuiteBuilder.using(
              new ReserializedMapGenerator(parentBuilder.getSubjectGenerator()))
          .withFeatures(computeReserializedMapFeatures(parentBuilder.getFeatures()))
          .named(parentBuilder.getName() + " reserialized")
          .suppressing(parentBuilder.getSuppressedTests())
          .createTestSuite());
    }

    derivedSuites.add(SetTestSuiteBuilder.using(
            new MapEntrySetGenerator(parentBuilder.getSubjectGenerator()))
        .withFeatures(computeEntrySetFeatures(parentBuilder.getFeatures()))
        .named(parentBuilder.getName() + " entrySet")
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite());

    derivedSuites.add(createDerivedKeySetSuite(
            keySetGenerator(parentBuilder.getSubjectGenerator()))
        .withFeatures(computeKeySetFeatures(parentBuilder.getFeatures()))
        .named(parentBuilder.getName() + " keys")
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite());

    derivedSuites.add(CollectionTestSuiteBuilder.using(
            new MapValueCollectionGenerator(
                parentBuilder.getSubjectGenerator()))
        .named(parentBuilder.getName() + " values")
        .withFeatures(computeValuesCollectionFeatures(
            parentBuilder.getFeatures()))
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite());

    return derivedSuites;
  }

  protected SetTestSuiteBuilder createDerivedKeySetSuite(TestSetGenerator keySetGenerator) {
    return SetTestSuiteBuilder.using(keySetGenerator);
  }

  private static Set> computeReserializedMapFeatures(
      Set> mapFeatures) {
    Set> derivedFeatures = Helpers.copyToSet(mapFeatures);
    derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
    derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS);
    return derivedFeatures;
  }

  private static Set> computeEntrySetFeatures(
      Set> mapFeatures) {
    Set> entrySetFeatures =
        computeCommonDerivedCollectionFeatures(mapFeatures);
    entrySetFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES);
    return entrySetFeatures;
  }

  private static Set> computeKeySetFeatures(
      Set> mapFeatures) {
    Set> keySetFeatures =
        computeCommonDerivedCollectionFeatures(mapFeatures);

    // TODO(user): make this trigger only if the map is a submap
    // currently, the KeySetGenerator won't work properly for a subset of a keyset of a submap
    keySetFeatures.add(CollectionFeature.SUBSET_VIEW);
    if (mapFeatures.contains(MapFeature.ALLOWS_NULL_KEYS)) {
      keySetFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES);
    } else if (mapFeatures.contains(MapFeature.ALLOWS_NULL_QUERIES)) {
      keySetFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES);
    }

    return keySetFeatures;
  }

  private static Set> computeValuesCollectionFeatures(
      Set> mapFeatures) {
    Set> valuesCollectionFeatures =
        computeCommonDerivedCollectionFeatures(mapFeatures);
    if (mapFeatures.contains(MapFeature.ALLOWS_NULL_QUERIES)) {
      valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES);
    }
    if (mapFeatures.contains(MapFeature.ALLOWS_NULL_VALUES)) {
      valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES);
    }

    return valuesCollectionFeatures;
  }

  public static Set> computeCommonDerivedCollectionFeatures(
      Set> mapFeatures) {
    mapFeatures = new HashSet>(mapFeatures);
    Set> derivedFeatures = new HashSet>();
    mapFeatures.remove(CollectionFeature.SERIALIZABLE);
    if (mapFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
      derivedFeatures.add(CollectionFeature.SERIALIZABLE);
    }
    if (mapFeatures.contains(MapFeature.SUPPORTS_REMOVE)) {
      derivedFeatures.add(CollectionFeature.SUPPORTS_REMOVE);
    }
    if (mapFeatures.contains(MapFeature.REJECTS_DUPLICATES_AT_CREATION)) {
      derivedFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION);
    }
    if (mapFeatures.contains(MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION)) {
      derivedFeatures.add(CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION);
    }
    // add the intersection of CollectionFeature.values() and mapFeatures
    for (CollectionFeature feature : CollectionFeature.values()) {
      if (mapFeatures.contains(feature)) {
        derivedFeatures.add(feature);
      }
    }
    // add the intersection of CollectionSize.values() and mapFeatures
    for (CollectionSize size : CollectionSize.values()) {
      if (mapFeatures.contains(size)) {
        derivedFeatures.add(size);
      }
    }
    return derivedFeatures;
  }

  private static class ReserializedMapGenerator
      implements TestMapGenerator {
    private final OneSizeTestContainerGenerator, Map.Entry>
        mapGenerator;

    public ReserializedMapGenerator(
        OneSizeTestContainerGenerator<
            Map, Map.Entry> mapGenerator) {
      this.mapGenerator = mapGenerator;
    }

    @Override
    public SampleElements> samples() {
      return mapGenerator.samples();
    }

    @Override
    public Map.Entry[] createArray(int length) {
      return mapGenerator.createArray(length);
    }

    @Override
    public Iterable> order(
        List> insertionOrder) {
      return mapGenerator.order(insertionOrder);
    }

    @Override
    public Map create(Object... elements) {
      return SerializableTester.reserialize(mapGenerator.create(elements));
    }

    @Override
    public K[] createKeyArray(int length) {
      return ((TestMapGenerator) mapGenerator.getInnerGenerator())
          .createKeyArray(length);
    }

    @Override
    public V[] createValueArray(int length) {
      return ((TestMapGenerator) mapGenerator.getInnerGenerator())
        .createValueArray(length);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy