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_KEY_QUERIES;
021import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
022import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
023import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
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;
030
031/**
032 * A generic JUnit test which tests {@code replace(K, V)} operations on a concurrent map. Can't be
033 * invoked directly; please see {@link
034 * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039public class ConcurrentMapReplaceTester<K, V> extends AbstractMapTester<K, V> {
040  @Override
041  protected ConcurrentMap<K, V> getMap() {
042    return (ConcurrentMap<K, V>) super.getMap();
043  }
044
045  @MapFeature.Require(SUPPORTS_PUT)
046  @CollectionSize.Require(absent = ZERO)
047  public void testReplace_supportedPresent() {
048    assertEquals(v0(), getMap().replace(k0(), v3()));
049    expectReplacement(entry(k0(), v3()));
050  }
051
052  @MapFeature.Require(SUPPORTS_PUT)
053  @CollectionSize.Require(absent = ZERO)
054  public void testReplace_supportedPresentNoChange() {
055    assertEquals(v0(), getMap().replace(k0(), v0()));
056    expectUnchanged();
057  }
058
059  @MapFeature.Require(SUPPORTS_PUT)
060  public void testReplace_supportedAbsent() {
061    assertNull(getMap().replace(k3(), v3()));
062    expectUnchanged();
063  }
064
065  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
066  @CollectionSize.Require(absent = ZERO)
067  public void testReplace_presentNullValueUnsupported() {
068    try {
069      getMap().replace(k0(), null);
070      fail("Expected NullPointerException");
071    } catch (NullPointerException expected) {
072    }
073    expectUnchanged();
074  }
075
076  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
077  public void testReplace_absentNullValueUnsupported() {
078    try {
079      getMap().replace(k3(), null);
080    } catch (NullPointerException tolerated) {
081      // permitted not to throw because it would be a no-op
082    }
083    expectUnchanged();
084  }
085
086  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
087  public void testReplace_absentNullKeyUnsupported() {
088    try {
089      getMap().replace(null, v3());
090    } catch (NullPointerException tolerated) {
091      // permitted not to throw because it would be a no-op
092    }
093    expectUnchanged();
094  }
095
096  @MapFeature.Require(absent = SUPPORTS_PUT)
097  @CollectionSize.Require(absent = ZERO)
098  public void testReplace_unsupportedPresent() {
099    try {
100      getMap().replace(k0(), v3());
101      fail("Expected UnsupportedOperationException");
102    } catch (UnsupportedOperationException expected) {
103    }
104    expectUnchanged();
105  }
106}