001/*
002 * Copyright (C) 2015 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.ZERO;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
023import static org.junit.Assert.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.collect.testing.AbstractMapTester;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.MapFeature;
029import java.util.Map;
030
031/**
032 * Tester for {@link Map#remove(Object, Object)}. Can't be invoked directly; please see {@link
033 * com.google.common.collect.testing.MapTestSuiteBuilder}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038public class MapRemoveEntryTester<K, V> extends AbstractMapTester<K, V> {
039  @MapFeature.Require(SUPPORTS_REMOVE)
040  @CollectionSize.Require(absent = ZERO)
041  public void testRemove_supportedPresent() {
042    assertTrue(getMap().remove(k0(), v0()));
043    expectMissing(e0());
044  }
045
046  @MapFeature.Require(SUPPORTS_REMOVE)
047  public void testRemove_supportedPresentKeyWrongValue() {
048    assertFalse(getMap().remove(k0(), v3()));
049    expectUnchanged();
050  }
051
052  @MapFeature.Require(SUPPORTS_REMOVE)
053  public void testRemove_supportedWrongKeyPresentValue() {
054    assertFalse(getMap().remove(k3(), v0()));
055    expectUnchanged();
056  }
057
058  @MapFeature.Require(SUPPORTS_REMOVE)
059  public void testRemove_supportedAbsentKeyAbsentValue() {
060    assertFalse(getMap().remove(k3(), v3()));
061    expectUnchanged();
062  }
063
064  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
065  public void testRemove_nullKeyQueriesUnsupported() {
066    try {
067      assertFalse(getMap().remove(null, v3()));
068    } catch (NullPointerException tolerated) {
069      // since the operation would be a no-op, the exception is not required
070    }
071    expectUnchanged();
072  }
073
074  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES)
075  public void testRemove_nullValueQueriesUnsupported() {
076    try {
077      assertFalse(getMap().remove(k3(), null));
078    } catch (NullPointerException tolerated) {
079      // since the operation would be a no-op, the exception is not required
080    }
081    expectUnchanged();
082  }
083
084  @MapFeature.Require(absent = SUPPORTS_REMOVE)
085  @CollectionSize.Require(absent = ZERO)
086  public void testRemove_unsupportedPresent() {
087    assertThrows(UnsupportedOperationException.class, () -> getMap().remove(k0(), v0()));
088    expectUnchanged();
089  }
090
091  @MapFeature.Require(absent = SUPPORTS_REMOVE)
092  public void testRemove_unsupportedAbsent() {
093    try {
094      assertFalse(getMap().remove(k0(), v3()));
095    } catch (UnsupportedOperationException tolerated) {
096      // since the operation would be a no-op, the exception is not required
097    }
098    expectUnchanged();
099  }
100}