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.Map;
029import org.junit.Ignore;
030
031/**
032 * A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked
033 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
034 *
035 * @author Louis Wasserman
036 */
037@GwtCompatible
038@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
040
041  @MapFeature.Require(SUPPORTS_PUT)
042  @CollectionSize.Require(absent = ZERO)
043  public void testReplaceEntry_supportedPresent() {
044    try {
045      assertTrue(getMap().replace(k0(), v0(), 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 testReplaceEntry_supportedPresentUnchanged() {
055    assertTrue(getMap().replace(k0(), v0(), v0()));
056    expectUnchanged();
057  }
058
059  @MapFeature.Require(SUPPORTS_PUT)
060  @CollectionSize.Require(absent = ZERO)
061  public void testReplaceEntry_supportedWrongValue() {
062    assertFalse(getMap().replace(k0(), v3(), v4()));
063    expectUnchanged();
064  }
065
066  @MapFeature.Require(SUPPORTS_PUT)
067  public void testReplaceEntry_supportedAbsentKey() {
068    assertFalse(getMap().replace(k3(), v3(), v4()));
069    expectUnchanged();
070  }
071
072  @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
073  @CollectionSize.Require(absent = ZERO)
074  public void testReplaceEntry_presentNullValueUnsupported() {
075    try {
076      getMap().replace(k0(), v0(), null);
077      fail("Expected NullPointerException");
078    } catch (NullPointerException expected) {
079    }
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    try {
124      getMap().replace(k0(), v0(), v3());
125      fail("Expected UnsupportedOperationException");
126    } catch (UnsupportedOperationException expected) {
127    }
128    expectUnchanged();
129  }
130
131  @MapFeature.Require(absent = SUPPORTS_PUT)
132  @CollectionSize.Require(absent = ZERO)
133  public void testReplaceEntry_unsupportedWrongValue() {
134    try {
135      getMap().replace(k0(), v3(), v4());
136    } catch (UnsupportedOperationException tolerated) {
137      // the operation would be a no-op, so exceptions are allowed but not required
138    }
139    expectUnchanged();
140  }
141
142  @MapFeature.Require(absent = SUPPORTS_PUT)
143  public void testReplaceEntry_unsupportedAbsentKey() {
144    try {
145      getMap().replace(k3(), v3(), v4());
146    } catch (UnsupportedOperationException tolerated) {
147      // the operation would be a no-op, so exceptions are allowed but not required
148    }
149    expectUnchanged();
150  }
151}