Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/CollectionClearTester.java

raw
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.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
20 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
21 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.AbstractCollectionTester;
26 import com.google.common.collect.testing.features.CollectionFeature;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import java.util.ConcurrentModificationException;
29 import java.util.Iterator;
30 import org.junit.Ignore;
31
32 /**
33 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked
34 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
35 *
36 * @author George van den Driessche
37 */
38 @GwtCompatible
39 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
40 public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
41 @CollectionFeature.Require(SUPPORTS_REMOVE)
42 public void testClear() {
43 collection.clear();
44 assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
45 assertEquals(0, collection.size());
46 assertFalse(collection.iterator().hasNext());
47 }
48
49 @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
50 @CollectionSize.Require(absent = ZERO)
51 public void testClear_unsupported() {
52 try {
53 collection.clear();
54 fail(
55 "clear() should throw UnsupportedOperation if a collection does "
56 + "not support it and is not empty.");
57 } catch (UnsupportedOperationException expected) {
58 }
59 expectUnchanged();
60 }
61
62 @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
63 @CollectionSize.Require(ZERO)
64 public void testClear_unsupportedByEmptyCollection() {
65 try {
66 collection.clear();
67 } catch (UnsupportedOperationException tolerated) {
68 }
69 expectUnchanged();
70 }
71
72 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
73 @CollectionSize.Require(SEVERAL)
74 public void testClearConcurrentWithIteration() {
75 try {
76 Iterator<E> iterator = collection.iterator();
77 collection.clear();
78 iterator.next();
79 /*
80 * We prefer for iterators to fail immediately on hasNext, but ArrayList
81 * and LinkedList will notably return true on hasNext here!
82 */
83 fail("Expected ConcurrentModificationException");
84 } catch (ConcurrentModificationException expected) {
85 // success
86 }
87 }
88 }