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