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