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