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@GwtCompatible
044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
046  @MapFeature.Require(SUPPORTS_REMOVE)
047  @CollectionSize.Require(absent = ZERO)
048  public void testRemove_present() {
049    int initialSize = getMap().size();
050    assertEquals("remove(present) should return the associated value", v0(), getMap().remove(k0()));
051    assertEquals(
052        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
053    expectMissing(e0());
054  }
055
056  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
057  @CollectionSize.Require(SEVERAL)
058  public void testRemovePresentConcurrentWithEntrySetIteration() {
059    try {
060      Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
061      getMap().remove(k0());
062      iterator.next();
063      fail("Expected ConcurrentModificationException");
064    } catch (ConcurrentModificationException expected) {
065      // success
066    }
067  }
068
069  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
070  @CollectionSize.Require(SEVERAL)
071  public void testRemovePresentConcurrentWithKeySetIteration() {
072    try {
073      Iterator<K> iterator = getMap().keySet().iterator();
074      getMap().remove(k0());
075      iterator.next();
076      fail("Expected ConcurrentModificationException");
077    } catch (ConcurrentModificationException expected) {
078      // success
079    }
080  }
081
082  @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE})
083  @CollectionSize.Require(SEVERAL)
084  public void testRemovePresentConcurrentWithValuesIteration() {
085    try {
086      Iterator<V> iterator = getMap().values().iterator();
087      getMap().remove(k0());
088      iterator.next();
089      fail("Expected ConcurrentModificationException");
090    } catch (ConcurrentModificationException expected) {
091      // success
092    }
093  }
094
095  @MapFeature.Require(SUPPORTS_REMOVE)
096  public void testRemove_notPresent() {
097    assertNull("remove(notPresent) should return null", getMap().remove(k3()));
098    expectUnchanged();
099  }
100
101  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
102  @CollectionSize.Require(absent = ZERO)
103  public void testRemove_nullPresent() {
104    initMapWithNullKey();
105
106    int initialSize = getMap().size();
107    assertEquals(
108        "remove(null) should return the associated value",
109        getValueForNullKey(),
110        getMap().remove(null));
111    assertEquals(
112        "remove(present) should decrease a map's size by one.", initialSize - 1, getMap().size());
113    expectMissing(entry(null, getValueForNullKey()));
114  }
115
116  @MapFeature.Require(absent = SUPPORTS_REMOVE)
117  @CollectionSize.Require(absent = ZERO)
118  public void testRemove_unsupported() {
119    try {
120      getMap().remove(k0());
121      fail("remove(present) should throw UnsupportedOperationException");
122    } catch (UnsupportedOperationException expected) {
123    }
124    expectUnchanged();
125    assertEquals("remove(present) should not remove the element", v0(), get(k0()));
126  }
127
128  @MapFeature.Require(absent = SUPPORTS_REMOVE)
129  public void testRemove_unsupportedNotPresent() {
130    try {
131      assertNull(
132          "remove(notPresent) should return null or throw UnsupportedOperationException",
133          getMap().remove(k3()));
134    } catch (UnsupportedOperationException tolerated) {
135    }
136    expectUnchanged();
137    expectMissing(e3());
138  }
139
140  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
141  public void testRemove_nullQueriesNotSupported() {
142    try {
143      assertNull(
144          "remove(null) should return null or throw NullPointerException", getMap().remove(null));
145    } catch (NullPointerException tolerated) {
146    }
147    expectUnchanged();
148  }
149
150  @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEY_QUERIES})
151  public void testRemove_nullSupportedMissing() {
152    assertNull("remove(null) should return null", getMap().remove(null));
153    expectUnchanged();
154  }
155
156  @MapFeature.Require(SUPPORTS_REMOVE)
157  public void testRemove_wrongType() {
158    try {
159      assertNull(getMap().remove(WrongType.VALUE));
160    } catch (ClassCastException tolerated) {
161    }
162    expectUnchanged();
163  }
164}