MapClearTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/*
2 * Copyright (C) 2008 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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
23
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.collect.testing.AbstractMapTester;
26import com.google.common.collect.testing.features.CollectionSize;
27import com.google.common.collect.testing.features.MapFeature;
28
29import java.util.ConcurrentModificationException;
30import java.util.Iterator;
31import java.util.Map.Entry;
32
33/**
34 * A generic JUnit test which tests {@code clear()} operations on a map.
35 * Can't be invoked directly; please see
36 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
37 *
38 * <p>This class is GWT compatible.
39 *
40 * @author George van den Driessche
41 * @author Chris Povirk
42 */
43@GwtCompatible
44public class MapClearTester<K, V> extends AbstractMapTester<K, V> {
45  @MapFeature.Require(SUPPORTS_REMOVE)
46  public void testClear() {
47    getMap().clear();
48    assertTrue("After clear(), a map should be empty.",
49        getMap().isEmpty());
50  }
51
52  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
53      SUPPORTS_REMOVE})
54  @CollectionSize.Require(SEVERAL)
55  public void testClearConcurrentWithEntrySetIteration() {
56    try {
57      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
58      getMap().clear();
59      iterator.next();
60      fail("Expected ConcurrentModificationException");
61    } catch (ConcurrentModificationException expected) {
62      // success
63    }
64  }
65
66  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
67      SUPPORTS_REMOVE})
68  @CollectionSize.Require(SEVERAL)
69  public void testClearConcurrentWithKeySetIteration() {
70    try {
71      Iterator<K> iterator = getMap().keySet().iterator();
72      getMap().clear();
73      iterator.next();
74      fail("Expected ConcurrentModificationException");
75    } catch (ConcurrentModificationException expected) {
76      // success
77    }
78  }
79
80  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
81      SUPPORTS_REMOVE})
82  @CollectionSize.Require(SEVERAL)
83  public void testClearConcurrentWithValuesIteration() {
84    try {
85      Iterator<V> iterator = getMap().values().iterator();
86      getMap().clear();
87      iterator.next();
88      fail("Expected ConcurrentModificationException");
89    } catch (ConcurrentModificationException expected) {
90      // success
91    }
92  }
93
94  @MapFeature.Require(absent = SUPPORTS_REMOVE)
95  @CollectionSize.Require(absent = ZERO)
96  public void testClear_unsupported() {
97    try {
98      getMap().clear();
99      fail("clear() should throw UnsupportedOperation if a map does "
100          + "not support it and is not empty.");
101    } catch (UnsupportedOperationException expected) {
102    }
103    expectUnchanged();
104  }
105
106  @MapFeature.Require(absent = SUPPORTS_REMOVE)
107  @CollectionSize.Require(ZERO)
108  public void testClear_unsupportedByEmptyCollection() {
109    try {
110      getMap().clear();
111    } catch (UnsupportedOperationException tolerated) {
112    }
113    expectUnchanged();
114  }
115}
116