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 import org.checkerframework.checker.nullness.qual.Nullable; 36 37 /** 38 * Tests for {@link Sets#synchronizedNavigableSet(NavigableSet)}. 39 * 40 * @author Louis Wasserman 41 */ 42 public class SynchronizedNavigableSetTest extends TestCase { 43 private static final Object MUTEX = new Integer(1); // something Serializable 44 45 @SuppressWarnings("unchecked") create()46 protected <E> NavigableSet<E> create() { 47 TestSet<E> inner = 48 new TestSet<>(new TreeSet<E>((Comparator<E>) Ordering.natural().nullsFirst()), MUTEX); 49 NavigableSet<E> outer = Synchronized.navigableSet(inner, MUTEX); 50 return outer; 51 } 52 53 static class TestSet<E> extends SynchronizedSetTest.TestSet<E> implements NavigableSet<E> { 54 TestSet(NavigableSet<E> delegate, @Nullable Object mutex)55 TestSet(NavigableSet<E> delegate, @Nullable Object mutex) { 56 super(delegate, mutex); 57 } 58 59 @Override delegate()60 protected NavigableSet<E> delegate() { 61 return (NavigableSet<E>) super.delegate(); 62 } 63 64 @Override ceiling(E e)65 public @Nullable E ceiling(E e) { 66 assertTrue(Thread.holdsLock(mutex)); 67 return delegate().ceiling(e); 68 } 69 70 @Override descendingIterator()71 public Iterator<E> descendingIterator() { 72 return delegate().descendingIterator(); 73 } 74 75 @Override descendingSet()76 public NavigableSet<E> descendingSet() { 77 assertTrue(Thread.holdsLock(mutex)); 78 return delegate().descendingSet(); 79 } 80 81 @Override floor(E e)82 public @Nullable E floor(E e) { 83 assertTrue(Thread.holdsLock(mutex)); 84 return delegate().floor(e); 85 } 86 87 @Override headSet(E toElement, boolean inclusive)88 public NavigableSet<E> headSet(E toElement, boolean inclusive) { 89 assertTrue(Thread.holdsLock(mutex)); 90 return delegate().headSet(toElement, inclusive); 91 } 92 93 @Override headSet(E toElement)94 public SortedSet<E> headSet(E toElement) { 95 return headSet(toElement, false); 96 } 97 98 @Override higher(E e)99 public @Nullable E higher(E e) { 100 assertTrue(Thread.holdsLock(mutex)); 101 return delegate().higher(e); 102 } 103 104 @Override lower(E e)105 public @Nullable E lower(E e) { 106 return delegate().lower(e); 107 } 108 109 @Override pollFirst()110 public @Nullable E pollFirst() { 111 assertTrue(Thread.holdsLock(mutex)); 112 return delegate().pollFirst(); 113 } 114 115 @Override pollLast()116 public @Nullable E pollLast() { 117 assertTrue(Thread.holdsLock(mutex)); 118 return delegate().pollLast(); 119 } 120 121 @Override subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)122 public NavigableSet<E> subSet( 123 E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { 124 assertTrue(Thread.holdsLock(mutex)); 125 return delegate().subSet(fromElement, fromInclusive, toElement, toInclusive); 126 } 127 128 @Override subSet(E fromElement, E toElement)129 public SortedSet<E> subSet(E fromElement, E toElement) { 130 return subSet(fromElement, true, toElement, false); 131 } 132 133 @Override tailSet(E fromElement, boolean inclusive)134 public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { 135 assertTrue(Thread.holdsLock(mutex)); 136 return delegate().tailSet(fromElement, inclusive); 137 } 138 139 @Override tailSet(E fromElement)140 public SortedSet<E> tailSet(E fromElement) { 141 return tailSet(fromElement, true); 142 } 143 144 @Override comparator()145 public Comparator<? super E> comparator() { 146 assertTrue(Thread.holdsLock(mutex)); 147 return delegate().comparator(); 148 } 149 150 @Override first()151 public E first() { 152 assertTrue(Thread.holdsLock(mutex)); 153 return delegate().first(); 154 } 155 156 @Override last()157 public E last() { 158 assertTrue(Thread.holdsLock(mutex)); 159 return delegate().last(); 160 } 161 162 private static final long serialVersionUID = 0; 163 } 164 suite()165 public static TestSuite suite() { 166 TestSuite suite = new TestSuite(); 167 suite.addTestSuite(SynchronizedNavigableSetTest.class); 168 suite.addTest( 169 NavigableSetTestSuiteBuilder.using( 170 new TestStringSortedSetGenerator() { 171 172 @Override 173 protected NavigableSet<String> create(String[] elements) { 174 NavigableSet<String> innermost = new SafeTreeSet<>(); 175 Collections.addAll(innermost, elements); 176 TestSet<String> inner = new TestSet<>(innermost, MUTEX); 177 NavigableSet<String> outer = Synchronized.navigableSet(inner, MUTEX); 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> map = create(); 199 NavigableSet<String> descendingSet = map.descendingSet(); 200 assertTrue(descendingSet instanceof SynchronizedNavigableSet); 201 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) descendingSet).mutex); 202 } 203 testHeadSet_E()204 public void testHeadSet_E() { 205 NavigableSet<String> map = create(); 206 SortedSet<String> headSet = map.headSet("a"); 207 assertTrue(headSet instanceof SynchronizedSortedSet); 208 assertSame(MUTEX, ((SynchronizedSortedSet<String>) headSet).mutex); 209 } 210 testHeadSet_E_B()211 public void testHeadSet_E_B() { 212 NavigableSet<String> map = create(); 213 NavigableSet<String> headSet = map.headSet("a", true); 214 assertTrue(headSet instanceof SynchronizedNavigableSet); 215 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) headSet).mutex); 216 } 217 testSubSet_E_E()218 public void testSubSet_E_E() { 219 NavigableSet<String> map = create(); 220 SortedSet<String> subSet = map.subSet("a", "b"); 221 assertTrue(subSet instanceof SynchronizedSortedSet); 222 assertSame(MUTEX, ((SynchronizedSortedSet<String>) subSet).mutex); 223 } 224 testSubSet_E_B_E_B()225 public void testSubSet_E_B_E_B() { 226 NavigableSet<String> map = create(); 227 NavigableSet<String> subSet = map.subSet("a", false, "b", true); 228 assertTrue(subSet instanceof SynchronizedNavigableSet); 229 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) subSet).mutex); 230 } 231 testTailSet_E()232 public void testTailSet_E() { 233 NavigableSet<String> map = create(); 234 SortedSet<String> tailSet = map.tailSet("a"); 235 assertTrue(tailSet instanceof SynchronizedSortedSet); 236 assertSame(MUTEX, ((SynchronizedSortedSet<String>) tailSet).mutex); 237 } 238 testTailSet_E_B()239 public void testTailSet_E_B() { 240 NavigableSet<String> map = create(); 241 NavigableSet<String> tailSet = map.tailSet("a", true); 242 assertTrue(tailSet instanceof SynchronizedNavigableSet); 243 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) tailSet).mutex); 244 } 245 } 246