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