• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 import java.util.ConcurrentModificationException;
30 import 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  * @author George van den Driessche
38  */
39 @GwtCompatible
40 public class CollectionClearTester<E> extends AbstractCollectionTester<E> {
41   @CollectionFeature.Require(SUPPORTS_REMOVE)
testClear()42   public void testClear() {
43     collection.clear();
44     assertTrue("After clear(), a collection should be empty.",
45         collection.isEmpty());
46   }
47 
48   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
49   @CollectionSize.Require(absent = ZERO)
testClear_unsupported()50   public void testClear_unsupported() {
51     try {
52       collection.clear();
53       fail("clear() should throw UnsupportedOperation if a collection does "
54           + "not support it and is not empty.");
55     } catch (UnsupportedOperationException expected) {
56     }
57     expectUnchanged();
58   }
59 
60   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
61   @CollectionSize.Require(ZERO)
testClear_unsupportedByEmptyCollection()62   public void testClear_unsupportedByEmptyCollection() {
63     try {
64       collection.clear();
65     } catch (UnsupportedOperationException tolerated) {
66     }
67     expectUnchanged();
68   }
69 
70   @CollectionFeature.Require({SUPPORTS_REMOVE,
71       FAILS_FAST_ON_CONCURRENT_MODIFICATION})
72   @CollectionSize.Require(SEVERAL)
testClearConcurrentWithIteration()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 }
88