Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

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

Author(s):
Jared Levy
Louis Wasserman
 
 public class MultisetTestSuiteBuilder<E> extends
   public static <E> MultisetTestSuiteBuilder<E> using(
       TestMultisetGenerator<E> generator) {
     return new MultisetTestSuiteBuilder<E>().usingGenerator(generator);
   }
 
   public enum NoRecurse implements Feature<Void> {
     NO_ENTRY_SET;
 
     @Override
     public Set<Feature<? super Void>> getImpliedFeatures() {
       return Collections.emptySet();
     }
   }
 
   @Override protected List<Class<? extends AbstractTester>> getTesters() {
     List<Class<? extends AbstractTester>> 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(MultisetReadsTester.class);
     testers.add(MultisetSetCountConditionallyTester.class);
     testers.add(MultisetRemoveTester.class);
     testers.add(MultisetEntrySetTester.class);
     testers.add(MultisetIteratorTester.class);
     testers.add(MultisetSerializationTester.class);
     return testers;
   }
 
   private static Set<Feature<?>> computeEntrySetFeatures(
       Set<Feature<?>> features) {
     Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>();
     derivedFeatures.addAll(features);
     derivedFeatures.remove(.);
     derivedFeatures.remove(.);
     derivedFeatures.remove(.);
     if (!derivedFeatures.remove(.)) {
      derivedFeatures.remove(.);
    }
    return derivedFeatures;
  }
      Set<Feature<?>> features) {
    Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>();
    derivedFeatures.addAll(features);
    derivedFeatures.remove(.);
    derivedFeatures.remove(.);
    if (!derivedFeatures.remove(.)) {
      derivedFeatures.remove(.);
    }
    return derivedFeatures;
  }
      Set<Feature<?>> features) {
    Set<Feature<?>> derivedFeatures = new HashSet<Feature<?>>();
    derivedFeatures.addAll(features);
    derivedFeatures.remove(.);
    return derivedFeatures;
  }
      ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder) {
    List<TestSuitederivedSuites = new ArrayList<TestSuite>(
        super.createDerivedSuites(parentBuilder));
    derivedSuites.add(createElementSetTestSuite(parentBuilder));
    if (!parentBuilder.getFeatures().contains(.)) {
      derivedSuites.add(
          SetTestSuiteBuilder.using(new EntrySetGenerator<E>(parentBuilder.getSubjectGenerator()))
              .named(getName() + ".entrySet")
              .withFeatures(computeEntrySetFeatures(parentBuilder.getFeatures()))
              .suppressing(parentBuilder.getSuppressedTests())
              .createTestSuite());
    }
    if (parentBuilder.getFeatures().contains(.)) {
      derivedSuites.add(MultisetTestSuiteBuilder
          .using(new ReserializedMultisetGenerator<E>(parentBuilder.getSubjectGenerator()))
          .named(getName() + " reserialized")
          .withFeatures(computeReserializedMultisetFeatures(parentBuilder.getFeatures()))
          .suppressing(parentBuilder.getSuppressedTests())
          .createTestSuite());
    }
    return derivedSuites;
  }
      ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> parentBuilder) {
    return SetTestSuiteBuilder
        .using(new ElementSetGenerator<E>(parentBuilder.getSubjectGenerator()))
        .named(getName() + ".elementSet")
        .withFeatures(computeElementSetFeatures(parentBuilder.getFeatures()))
        .suppressing(parentBuilder.getSuppressedTests())
        .createTestSuite();
  }
  static class ElementSetGenerator<E> implements TestSetGenerator<E> {
      this. = gen;
    }
    @Override
    public SampleElements<E> samples() {
      return .samples();
    }
    @Override
    public Set<E> create(Object... elements) {
      Object[] duplicated = new Object[elements.length * 2];
      for (int i = 0; i < elements.lengthi++) {
        duplicated[i] = elements[i];
        duplicated[i + elements.length] = elements[i];
      }
      return ((Multiset<E>) .create(duplicated)).elementSet();
    }
    @Override
    public E[] createArray(int length) {
      return .createArray(length);
    }
    @Override
    public Iterable<E> order(List<E> insertionOrder) {
      return .order(new ArrayList<E>(new LinkedHashSet<E>(insertionOrder)));
    }
  }
  static class EntrySetGenerator<E> implements TestSetGenerator<Multiset.Entry<E>> {
      this. = gen;
    }
    @Override
    public SampleElements<Multiset.Entry<E>> samples() {
      SampleElements<E> samples = .samples();
      return new SampleElements<Multiset.Entry<E>>(
          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<Multiset.Entry<E>> create(Object... entries) {
      List<Objectcontents = new ArrayList<Object>();
      Set<E> elements = new HashSet<E>();
      for (Object o : entries) {
        @SuppressWarnings("unchecked")
        Multiset.Entry<E> entry = (Entry<E>) o;
        checkArgument(elements.add(entry.getElement()),
            "Duplicate keys not allowed in EntrySetGenerator");
        for (int i = 0; i < entry.getCount(); i++) {
          contents.add(entry.getElement());
        }
      }
      return ((Multiset<E>) .create(contents.toArray())).entrySet();
    }
    @SuppressWarnings("unchecked")
    @Override
    public Multiset.Entry<E>[] createArray(int length) {
      return new Multiset.Entry[length];
    }
    @Override
    public Iterable<Entry<E>> order(List<Entry<E>> insertionOrder) {
      // We mimic the order from gen.
      Map<E, Entry<E>> map = new LinkedHashMap<E, Entry<E>>();
      for (Entry<E> entry : insertionOrder) {
        map.put(entry.getElement(), entry);
      }
      Set<E> seen = new HashSet<E>();
      List<Entry<E>> order = new ArrayList<Entry<E>>();
      for (E e : .order(new ArrayList<E>(map.keySet()))) {
        if (seen.add(e)) {
          order.add(map.get(e));
        }
      }
      return order;
    }
  }
  static class ReserializedMultisetGenerator<E> implements TestMultisetGenerator<E>{
      this. = gen;
    }
    @Override
    public SampleElements<E> samples() {
      return .samples();
    }
    @Override
    public Multiset<E> create(Object... elements) {
      return (Multiset<E>) SerializableTester.reserialize(.create(elements));
    }
    @Override
    public E[] createArray(int length) {
      return .createArray(length);
    }
    @Override
    public Iterable<E> order(List<E> insertionOrder) {
      return .order(insertionOrder);
    }
  }
New to GrepCode? Check out our FAQ X