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