1 /* 2 * Copyright (C) 2012 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.google; 18 19 import com.google.common.collect.ListMultimap; 20 import com.google.common.collect.testing.AbstractTester; 21 import com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder; 22 import com.google.common.collect.testing.Helpers; 23 import com.google.common.collect.testing.ListTestSuiteBuilder; 24 import com.google.common.collect.testing.OneSizeTestContainerGenerator; 25 import com.google.common.collect.testing.TestListGenerator; 26 import com.google.common.collect.testing.features.CollectionFeature; 27 import com.google.common.collect.testing.features.CollectionSize; 28 import com.google.common.collect.testing.features.Feature; 29 import com.google.common.collect.testing.features.ListFeature; 30 31 import junit.framework.TestSuite; 32 33 import java.util.Collections; 34 import java.util.EnumSet; 35 import java.util.List; 36 import java.util.Map.Entry; 37 import java.util.Set; 38 39 /** 40 * Creates, based on your criteria, a JUnit test suite that exhaustively tests 41 * a {@code ListMultimap} implementation. 42 * 43 * @author Louis Wasserman 44 */ 45 public class ListMultimapTestSuiteBuilder<K, V> extends 46 MultimapTestSuiteBuilder<K, V, ListMultimap<K, V>> { 47 using( TestListMultimapGenerator<K, V> generator)48 public static <K, V> ListMultimapTestSuiteBuilder<K, V> using( 49 TestListMultimapGenerator<K, V> generator) { 50 ListMultimapTestSuiteBuilder<K, V> result = new ListMultimapTestSuiteBuilder<K, V>(); 51 result.usingGenerator(generator); 52 return result; 53 } 54 getTesters()55 @Override protected List<Class<? extends AbstractTester>> getTesters() { 56 List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); 57 testers.add(ListMultimapAsMapTester.class); 58 testers.add(ListMultimapEqualsTester.class); 59 testers.add(ListMultimapPutTester.class); 60 testers.add(ListMultimapPutAllTester.class); 61 testers.add(ListMultimapRemoveTester.class); 62 testers.add(ListMultimapReplaceValuesTester.class); 63 return testers; 64 } 65 66 @Override computeMultimapGetTestSuite( FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>>> parentBuilder)67 TestSuite computeMultimapGetTestSuite( 68 FeatureSpecificTestSuiteBuilder<?, ? extends 69 OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>>> parentBuilder) { 70 return ListTestSuiteBuilder.using( 71 new MultimapGetGenerator<K, V>(parentBuilder.getSubjectGenerator())) 72 .withFeatures(computeMultimapGetFeatures(parentBuilder.getFeatures())) 73 .named(parentBuilder.getName() + ".get[key]") 74 .suppressing(parentBuilder.getSuppressedTests()) 75 .createTestSuite(); 76 } 77 78 @Override computeMultimapAsMapGetTestSuite( FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>>> parentBuilder)79 TestSuite computeMultimapAsMapGetTestSuite( 80 FeatureSpecificTestSuiteBuilder<?, ? extends 81 OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>>> parentBuilder) { 82 Set<Feature<?>> features = computeMultimapAsMapGetFeatures(parentBuilder.getFeatures()); 83 if (Collections.disjoint(features, EnumSet.allOf(CollectionSize.class))) { 84 return new TestSuite(); 85 } else { 86 return ListTestSuiteBuilder.using( 87 new MultimapAsMapGetGenerator<K, V>(parentBuilder.getSubjectGenerator())) 88 .withFeatures(features) 89 .named(parentBuilder.getName() + ".asMap[].get[key]") 90 .suppressing(parentBuilder.getSuppressedTests()) 91 .createTestSuite(); 92 } 93 } 94 95 @Override computeMultimapGetFeatures( Set<Feature<?>> multimapFeatures)96 Set<Feature<?>> computeMultimapGetFeatures( 97 Set<Feature<?>> multimapFeatures) { 98 Set<Feature<?>> derivedFeatures = super.computeMultimapGetFeatures(multimapFeatures); 99 if (derivedFeatures.contains(CollectionFeature.SUPPORTS_ADD)) { 100 derivedFeatures.add(ListFeature.SUPPORTS_ADD_WITH_INDEX); 101 } 102 if (derivedFeatures.contains(CollectionFeature.GENERAL_PURPOSE)) { 103 derivedFeatures.add(ListFeature.GENERAL_PURPOSE); 104 } 105 return derivedFeatures; 106 } 107 108 private static class MultimapGetGenerator<K, V> 109 extends MultimapTestSuiteBuilder.MultimapGetGenerator<K, V, ListMultimap<K, V>> 110 implements TestListGenerator<V> { MultimapGetGenerator( OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>> multimapGenerator)111 public MultimapGetGenerator( 112 OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>> multimapGenerator) { 113 super(multimapGenerator); 114 } 115 116 @Override create(Object... elements)117 public List<V> create(Object... elements) { 118 return (List<V>) super.create(elements); 119 } 120 121 } 122 123 private static class MultimapAsMapGetGenerator<K, V> 124 extends MultimapTestSuiteBuilder.MultimapAsMapGetGenerator<K, V, ListMultimap<K, V>> 125 implements TestListGenerator<V> { MultimapAsMapGetGenerator( OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>> multimapGenerator)126 public MultimapAsMapGetGenerator( 127 OneSizeTestContainerGenerator<ListMultimap<K, V>, Entry<K, V>> multimapGenerator) { 128 super(multimapGenerator); 129 } 130 131 @Override create(Object... elements)132 public List<V> create(Object... elements) { 133 return (List<V>) super.create(elements); 134 } 135 } 136 } 137