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 com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.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;
030import org.junit.Ignore;
031
032/**
033 * Tester for {@link Map#remove(Object, Object)}. Can't be invoked directly; please see {@link
034 * com.google.common.collect.testing.MapTestSuiteBuilder}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039@Ignore("test runners must not instantiate and run this directly, only via suites we build")
040// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042@IgnoreJRERequirement // We opt into library desugaring for our tests.
043public class MapRemoveEntryTester<K, V> extends AbstractMapTester<K, V> {
044  @MapFeature.Require(SUPPORTS_REMOVE)
045  @CollectionSize.Require(absent = ZERO)
046  public void testRemove_supportedPresent() {
047    assertTrue(getMap().remove(k0(), v0()));
048    expectMissing(e0());
049  }
050
051  @MapFeature.Require(SUPPORTS_REMOVE)
052  public void testRemove_supportedPresentKeyWrongValue() {
053    assertFalse(getMap().remove(k0(), v3()));
054    expectUnchanged();
055  }
056
057  @MapFeature.Require(SUPPORTS_REMOVE)
058  public void testRemove_supportedWrongKeyPresentValue() {
059    assertFalse(getMap().remove(k3(), v0()));
060    expectUnchanged();
061  }
062
063  @MapFeature.Require(SUPPORTS_REMOVE)
064  public void testRemove_supportedAbsentKeyAbsentValue() {
065    assertFalse(getMap().remove(k3(), v3()));
066    expectUnchanged();
067  }
068
069  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
070  public void testRemove_nullKeyQueriesUnsupported() {
071    try {
072      assertFalse(getMap().remove(null, v3()));
073    } catch (NullPointerException tolerated) {
074      // since the operation would be a no-op, the exception is not required
075    }
076    expectUnchanged();
077  }
078
079  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES)
080  public void testRemove_nullValueQueriesUnsupported() {
081    try {
082      assertFalse(getMap().remove(k3(), null));
083    } catch (NullPointerException tolerated) {
084      // since the operation would be a no-op, the exception is not required
085    }
086    expectUnchanged();
087  }
088
089  @MapFeature.Require(absent = SUPPORTS_REMOVE)
090  @CollectionSize.Require(absent = ZERO)
091  public void testRemove_unsupportedPresent() {
092    assertThrows(UnsupportedOperationException.class, () -> getMap().remove(k0(), v0()));
093    expectUnchanged();
094  }
095
096  @MapFeature.Require(absent = SUPPORTS_REMOVE)
097  public void testRemove_unsupportedAbsent() {
098    try {
099      assertFalse(getMap().remove(k0(), v3()));
100    } catch (UnsupportedOperationException tolerated) {
101      // since the operation would be a no-op, the exception is not required
102    }
103    expectUnchanged();
104  }
105}