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