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;
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.Map;
029import org.junit.Ignore;
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
038@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039@SuppressWarnings("JUnit4ClassUsedInJUnit3")
040public class MapRemoveEntryTester<K, V> extends AbstractMapTester<K, V> {
041  @MapFeature.Require(SUPPORTS_REMOVE)
042  @CollectionSize.Require(absent = ZERO)
043  public void testRemove_supportedPresent() {
044    assertTrue(getMap().remove(k0(), v0()));
045    expectMissing(e0());
046  }
047
048  @MapFeature.Require(SUPPORTS_REMOVE)
049  public void testRemove_supportedPresentKeyWrongValue() {
050    assertFalse(getMap().remove(k0(), v3()));
051    expectUnchanged();
052  }
053
054  @MapFeature.Require(SUPPORTS_REMOVE)
055  public void testRemove_supportedWrongKeyPresentValue() {
056    assertFalse(getMap().remove(k3(), v0()));
057    expectUnchanged();
058  }
059
060  @MapFeature.Require(SUPPORTS_REMOVE)
061  public void testRemove_supportedAbsentKeyAbsentValue() {
062    assertFalse(getMap().remove(k3(), v3()));
063    expectUnchanged();
064  }
065
066  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
067  public void testRemove_nullKeyQueriesUnsupported() {
068    try {
069      assertFalse(getMap().remove(null, v3()));
070    } catch (NullPointerException tolerated) {
071      // since the operation would be a no-op, the exception is not required
072    }
073    expectUnchanged();
074  }
075
076  @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES)
077  public void testRemove_nullValueQueriesUnsupported() {
078    try {
079      assertFalse(getMap().remove(k3(), null));
080    } catch (NullPointerException tolerated) {
081      // since the operation would be a no-op, the exception is not required
082    }
083    expectUnchanged();
084  }
085
086  @MapFeature.Require(absent = SUPPORTS_REMOVE)
087  @CollectionSize.Require(absent = ZERO)
088  public void testRemove_unsupportedPresent() {
089    try {
090      getMap().remove(k0(), v0());
091      fail("Expected UnsupportedOperationException");
092    } catch (UnsupportedOperationException expected) {
093    }
094    expectUnchanged();
095  }
096
097  @MapFeature.Require(absent = SUPPORTS_REMOVE)
098  public void testRemove_unsupportedAbsent() {
099    try {
100      assertFalse(getMap().remove(k0(), v3()));
101    } catch (UnsupportedOperationException tolerated) {
102      // since the operation would be a no-op, the exception is not required
103    }
104    expectUnchanged();
105  }
106}