1/*
2 * Copyright (C) 2007 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 junit.framework.TestCase;
20
21import java.util.Collection;
22import java.util.Iterator;
23import java.util.Queue;
24
25/**
26 * Tests for {@link Synchronized#queue} and {@link Queues#synchronizedQueue}.
27 *
28 * @author Kurt Alfred Kluever
29 */
30public class SynchronizedQueueTest extends TestCase {
31
32  protected Queue<String> create() {
33    TestQueue<String> inner = new TestQueue<String>();
34    Queue<String> outer = Synchronized.queue(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 TestQueue<E> implements Queue<E> {
40    private final Queue<E> delegate = Lists.newLinkedList();
41    public final Object mutex = new Integer(1); // something Serializable
42
43    @Override
44    public boolean offer(E o) {
45      assertTrue(Thread.holdsLock(mutex));
46      return delegate.offer(o);
47    }
48
49    @Override
50    public E poll() {
51      assertTrue(Thread.holdsLock(mutex));
52      return delegate.poll();
53    }
54
55    @Override
56    public E remove() {
57      assertTrue(Thread.holdsLock(mutex));
58      return delegate.remove();
59    }
60
61    @Override
62    public E peek() {
63      assertTrue(Thread.holdsLock(mutex));
64      return delegate.peek();
65    }
66
67    @Override
68    public E element() {
69      assertTrue(Thread.holdsLock(mutex));
70      return delegate.element();
71    }
72
73    @Override
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
81    public int size() {
82      assertTrue(Thread.holdsLock(mutex));
83      return delegate.size();
84    }
85
86    @Override
87    public boolean removeAll(Collection<?> collection) {
88      assertTrue(Thread.holdsLock(mutex));
89      return delegate.removeAll(collection);
90    }
91
92    @Override
93    public boolean isEmpty() {
94      assertTrue(Thread.holdsLock(mutex));
95      return delegate.isEmpty();
96    }
97
98    @Override
99    public boolean contains(Object object) {
100      assertTrue(Thread.holdsLock(mutex));
101      return delegate.contains(object);
102    }
103
104    @Override
105    public boolean add(E element) {
106      assertTrue(Thread.holdsLock(mutex));
107      return delegate.add(element);
108    }
109
110    @Override
111    public boolean remove(Object object) {
112      assertTrue(Thread.holdsLock(mutex));
113      return delegate.remove(object);
114    }
115
116    @Override
117    public boolean containsAll(Collection<?> collection) {
118      assertTrue(Thread.holdsLock(mutex));
119      return delegate.containsAll(collection);
120    }
121
122    @Override
123    public boolean addAll(Collection<? extends E> collection) {
124      assertTrue(Thread.holdsLock(mutex));
125      return delegate.addAll(collection);
126    }
127
128    @Override
129    public boolean retainAll(Collection<?> collection) {
130      assertTrue(Thread.holdsLock(mutex));
131      return delegate.retainAll(collection);
132    }
133
134    @Override
135    public void clear() {
136      assertTrue(Thread.holdsLock(mutex));
137      delegate.clear();
138    }
139
140    @Override
141    public Object[] toArray() {
142      assertTrue(Thread.holdsLock(mutex));
143      return delegate.toArray();
144    }
145
146    @Override
147    public <T> T[] toArray(T[] array) {
148      assertTrue(Thread.holdsLock(mutex));
149      return delegate.toArray(array);
150    }
151
152    private static final long serialVersionUID = 0;
153  }
154
155  public void testHoldsLockOnAllOperations() {
156    create().element();
157    create().offer("foo");
158    create().peek();
159    create().poll();
160    create().remove();
161    create().add("foo");
162    create().addAll(ImmutableList.of("foo"));
163    create().clear();
164    create().contains("foo");
165    create().containsAll(ImmutableList.of("foo"));
166    create().equals(ImmutableList.of("foo"));
167    create().hashCode();
168    create().isEmpty();
169    create().iterator();
170    create().remove("foo");
171    create().removeAll(ImmutableList.of("foo"));
172    create().retainAll(ImmutableList.of("foo"));
173    create().size();
174    create().toArray();
175    create().toArray(new String[] { "foo" });
176  }
177}
178