• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.collect;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.common.collect.testing.SetTestSuiteBuilder;
22 import com.google.common.collect.testing.TestStringSetGenerator;
23 import com.google.common.collect.testing.features.CollectionFeature;
24 import com.google.common.collect.testing.features.CollectionSize;
25 import java.io.Serializable;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.Set;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import org.checkerframework.checker.nullness.qual.Nullable;
33 
34 /**
35  * Tests for {@code Synchronized#set}.
36  *
37  * @author Mike Bostock
38  */
39 public class SynchronizedSetTest extends TestCase {
40 
41   public static final Object MUTEX = new Integer(1); // something Serializable
42 
43   // TODO(cpovirk): Resolve difference between branches in their choice of mutex:
44   // - The mainline uses `null` (even since the change in cl/99720576 was integrated).
45   // - The backport continued to use MUTEX.
suite()46   public static Test suite() {
47     return SetTestSuiteBuilder.using(
48             new TestStringSetGenerator() {
49               @Override
50               protected Set<String> create(String[] elements) {
51                 TestSet<String> inner = new TestSet<>(new HashSet<String>(), MUTEX);
52                 Set<String> outer = Synchronized.set(inner, inner.mutex);
53                 Collections.addAll(outer, elements);
54                 return outer;
55               }
56             })
57         .named("Synchronized.set")
58         .withFeatures(
59             CollectionFeature.GENERAL_PURPOSE,
60             CollectionFeature.ALLOWS_NULL_VALUES,
61             CollectionSize.ANY,
62             CollectionFeature.SERIALIZABLE)
63         .createTestSuite();
64   }
65 
66   static class TestSet<E> extends ForwardingSet<E> implements Serializable {
67     final Set<E> delegate;
68     public final Object mutex;
69 
70     public TestSet(Set<E> delegate, Object mutex) {
71       checkNotNull(mutex);
72       this.delegate = delegate;
73       this.mutex = mutex;
74     }
75 
76     @Override
77     protected Set<E> delegate() {
78       return delegate;
79     }
80 
81     @Override
82     public String toString() {
83       assertTrue(Thread.holdsLock(mutex));
84       return super.toString();
85     }
86 
87     @Override
88     public boolean equals(@Nullable Object o) {
89       assertTrue(Thread.holdsLock(mutex));
90       return super.equals(o);
91     }
92 
93     @Override
94     public int hashCode() {
95       assertTrue(Thread.holdsLock(mutex));
96       return super.hashCode();
97     }
98 
99     @Override
100     public boolean add(@Nullable E o) {
101       assertTrue(Thread.holdsLock(mutex));
102       return super.add(o);
103     }
104 
105     @Override
106     public boolean addAll(Collection<? extends E> c) {
107       assertTrue(Thread.holdsLock(mutex));
108       return super.addAll(c);
109     }
110 
111     @Override
112     public void clear() {
113       assertTrue(Thread.holdsLock(mutex));
114       super.clear();
115     }
116 
117     @Override
118     public boolean contains(@Nullable Object o) {
119       assertTrue(Thread.holdsLock(mutex));
120       return super.contains(o);
121     }
122 
123     @Override
124     public boolean containsAll(Collection<?> c) {
125       assertTrue(Thread.holdsLock(mutex));
126       return super.containsAll(c);
127     }
128 
129     @Override
130     public boolean isEmpty() {
131       assertTrue(Thread.holdsLock(mutex));
132       return super.isEmpty();
133     }
134 
135     /* Don't test iterator(); it may or may not hold the mutex. */
136 
137     @Override
138     public boolean remove(@Nullable Object o) {
139       assertTrue(Thread.holdsLock(mutex));
140       return super.remove(o);
141     }
142 
143     @Override
144     public boolean removeAll(Collection<?> c) {
145       assertTrue(Thread.holdsLock(mutex));
146       return super.removeAll(c);
147     }
148 
149     @Override
150     public boolean retainAll(Collection<?> c) {
151       assertTrue(Thread.holdsLock(mutex));
152       return super.retainAll(c);
153     }
154 
155     @Override
156     public int size() {
157       assertTrue(Thread.holdsLock(mutex));
158       return super.size();
159     }
160 
161     @Override
162     public Object[] toArray() {
163       assertTrue(Thread.holdsLock(mutex));
164       return super.toArray();
165     }
166 
167     @Override
168     public <T> T[] toArray(T[] a) {
169       assertTrue(Thread.holdsLock(mutex));
170       return super.toArray(a);
171     }
172 
173     private static final long serialVersionUID = 0;
174   }
175 }
176