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;
023import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
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.Map;
030import org.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked
034 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
035 *
036 * @author Louis Wasserman
037 */
038@GwtCompatible
039@Ignore("test runners must not instantiate and run this directly, only via suites we build")
040// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041@SuppressWarnings("JUnit4ClassUsedInJUnit3")
042@IgnoreJRERequirement // We opt into library desugaring for our tests.
043public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
044
045  @MapFeature.Require(SUPPORTS_PUT)
046  @CollectionSize.Require(absent = ZERO)
047  public void testReplaceEntry_supportedPresent() {
048    try {
049      assertTrue(getMap().replace(k0(), v0(), v3()));
050      expectReplacement(entry(k0(), v3()));
051    } catch (ClassCastException tolerated) { // for ClassToInstanceMap
052      expectUnchanged();
053    }
054  }
055
056  @MapFeature.Require(SUPPORTS_PUT)
057  @CollectionSize.Require(absent = ZERO)
058  public void testReplaceEntry_supportedPresentUnchanged() {
059    assertTrue(getMap().replace(k0(), v0(), v0()));
060    expectUnchanged();
061  }
062
063  @MapFeature.Require(SUPPORTS_PUT)
064  @CollectionSize.Require(absent = ZERO)
065  public void testReplaceEntry_supportedWrongValue() {
066    assertFalse(getMap().replace(k0(), v3(), v4()));
067    expectUnchanged();
068  }
069
070  @MapFeature.Require(SUPPORTS_PUT)
071  public void testReplaceEntry_supportedAbsentKey() {
072    assertFalse(getMap().replace(k3(), v3(), v4()));
073    expectUnchanged();
074  }
075
076  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
077  @CollectionSize.Require(absent = ZERO)
078  public void testReplaceEntry_presentNullValueUnsupported() {
079    assertThrows(NullPointerException.class, () -> getMap().replace(k0(), v0(), null));
080    expectUnchanged();
081  }
082
083  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
084  @CollectionSize.Require(absent = ZERO)
085  public void testReplaceEntry_wrongValueNullValueUnsupported() {
086    try {
087      assertFalse(getMap().replace(k0(), v3(), null));
088    } catch (NullPointerException tolerated) {
089      // the operation would be a no-op, so exceptions are allowed but not required
090    }
091    expectUnchanged();
092  }
093
094  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
095  public void testReplaceEntry_absentKeyNullValueUnsupported() {
096    try {
097      assertFalse(getMap().replace(k3(), v3(), null));
098    } catch (NullPointerException tolerated) {
099      // the operation would be a no-op, so exceptions are allowed but not required
100    }
101    expectUnchanged();
102  }
103
104  @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
105  public void testReplaceEntry_nullDifferentFromAbsent() {
106    assertFalse(getMap().replace(k3(), null, v3()));
107    expectUnchanged();
108  }
109
110  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
111  public void testReplaceEntry_expectNullUnsupported() {
112    try {
113      assertFalse(getMap().replace(k3(), null, v3()));
114    } catch (NullPointerException tolerated) {
115      // the operation would be a no-op, so exceptions are allowed but not required
116    }
117    expectUnchanged();
118  }
119
120  @MapFeature.Require(absent = SUPPORTS_PUT)
121  @CollectionSize.Require(absent = ZERO)
122  public void testReplaceEntry_unsupportedPresent() {
123    assertThrows(UnsupportedOperationException.class, () -> getMap().replace(k0(), v0(), v3()));
124    expectUnchanged();
125  }
126
127  @MapFeature.Require(absent = SUPPORTS_PUT)
128  @CollectionSize.Require(absent = ZERO)
129  public void testReplaceEntry_unsupportedWrongValue() {
130    try {
131      getMap().replace(k0(), v3(), v4());
132    } catch (UnsupportedOperationException tolerated) {
133      // the operation would be a no-op, so exceptions are allowed but not required
134    }
135    expectUnchanged();
136  }
137
138  @MapFeature.Require(absent = SUPPORTS_PUT)
139  public void testReplaceEntry_unsupportedAbsentKey() {
140    try {
141      getMap().replace(k3(), v3(), v4());
142    } catch (UnsupportedOperationException tolerated) {
143      // the operation would be a no-op, so exceptions are allowed but not required
144    }
145    expectUnchanged();
146  }
147}