guava
88 строк · 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.
40public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
41@CollectionFeature.Require(SUPPORTS_REMOVE)
42public void testClear() {
43collection.clear();
44assertTrue("After clear(), a collection should be empty.", collection.isEmpty());
45assertEquals(0, collection.size());
46assertFalse(collection.iterator().hasNext());
47}
48
49@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
50@CollectionSize.Require(absent = ZERO)
51public void testClear_unsupported() {
52try {
53collection.clear();
54fail(
55"clear() should throw UnsupportedOperation if a collection does "
56+ "not support it and is not empty.");
57} catch (UnsupportedOperationException expected) {
58}
59expectUnchanged();
60}
61
62@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
63@CollectionSize.Require(ZERO)
64public void testClear_unsupportedByEmptyCollection() {
65try {
66collection.clear();
67} catch (UnsupportedOperationException tolerated) {
68}
69expectUnchanged();
70}
71
72@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
73@CollectionSize.Require(SEVERAL)
74public void testClearConcurrentWithIteration() {
75try {
76Iterator<E> iterator = collection.iterator();
77collection.clear();
78iterator.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*/
83fail("Expected ConcurrentModificationException");
84} catch (ConcurrentModificationException expected) {
85// success
86}
87}
88}
89