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