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