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; 023import static org.junit.Assert.assertThrows; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.AbstractCollectionTester; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import java.util.ConcurrentModificationException; 030import java.util.Iterator; 031 032/** 033 * A generic JUnit test which tests {@code clear()} operations on a collection. Can't be invoked 034 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 035 * 036 * @author George van den Driessche 037 */ 038@GwtCompatible 039public class CollectionClearTester<E> extends AbstractCollectionTester<E> { 040 @CollectionFeature.Require(SUPPORTS_REMOVE) 041 public void testClear() { 042 collection.clear(); 043 assertTrue("After clear(), a collection should be empty.", collection.isEmpty()); 044 assertEquals(0, collection.size()); 045 assertFalse(collection.iterator().hasNext()); 046 } 047 048 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 049 @CollectionSize.Require(absent = ZERO) 050 public void testClear_unsupported() { 051 assertThrows(UnsupportedOperationException.class, () -> collection.clear()); 052 expectUnchanged(); 053 } 054 055 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 056 @CollectionSize.Require(ZERO) 057 public void testClear_unsupportedByEmptyCollection() { 058 try { 059 collection.clear(); 060 } catch (UnsupportedOperationException tolerated) { 061 } 062 expectUnchanged(); 063 } 064 065 @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 066 @CollectionSize.Require(SEVERAL) 067 public void testClearConcurrentWithIteration() { 068 Iterator<E> iterator = collection.iterator(); 069 collection.clear(); 070 assertThrows(ConcurrentModificationException.class, iterator::next); 071 } 072}