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