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 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 com.google.errorprone.annotations.CanIgnoreReturnValue;
030import java.util.Map.Entry;
031import java.util.concurrent.ConcurrentMap;
032import org.jspecify.annotations.NullMarked;
033import org.junit.Ignore;
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@Ignore("test runners must not instantiate and run this directly, only via suites we build")
044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045@SuppressWarnings("JUnit4ClassUsedInJUnit3")
046@NullMarked
047public class ConcurrentMapPutIfAbsentTester<K, V> extends AbstractMapTester<K, V> {
048  @Override
049  protected ConcurrentMap<K, V> getMap() {
050    return (ConcurrentMap<K, V>) super.getMap();
051  }
052
053  @MapFeature.Require(SUPPORTS_PUT)
054  public void testPutIfAbsent_supportedAbsent() {
055    assertNull("putIfAbsent(notPresent, value) should return null", putIfAbsent(e3()));
056    expectAdded(e3());
057  }
058
059  @MapFeature.Require(SUPPORTS_PUT)
060  @CollectionSize.Require(absent = ZERO)
061  public void testPutIfAbsent_supportedPresent() {
062    assertEquals(
063        "putIfAbsent(present, value) should return existing value",
064        v0(),
065        getMap().putIfAbsent(k0(), v3()));
066    expectUnchanged();
067  }
068
069  @MapFeature.Require(absent = SUPPORTS_PUT)
070  public void testPutIfAbsent_unsupportedAbsent() {
071    assertThrows(UnsupportedOperationException.class, () -> putIfAbsent(e3()));
072    expectUnchanged();
073    expectMissing(e3());
074  }
075
076  @MapFeature.Require(absent = SUPPORTS_PUT)
077  @CollectionSize.Require(absent = ZERO)
078  public void testPutIfAbsent_unsupportedPresentExistingValue() {
079    try {
080      assertEquals(
081          "putIfAbsent(present, existingValue) should return present or throw",
082          v0(),
083          putIfAbsent(e0()));
084    } catch (UnsupportedOperationException tolerated) {
085    }
086    expectUnchanged();
087  }
088
089  @MapFeature.Require(absent = SUPPORTS_PUT)
090  @CollectionSize.Require(absent = ZERO)
091  public void testPutIfAbsent_unsupportedPresentDifferentValue() {
092    try {
093      getMap().putIfAbsent(k0(), v3());
094    } catch (UnsupportedOperationException tolerated) {
095    }
096    expectUnchanged();
097  }
098
099  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
100  public void testPutIfAbsent_nullKeyUnsupported() {
101    assertThrows(NullPointerException.class, () -> getMap().putIfAbsent(null, v3()));
102    expectUnchanged();
103    expectNullKeyMissingWhenNullKeysUnsupported(
104        "Should not contain null key after unsupported putIfAbsent(null, value)");
105  }
106
107  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
108  public void testPutIfAbsent_nullValueUnsupported() {
109    assertThrows(NullPointerException.class, () -> getMap().putIfAbsent(k3(), null));
110    expectUnchanged();
111    expectNullValueMissingWhenNullValuesUnsupported(
112        "Should not contain null value after unsupported put(key, null)");
113  }
114
115  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
116  @CollectionSize.Require(absent = ZERO)
117  public void testPutIfAbsent_putWithNullValueUnsupported() {
118    try {
119      getMap().putIfAbsent(k0(), null);
120    } catch (NullPointerException tolerated) {
121    }
122    expectUnchanged();
123    expectNullValueMissingWhenNullValuesUnsupported(
124        "Should not contain null after unsupported putIfAbsent(present, null)");
125  }
126
127  @CanIgnoreReturnValue
128  private V putIfAbsent(Entry<K, V> entry) {
129    return getMap().putIfAbsent(entry.getKey(), entry.getValue());
130  }
131}