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.Synchronized.SynchronizedCollection;
22 import com.google.common.collect.Synchronized.SynchronizedSet;
23 import com.google.common.testing.SerializableTester;
24 import java.io.Serializable;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30 import junit.framework.TestCase;
31 
32 /**
33  * Tests for {@code Synchronized#map}.
34  *
35  * @author Mike Bostock
36  */
37 public class SynchronizedMapTest extends TestCase {
38   public final Object mutex = new Integer(1); // something Serializable
39 
create()40   protected <K, V> Map<K, V> create() {
41     TestMap<K, V> inner = new TestMap<>(new HashMap<K, V>(), mutex);
42     Map<K, V> outer = Synchronized.map(inner, mutex);
43     return outer;
44   }
45 
46   static class TestMap<K, V> extends ForwardingMap<K, V> implements Serializable {
47     public final Object mutex;
48     private Map<K, V> delegate;
49 
TestMap(Map<K, V> delegate, Object mutex)50     public TestMap(Map<K, V> delegate, Object mutex) {
51       checkNotNull(mutex);
52       this.delegate = delegate;
53       this.mutex = mutex;
54     }
55 
56     @Override
delegate()57     protected Map<K, V> delegate() {
58       return delegate;
59     }
60 
61     @Override
size()62     public int size() {
63       assertTrue(Thread.holdsLock(mutex));
64       return super.size();
65     }
66 
67     @Override
isEmpty()68     public boolean isEmpty() {
69       assertTrue(Thread.holdsLock(mutex));
70       return super.isEmpty();
71     }
72 
73     @Override
remove(Object object)74     public V remove(Object object) {
75       assertTrue(Thread.holdsLock(mutex));
76       return super.remove(object);
77     }
78 
79     @Override
clear()80     public void clear() {
81       assertTrue(Thread.holdsLock(mutex));
82       super.clear();
83     }
84 
85     @Override
containsKey(Object key)86     public boolean containsKey(Object key) {
87       assertTrue(Thread.holdsLock(mutex));
88       return super.containsKey(key);
89     }
90 
91     @Override
containsValue(Object value)92     public boolean containsValue(Object value) {
93       assertTrue(Thread.holdsLock(mutex));
94       return super.containsValue(value);
95     }
96 
97     @Override
get(Object key)98     public V get(Object key) {
99       assertTrue(Thread.holdsLock(mutex));
100       return super.get(key);
101     }
102 
103     @Override
put(K key, V value)104     public V put(K key, V value) {
105       assertTrue(Thread.holdsLock(mutex));
106       return super.put(key, value);
107     }
108 
109     @Override
putAll(Map<? extends K, ? extends V> map)110     public void putAll(Map<? extends K, ? extends V> map) {
111       assertTrue(Thread.holdsLock(mutex));
112       super.putAll(map);
113     }
114 
115     @Override
keySet()116     public Set<K> keySet() {
117       assertTrue(Thread.holdsLock(mutex));
118       return super.keySet();
119     }
120 
121     @Override
values()122     public Collection<V> values() {
123       assertTrue(Thread.holdsLock(mutex));
124       return super.values();
125     }
126 
127     @Override
entrySet()128     public Set<Entry<K, V>> entrySet() {
129       assertTrue(Thread.holdsLock(mutex));
130       return super.entrySet();
131     }
132 
133     @Override
equals(Object obj)134     public boolean equals(Object obj) {
135       assertTrue(Thread.holdsLock(mutex));
136       return super.equals(obj);
137     }
138 
139     @Override
hashCode()140     public int hashCode() {
141       assertTrue(Thread.holdsLock(mutex));
142       return super.hashCode();
143     }
144 
145     @Override
toString()146     public String toString() {
147       assertTrue(Thread.holdsLock(mutex));
148       return super.toString();
149     }
150 
151     private static final long serialVersionUID = 0;
152   }
153 
154   /*
155    * This is somewhat of a weak test; we verify that all of the methods are
156    * correct, but not that they're actually forwarding correctly. We also rely
157    * on the other tests (e.g., SynchronizedSetTest) to verify that the
158    * collection views are synchronized correctly.
159    */
160 
testSize()161   public void testSize() {
162     create().size();
163   }
164 
testIsEmpty()165   public void testIsEmpty() {
166     create().isEmpty();
167   }
168 
testRemove()169   public void testRemove() {
170     create().remove(null);
171   }
172 
testClear()173   public void testClear() {
174     create().clear();
175   }
176 
testContainsKey()177   public void testContainsKey() {
178     boolean unused = create().containsKey(null);
179   }
180 
testContainsValue()181   public void testContainsValue() {
182     boolean unused = create().containsValue(null);
183   }
184 
testGet()185   public void testGet() {
186     create().get(null);
187   }
188 
testPut()189   public void testPut() {
190     create().put(null, null);
191   }
192 
testPutAll()193   public void testPutAll() {
194     create().putAll(new HashMap<String, Integer>());
195   }
196 
testKeySet()197   public void testKeySet() {
198     Map<String, Integer> map = create();
199     Set<String> keySet = map.keySet();
200     assertTrue(keySet instanceof SynchronizedSet);
201     assertSame(mutex, ((SynchronizedSet<?>) keySet).mutex);
202   }
203 
testValues()204   public void testValues() {
205     Map<String, Integer> map = create();
206     Collection<Integer> values = map.values();
207     assertTrue(values instanceof SynchronizedCollection);
208     assertSame(mutex, ((SynchronizedCollection<?>) values).mutex);
209   }
210 
testEntrySet()211   public void testEntrySet() {
212     Map<String, Integer> map = create();
213     Set<Entry<String, Integer>> entrySet = map.entrySet();
214     assertTrue(entrySet instanceof SynchronizedSet);
215     assertSame(mutex, ((SynchronizedSet<?>) entrySet).mutex);
216   }
217 
testEquals()218   public void testEquals() {
219     create().equals(new HashMap<String, Integer>());
220   }
221 
testHashCode()222   public void testHashCode() {
223     create().hashCode();
224   }
225 
testToString()226   public void testToString() {
227     create().toString();
228   }
229 
testSerialization()230   public void testSerialization() {
231     SerializableTester.reserializeAndAssert(create());
232   }
233 }
234