File: SynchronizedNavigableMapTest.java

package info (click to toggle)
guava-libraries 31.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 40,360 kB
  • sloc: java: 367,214; xml: 2,492; sh: 34; makefile: 9; javascript: 9
file content (419 lines) | stat: -rw-r--r-- 12,311 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.collect.Synchronized.SynchronizedNavigableMap;
import com.google.common.collect.Synchronized.SynchronizedNavigableSet;
import com.google.common.collect.Synchronized.SynchronizedSortedMap;
import com.google.common.collect.testing.NavigableMapTestSuiteBuilder;
import com.google.common.collect.testing.SafeTreeMap;
import com.google.common.collect.testing.TestStringSortedMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.testing.SerializableTester;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.SortedMap;
import junit.framework.TestSuite;

/**
 * Tests for {@link Maps#synchronizedNavigableMap(NavigableMap)}.
 *
 * @author Louis Wasserman
 */
public class SynchronizedNavigableMapTest extends SynchronizedMapTest {
  @Override
  protected <K, V> NavigableMap<K, V> create() {
    @SuppressWarnings("unchecked")
    NavigableMap<K, V> innermost =
        new SafeTreeMap<>((Comparator<? super K>) Ordering.natural().nullsFirst());
    TestMap<K, V> inner = new TestMap<>(innermost, mutex);
    NavigableMap<K, V> outer = Synchronized.navigableMap(inner, mutex);
    return outer;
  }

  static class TestEntry<K, V> extends ForwardingMapEntry<K, V> implements Serializable {
    private final Entry<K, V> delegate;
    private final Object mutex;

    TestEntry(Entry<K, V> delegate, Object mutex) {
      this.delegate = delegate;
      this.mutex = mutex;
    }

    @Override
    protected Entry<K, V> delegate() {
      return delegate;
    }

    @Override
    public boolean equals(Object object) {
      assertTrue(Thread.holdsLock(mutex));
      return super.equals(object);
    }

    @Override
    public K getKey() {
      assertTrue(Thread.holdsLock(mutex));
      return super.getKey();
    }

    @Override
    public V getValue() {
      assertTrue(Thread.holdsLock(mutex));
      return super.getValue();
    }

    @Override
    public int hashCode() {
      assertTrue(Thread.holdsLock(mutex));
      return super.hashCode();
    }

    @Override
    public V setValue(V value) {
      assertTrue(Thread.holdsLock(mutex));
      return super.setValue(value);
    }

    private static final long serialVersionUID = 0;
  }

  static class TestMap<K, V> extends SynchronizedMapTest.TestMap<K, V>
      implements NavigableMap<K, V> {

    public TestMap(NavigableMap<K, V> delegate, Object mutex) {
      super(delegate, mutex);
    }

    @Override
    protected NavigableMap<K, V> delegate() {
      return (NavigableMap<K, V>) super.delegate();
    }

    @Override
    public Entry<K, V> ceilingEntry(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().ceilingEntry(key);
    }

    @Override
    public K ceilingKey(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().ceilingKey(key);
    }

    @Override
    public NavigableSet<K> descendingKeySet() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().descendingKeySet();
    }

