1 /*
2  * Copyright (C) 2015 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect.testing.testers;
18 
19 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
23 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
24 
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.collect.testing.AbstractMapTester;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import com.google.common.collect.testing.features.MapFeature;
29 import java.util.Map;
30 import org.junit.Ignore;
31 
32 /**
33  * A generic JUnit test which tests {@link Map#replace(Object, Object)}. Can't be invoked directly;
34  * please see {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
35  *
36  * @author Louis Wasserman
37  */
38 @GwtCompatible
39 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
40 public class MapReplaceTester<K, V> extends AbstractMapTester<K, V> {
41 
42   @MapFeature.Require(SUPPORTS_PUT)
43   @CollectionSize.Require(absent = ZERO)
testReplace_supportedPresent()44   public void testReplace_supportedPresent() {
45     try {
46       assertEquals(v0(), getMap().replace(k0(), v3()));
47       expectReplacement(entry(k0(), v3()));
48     } catch (ClassCastException tolerated) { // for ClassToInstanceMap
49       expectUnchanged();
50     }
51   }
52 
53   @MapFeature.Require(SUPPORTS_PUT)
54   @CollectionSize.Require(absent = ZERO)
testReplace_supportedPresentNoChange()55   public void testReplace_supportedPresentNoChange() {
56     assertEquals(v0(), getMap().replace(k0(), v0()));
57     expectUnchanged();
58   }
59 
60   @MapFeature.Require(SUPPORTS_PUT)
testReplace_supportedAbsent()61   public void testReplace_supportedAbsent() {
62     assertNull(getMap().replace(k3(), v3()));
63     expectUnchanged();
64   }
65 
66   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
67   @CollectionSize.Require(absent = ZERO)
testReplace_presentNullValueUnsupported()68   public void testReplace_presentNullValueUnsupported() {
69     try {
70       getMap().replace(k0(), null);
71       fail("Expected NullPointerException");
72     } catch (NullPointerException expected) {
73     }
74     expectUnchanged();
75   }
76 
77   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
testReplace_absentNullValueUnsupported()78   public void testReplace_absentNullValueUnsupported() {
79     try {
80       getMap().replace(k3(), null);
81     } catch (NullPointerException tolerated) {
82       // permitted not to throw because it would be a no-op
83     }
84     expectUnchanged();
85   }
86 
87   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
testReplace_absentNullKeyUnsupported()88   public void testReplace_absentNullKeyUnsupported() {
89     try {
90       getMap().replace(null, v3());
91     } catch (NullPointerException tolerated) {
92       // permitted not to throw because it would be a no-op
93     }
94     expectUnchanged();
95   }
96 
97   @MapFeature.Require(absent = SUPPORTS_PUT)
98   @CollectionSize.Require(absent = ZERO)
testReplace_unsupportedPresent()99   public void testReplace_unsupportedPresent() {
100     try {
101       getMap().replace(k0(), v3());
102       fail("Expected UnsupportedOperationException");
103     } catch (UnsupportedOperationException expected) {
104     } catch (ClassCastException tolerated) {
105       // for ClassToInstanceMap
106     }
107 
108     expectUnchanged();
109   }
110 }
111