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.compatqual.NullableDecl; 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 suite()43 public static Test suite() { 44 return SetTestSuiteBuilder.using( 45 new TestStringSetGenerator() { 46 @Override 47 protected Set<String> create(String[] elements) { 48 TestSet<String> inner = new TestSet<>(new HashSet<String>(), MUTEX); 49 Set<String> outer = Synchronized.set(inner, inner.mutex); 50 Collections.addAll(outer, elements); 51 return outer; 52 } 53 }) 54 .named("Synchronized.set") 55 .withFeatures( 56 CollectionFeature.GENERAL_PURPOSE, 57 CollectionFeature.ALLOWS_NULL_VALUES, 58 CollectionSize.ANY, 59 CollectionFeature.SERIALIZABLE) 60 .createTestSuite(); 61 } 62 63 static class TestSet<E> extends ForwardingSet<E> implements Serializable { 64 final Set<E> delegate; 65 public final Object mutex; 66 67 public TestSet(Set<E> delegate, Object mutex) { 68 checkNotNull(mutex); 69 this.delegate = delegate; 70 this.mutex = mutex; 71 } 72 73 @Override 74 protected Set<E> delegate() { 75 return delegate; 76 } 77 78 @Override 79 public String toString() { 80 assertTrue(Thread.holdsLock(mutex)); 81 return super.toString(); 82 } 83 84 @Override 85 public boolean equals(@NullableDecl Object o) { 86 assertTrue(Thread.holdsLock(mutex)); 87 return super.equals(o); 88 } 89 90 @Override 91 public int hashCode() { 92 assertTrue(Thread.holdsLock(mutex)); 93 return super.hashCode(); 94 } 95 96 @Override 97 public boolean add(@NullableDecl E o) { 98 assertTrue(Thread.holdsLock(mutex)); 99 return super.add(o); 100 } 101 102 @Override 103 public boolean addAll(Collection<? extends E> c) { 104 assertTrue(Thread.holdsLock(mutex)); 105 return super.addAll(c); 106 } 107 108 @Override 109 public void clear() { 110 assertTrue(Thread.holdsLock(mutex)); 111 super.clear(); 112 } 113 114 @Override 115 public boolean contains(@NullableDecl Object o) { 116 assertTrue(Thread.holdsLock(mutex)); 117 return super.contains(o); 118 } 119 120 @Override 121 public boolean containsAll(Collection<?> c) { 122 assertTrue(Thread.holdsLock(mutex)); 123 return super.containsAll(c); 124 } 125 126 @Override 127 public boolean isEmpty() { 128 assertTrue(Thread.holdsLock(mutex)); 129 return super.isEmpty(); 130 } 131 132 /* Don't test iterator(); it may or may not hold the mutex. */ 133 134 @Override 135 public boolean remove(@NullableDecl Object o) { 136 assertTrue(Thread.holdsLock(mutex)); 137 return super.remove(o); 138 } 139 140 @Override 141 public boolean removeAll(Collection<?> c) { 142 assertTrue(Thread.holdsLock(mutex)); 143 return super.removeAll(c); 144 } 145 146 @Override 147 public boolean retainAll(Collection<?> c) { 148 assertTrue(Thread.holdsLock(mutex)); 149 return super.retainAll(c); 150 } 151 152 @Override 153 public int size() { 154 assertTrue(Thread.holdsLock(mutex)); 155 return super.size(); 156 } 157 158 @Override 159 public Object[] toArray() { 160 assertTrue(Thread.holdsLock(mutex)); 161 return super.toArray(); 162 } 163 164 @Override 165 public <T> T[] toArray(T[] a) { 166 assertTrue(Thread.holdsLock(mutex)); 167 return super.toArray(a); 168 } 169 170 private static final long serialVersionUID = 0; 171 } 172 } 173