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. 042public class MapClearTester<K, V> extends AbstractMapTester<K, V> { 043 @MapFeature.Require(SUPPORTS_REMOVE) 044 public void testClear() { 045 getMap().clear(); 046 assertTrue("After clear(), a map should be empty.", getMap().isEmpty()); 047 assertEquals(0, getMap().size()); 048 assertFalse(getMap().entrySet().iterator().hasNext()); 049 } 050 051 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 052 @CollectionSize.Require(SEVERAL) 053 public void testClearConcurrentWithEntrySetIteration() { 054 try { 055 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); 056 getMap().clear(); 057 iterator.next(); 058 fail("Expected ConcurrentModificationException"); 059 } catch (ConcurrentModificationException expected) { 060 // success 061 } 062 } 063 064 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 065 @CollectionSize.Require(SEVERAL) 066 public void testClearConcurrentWithKeySetIteration() { 067 try { 068 Iterator<K> iterator = getMap().keySet().iterator(); 069 getMap().clear(); 070 iterator.next(); 071 fail("Expected ConcurrentModificationException"); 072 } catch (ConcurrentModificationException expected) { 073 // success 074 } 075 } 076 077 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) 078 @CollectionSize.Require(SEVERAL) 079 public void testClearConcurrentWithValuesIteration() { 080 try { 081 Iterator<V> iterator = getMap().values().iterator(); 082 getMap().clear(); 083 iterator.next(); 084 fail("Expected ConcurrentModificationException"); 085 } catch (ConcurrentModificationException expected) { 086 // success 087 } 088 } 089 090 @MapFeature.Require(absent = SUPPORTS_REMOVE) 091 @CollectionSize.Require(absent = ZERO) 092 public void testClear_unsupported() { 093 try { 094 getMap().clear(); 095 fail( 096 "clear() should throw UnsupportedOperation if a map does " 097 + "not support it and is not empty."); 098 } catch (UnsupportedOperationException expected) { 099 } 100 expectUnchanged(); 101 } 102 103 @MapFeature.Require(absent = SUPPORTS_REMOVE) 104 @CollectionSize.Require(ZERO) 105 public void testClear_unsupportedByEmptyCollection() { 106 try { 107 getMap().clear(); 108 } catch (UnsupportedOperationException tolerated) { 109 } 110 expectUnchanged(); 111 } 112}