View Javadoc
1   /*
2    * Copyright (C) 2010 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.Helpers.castOrCopyToList;
20  import static java.util.Collections.reverse;
21  
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.testing.DerivedCollectionGenerators.Bound;
24  import com.google.common.collect.testing.DerivedCollectionGenerators.ForwardingTestMapGenerator;
25  import com.google.common.collect.testing.DerivedCollectionGenerators.SortedMapSubmapTestMapGenerator;
26  import com.google.common.collect.testing.features.Feature;
27  import com.google.common.collect.testing.testers.NavigableMapNavigationTester;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Map.Entry;
32  import java.util.NavigableMap;
33  import java.util.SortedMap;
34  import junit.framework.TestSuite;
35  
36  /**
37   * Creates, based on your criteria, a JUnit test suite that exhaustively tests
38   * a NavigableMap implementation.
39   */
40  @GwtIncompatible
41  public class NavigableMapTestSuiteBuilder<K, V> extends SortedMapTestSuiteBuilder<K, V> {
42    public static <K, V> NavigableMapTestSuiteBuilder<K, V> using(
43        TestSortedMapGenerator<K, V> generator) {
44      NavigableMapTestSuiteBuilder<K, V> result = new NavigableMapTestSuiteBuilder<>();
45      result.usingGenerator(generator);
46      return result;
47    }
48  
49    @Override
50    protected List<Class<? extends AbstractTester>> getTesters() {
51      List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
52      testers.add(NavigableMapNavigationTester.class);
53      return testers;
54    }
55  
56    @Override
57    protected List<TestSuite> createDerivedSuites(
58        FeatureSpecificTestSuiteBuilder<
59                ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>>
60            parentBuilder) {
61      List<TestSuite> derivedSuites = super.createDerivedSuites(parentBuilder);
62  
63      if (!parentBuilder.getFeatures().contains(NoRecurse.DESCENDING)) {
64        derivedSuites.add(createDescendingSuite(parentBuilder));
65      }
66  
67      if (!parentBuilder.getFeatures().contains(NoRecurse.SUBMAP)) {
68        // Other combinations are inherited from SortedMapTestSuiteBuilder.
69        derivedSuites.add(createSubmapSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE));
70        derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND));
71        derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE));
72        derivedSuites.add(createSubmapSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE));
73        derivedSuites.add(createSubmapSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE));
74      }
75  
76      return derivedSuites;
77    }
78  
79    @Override
80    protected NavigableSetTestSuiteBuilder<K> createDerivedKeySetSuite(
81        TestSetGenerator<K> keySetGenerator) {
82      return NavigableSetTestSuiteBuilder.using((TestSortedSetGenerator<K>) keySetGenerator);
83    }
84  
85    public static final class NavigableMapSubmapTestMapGenerator<K, V>
86        extends SortedMapSubmapTestMapGenerator<K, V> {
87      public NavigableMapSubmapTestMapGenerator(
88          TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) {
89        super(delegate, to, from);
90      }
91  
92      @Override
93      NavigableMap<K, V> createSubMap(SortedMap<K, V> sortedMap, K firstExclusive, K lastExclusive) {
94        NavigableMap<K, V> map = (NavigableMap<K, V>) sortedMap;
95        if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
96          return map.headMap(lastInclusive, true);
97        } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
98          return map.tailMap(firstExclusive, false);
99        } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
100         return map.subMap(firstExclusive, false, lastExclusive, false);
101       } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
102         return map.subMap(firstExclusive, false, lastInclusive, true);
103       } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
104         return map.subMap(firstInclusive, true, lastInclusive, true);
105       } else {
106         return (NavigableMap<K, V>) super.createSubMap(map, firstExclusive, lastExclusive);
107       }
108     }
109   }
110 
111   @Override
112   public NavigableMapTestSuiteBuilder<K, V> newBuilderUsing(
113       TestSortedMapGenerator<K, V> delegate, Bound to, Bound from) {
114     return subSuiteUsing(new NavigableMapSubmapTestMapGenerator<K, V>(delegate, to, from));
115   }
116 
117   /**
118    * Create a suite whose maps are descending views of other maps.
119    */
120   private TestSuite createDescendingSuite(
121       final FeatureSpecificTestSuiteBuilder<
122               ?, ? extends OneSizeTestContainerGenerator<Map<K, V>, Entry<K, V>>>
123           parentBuilder) {
124     final TestSortedMapGenerator<K, V> delegate =
125         (TestSortedMapGenerator<K, V>) parentBuilder.getSubjectGenerator().getInnerGenerator();
126 
127     List<Feature<?>> features = new ArrayList<>();
128     features.add(NoRecurse.DESCENDING);
129     features.addAll(parentBuilder.getFeatures());
130 
131     return subSuiteUsing(new DescendingTestMapGenerator<K, V>(delegate))
132         .named(parentBuilder.getName() + " descending")
133         .withFeatures(features)
134         .suppressing(parentBuilder.getSuppressedTests())
135         .createTestSuite();
136   }
137 
138   NavigableMapTestSuiteBuilder<K, V> subSuiteUsing(TestSortedMapGenerator<K, V> generator) {
139     return using(generator);
140   }
141 
142   static class DescendingTestMapGenerator<K, V> extends ForwardingTestMapGenerator<K, V>
143       implements TestSortedMapGenerator<K, V> {
144     DescendingTestMapGenerator(TestSortedMapGenerator<K, V> delegate) {
145       super(delegate);
146     }
147 
148     @Override
149     public NavigableMap<K, V> create(Object... entries) {
150       NavigableMap<K, V> map = (NavigableMap<K, V>) delegate.create(entries);
151       return map.descendingMap();
152     }
153 
154     @Override
155     public Iterable<Entry<K, V>> order(List<Entry<K, V>> insertionOrder) {
156       insertionOrder = castOrCopyToList(delegate.order(insertionOrder));
157       reverse(insertionOrder);
158       return insertionOrder;
159     }
160 
161     TestSortedMapGenerator<K, V> delegate() {
162       return (TestSortedMapGenerator<K, V>) delegate;
163     }
164 
165     @Override
166     public Entry<K, V> belowSamplesLesser() {
167       return delegate().aboveSamplesGreater();
168     }
169 
170     @Override
171     public Entry<K, V> belowSamplesGreater() {
172       return delegate().aboveSamplesLesser();
173     }
174 
175     @Override
176     public Entry<K, V> aboveSamplesLesser() {
177       return delegate().belowSamplesGreater();
178     }
179 
180     @Override
181     public Entry<K, V> aboveSamplesGreater() {
182       return delegate().belowSamplesLesser();
183     }
184   }
185 }