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