• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import com.google.common.base.Function;
20 import com.google.common.collect.testing.SafeTreeSet;
21 import com.google.common.collect.testing.SetTestSuiteBuilder;
22 import com.google.common.collect.testing.TestStringSetGenerator;
23 import com.google.common.collect.testing.features.CollectionFeature;
24 import com.google.common.collect.testing.features.CollectionSize;
25 import com.google.common.testing.EqualsTester;
26 import com.google.common.testing.ForwardingWrapperTester;
27 import java.util.Arrays;
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.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 
38 /**
39  * Tests for {@code ForwardingNavigableSet}.
40  *
41  * @author Louis Wasserman
42  */
43 public class ForwardingNavigableSetTest extends TestCase {
44   static class StandardImplForwardingNavigableSet<T> extends ForwardingNavigableSet<T> {
45     private final NavigableSet<T> backingSet;
46 
StandardImplForwardingNavigableSet(NavigableSet<T> backingSet)47     StandardImplForwardingNavigableSet(NavigableSet<T> backingSet) {
48       this.backingSet = backingSet;
49     }
50 
51     @Override
delegate()52     protected NavigableSet<T> delegate() {
53       return backingSet;
54     }
55 
56     @Override
equals(Object object)57     public boolean equals(Object object) {
58       return standardEquals(object);
59     }
60 
61     @Override
hashCode()62     public int hashCode() {
63       return standardHashCode();
64     }
65 
66     @Override
addAll(Collection<? extends T> collection)67     public boolean addAll(Collection<? extends T> collection) {
68       return standardAddAll(collection);
69     }
70 
71     @Override
clear()72     public void clear() {
73       standardClear();
74     }
75 
76     @Override
contains(Object object)77     public boolean contains(Object object) {
78       return standardContains(object);
79     }
80 
81     @Override
containsAll(Collection<?> collection)82     public boolean containsAll(Collection<?> collection) {
83       return standardContainsAll(collection);
84     }
85 
86     @Override
remove(Object object)87     public boolean remove(Object object) {
88       return standardRemove(object);
89     }
90 
91     @Override
removeAll(Collection<?> collection)92     public boolean removeAll(Collection<?> collection) {
93       return standardRemoveAll(collection);
94     }
95 
96     @Override
retainAll(Collection<?> collection)97     public boolean retainAll(Collection<?> collection) {
98       return standardRetainAll(collection);
99     }
100 
101     @Override
toArray()102     public Object[] toArray() {
103       return standardToArray();
104     }
105 
106     @Override
toArray(T[] array)107     public <T> T[] toArray(T[] array) {
108       return standardToArray(array);
109     }
110 
111     @Override
toString()112     public String toString() {
113       return standardToString();
114     }
115 
116     @Override
subSet(T fromElement, T toElement)117     public SortedSet<T> subSet(T fromElement, T toElement) {
118       return standardSubSet(fromElement, toElement);
119     }
120 
121     @Override
lower(T e)122     public T lower(T e) {
123       return standardLower(e);
124     }
125 
126     @Override
floor(T e)127     public T floor(T e) {
128       return standardFloor(e);
129     }
130 
131     @Override
ceiling(T e)132     public T ceiling(T e) {
133       return standardCeiling(e);
134     }
135 
136     @Override
higher(T e)137     public T higher(T e) {
138       return standardHigher(e);
139     }
140 
141     @Override
pollFirst()142     public T pollFirst() {
143       return standardPollFirst();
144     }
145 
146     @Override
pollLast()147     public T pollLast() {
148       return standardPollLast();
149     }
150 
151     @Override
headSet(T toElement)152     public SortedSet<T> headSet(T toElement) {
153       return standardHeadSet(toElement);
154     }
155 
156     @Override
tailSet(T fromElement)157     public SortedSet<T> tailSet(T fromElement) {
158       return standardTailSet(fromElement);
159     }
160   }
161 
suite()162   public static Test suite() {
163     TestSuite suite = new TestSuite();
164 
165     suite.addTestSuite(ForwardingNavigableSetTest.class);
166     suite.addTest(
167         SetTestSuiteBuilder.using(
168                 new TestStringSetGenerator() {
169                   @Override
170                   protected Set<String> create(String[] elements) {
171                     return new StandardImplForwardingNavigableSet<>(
172                         new SafeTreeSet<String>(Arrays.asList(elements)));
173                   }
174 
175                   @Override
176                   public List<String> order(List<String> insertionOrder) {
177                     return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
178                   }
179                 })
180             .named("ForwardingNavigableSet[SafeTreeSet] with standard implementations")
181             .withFeatures(
182                 CollectionSize.ANY,
183                 CollectionFeature.KNOWN_ORDER,
184                 CollectionFeature.GENERAL_PURPOSE)
185             .createTestSuite());
186     suite.addTest(
187         SetTestSuiteBuilder.using(
188                 new TestStringSetGenerator() {
189                   @Override
190                   protected Set<String> create(String[] elements) {
191                     SafeTreeSet<String> set = new SafeTreeSet<>(Ordering.natural().nullsFirst());
192                     Collections.addAll(set, elements);
193                     return new StandardImplForwardingNavigableSet<>(set);
194                   }
195 
196                   @Override
197                   public List<String> order(List<String> insertionOrder) {
198                     return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
199                   }
200                 })
201             .named(
202                 "ForwardingNavigableSet[SafeTreeSet[Ordering.natural.nullsFirst]]"
203                     + " with standard implementations")
204             .withFeatures(
205                 CollectionSize.ANY,
206                 CollectionFeature.KNOWN_ORDER,
207                 CollectionFeature.GENERAL_PURPOSE,
208                 CollectionFeature.ALLOWS_NULL_VALUES)
209             .createTestSuite());
210 
211     return suite;
212   }
213 
214   @SuppressWarnings({"rawtypes", "unchecked"})
testForwarding()215   public void testForwarding() {
216     new ForwardingWrapperTester()
217         .testForwarding(
218             NavigableSet.class,
219             new Function<NavigableSet, NavigableSet>() {
220               @Override
221               public NavigableSet apply(NavigableSet delegate) {
222                 return wrap(delegate);
223               }
224             });
225   }
226 
testEquals()227   public void testEquals() {
228     NavigableSet<String> set1 = ImmutableSortedSet.of("one");
229     NavigableSet<String> set2 = ImmutableSortedSet.of("two");
230     new EqualsTester()
231         .addEqualityGroup(set1, wrap(set1), wrap(set1))
232         .addEqualityGroup(set2, wrap(set2))
233         .testEquals();
234   }
235 
wrap(final NavigableSet<T> delegate)236   private static <T> NavigableSet<T> wrap(final NavigableSet<T> delegate) {
237     return new ForwardingNavigableSet<T>() {
238       @Override
239       protected NavigableSet<T> delegate() {
240         return delegate;
241       }
242     };
243   }
244 }
245