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 private static final Object MUTEX = new Integer(1); // something Serializable 43 44 @SuppressWarnings("unchecked") create()45 protected <E> NavigableSet<E> create() { 46 TestSet<E> inner = 47 new TestSet<>(new TreeSet<E>((Comparator<E>) Ordering.natural().nullsFirst()), MUTEX); 48 NavigableSet<E> outer = Synchronized.navigableSet(inner, MUTEX); 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, MUTEX); 176 NavigableSet<String> outer = Synchronized.navigableSet(inner, MUTEX); 177 return outer; 178 } 179 180 @Override 181 public List<String> order(List<String> insertionOrder) { 182 return Ordering.natural().sortedCopy(insertionOrder); 183 } 184 }) 185 .named("Sets.synchronizedNavigableSet[SafeTreeSet]") 186 .withFeatures( 187 CollectionSize.ANY, 188 CollectionFeature.KNOWN_ORDER, 189 CollectionFeature.GENERAL_PURPOSE, 190 CollectionFeature.SERIALIZABLE) 191 .createTestSuite()); 192 193 return suite; 194 } 195 testDescendingSet()196 public void testDescendingSet() { 197 NavigableSet<String> map = create(); 198 NavigableSet<String> descendingSet = map.descendingSet(); 199 assertTrue(descendingSet instanceof SynchronizedNavigableSet); 200 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) descendingSet).mutex); 201 } 202 testHeadSet_E()203 public void testHeadSet_E() { 204 NavigableSet<String> map = create(); 205 SortedSet<String> headSet = map.headSet("a"); 206 assertTrue(headSet instanceof SynchronizedSortedSet); 207 assertSame(MUTEX, ((SynchronizedSortedSet<String>) headSet).mutex); 208 } 209 testHeadSet_E_B()210 public void testHeadSet_E_B() { 211 NavigableSet<String> map = create(); 212 NavigableSet<String> headSet = map.headSet("a", true); 213 assertTrue(headSet instanceof SynchronizedNavigableSet); 214 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) headSet).mutex); 215 } 216 testSubSet_E_E()217 public void testSubSet_E_E() { 218 NavigableSet<String> map = create(); 219 SortedSet<String> subSet = map.subSet("a", "b"); 220 assertTrue(subSet instanceof SynchronizedSortedSet); 221 assertSame(MUTEX, ((SynchronizedSortedSet<String>) subSet).mutex); 222 } 223 testSubSet_E_B_E_B()224 public void testSubSet_E_B_E_B() { 225 NavigableSet<String> map = create(); 226 NavigableSet<String> subSet = map.subSet("a", false, "b", true); 227 assertTrue(subSet instanceof SynchronizedNavigableSet); 228 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) subSet).mutex); 229 } 230 testTailSet_E()231 public void testTailSet_E() { 232 NavigableSet<String> map = create(); 233 SortedSet<String> tailSet = map.tailSet("a"); 234 assertTrue(tailSet instanceof SynchronizedSortedSet); 235 assertSame(MUTEX, ((SynchronizedSortedSet<String>) tailSet).mutex); 236 } 237 testTailSet_E_B()238 public void testTailSet_E_B() { 239 NavigableSet<String> map = create(); 240 NavigableSet<String> tailSet = map.tailSet("a", true); 241 assertTrue(tailSet instanceof SynchronizedNavigableSet); 242 assertSame(MUTEX, ((SynchronizedNavigableSet<String>) tailSet).mutex); 243 } 244 } 245