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;
024import static org.junit.Assert.assertThrows;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.AbstractMapTester;
028import com.google.common.collect.testing.features.CollectionSize;
029import com.google.common.collect.testing.features.MapFeature;
030import java.util.Map;
031
032/**
033 * A generic JUnit test which tests {@link Map#replace(Object, Object)}. Can't be invoked directly;
034 * please see {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039public class MapReplaceTester<K, V> extends AbstractMapTester<K, V> {
040
041  @MapFeature.Require(SUPPORTS_PUT)
042  @CollectionSize.Require(absent = ZERO)
043  public void testReplace_supportedPresent() {
044    try {
045      assertEquals(v0(), getMap().replace(k0(), v3()));
046      expectReplacement(entry(k0(), v3()));
047    } catch (ClassCastException tolerated) { // for ClassToInstanceMap
048      expectUnchanged();
049    }
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    assertThrows(NullPointerException.class, () -> getMap().replace(k0(), null));
069    expectUnchanged();
070  }
071
072  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
073  public void testReplace_absentNullValueUnsupported() {
074    try {
075      getMap().replace(k3(), null);
076    } catch (NullPointerException tolerated) {
077      // permitted not to throw because it would be a no-op
078    }
079    expectUnchanged();
080  }
081
082  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
083  public void testReplace_absentNullKeyUnsupported() {
084    try {
085      getMap().replace(null, v3());
086    } catch (NullPointerException tolerated) {
087      // permitted not to throw because it would be a no-op
088    }
089    expectUnchanged();
090  }
091
092  @MapFeature.Require(absent = SUPPORTS_PUT)
093  @CollectionSize.Require(absent = ZERO)
094  public void testReplace_unsupportedPresent() {
095    RuntimeException expected =
096        assertThrows(RuntimeException.class, () -> getMap().replace(k0(), v3()));
097    if (!(expected instanceof UnsupportedOperationException
098        || expected instanceof ClassCastException)) {
099      throw expected;
100    }
101    expectUnchanged();
102  }
103}