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 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import java.util.Collections; 31 import java.util.Comparator; 32 import java.util.Iterator; 33 import java.util.List; 34 import java.util.NavigableSet; 35 import java.util.SortedSet; 36 import java.util.TreeSet; 37 38 /** 39 * Tests for {@link Sets#synchronizedNavigableSet(NavigableSet)}. 40 * 41 * @author Louis Wasserman 42 */ 43 public class SynchronizedNavigableSetTest extends TestCase { 44 private static final Object MUTEX = new Integer(1); // something Serializable 45 46 @SuppressWarnings("unchecked") create()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 TestSet(NavigableSet<E> delegate, Object mutex)58 TestSet(NavigableSet<E> delegate, Object mutex) { 59 super(delegate, mutex); 60 } 61 delegate()62 @Override protected NavigableSet<E> delegate() { 63 return (NavigableSet<E>) super.delegate(); 64 } 65 ceiling(E e)66 @Override public E ceiling(E e) { 67 assertTrue(Thread.holdsLock(mutex)); 68 return delegate().ceiling(e); 69 } 70 descendingIterator()71 @Override public Iterator<E> descendingIterator() { 72 return delegate().descendingIterator(); 73 } 74 descendingSet()75 @Override public NavigableSet<E> descendingSet() { 76 assertTrue(Thread.holdsLock(mutex)); 77 return delegate().descendingSet(); 78 } 79 floor(E e)80 @Override public E floor(E e) { 81 assertTrue(Thread.holdsLock(mutex)); 82 return delegate().floor(e); 83 } 84 headSet(E toElement, boolean inclusive)85 @Override public NavigableSet<E> headSet(E toElement, boolean inclusive) { 86 assertTrue(Thread.holdsLock(mutex)); 87 return delegate().headSet(toElement, inclusive); 88 } 89 headSet(E toElement)90 @Override public SortedSet<E> headSet(E toElement) { 91 return headSet(toElement, false); 92 } 93 higher(E e)94 @Override public E higher(E e) { 95 assertTrue(Thread.holdsLock(mutex)); 96 return delegate().higher(e); 97 } 98 lower(E e)99 @Override public E lower(E e) { 100 return delegate().lower(e); 101 } 102 pollFirst()103 @Override public E pollFirst() { 104 assertTrue(Thread.holdsLock(mutex)); 105 return delegate().pollFirst(); 106 } 107 pollLast()108 @Override public E pollLast() { 109 assertTrue(Thread.holdsLock(mutex)); 110 return delegate().pollLast(); 111 } 112 subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)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 subSet(E fromElement, E toElement)120 @Override public SortedSet<E> subSet(E fromElement, E toElement) { 121 return subSet(fromElement, true, toElement, false); 122 } 123 tailSet(E fromElement, boolean inclusive)124 @Override public NavigableSet<E> tailSet(E fromElement, boolean inclusive) { 125 assertTrue(Thread.holdsLock(mutex)); 126 return delegate().tailSet(fromElement, inclusive); 127 } 128 tailSet(E fromElement)129 @Override public SortedSet<E> tailSet(E fromElement) { 130 return tailSet(fromElement, true); 131 } 132 comparator()133 @Override public Comparator<? super E> comparator() { 134 assertTrue(Thread.holdsLock(mutex)); 135 return delegate().comparator(); 136 } 137 first()138 @Override public E first() { 139 assertTrue(Thread.holdsLock(mutex)); 140 return delegate().first(); 141 } 142 last()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 suite()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 testDescendingSet()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 testHeadSet_E()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 testHeadSet_E_B()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 testSubSet_E_E()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 testSubSet_E_B_E_B()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 testTailSet_E()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 testTailSet_E_B()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