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.ALLOWS_NULL_KEYS;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
023import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
024import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.AbstractMapTester;
028import com.google.common.collect.testing.WrongType;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.MapFeature;
031import java.util.ConcurrentModificationException;
032import java.util.Iterator;
033import java.util.Map.Entry;
034import org.junit.Ignore;
035
036/**
037 * A generic JUnit test which tests {@code remove} operations on a map. Can't be invoked directly;
038 * please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
039 *
040 * @author George van den Driessche
041 * @author Chris Povirk
042 */
043@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
044@GwtCompatible
045@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
046public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
047  @MapFeature.Require(SUPPORTS_REMOVE)
048  @CollectionSize.Require(absent = ZERO)
049  public void testRemove_present() {
050    int initialSize = getMap().size();
051    assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0()));
052    assertEquals(
053        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
054    expectMissing(e0());
055  }
056
057  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
058  @CollectionSize.Require(SEVERAL)
059  public void testRemovePresentConcurrentWithEntrySetIteration() {
060    try {
061      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
062      getMap().remove(k0());
063      iterator.next();
064      fail("Expected ConcurrentModificationException");
065    } catch (ConcurrentModificationException expected) {
066      // success
067    }
068  }
069
070  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
071  @CollectionSize.Require(SEVERAL)
072  public void testRemovePresentConcurrentWithKeySetIteration() {
073    try {
074      Iterator<K> iterator = getMap().keySet().iterator();
075      getMap().remove(k0());
076      iterator.next();
077      fail("Expected ConcurrentModificationException");
078    } catch (ConcurrentModificationException expected) {
079      // success
080    }
081  }
082
083  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
084  @CollectionSize.Require(SEVERAL)
085  public void testRemovePresentConcurrentWithValuesIteration() {
086    try {
087      Iterator<V> iterator = getMap().values().iterator();
088      getMap().remove(k0());
089      iterator.next();
090      fail("Expected ConcurrentModificationException");
091    } catch (ConcurrentModificationException expected) {
092      // success
093    }
094  }
095
096  @MapFeature.Require(SUPPORTS_REMOVE)
097  public void testRemove_notPresent() {
098    assertNull("remove(notPresent) should return null", getMap().remove(k3()));
099    expectUnchanged();
100  }
101
102  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
103  @CollectionSize.Require(absent = ZERO)
104  public void testRemove_nullPresent() {
105    initMapWithNullKey();
106
107    int initialSize = getMap().size();
108    assertEquals(
109        "remove(null) should return the associated value",
110        getValueForNullKey(),
111        getMap().remove(null));
112    assertEquals(
113        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
114    expectMissing(entry(null, getValueForNullKey()));
115  }
116
117  @MapFeature.Require(absent = SUPPORTS_REMOVE)
118  @CollectionSize.Require(absent = ZERO)
119  public void testRemove_unsupported() {
120    try {
121      getMap().remove(k0());
122      fail("remove(present) should throw UnsupportedOperationException");
123    } catch (UnsupportedOperationException expected) {
124    }
125    expectUnchanged();
126    assertEquals("remove(present) should not remove the element", v0(), get(k0()));
127  }
128
129  @MapFeature.Require(absent = SUPPORTS_REMOVE)
130  public void testRemove_unsupportedNotPresent() {
131    try {
132      assertNull(
133          "remove(notPresent) should return null or throw UnsupportedOperationException",
134          getMap().remove(k3()));
135    } catch (UnsupportedOperationException tolerated) {
136    }
137    expectUnchanged();
138    expectMissing(e3());
139  }
140
141  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
142  public void testRemove_nullQueriesNotSupported() {
143    try {
144      assertNull(
145          "remove(null) should return null or throw NullPointerException", getMap().remove(null));
146    } catch (NullPointerException tolerated) {
147    }
148    expectUnchanged();
149  }
150
151  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
152  public void testRemove_nullSupportedMissing() {
153    assertNull("remove(null) should return null", getMap().remove(null));
154    expectUnchanged();
155  }
156
157  @MapFeature.Require(SUPPORTS_REMOVE)
158  public void testRemove_wrongType() {
159    try {
160      assertNull(getMap().remove(WrongType.VALUE));
161    } catch (ClassCastException tolerated) {
162    }
163    expectUnchanged();
164  }
165}