001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 020import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.testing.AbstractCollectionTester; 026import com.google.common.collect.testing.features.CollectionFeature; 027import com.google.common.collect.testing.features.CollectionSize; 028import java.util.ConcurrentModificationException; 029import java.util.Iterator; 030 031/** 032 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked 033 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 034 * 035 * @author George van den Driessche 036 */ 037@GwtCompatible 038public class CollectionClearTester<E> extends AbstractCollectionTester<E> { 039 @CollectionFeature.Require(SUPPORTS_REMOVE) 040 public void testClear() { 041 collection.clear(); 042 assertTrue("After clear(), a collection should be empty.", collection.isEmpty()); 043 assertEquals(0, collection.size()); 044 assertFalse(collection.iterator().hasNext()); 045 } 046 047 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 048 @CollectionSize.Require(absent = ZERO) 049 public void testClear_unsupported() { 050 try { 051 collection.clear(); 052 fail( 053 "clear() should throw UnsupportedOperation if a collection does " 054 + "not support it and is not empty."); 055 } catch (UnsupportedOperationException expected) { 056 } 057 expectUnchanged(); 058 } 059 060 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 061 @CollectionSize.Require(ZERO) 062 public void testClear_unsupportedByEmptyCollection() { 063 try { 064 collection.clear(); 065 } catch (UnsupportedOperationException tolerated) { 066 } 067 expectUnchanged(); 068 } 069 070 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 071 @CollectionSize.Require(SEVERAL) 072 public void testClearConcurrentWithIteration() { 073 try { 074 Iterator<E> iterator = collection.iterator(); 075 collection.clear(); 076 iterator.next(); 077 /* 078 * We prefer for iterators to fail immediately on hasNext, but ArrayList 079 * and LinkedList will notably return true on hasNext here! 080 */ 081 fail("Expected ConcurrentModificationException"); 082 } catch (ConcurrentModificationException expected) { 083 // success 084 } 085 } 086}