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