Java
89 строк · 3.3 Кб
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.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
20import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
21import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
22import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23
24import com.google.common.annotations.GwtCompatible;
25import com.google.common.collect.testing.AbstractCollectionTester;
26import com.google.common.collect.testing.features.CollectionFeature;
27import com.google.common.collect.testing.features.CollectionSize;
28import java.util.ConcurrentModificationException;
29import java.util.Iterator;
30import 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@SuppressWarnings("JUnit4ClassUsedInJUnit3")
41public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
42@CollectionFeature.Require(SUPPORTS_REMOVE)
43public void testClear() {
44collection.clear();
45assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
46assertEquals(0, collection.size());
47assertFalse(collection.iterator().hasNext());
48}
49
50@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
51@CollectionSize.Require(absent = ZERO)
52public void testClear_unsupported() {
53try {
54collection.clear();
55fail(
56"clear() should throw UnsupportedOperation if a collection does "
57+ "not support it and is not empty.");
58} catch (UnsupportedOperationException expected) {
59}
60expectUnchanged();
61}
62
63@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
64@CollectionSize.Require(ZERO)
65public void testClear_unsupportedByEmptyCollection() {
66try {
67collection.clear();
68} catch (UnsupportedOperationException tolerated) {
69}
70expectUnchanged();
71}
72
73@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
74@CollectionSize.Require(SEVERAL)
75public void testClearConcurrentWithIteration() {
76try {
77Iterator<E> iterator = collection.iterator();
78collection.clear();
79iterator.next();
80/*
81* We prefer for iterators to fail immediately on hasNext, but ArrayList
82* and LinkedList will notably return true on hasNext here!
83*/
84fail("Expected ConcurrentModificationException");
85} catch (ConcurrentModificationException expected) {
86// success
87}
88}
89}
90