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; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.collect.testing.AbstractMapTester; 026import com.google.common.collect.testing.features.CollectionSize; 027import com.google.common.collect.testing.features.MapFeature; 028import java.util.ConcurrentModificationException; 029import java.util.Iterator; 030import java.util.Map.Entry; 031import org.junit.Ignore; 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 041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 042@SuppressWarnings("JUnit4ClassUsedInJUnit3") 043public class MapClearTester<K, V> extends AbstractMapTester<K, V> { 044 @MapFeature.Require(SUPPORTS_REMOVE) 045 public void testClear() { 046 getMap().clear(); 047 assertTrue("After clear(), a map should be empty.", getMap().isEmpty()); 048 assertEquals(0, getMap().size()); 049 assertFalse(getMap().entrySet().iterator().hasNext()); 050 } 051 052 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 053 @CollectionSize.Require(SEVERAL) 054 public void testClearConcurrentWithEntrySetIteration() { 055 try { 056 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 057 getMap().clear(); 058 iterator.next(); 059 fail("Expected ConcurrentModificationException"); 060 } catch (ConcurrentModificationException expected) { 061 // success 062 } 063 } 064 065 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 066 @CollectionSize.Require(SEVERAL) 067 public void testClearConcurrentWithKeySetIteration() { 068 try { 069 Iterator<K> iterator = getMap().keySet().iterator(); 070 getMap().clear(); 071 iterator.next(); 072 fail("Expected ConcurrentModificationException"); 073 } catch (ConcurrentModificationException expected) { 074 // success 075 } 076 } 077 078 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 079 @CollectionSize.Require(SEVERAL) 080 public void testClearConcurrentWithValuesIteration() { 081 try { 082 Iterator<V> iterator = getMap().values().iterator(); 083 getMap().clear(); 084 iterator.next(); 085 fail("Expected ConcurrentModificationException"); 086 } catch (ConcurrentModificationException expected) { 087 // success 088 } 089 } 090 091 @MapFeature.Require(absent = SUPPORTS_REMOVE) 092 @CollectionSize.Require(absent = ZERO) 093 public void testClear_unsupported() { 094 try { 095 getMap().clear(); 096 fail( 097 "clear() should throw UnsupportedOperation if a map does " 098 + "not support it and is not empty."); 099 } catch (UnsupportedOperationException expected) { 100 } 101 expectUnchanged(); 102 } 103 104 @MapFeature.Require(absent = SUPPORTS_REMOVE) 105 @CollectionSize.Require(ZERO) 106 public void testClear_unsupportedByEmptyCollection() { 107 try { 108 getMap().clear(); 109 } catch (UnsupportedOperationException tolerated) { 110 } 111 expectUnchanged(); 112 } 113}