Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 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.Set;

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

Warning: expects that E is a String.

Author(s):
Louis Wasserman
 
 public class SortedMultisetTestSuiteBuilder<E> extends
   public static <E> SortedMultisetTestSuiteBuilder<E> using(
       TestMultisetGenerator<E> generator) {
     SortedMultisetTestSuiteBuilder<E> result =
         new SortedMultisetTestSuiteBuilder<E>();
     result.usingGenerator(generator);
     return result;
   }
 
   @Override
   public TestSuite createTestSuite() {
     TestSuite suite = super.createTestSuite();
     for (TestSuite subSuite : createDerivedSuites(this)) {
       suite.addTest(subSuite);
     }
     return suite;
   }
 
   @Override
   protected List<Class<? extends AbstractTester>> getTesters() {
     List<Class<? extends AbstractTester>> testers =
         Helpers.copyToList(super.getTesters());
     testers.add(MultisetNavigationTester.class);
     return testers;
   }
 
   @Override
       ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder) {
     // TODO(user): make a SortedElementSetGenerator
     return SetTestSuiteBuilder
         .using(new ElementSetGenerator<E>(parentBuilder.getSubjectGenerator()))
         .named(getName() + ".elementSet")
         .withFeatures(computeElementSetFeatures(parentBuilder.getFeatures()))
         .suppressing(parentBuilder.getSuppressedTests())
         .createTestSuite();
   }

  
To avoid infinite recursion, test suites with these marker features won't have derived suites created for them.
 
   enum NoRecurse implements Feature<Void> {
     SUBMULTISET, DESCENDING;
 
    @Override
    public Set<Feature<? super Void>> getImpliedFeatures() {
      return Collections.emptySet();
    }
  }

  
Two bounds (from and to) define how to build a subMultiset.
  enum Bound {
    INCLUSIVE, EXCLUSIVE, NO_BOUND;
  }
      SortedMultisetTestSuiteBuilder<E> parentBuilder) {
    List<TestSuitederivedSuites = Lists.newArrayList();
    if (!parentBuilder.getFeatures().contains(.)) {
      derivedSuites.add(createDescendingSuite(parentBuilder));
    }
    if (parentBuilder.getFeatures().contains(.)) {
      derivedSuites.add(createReserializedSuite(parentBuilder));
    }
    if (!parentBuilder.getFeatures().contains(.)) {
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
      derivedSuites.add(createSubMultisetSuite(parentBuilder.,
          .));
    }
    return derivedSuites;
  }
      SortedMultisetTestSuiteBuilder<E> parentBuilderfinal Bound from,
      final Bound to) {
    final TestMultisetGenerator<E> delegate =
        (TestMultisetGenerator<E>) parentBuilder.getSubjectGenerator();
    Set<Feature<?>> features = new HashSet<Feature<?>>();
    features.add(.);
    features.addAll(parentBuilder.getFeatures());
    }
    SortedMultiset<E> emptyMultiset = (SortedMultiset<E>) delegate.create();
    final Comparator<? super E> comparator = emptyMultiset.comparator();
    SampleElements<E> samples = delegate.samples();
    @SuppressWarnings("unchecked")
    List<E> samplesList =
        Arrays.asList(samples.e0samples.e1samples.e2samples.e3,
            samples.e4);
    Collections.sort(samplesListcomparator);
    final E firstInclusive = samplesList.get(0);
    final E lastInclusive = samplesList.get(samplesList.size() - 1);
    return SortedMultisetTestSuiteBuilder
        .using(new ForwardingTestMultisetGenerator<E>(delegate) {
          @Override
          public SortedMultiset<E> create(Object... entries) {
            @SuppressWarnings("unchecked")
            // we dangerously assume E is a string
            List<E> extremeValues = (ListgetExtremeValues();
            @SuppressWarnings("unchecked")
            // map generators must past entry objects
            List<E> normalValues = (List) Arrays.asList(entries);
            // prepare extreme values to be filtered out of view
            Collections.sort(extremeValuescomparator);
            E firstExclusive = extremeValues.get(1);
            E lastExclusive = extremeValues.get(2);
            if (from == .) {
              extremeValues.remove(0);
              extremeValues.remove(0);
            }
            if (to == .) {
              extremeValues.remove(extremeValues.size() - 1);
              extremeValues.remove(extremeValues.size() - 1);
            }
            // the regular values should be visible after filtering
            List<E> allEntries = new ArrayList<E>();
            allEntries.addAll(extremeValues);
            allEntries.addAll(normalValues);
            SortedMultiset<E> multiset =
                (SortedMultiset<E>) delegate.create(allEntries.toArray());
            // call the smallest subMap overload that filters out the extreme
            // values
            if (from == .) {
              multiset =
                  multiset.tailMultiset(firstInclusive.);
            } else if (from == .) {
              multiset = multiset.tailMultiset(firstExclusive.);
            }
            if (to == .) {
              multiset = multiset.headMultiset(lastInclusive.);
            } else if (to == .) {
              multiset = multiset.headMultiset(lastExclusive.);
            }
            return multiset;
          }
        })
        .named(parentBuilder.getName() + " subMultiset " + from + "-" + to)
        .withFeatures(features)
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }

  
Returns an array of four bogus elements that will always be too high or too low for the display. This includes two values for each extreme.

