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.CollectionSize.SEVERAL; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE; 023import static org.junit.Assert.assertThrows; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.AbstractMapTester; 027import com.google.common.collect.testing.features.CollectionSize; 028import com.google.common.collect.testing.features.MapFeature; 029import java.util.ConcurrentModificationException; 030import java.util.Iterator; 031import java.util.Map.Entry; 032 033/** 034 * A generic JUnit test which tests {@code clear()} operations on a map. Can't be invoked directly; 035 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 036 * 037 * @author George van den Driessche 038 * @author Chris Povirk 039 */ 040@GwtCompatible 041public class MapClearTester<K, V> extends AbstractMapTester<K, V> { 042 @MapFeature.Require(SUPPORTS_REMOVE) 043 public void testClear() { 044 getMap().clear(); 045 assertTrue("After clear(), a map should be empty.", getMap().isEmpty()); 046 assertEquals(0, getMap().size()); 047 assertFalse(getMap().entrySet().iterator().hasNext()); 048 } 049 050 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 051 @CollectionSize.Require(SEVERAL) 052 public void testClearConcurrentWithEntrySetIteration() { 053 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 054 getMap().clear(); 055 assertThrows(ConcurrentModificationException.class, iterator::next); 056 } 057 058 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 059 @CollectionSize.Require(SEVERAL) 060 public void testClearConcurrentWithKeySetIteration() { 061 Iterator<K> iterator = getMap().keySet().iterator(); 062 getMap().clear(); 063 assertThrows(ConcurrentModificationException.class, iterator::next); 064 } 065 066 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 067 @CollectionSize.Require(SEVERAL) 068 public void testClearConcurrentWithValuesIteration() { 069 Iterator<V> iterator = getMap().values().iterator(); 070 getMap().clear(); 071 assertThrows(ConcurrentModificationException.class, iterator::next); 072 } 073 074 @MapFeature.Require(absent = SUPPORTS_REMOVE) 075 @CollectionSize.Require(absent = ZERO) 076 public void testClear_unsupported() { 077 assertThrows(UnsupportedOperationException.class, () -> getMap().clear()); 078 expectUnchanged(); 079 } 080 081 @MapFeature.Require(absent = SUPPORTS_REMOVE) 082 @CollectionSize.Require(ZERO) 083 public void testClear_unsupportedByEmptyCollection() { 084 try { 085 getMap().clear(); 086 } catch (UnsupportedOperationException tolerated) { 087 } 088 expectUnchanged(); 089 } 090}