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@ElementTypesAreNonnullByDefault
041public class ConcurrentMapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
042  @Override
043  protected ConcurrentMap<K, V> getMap() {
044    return (ConcurrentMap<K, V>) super.getMap();
045  }
046
047  @MapFeature.Require(SUPPORTS_PUT)
048  @CollectionSize.Require(absent = ZERO)
049  public void testReplaceEntry_supportedPresent() {
050    assertTrue(getMap().replace(k0(), v0(), v3()));
051    expectReplacement(entry(k0(), v3()));
052  }
053
054  @MapFeature.Require(SUPPORTS_PUT)
055  @CollectionSize.Require(absent = ZERO)
056  public void testReplaceEntry_supportedPresentUnchanged() {
057    assertTrue(getMap().replace(k0(), v0(), v0()));
058    expectUnchanged();
059  }
060
061  @MapFeature.Require(SUPPORTS_PUT)
062  @CollectionSize.Require(absent = ZERO)
063  public void testReplaceEntry_supportedWrongValue() {
064    assertFalse(getMap().replace(k0(), v3(), v4()));
065    expectUnchanged();
066  }
067
068  @MapFeature.Require(SUPPORTS_PUT)
069  public void testReplaceEntry_supportedAbsentKey() {
070    assertFalse(getMap().replace(k3(), v3(), v4()));
071    expectUnchanged();
072  }
073
074  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
075  @CollectionSize.Require(absent = ZERO)
076  public void testReplaceEntry_presentNullValueUnsupported() {
077    try {
078      getMap().replace(k0(), v0(), null);
079      fail("Expected NullPointerException");
080    } catch (NullPointerException expected) {
081    }
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    try {
126      getMap().replace(k0(), v0(), v3());
127      fail("Expected UnsupportedOperationException");
128    } catch (UnsupportedOperationException expected) {
129    }
130    expectUnchanged();
131  }
132
133  @MapFeature.Require(absent = SUPPORTS_PUT)
134  @CollectionSize.Require(absent = ZERO)
135  public void testReplaceEntry_unsupportedWrongValue() {
136    try {
137      getMap().replace(k0(), v3(), v4());
138    } catch (UnsupportedOperationException tolerated) {
139      // the operation would be a no-op, so exceptions are allowed but not required
140    }
141    expectUnchanged();
142  }
143
144  @MapFeature.Require(absent = SUPPORTS_PUT)
145  public void testReplaceEntry_unsupportedAbsentKey() {
146    try {
147      getMap().replace(k3(), v3(), v4());
148    } catch (UnsupportedOperationException tolerated) {
149      // the operation would be a no-op, so exceptions are allowed but not required
150    }
151    expectUnchanged();
152  }
153}