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 com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.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; 032import org.junit.Ignore; 033 034/** 035 * A generic JUnit test which tests {@code clear()} operations on a map. Can't be invoked directly; 036 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 037 * 038 * @author George van den Driessche 039 * @author Chris Povirk 040 */ 041@GwtCompatible 042@Ignore("test runners must not instantiate and run this directly, only via suites we build") 043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044@SuppressWarnings("JUnit4ClassUsedInJUnit3") 045public class MapClearTester<K, V> extends AbstractMapTester<K, V> { 046 @MapFeature.Require(SUPPORTS_REMOVE) 047 public void testClear() { 048 getMap().clear(); 049 assertTrue("After clear(), a map should be empty.", getMap().isEmpty()); 050 assertEquals(0, getMap().size()); 051 assertFalse(getMap().entrySet().iterator().hasNext()); 052 } 053 054 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 055 @CollectionSize.Require(SEVERAL) 056 public void testClearConcurrentWithEntrySetIteration() { 057 assertThrows( 058 ConcurrentModificationException.class, 059 () -> { 060 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 061 getMap().clear(); 062 iterator.next(); 063 }); 064 } 065 066 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 067 @CollectionSize.Require(SEVERAL) 068 public void testClearConcurrentWithKeySetIteration() { 069 assertThrows( 070 ConcurrentModificationException.class, 071 () -> { 072 Iterator<K> iterator = getMap().keySet().iterator(); 073 getMap().clear(); 074 iterator.next(); 075 }); 076 } 077 078 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 079 @CollectionSize.Require(SEVERAL) 080 public void testClearConcurrentWithValuesIteration() { 081 assertThrows( 082 ConcurrentModificationException.class, 083 () -> { 084 Iterator<V> iterator = getMap().values().iterator(); 085 getMap().clear(); 086 iterator.next(); 087 }); 088 } 089 090 @MapFeature.Require(absent = SUPPORTS_REMOVE) 091 @CollectionSize.Require(absent = ZERO) 092 public void testClear_unsupported() { 093 assertThrows(UnsupportedOperationException.class, () -> getMap().clear()); 094 expectUnchanged(); 095 } 096 097 @MapFeature.Require(absent = SUPPORTS_REMOVE) 098 @CollectionSize.Require(ZERO) 099 public void testClear_unsupportedByEmptyCollection() { 100 try { 101 getMap().clear(); 102 } catch (UnsupportedOperationException tolerated) { 103 } 104 expectUnchanged(); 105 } 106}