1 /* 2 * Copyright (C) 2013 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 junit.framework.TestCase; 20 21 import java.util.Collection; 22 import java.util.Deque; 23 import java.util.Iterator; 24 25 /** 26 * Tests for {@link Synchronized#deque} and {@link Queues#synchronizedDeque}. 27 * 28 * @author Kurt Alfred Kluever 29 */ 30 public class SynchronizedDequeTest extends TestCase { 31 create()32 protected Deque<String> create() { 33 TestDeque<String> inner = new TestDeque<String>(); 34 Deque<String> outer = Synchronized.deque(inner, inner.mutex); 35 outer.add("foo"); // necessary because we try to remove elements later on 36 return outer; 37 } 38 39 private static final class TestDeque<E> implements Deque<E> { 40 private final Deque<E> delegate = Lists.newLinkedList(); 41 public final Object mutex = new Integer(1); // something Serializable 42 43 @Override offer(E o)44 public boolean offer(E o) { 45 assertTrue(Thread.holdsLock(mutex)); 46 return delegate.offer(o); 47 } 48 49 @Override poll()50 public E poll() { 51 assertTrue(Thread.holdsLock(mutex)); 52 return delegate.poll(); 53 } 54 55 @Override remove()56 public E remove() { 57 assertTrue(Thread.holdsLock(mutex)); 58 return delegate.remove(); 59 } 60 61 @Override peek()62 public E peek() { 63 assertTrue(Thread.holdsLock(mutex)); 64 return delegate.peek(); 65 } 66 67 @Override element()68 public E element() { 69 assertTrue(Thread.holdsLock(mutex)); 70 return delegate.element(); 71 } 72 73 @Override iterator()74 public Iterator<E> iterator() { 75 // We explicitly don't lock for iterator() 76 assertFalse(Thread.holdsLock(mutex)); 77 return delegate.iterator(); 78 } 79 80 @Override size()81 public int size() { 82 assertTrue(Thread.holdsLock(mutex)); 83 return delegate.size(); 84 } 85 86 @Override removeAll(Collection<?> collection)87 public boolean removeAll(Collection<?> collection) { 88 assertTrue(Thread.holdsLock(mutex)); 89 return delegate.removeAll(collection); 90 } 91 92 @Override isEmpty()93 public boolean isEmpty() { 94 assertTrue(Thread.holdsLock(mutex)); 95 return delegate.isEmpty(); 96 } 97 98 @Override contains(Object object)99 public boolean contains(Object object) { 100 assertTrue(Thread.holdsLock(mutex)); 101 return delegate.contains(object); 102 } 103 104 @Override add(E element)105 public boolean add(E element) { 106 assertTrue(Thread.holdsLock(mutex)); 107 return delegate.add(element); 108 } 109 110 @Override remove(Object object)111 public boolean remove(Object object) { 112 assertTrue(Thread.holdsLock(mutex)); 113 return delegate.remove(object); 114 } 115 116 @Override containsAll(Collection<?> collection)117 public boolean containsAll(Collection<?> collection) { 118 assertTrue(Thread.holdsLock(mutex)); 119 return delegate.containsAll(collection); 120 } 121 122 @Override addAll(Collection<? extends E> collection)123 public boolean addAll(Collection<? extends E> collection) { 124 assertTrue(Thread.holdsLock(mutex)); 125 return delegate.addAll(collection); 126 } 127 128 @Override retainAll(Collection<?> collection)129 public boolean retainAll(Collection<?> collection) { 130 assertTrue(Thread.holdsLock(mutex)); 131 return delegate.retainAll(collection); 132 } 133 134 @Override clear()135 public void clear() { 136 assertTrue(Thread.holdsLock(mutex)); 137 delegate.clear(); 138 } 139 140 @Override toArray()141 public Object[] toArray() { 142 assertTrue(Thread.holdsLock(mutex)); 143 return delegate.toArray(); 144 } 145 146 @Override toArray(T[] array)147 public <T> T[] toArray(T[] array) { 148 assertTrue(Thread.holdsLock(mutex)); 149 return delegate.toArray(array); 150 } 151 152 @Override addFirst(E e)153 public void addFirst(E e) { 154 assertTrue(Thread.holdsLock(mutex)); 155 delegate.addFirst(e); 156 } 157 158 @Override addLast(E e)159 public void addLast(E e) { 160 assertTrue(Thread.holdsLock(mutex)); 161 delegate.addLast(e); 162 } 163 164 @Override offerFirst(E e)165 public boolean offerFirst(E e) { 166 assertTrue(Thread.holdsLock(mutex)); 167 return delegate.offerFirst(e); 168 } 169 170 @Override offerLast(E e)171 public boolean offerLast(E e) { 172 assertTrue(Thread.holdsLock(mutex)); 173 return delegate.offerLast(e); 174 } 175 176 @Override removeFirst()177 public E removeFirst() { 178 assertTrue(Thread.holdsLock(mutex)); 179 return delegate.removeFirst(); 180 } 181 182 @Override removeLast()183 public E removeLast() { 184 assertTrue(Thread.holdsLock(mutex)); 185 return delegate.removeLast(); 186 } 187 188 @Override pollFirst()189 public E pollFirst() { 190 assertTrue(Thread.holdsLock(mutex)); 191 return delegate.pollFirst(); 192 } 193 194 @Override pollLast()195 public E pollLast() { 196 assertTrue(Thread.holdsLock(mutex)); 197 return delegate.pollLast(); 198 } 199 200 @Override getFirst()201 public E getFirst() { 202 assertTrue(Thread.holdsLock(mutex)); 203 return delegate.getFirst(); 204 } 205 206 @Override getLast()207 public E getLast() { 208 assertTrue(Thread.holdsLock(mutex)); 209 return delegate.getLast(); 210 } 211 212 @Override peekFirst()213 public E peekFirst() { 214 assertTrue(Thread.holdsLock(mutex)); 215 return delegate.peekFirst(); 216 } 217 218 @Override peekLast()219 public E peekLast() { 220 assertTrue(Thread.holdsLock(mutex)); 221 return delegate.peekLast(); 222 } 223 224 @Override removeFirstOccurrence(Object o)225 public boolean removeFirstOccurrence(Object o) { 226 assertTrue(Thread.holdsLock(mutex)); 227 return delegate.removeFirstOccurrence(o); 228 } 229 230 @Override removeLastOccurrence(Object o)231 public boolean removeLastOccurrence(Object o) { 232 assertTrue(Thread.holdsLock(mutex)); 233 return delegate.removeLastOccurrence(o); 234 } 235 236 @Override push(E e)237 public void push(E e) { 238 assertTrue(Thread.holdsLock(mutex)); 239 delegate.push(e); 240 } 241 242 @Override pop()243 public E pop() { 244 assertTrue(Thread.holdsLock(mutex)); 245 return delegate.pop(); 246 } 247 248 @Override descendingIterator()249 public Iterator<E> descendingIterator() { 250 assertTrue(Thread.holdsLock(mutex)); 251 return delegate.descendingIterator(); 252 } 253 254 private static final long serialVersionUID = 0; 255 } 256 testHoldsLockOnAllOperations()257 public void testHoldsLockOnAllOperations() { 258 create().element(); 259 create().offer("foo"); 260 create().peek(); 261 create().poll(); 262 create().remove(); 263 create().add("foo"); 264 create().addAll(ImmutableList.of("foo")); 265 create().clear(); 266 create().contains("foo"); 267 create().containsAll(ImmutableList.of("foo")); 268 create().equals(ImmutableList.of("foo")); 269 create().hashCode(); 270 create().isEmpty(); 271 create().iterator(); 272 create().remove("foo"); 273 create().removeAll(ImmutableList.of("foo")); 274 create().retainAll(ImmutableList.of("foo")); 275 create().size(); 276 create().toArray(); 277 create().toArray(new String[] { "foo" }); 278 create().addFirst("e"); 279 create().addLast("e"); 280 create().offerFirst("e"); 281 create().offerLast("e"); 282 create().removeFirst(); 283 create().removeLast(); 284 create().pollFirst(); 285 create().pollLast(); 286 create().getFirst(); 287 create().getLast(); 288 create().peekFirst(); 289 create().peekLast(); 290 create().removeFirstOccurrence("e"); 291 create().removeLastOccurrence("e"); 292 create().push("e"); 293 create().pop(); 294 create().descendingIterator(); 295 } 296 } 297