Coverage Summary for Class: SetTestSuiteBuilder (com.google.common.collect.testing)

Class Method, % Line, %
SetTestSuiteBuilder 100% (5/5) 100% (25/25)
SetTestSuiteBuilder$ReserializedSetGenerator 100% (6/6) 100% (8/8)
Total 100% (11/11) 100% (33/33)


1 /* 2  * Copyright (C) 2008 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect.testing; 18  19 import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE; 20 import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS; 21  22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.collect.testing.features.Feature; 24 import com.google.common.collect.testing.testers.CollectionSerializationEqualTester; 25 import com.google.common.collect.testing.testers.SetAddAllTester; 26 import com.google.common.collect.testing.testers.SetAddTester; 27 import com.google.common.collect.testing.testers.SetCreationTester; 28 import com.google.common.collect.testing.testers.SetEqualsTester; 29 import com.google.common.collect.testing.testers.SetHashCodeTester; 30 import com.google.common.collect.testing.testers.SetRemoveTester; 31 import com.google.common.testing.SerializableTester; 32 import java.util.ArrayList; 33 import java.util.Collection; 34 import java.util.HashSet; 35 import java.util.List; 36 import java.util.Set; 37 import junit.framework.TestSuite; 38  39 /** 40  * Creates, based on your criteria, a JUnit test suite that exhaustively tests a Set implementation. 41  * 42  * @author George van den Driessche 43  */ 44 @GwtIncompatible 45 public class SetTestSuiteBuilder<E> 46  extends AbstractCollectionTestSuiteBuilder<SetTestSuiteBuilder<E>, E> { 47  public static <E> SetTestSuiteBuilder<E> using(TestSetGenerator<E> generator) { 48  return new SetTestSuiteBuilder<E>().usingGenerator(generator); 49  } 50  51  @Override 52  protected List<Class<? extends AbstractTester>> getTesters() { 53  List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 54  55  testers.add(CollectionSerializationEqualTester.class); 56  testers.add(SetAddAllTester.class); 57  testers.add(SetAddTester.class); 58  testers.add(SetCreationTester.class); 59  testers.add(SetHashCodeTester.class); 60  testers.add(SetEqualsTester.class); 61  testers.add(SetRemoveTester.class); 62  // SetRemoveAllTester doesn't exist because, Sets not permitting 63  // duplicate elements, there are no tests for Set.removeAll() that aren't 64  // covered by CollectionRemoveAllTester. 65  return testers; 66  } 67  68  @Override 69  protected List<TestSuite> createDerivedSuites( 70  FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> 71  parentBuilder) { 72  List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); 73  74  if (parentBuilder.getFeatures().contains(SERIALIZABLE)) { 75  derivedSuites.add( 76  SetTestSuiteBuilder.using( 77  new ReserializedSetGenerator<E>(parentBuilder.getSubjectGenerator())) 78  .named(getName() + " reserialized") 79  .withFeatures(computeReserializedCollectionFeatures(parentBuilder.getFeatures())) 80  .suppressing(parentBuilder.getSuppressedTests()) 81  .createTestSuite()); 82  } 83  return derivedSuites; 84  } 85  86  static class ReserializedSetGenerator<E> implements TestSetGenerator<E> { 87  final OneSizeTestContainerGenerator<Collection<E>, E> gen; 88  89  private ReserializedSetGenerator(OneSizeTestContainerGenerator<Collection<E>, E> gen) { 90  this.gen = gen; 91  } 92  93  @Override 94  public SampleElements<E> samples() { 95  return gen.samples(); 96  } 97  98  @Override 99  public Set<E> create(Object... elements) { 100  return (Set<E>) SerializableTester.reserialize(gen.create(elements)); 101  } 102  103  @Override 104  public E[] createArray(int length) { 105  return gen.createArray(length); 106  } 107  108  @Override 109  public Iterable<E> order(List<E> insertionOrder) { 110  return gen.order(insertionOrder); 111  } 112  } 113  114  private static Set<Feature<?>> computeReserializedCollectionFeatures(Set<Feature<?>> features) { 115  Set<Feature<?>> derivedFeatures = new HashSet<>(features); 116  derivedFeatures.remove(SERIALIZABLE); 117  derivedFeatures.remove(SERIALIZABLE_INCLUDING_VIEWS); 118  return derivedFeatures; 119  } 120 }