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