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_KEYS;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
022import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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 com.google.errorprone.annotations.CanIgnoreReturnValue;
030import java.util.Map.Entry;
031import java.util.concurrent.ConcurrentMap;
032import org.jspecify.annotations.NullMarked;
033import org.jspecify.annotations.Nullable;
034
035/**
036 * A generic JUnit test which tests {@code putIfAbsent} operations on a concurrent map. Can't be
037 * invoked directly; please see {@link
038 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
039 *
040 * @author Louis Wasserman
041 */
042@GwtCompatible
043@NullMarked
044public class ConcurrentMapPutIfAbsentTester<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_PUT)
051  public void testPutIfAbsent_supportedAbsent() {
052    assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3()));
053    expectAdded(e3());
054  }
055
056  @MapFeature.Require(SUPPORTS_PUT)
057  @CollectionSize.Require(absent = ZERO)
058  public void testPutIfAbsent_supportedPresent() {
059    assertEquals(
060        "putIfAbsent(present, value) should return existing value",
061        v0(),
062        getMap().putIfAbsent(k0(), v3()));
063    expectUnchanged();
064  }
065
066  @MapFeature.Require(absent = SUPPORTS_PUT)
067  public void testPutIfAbsent_unsupportedAbsent() {
068    assertThrows(UnsupportedOperationException.class, () -> putIfAbsent(e3()));
069    expectUnchanged();
070    expectMissing(e3());
071  }
072
073  @MapFeature.Require(absent = SUPPORTS_PUT)
074  @CollectionSize.Require(absent = ZERO)
075  public void testPutIfAbsent_unsupportedPresentExistingValue() {
076    try {
077      assertEquals(
078          "putIfAbsent(present, existingValue) should return present or throw",
079          v0(),
080          putIfAbsent(e0()));
081    } catch (UnsupportedOperationException tolerated) {
082    }
083    expectUnchanged();
084  }
085
086  @MapFeature.Require(absent = SUPPORTS_PUT)
087  @CollectionSize.Require(absent = ZERO)
088  public void testPutIfAbsent_unsupportedPresentDifferentValue() {
089    try {
090      getMap().putIfAbsent(k0(), v3());
091    } catch (UnsupportedOperationException tolerated) {
092    }
093    expectUnchanged();
094  }
095
096  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
097  public void testPutIfAbsent_nullKeyUnsupported() {
098    assertThrows(NullPointerException.class, () -> getMap().putIfAbsent(null, v3()));
099    expectUnchanged();
100    expectNullKeyMissingWhenNullKeysUnsupported(
101        "Should not contain null key after unsupported putIfAbsent(null, value)");
102  }
103
104  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
105  public void testPutIfAbsent_nullValueUnsupported() {
106    assertThrows(NullPointerException.class, () -> getMap().putIfAbsent(k3(), null));
107    expectUnchanged();
108    expectNullValueMissingWhenNullValuesUnsupported(
109        "Should not contain null value after unsupported put(key, null)");
110  }
111
112  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
113  @CollectionSize.Require(absent = ZERO)
114  public void testPutIfAbsent_putWithNullValueUnsupported() {
115    try {
116      getMap().putIfAbsent(k0(), null);
117    } catch (NullPointerException tolerated) {
118    }
119    expectUnchanged();
120    expectNullValueMissingWhenNullValuesUnsupported(
121        "Should not contain null after unsupported putIfAbsent(present, null)");
122  }
123
124  @CanIgnoreReturnValue
125  private @Nullable V putIfAbsent(Entry<K, V> entry) {
126    return getMap().putIfAbsent(entry.getKey(), entry.getValue());
127  }
128}