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.testing.SerializableTester; 22 23 import java.io.Serializable; 24 import java.util.Collection; 25 import java.util.HashSet; 26 import java.util.Set; 27 28 import javax.annotation.Nullable; 29 30 /** 31 * Tests for {@code Synchronized#set}. 32 * 33 * @author Mike Bostock 34 */ 35 public class SynchronizedSetTest extends AbstractCollectionTest { 36 public final Object mutex = new Integer(1); // something Serializable 37 create()38 @Override protected <E> Set<E> create() { 39 TestSet<E> inner = new TestSet<E>(new HashSet<E>(), mutex); 40 Set<E> outer = Synchronized.set(inner, inner.mutex); 41 return outer; 42 } 43 testNullPointerExceptions()44 @Override public void testNullPointerExceptions() throws Exception { 45 /* Skip this test, as SynchronizedSet is not a public class. */ 46 } 47 testSerialization()48 public void testSerialization() { 49 SerializableTester.reserializeAndAssert(create()); 50 } 51 52 static class TestSet<E> extends ForwardingSet<E> implements Serializable { 53 final Set<E> delegate; 54 public final Object mutex; 55 TestSet(Set<E> delegate, Object mutex)56 public TestSet(Set<E> delegate, Object mutex) { 57 checkNotNull(mutex); 58 this.delegate = delegate; 59 this.mutex = mutex; 60 } 61 delegate()62 @Override protected Set<E> delegate() { 63 return delegate; 64 } 65 toString()66 @Override public String toString() { 67 assertTrue(Thread.holdsLock(mutex)); 68 return super.toString(); 69 } 70 equals(@ullable Object o)71 @Override public boolean equals(@Nullable Object o) { 72 assertTrue(Thread.holdsLock(mutex)); 73 return super.equals(o); 74 } 75 hashCode()76 @Override public int hashCode() { 77 assertTrue(Thread.holdsLock(mutex)); 78 return super.hashCode(); 79 } 80 add(@ullable E o)81 @Override public boolean add(@Nullable E o) { 82 assertTrue(Thread.holdsLock(mutex)); 83 return super.add(o); 84 } 85 addAll(Collection<? extends E> c)86 @Override public boolean addAll(Collection<? extends E> c) { 87 assertTrue(Thread.holdsLock(mutex)); 88 return super.addAll(c); 89 } 90 clear()91 @Override public void clear() { 92 assertTrue(Thread.holdsLock(mutex)); 93 super.clear(); 94 } 95 contains(@ullable Object o)96 @Override public boolean contains(@Nullable Object o) { 97 assertTrue(Thread.holdsLock(mutex)); 98 return super.contains(o); 99 } 100 containsAll(Collection<?> c)101 @Override public boolean containsAll(Collection<?> c) { 102 assertTrue(Thread.holdsLock(mutex)); 103 return super.containsAll(c); 104 } 105 isEmpty()106 @Override public boolean isEmpty() { 107 assertTrue(Thread.holdsLock(mutex)); 108 return super.isEmpty(); 109 } 110 111 /* Don't test iterator(); it may or may not hold the mutex. */ 112 remove(@ullable Object o)113 @Override public boolean remove(@Nullable Object o) { 114 assertTrue(Thread.holdsLock(mutex)); 115 return super.remove(o); 116 } 117 removeAll(Collection<?> c)118 @Override public boolean removeAll(Collection<?> c) { 119 assertTrue(Thread.holdsLock(mutex)); 120 return super.removeAll(c); 121 } 122 retainAll(Collection<?> c)123 @Override public boolean retainAll(Collection<?> c) { 124 assertTrue(Thread.holdsLock(mutex)); 125 return super.retainAll(c); 126 } 127 size()128 @Override public int size() { 129 assertTrue(Thread.holdsLock(mutex)); 130 return super.size(); 131 } 132 toArray()133 @Override public Object[] toArray() { 134 assertTrue(Thread.holdsLock(mutex)); 135 return super.toArray(); 136 } 137 toArray(T[] a)138 @Override public <T> T[] toArray(T[] a) { 139 assertTrue(Thread.holdsLock(mutex)); 140 return super.toArray(a); 141 } 142 143 private static final long serialVersionUID = 0; 144 } 145 } 146