CollectionClearTester.java revision 7dd252788645e940eada959bdde927426e2531c9
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;
28
29import java.util.ConcurrentModificationException;
30import java.util.Iterator;
31
32/**
33 * A generic JUnit test which tests {@code clear()} operations on a collection.
34 * Can't be invoked directly; please see
35 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
36 *
37 * <p>This class is GWT compatible.
38 *
39 * @author George van den Driessche
40 */
41@GwtCompatible
42public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
43  @CollectionFeature.Require(SUPPORTS_REMOVE)
44  public void testClear() {
45    collection.clear();
46    assertTrue("After clear(), a collection should be empty.",
47        collection.isEmpty());
48  }
49
50  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
51  @CollectionSize.Require(absent = ZERO)
52  public void testClear_unsupported() {
53    try {
54      collection.clear();
55      fail("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,
73      FAILS_FAST_ON_CONCURRENT_MODIFICATION})
74  @CollectionSize.Require(SEVERAL)
75  public void testClearConcurrentWithIteration() {
76    try {
77      Iterator<E> iterator = collection.iterator();
78      collection.clear();
79      iterator.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       */
84      fail("Expected ConcurrentModificationException");
85    } catch (ConcurrentModificationException expected) {
86      // success
87    }
88  }
89}
90