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