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_VALUES;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
024import static org.junit.Assert.assertThrows;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.AbstractMapTester;
028import com.google.common.collect.testing.features.CollectionSize;
029import com.google.common.collect.testing.features.MapFeature;
030import java.util.concurrent.ConcurrentMap;
031import org.jspecify.annotations.NullMarked;
032
033/**
034 * A generic JUnit test which tests {@code replace(K, V)} operations on a concurrent map. Can't be
035 * invoked directly; please see {@link
036 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
037 *
038 * @author Louis Wasserman
039 */
040@GwtCompatible
041@NullMarked
042public class ConcurrentMapReplaceTester<K, V> extends AbstractMapTester<K, V> {
043  @Override
044  protected ConcurrentMap<K, V> getMap() {
045    return (ConcurrentMap<K, V>) super.getMap();
046  }
047
048  @MapFeature.Require(SUPPORTS_PUT)
049  @CollectionSize.Require(absent = ZERO)
050  public void testReplace_supportedPresent() {
051    assertEquals(v0(), getMap().replace(k0(), v3()));
052    expectReplacement(entry(k0(), v3()));
053  }
054
055  @MapFeature.Require(SUPPORTS_PUT)
056  @CollectionSize.Require(absent = ZERO)
057  public void testReplace_supportedPresentNoChange() {
058    assertEquals(v0(), getMap().replace(k0(), v0()));
059    expectUnchanged();
060  }
061
062  @MapFeature.Require(SUPPORTS_PUT)
063  public void testReplace_supportedAbsent() {
064    assertNull(getMap().replace(k3(), v3()));
065    expectUnchanged();
066  }
067
068  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
069  @CollectionSize.Require(absent = ZERO)
070  public void testReplace_presentNullValueUnsupported() {
071    assertThrows(NullPointerException.class, () -> getMap().replace(k0(), null));
072    expectUnchanged();
073  }
074
075  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
076  public void testReplace_absentNullValueUnsupported() {
077    try {
078      getMap().replace(k3(), null);
079    } catch (NullPointerException tolerated) {
080      // permitted not to throw because it would be a no-op
081    }
082    expectUnchanged();
083  }
084
085  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
086  public void testReplace_absentNullKeyUnsupported() {
087    try {
088      getMap().replace(null, v3());
089    } catch (NullPointerException tolerated) {
090      // permitted not to throw because it would be a no-op
091    }
092    expectUnchanged();
093  }
094
095  @MapFeature.Require(absent = SUPPORTS_PUT)
096  @CollectionSize.Require(absent = ZERO)
097  public void testReplace_unsupportedPresent() {
098    assertThrows(UnsupportedOperationException.class, () -> getMap().replace(k0(), v3()));
099    expectUnchanged();
100  }
101}