This method (dangerously) assume that the strings "!! a" and "~~ z" will work for this purpose, which may cause problems for navigable maps with non-string or unicode generators.

  private List<StringgetExtremeValues() {
    List<Stringresult = new ArrayList<String>();
    result.add("!! a");
    result.add("!! b");
    result.add("~~ y");
    result.add("~~ z");
    return result;
  }
      SortedMultisetTestSuiteBuilder<E> parentBuilder) {
    final TestMultisetGenerator<E> delegate =
        (TestMultisetGenerator<E>) parentBuilder.getSubjectGenerator();
    Set<Feature<?>> features = new HashSet<Feature<?>>();
    features.add(.);
    features.addAll(parentBuilder.getFeatures());
    }
    return SortedMultisetTestSuiteBuilder
        .using(new ForwardingTestMultisetGenerator<E>(delegate) {
          @Override
          public SortedMultiset<E> create(Object... entries) {
            return ((SortedMultiset<E>) super.create(entries))
                .descendingMultiset();
          }
          @Override
          public Iterable<E> order(List<E> insertionOrder) {
            return ImmutableList.copyOf(super.order(insertionOrder)).reverse();
          }
        })
        .named(parentBuilder.getName() + " descending")
        .withFeatures(features)
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }
      SortedMultisetTestSuiteBuilder<E> parentBuilder) {
    final TestMultisetGenerator<E> delegate =
        (TestMultisetGenerator<E>) parentBuilder.getSubjectGenerator();
    Set<Feature<?>> features = new HashSet<Feature<?>>();
    features.addAll(parentBuilder.getFeatures());
    return SortedMultisetTestSuiteBuilder
        .using(new ForwardingTestMultisetGenerator<E>(delegate) {
          @Override
          public SortedMultiset<E> create(Object... entries) {
            return SerializableTester.reserialize(((SortedMultiset<E>) super.create(entries)));
          }
        })
        .named(parentBuilder.getName() + " reserialized")
        .withFeatures(features)
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }
  private static class ForwardingTestMultisetGenerator<E>
      implements TestMultisetGenerator<E> {
    private final TestMultisetGenerator<E> delegate;
      this. = delegate;
    }
    @Override
    public SampleElements<E> samples() {
      return .samples();
    }
    @Override
    public E[] createArray(int length) {
      return .createArray(length);
    }
    @Override
    public Iterable<E> order(List<E> insertionOrder) {
      return .order(insertionOrder);
    }
    @Override
    public Multiset<E> create(Object... elements) {
      return .create(elements);
    }
  }
New to GrepCode? Check out our FAQ X