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