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.features.CollectionFeature.DESCENDING_VIEW;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUBSET_VIEW;
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.SortedSetSubsetTestSetGenerator;
25  import com.google.common.collect.testing.features.Feature;
26  import com.google.common.collect.testing.testers.NavigableSetNavigationTester;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.NavigableSet;
32  import java.util.Set;
33  import java.util.SortedSet;
34  import junit.framework.TestSuite;
35  
36  /**
37   * Creates, based on your criteria, a JUnit test suite that exhaustively tests
38   * a NavigableSet implementation.
39   */
40  @GwtIncompatible
41  public final class NavigableSetTestSuiteBuilder<E> extends SortedSetTestSuiteBuilder<E> {
42    public static <E> NavigableSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) {
43      NavigableSetTestSuiteBuilder<E> builder = new NavigableSetTestSuiteBuilder<E>();
44      builder.usingGenerator(generator);
45      return builder;
46    }
47  
48    @Override
49    protected List<TestSuite> createDerivedSuites(
50        FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
51            parentBuilder) {
52      List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder));
53  
54      if (!parentBuilder.getFeatures().contains(SUBSET_VIEW)) {
55        // Other combinations are inherited from SortedSetTestSuiteBuilder.
56        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE));
57        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND));
58        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE));
59        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE));
60        derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE));
61      }
62      if (!parentBuilder.getFeatures().contains(DESCENDING_VIEW)) {
63        derivedSuites.add(createDescendingSuite(parentBuilder));
64      }
65      return derivedSuites;
66    }
67  
68    public static final class NavigableSetSubsetTestSetGenerator<E>
69        extends SortedSetSubsetTestSetGenerator<E> {
70      public NavigableSetSubsetTestSetGenerator(
71          TestSortedSetGenerator<E> delegate, Bound to, Bound from) {
72        super(delegate, to, from);
73      }
74  
75      @Override
76      NavigableSet<E> createSubSet(SortedSet<E> sortedSet, E firstExclusive, E lastExclusive) {
77        NavigableSet<E> set = (NavigableSet<E>) sortedSet;
78        if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
79          return set.headSet(lastInclusive, true);
80        } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
81          return set.tailSet(firstExclusive, false);
82        } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
83          return set.subSet(firstExclusive, false, lastExclusive, false);
84        } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
85          return set.subSet(firstExclusive, false, lastInclusive, true);
86        } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
87          return set.subSet(firstInclusive, true, lastInclusive, true);
88        } else {
89          return (NavigableSet<E>) super.createSubSet(set, firstExclusive, lastExclusive);
90        }
91      }
92    }
93  
94    @Override
95    public NavigableSetTestSuiteBuilder<E> newBuilderUsing(
96        TestSortedSetGenerator<E> delegate, Bound to, Bound from) {
97      return using(new NavigableSetSubsetTestSetGenerator<E>(delegate, to, from));
98    }
99  
100   /**
101    * Create a suite whose maps are descending views of other maps.
102    */
103   private TestSuite createDescendingSuite(
104       final FeatureSpecificTestSuiteBuilder<
105               ?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>>
106           parentBuilder) {
107     final TestSetGenerator<E> delegate =
108         (TestSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator();
109 
110     List<Feature<?>> features = new ArrayList<>();
111     features.add(DESCENDING_VIEW);
112     features.addAll(parentBuilder.getFeatures());
113 
114     return NavigableSetTestSuiteBuilder.using(
115             new TestSetGenerator<E>() {
116 
117               @Override
118               public SampleElements<E> samples() {
119                 return delegate.samples();
120               }
121 
122               @Override
123               public E[] createArray(int length) {
124                 return delegate.createArray(length);
125               }
126 
127               @Override
128               public Iterable<E> order(List<E> insertionOrder) {
129                 List<E> list = new ArrayList<E>();
130                 for (E e : delegate.order(insertionOrder)) {
131                   list.add(e);
132                 }
133                 Collections.reverse(list);
134                 return list;
135               }
136 
137               @Override
138               public Set<E> create(Object... elements) {
139                 NavigableSet<E> navigableSet = (NavigableSet<E>) delegate.create(elements);
140                 return navigableSet.descendingSet();
141               }
142             })
143         .named(parentBuilder.getName() + " descending")
144         .withFeatures(features)
145         .suppressing(parentBuilder.getSuppressedTests())
146         .createTestSuite();
147   }
148 
149   @Override
150   protected List<Class<? extends AbstractTester>> getTesters() {
151     List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters());
152     testers.add(NavigableSetNavigationTester.class);
153     return testers;
154   }
155 }