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_VALUES;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
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 java.util.concurrent.ConcurrentMap;
030import org.jspecify.annotations.NullMarked;
031import org.junit.Ignore;
032
033/**
034 * A generic JUnit test which tests {@code replace(K, V, V)} operations on a concurrent map. Can't
035 * be invoked directly; please see {@link
036 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
037 *
038 * @author Louis Wasserman
039 */
040@GwtCompatible
041@Ignore("test runners must not instantiate and run this directly, only via suites we build")
042// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044@NullMarked
045public class ConcurrentMapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
046  @Override
047  protected ConcurrentMap<K, V> getMap() {
048    return (ConcurrentMap<K, V>) super.getMap();
049  }
050
051  @MapFeature.Require(SUPPORTS_PUT)
052  @CollectionSize.Require(absent = ZERO)
053  public void testReplaceEntry_supportedPresent() {
054    assertTrue(getMap().replace(k0(), v0(), v3()));
055    expectReplacement(entry(k0(), v3()));
056  }
057
058  @MapFeature.Require(SUPPORTS_PUT)
059  @CollectionSize.Require(absent = ZERO)
060  public void testReplaceEntry_supportedPresentUnchanged() {
061    assertTrue(getMap().replace(k0(), v0(), v0()));
062    expectUnchanged();
063  }
064
065  @MapFeature.Require(SUPPORTS_PUT)
066  @CollectionSize.Require(absent = ZERO)
067  public void testReplaceEntry_supportedWrongValue() {
068    assertFalse(getMap().replace(k0(), v3(), v4()));
069    expectUnchanged();
070  }
071
072  @MapFeature.Require(SUPPORTS_PUT)
073  public void testReplaceEntry_supportedAbsentKey() {
074    assertFalse(getMap().replace(k3(), v3(), v4()));
075    expectUnchanged();
076  }
077
078  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
079  @CollectionSize.Require(absent = ZERO)
080  public void testReplaceEntry_presentNullValueUnsupported() {
081    assertThrows(NullPointerException.class, () -> getMap().replace(k0(), v0(), null));
082    expectUnchanged();
083  }
084
085  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
086  @CollectionSize.Require(absent = ZERO)
087  public void testReplaceEntry_wrongValueNullValueUnsupported() {
088    try {
089      assertFalse(getMap().replace(k0(), v3(), null));
090    } catch (NullPointerException tolerated) {
091      // the operation would be a no-op, so exceptions are allowed but not required
092    }
093    expectUnchanged();
094  }
095
096  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
097  public void testReplaceEntry_absentKeyNullValueUnsupported() {
098    try {
099      assertFalse(getMap().replace(k3(), v3(), null));
100    } catch (NullPointerException tolerated) {
101      // the operation would be a no-op, so exceptions are allowed but not required
102    }
103    expectUnchanged();
104  }
105
106  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
107  public void testReplaceEntry_nullDifferentFromAbsent() {
108    assertFalse(getMap().replace(k3(), null, v3()));
109    expectUnchanged();
110  }
111
112  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
113  public void testReplaceEntry_expectNullUnsupported() {
114    try {
115      assertFalse(getMap().replace(k3(), null, v3()));
116    } catch (NullPointerException tolerated) {
117      // the operation would be a no-op, so exceptions are allowed but not required
118    }
119    expectUnchanged();
120  }
121
122  @MapFeature.Require(absent = SUPPORTS_PUT)
123  @CollectionSize.Require(absent = ZERO)
124  public void testReplaceEntry_unsupportedPresent() {
125    assertThrows(UnsupportedOperationException.class, () -> getMap().replace(k0(), v0(), v3()));
126    expectUnchanged();
127  }
128
129  @MapFeature.Require(absent = SUPPORTS_PUT)
130  @CollectionSize.Require(absent = ZERO)
131  public void testReplaceEntry_unsupportedWrongValue() {
132    try {
133      getMap().replace(k0(), v3(), v4());
134    } catch (UnsupportedOperationException tolerated) {
135      // the operation would be a no-op, so exceptions are allowed but not required
136    }
137    expectUnchanged();
138  }
139
140  @MapFeature.Require(absent = SUPPORTS_PUT)
141  public void testReplaceEntry_unsupportedAbsentKey() {
142    try {
143      getMap().replace(k3(), v3(), v4());
144    } catch (UnsupportedOperationException tolerated) {
145      // the operation would be a no-op, so exceptions are allowed but not required
146    }
147    expectUnchanged();
148  }
149}