• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import com.google.common.collect.Synchronized.SynchronizedNavigableSet;
20 import com.google.common.collect.Synchronized.SynchronizedSortedSet;
21 import com.google.common.collect.testing.NavigableSetTestSuiteBuilder;
22 import com.google.common.collect.testing.SafeTreeSet;
23 import com.google.common.collect.testing.TestStringSortedSetGenerator;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.NavigableSet;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 
36 /**
37  * Tests for {@link Sets#synchronizedNavigableSet(NavigableSet)}.
38  *
39  * @author Louis Wasserman
40  */
41 public class SynchronizedNavigableSetTest extends TestCase {
42 
43   @SuppressWarnings("unchecked")
create()44   protected <E> NavigableSet<E> create() {
45     TestSet<E> inner =
46         new TestSet<>(new TreeSet<E>((Comparator<E>) Ordering.natural().nullsFirst()), null);
47     NavigableSet<E> outer = Synchronized.navigableSet(inner, null);
48     inner.mutex = outer;
49     return outer;
50   }
51 
52   static class TestSet<E> extends SynchronizedSetTest.TestSet<E> implements NavigableSet<E> {
53 
TestSet(NavigableSet<E> delegate, Object mutex)54     TestSet(NavigableSet<E> delegate, Object mutex) {
55       super(delegate, mutex);
56     }
57 
58     @Override
delegate()59     protected NavigableSet<E> delegate() {
60       return (NavigableSet<E>) super.delegate();
61     }
62 
63     @Override
ceiling(E e)64     public E ceiling(E e) {
65       assertTrue(Thread.holdsLock(mutex));
66       return delegate().ceiling(e);
67     }
68 
69     @Override
descendingIterator()70     public Iterator<E> descendingIterator() {
71       return delegate().descendingIterator();
72     }
73 
74     @Override
descendingSet()75     public NavigableSet<E> descendingSet() {
76       assertTrue(Thread.holdsLock(mutex));
77       return delegate().descendingSet();
78     }
79 
80     @Override
floor(E e)81     public E floor(E e) {
82       assertTrue(Thread.holdsLock(mutex));
83       return delegate().floor(e);
84     }
85 
86     @Override
headSet(E toElement, boolean inclusive)87     public NavigableSet<E> headSet(E toElement, boolean inclusive) {
88       assertTrue(Thread.holdsLock(mutex));
89       return delegate().headSet(toElement, inclusive);
90     }
91 
92     @Override
headSet(E toElement)93     public SortedSet<E> headSet(E toElement) {
94       return headSet(toElement, false);
95     }
96 
97     @Override
higher(E e)98     public E higher(E e) {
99       assertTrue(Thread.holdsLock(mutex));
100       return delegate().higher(e);
101     }
102 
103     @Override
lower(E e)104     public E lower(E e) {
105       return delegate().lower(e);
106     }
107 
108     @Override
pollFirst()109     public E pollFirst() {
110       assertTrue(Thread.holdsLock(mutex));
111       return delegate().pollFirst();
112     }
113 
114     @Override
pollLast()115     public E pollLast() {
116       assertTrue(Thread.holdsLock(mutex));
117       return delegate().pollLast();
118     }
119 
120     @Override
subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)121     public NavigableSet<E> subSet(
122         E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
123       assertTrue(Thread.holdsLock(mutex));
124       return delegate().subSet(fromElement, fromInclusive, toElement, toInclusive);
125     }
126 
127     @Override
subSet(E fromElement, E toElement)128     public SortedSet<E> subSet(E fromElement, E toElement) {
129       return subSet(fromElement, true, toElement, false);
130     }
131 
132     @Override
tailSet(E fromElement, boolean inclusive)133     public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
134       assertTrue(Thread.holdsLock(mutex));
135       return delegate().tailSet(fromElement, inclusive);
136     }
137 
138     @Override
tailSet(E fromElement)139     public SortedSet<E> tailSet(E fromElement) {
140       return tailSet(fromElement, true);
141     }
142 
143     @Override
comparator()144     public Comparator<? super E> comparator() {
145       assertTrue(Thread.holdsLock(mutex));
146       return delegate().comparator();
147     }
148 
149     @Override
first()150     public E first() {
151       assertTrue(Thread.holdsLock(mutex));
152       return delegate().first();
153     }
154 
155     @Override
last()156     public E last() {
157       assertTrue(Thread.holdsLock(mutex));
158       return delegate().last();
159     }
160 
161     private static final long serialVersionUID = 0;
162   }
163 
suite()164   public static TestSuite suite() {
165     TestSuite suite = new TestSuite();
166     suite.addTestSuite(SynchronizedNavigableSetTest.class);
167     suite.addTest(
168         NavigableSetTestSuiteBuilder.using(
169                 new TestStringSortedSetGenerator() {
170 
171                   @Override
172                   protected NavigableSet<String> create(String[] elements) {
173                     NavigableSet<String> innermost = new SafeTreeSet<>();
174                     Collections.addAll(innermost, elements);
175                     TestSet<String> inner = new TestSet<>(innermost, null);
176                     NavigableSet<String> outer = Synchronized.navigableSet(inner, null);
177                     inner.mutex = outer;
178                     return outer;
179                   }
180 
181                   @Override
182                   public List<String> order(List<String> insertionOrder) {
183                     return Ordering.natural().sortedCopy(insertionOrder);
184                   }
185                 })
186             .named("Sets.synchronizedNavigableSet[SafeTreeSet]")
187             .withFeatures(
188                 CollectionSize.ANY,
189                 CollectionFeature.KNOWN_ORDER,
190                 CollectionFeature.GENERAL_PURPOSE,
191                 CollectionFeature.SERIALIZABLE)
192             .createTestSuite());
193 
194     return suite;
195   }
196 
testDescendingSet()197   public void testDescendingSet() {
198     NavigableSet<String> set = create();
199     NavigableSet<String> descendingSet = set.descendingSet();
200     assertTrue(descendingSet instanceof SynchronizedNavigableSet);
201     assertSame(set, ((SynchronizedNavigableSet<String>) descendingSet).mutex);
202   }
203 
testHeadSet_E()204   public void testHeadSet_E() {
205     NavigableSet<String> set = create();
206     SortedSet<String> headSet = set.headSet("a");
207     assertTrue(headSet instanceof SynchronizedSortedSet);
208     assertSame(set, ((SynchronizedSortedSet<String>) headSet).mutex);
209   }
210 
testHeadSet_E_B()211   public void testHeadSet_E_B() {
212     NavigableSet<String> set = create();
213     NavigableSet<String> headSet = set.headSet("a", true);
214     assertTrue(headSet instanceof SynchronizedNavigableSet);
215     assertSame(set, ((SynchronizedNavigableSet<String>) headSet).mutex);
216   }
217 
testSubSet_E_E()218   public void testSubSet_E_E() {
219     NavigableSet<String> set = create();
220     SortedSet<String> subSet = set.subSet("a", "b");
221     assertTrue(subSet instanceof SynchronizedSortedSet);
222     assertSame(set, ((SynchronizedSortedSet<String>) subSet).mutex);
223   }
224 
testSubSet_E_B_E_B()225   public void testSubSet_E_B_E_B() {
226     NavigableSet<String> set = create();
227     NavigableSet<String> subSet = set.subSet("a", false, "b", true);
228     assertTrue(subSet instanceof SynchronizedNavigableSet);
229     assertSame(set, ((SynchronizedNavigableSet<String>) subSet).mutex);
230   }
231 
testTailSet_E()232   public void testTailSet_E() {
233     NavigableSet<String> set = create();
234     SortedSet<String> tailSet = set.tailSet("a");
235     assertTrue(tailSet instanceof SynchronizedSortedSet);
236     assertSame(set, ((SynchronizedSortedSet<String>) tailSet).mutex);
237   }
238 
testTailSet_E_B()239   public void testTailSet_E_B() {
240     NavigableSet<String> set = create();
241     NavigableSet<String> tailSet = set.tailSet("a", true);
242     assertTrue(tailSet instanceof SynchronizedNavigableSet);
243     assertSame(set, ((SynchronizedNavigableSet<String>) tailSet).mutex);
244   }
245 }
246