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