Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 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 java.util.List;
 import java.util.Map;
 import java.util.Set;

Creates, based on your criteria, a JUnit test suite that exhaustively tests a BiMap implementation.

Author(s):
Louis Wasserman
 
 public class BiMapTestSuiteBuilder<K, V>
             TestBiMapGenerator<K, V>, BiMap<K, V>, Map.Entry<K, V>> {
   public static <K, V> BiMapTestSuiteBuilder<K, V> using(TestBiMapGenerator<K, V> generator) {
     return new BiMapTestSuiteBuilder<K, V>().usingGenerator(generator);
   }
 
   @Override
   protected List<Class<? extends AbstractTester>> getTesters() {
     List<Class<? extends AbstractTester>> testers =
         new ArrayList<Class<? extends AbstractTester>>();
     testers.add(BiMapPutTester.class);
     testers.add(BiMapInverseTester.class);
     testers.add(BiMapRemoveTester.class);
     testers.add(BiMapClearTester.class);
     return testers;
   }
 
   enum NoRecurse implements Feature<Void> {
     INVERSE;
 
     @Override
     public Set<Feature<? super Void>> getImpliedFeatures() {
       return Collections.emptySet();
     }
   }
 
   @Override
   protected
       List<TestSuite>
       createDerivedSuites(
           FeatureSpecificTestSuiteBuilder<?,
               ? extends OneSizeTestContainerGenerator<BiMap<K, V>, Entry<K, V>>> parentBuilder) {
     List<TestSuitederived = super.createDerivedSuites(parentBuilder);
     // TODO(cpovirk): consider using this approach (derived suites instead of extension) in
     // ListTestSuiteBuilder, etc.?
     derived.add(MapTestSuiteBuilder
         .using(new MapGenerator<K, V>(parentBuilder.getSubjectGenerator()))
         .withFeatures(parentBuilder.getFeatures())
         .named(parentBuilder.getName() + " [Map]")
         .suppressing(parentBuilder.getSuppressedTests())
         .suppressing(SetCreationTester.class.getMethods())
            // BiMap.entrySet() duplicate-handling behavior is too confusing for SetCreationTester
         .createTestSuite());
     /*
      * TODO(cpovirk): the Map tests duplicate most of this effort by using a
      * CollectionTestSuiteBuilder on values(). It would be nice to avoid that
      */
     derived.add(SetTestSuiteBuilder
         .using(new BiMapValueSetGenerator<K, V>(parentBuilder.getSubjectGenerator()))
        .withFeatures(computeValuesSetFeatures(parentBuilder.getFeatures()))
        .named(parentBuilder.getName() + " values [Set]")
        .suppressing(parentBuilder.getSuppressedTests())
        .suppressing(SetCreationTester.class.getMethods())
          // BiMap.values() duplicate-handling behavior is too confusing for SetCreationTester
        .createTestSuite());
    if (!parentBuilder.getFeatures().contains(.)) {
      derived.add(BiMapTestSuiteBuilder
          .using(new InverseBiMapGenerator<K, V>(parentBuilder.getSubjectGenerator()))
          .withFeatures(computeInverseFeatures(parentBuilder.getFeatures()))
          .named(parentBuilder.getName() + " inverse")
          .suppressing(parentBuilder.getSuppressedTests())
          .createTestSuite());
    }
    return derived;
  }
  private static Set<Feature<?>> computeInverseFeatures(Set<Feature<?>> mapFeatures) {
    Set<Feature<?>> inverseFeatures = new HashSet<Feature<?>>(mapFeatures);
    boolean nullKeys = inverseFeatures.remove(.);
    boolean nullValues = inverseFeatures.remove(.);
    if (nullKeys) {
      inverseFeatures.add(.);
    }
    if (nullValues) {
      inverseFeatures.add(.);
    }
    inverseFeatures.add(.);
    inverseFeatures.remove(.);
    return inverseFeatures;
  }
  // TODO(user): can we eliminate the duplication from MapTestSuiteBuilder here?
  private static Set<Feature<?>> computeValuesSetFeatures(
      Set<Feature<?>> mapFeatures) {
    Set<Feature<?>> valuesCollectionFeatures =
        computeCommonDerivedCollectionFeatures(mapFeatures);
    valuesCollectionFeatures.add(.);
    if (mapFeatures.contains(.)) {
      valuesCollectionFeatures.add(.);
    }
    valuesCollectionFeatures.add(.);
    return valuesCollectionFeatures;
  }
      Set<Feature<?>> mapFeatures) {
    return MapTestSuiteBuilder.computeCommonDerivedCollectionFeatures(mapFeatures);
  }
New to GrepCode? Check out our FAQ X