    @Override
    public NavigableMap<K, V> descendingMap() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().descendingMap();
    }

    @Override
    public Entry<K, V> firstEntry() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().firstEntry();
    }

    @Override
    public Entry<K, V> floorEntry(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().floorEntry(key);
    }

    @Override
    public K floorKey(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().floorKey(key);
    }

    @Override
    public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().headMap(toKey, inclusive);
    }

    @Override
    public SortedMap<K, V> headMap(K toKey) {
      return headMap(toKey, false);
    }

    @Override
    public Entry<K, V> higherEntry(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().higherEntry(key);
    }

    @Override
    public K higherKey(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().higherKey(key);
    }

    @Override
    public Entry<K, V> lastEntry() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().lastEntry();
    }

    @Override
    public Entry<K, V> lowerEntry(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().lowerEntry(key);
    }

    @Override
    public K lowerKey(K key) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().lowerKey(key);
    }

    @Override
    public NavigableSet<K> navigableKeySet() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().navigableKeySet();
    }

    @Override
    public Entry<K, V> pollFirstEntry() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().pollFirstEntry();
    }

    @Override
    public Entry<K, V> pollLastEntry() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().pollLastEntry();
    }

    @Override
    public NavigableMap<K, V> subMap(
        K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().subMap(fromKey, fromInclusive, toKey, toInclusive);
    }

    @Override
    public SortedMap<K, V> subMap(K fromKey, K toKey) {
      return delegate().subMap(fromKey, true, toKey, false);
    }

    @Override
    public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().tailMap(fromKey, inclusive);
    }

    @Override
    public SortedMap<K, V> tailMap(K fromKey) {
      return tailMap(fromKey, true);
    }

    @Override
    public Comparator<? super K> comparator() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().comparator();
    }

    @Override
    public K firstKey() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().firstKey();
    }

    @Override
    public K lastKey() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().lastKey();
    }

    private static final long serialVersionUID = 0;
  }

  public static TestSuite suite() {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(SynchronizedNavigableMapTest.class);
    suite.addTest(
        NavigableMapTestSuiteBuilder.using(
                new TestStringSortedMapGenerator() {
                  private final Object mutex = new Integer(1);

                  @Override
                  protected SortedMap<String, String> create(Entry<String, String>[] entries) {
                    NavigableMap<String, String> innermost = new SafeTreeMap<>();
                    for (Entry<String, String> entry : entries) {
                      innermost.put(entry.getKey(), entry.getValue());
                    }
                    TestMap<String, String> inner = new TestMap<>(innermost, mutex);
                    NavigableMap<String, String> outer = Synchronized.navigableMap(inner, mutex);
                    return outer;
                  }
                })
            .named("Maps.synchronizedNavigableMap[SafeTreeMap]")
            .withFeatures(
                CollectionSize.ANY,
                CollectionFeature.KNOWN_ORDER,
                MapFeature.GENERAL_PURPOSE,
                MapFeature.ALLOWS_NULL_VALUES,
                CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
            .createTestSuite());

    return suite;
  }

  public void testComparator() {
    create().comparator();
  }

  public void testCeilingEntry() {
    create().ceilingEntry("a");
  }

  public void testCeilingKey() {
    create().ceilingKey("a");
  }

  public void testDescendingKeySet() {
    NavigableMap<String, Integer> map = create();
    NavigableSet<String> descendingKeySet = map.descendingKeySet();
    assertTrue(descendingKeySet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) descendingKeySet).mutex);
  }

  public void testDescendingMap() {
    NavigableMap<String, Integer> map = create();
    NavigableMap<String, Integer> descendingMap = map.descendingMap();
    assertTrue(descendingMap instanceof SynchronizedNavigableMap);
    assertSame(mutex, ((SynchronizedNavigableMap<String, Integer>) descendingMap).mutex);
  }

  public void testFirstEntry() {
    create().firstEntry();
  }

  public void testFirstKey() {
    NavigableMap<String, Integer> map = create();
    map.put("a", 1);
    map.firstKey();
  }

  public void testFloorEntry() {
    create().floorEntry("a");
  }

  public void testFloorKey() {
    create().floorKey("a");
  }

  public void testHeadMap_K() {
    NavigableMap<String, Integer> map = create();
    SortedMap<String, Integer> headMap = map.headMap("a");
    assertTrue(headMap instanceof SynchronizedSortedMap);
    assertSame(mutex, ((SynchronizedSortedMap<String, Integer>) headMap).mutex);
  }

  public void testHeadMap_K_B() {
    NavigableMap<String, Integer> map = create();
    NavigableMap<String, Integer> headMap = map.headMap("a", true);
    assertTrue(headMap instanceof SynchronizedNavigableMap);
    assertSame(mutex, ((SynchronizedNavigableMap<String, Integer>) headMap).mutex);
  }

  public void testHigherEntry() {
    create().higherEntry("a");
  }

  public void testHigherKey() {
    create().higherKey("a");
  }

  public void testLastEntry() {
    create().lastEntry();
  }

  public void testLastKey() {
    NavigableMap<String, Integer> map = create();
    map.put("a", 1);
    map.lastKey();
  }

  public void testLowerEntry() {
    create().lowerEntry("a");
  }

  public void testLowerKey() {
    create().lowerKey("a");
  }

  public void testNavigableKeySet() {
    NavigableMap<String, Integer> map = create();
    NavigableSet<String> navigableKeySet = map.navigableKeySet();
    assertTrue(navigableKeySet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) navigableKeySet).mutex);
  }

  public void testPollFirstEntry() {
    create().pollFirstEntry();
  }

  public void testPollLastEntry() {
    create().pollLastEntry();
  }

  public void testSubMap_K_K() {
    NavigableMap<String, Integer> map = create();
    SortedMap<String, Integer> subMap = map.subMap("a", "b");
    assertTrue(subMap instanceof SynchronizedSortedMap);
    assertSame(mutex, ((SynchronizedSortedMap<String, Integer>) subMap).mutex);
  }

  public void testSubMap_K_B_K_B() {
    NavigableMap<String, Integer> map = create();
    NavigableMap<String, Integer> subMap = map.subMap("a", true, "b", false);
    assertTrue(subMap instanceof SynchronizedNavigableMap);
    assertSame(mutex, ((SynchronizedNavigableMap<String, Integer>) subMap).mutex);
  }

  public void testTailMap_K() {
    NavigableMap<String, Integer> map = create();
    SortedMap<String, Integer> subMap = map.tailMap("a");
    assertTrue(subMap instanceof SynchronizedSortedMap);
    assertSame(mutex, ((SynchronizedSortedMap<String, Integer>) subMap).mutex);
  }

  public void testTailMap_K_B() {
    NavigableMap<String, Integer> map = create();
    NavigableMap<String, Integer> subMap = map.tailMap("a", true);
    assertTrue(subMap instanceof SynchronizedNavigableMap);
    assertSame(mutex, ((SynchronizedNavigableMap<String, Integer>) subMap).mutex);
  }

  @Override
  public void testSerialization() {
    SerializableTester.reserializeAndAssert(create());
  }
}