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_VALUES;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.AbstractMapTester;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28 import java.util.Map;
29 import org.junit.Ignore;
30 
31 /**
32  * A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't be invoked
33  * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
34  *
35  * @author Louis Wasserman
36  */
37 @GwtCompatible
38 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
39 public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
40 
41   @MapFeature.Require(SUPPORTS_PUT)
42   @CollectionSize.Require(absent = ZERO)
testReplaceEntry_supportedPresent()43   public void testReplaceEntry_supportedPresent() {
44     try {
45       assertTrue(getMap().replace(k0(), v0(), v3()));
46       expectReplacement(entry(k0(), v3()));
47     } catch (ClassCastException tolerated) { // for ClassToInstanceMap
48       expectUnchanged();
49     }
50   }
51 
52   @MapFeature.Require(SUPPORTS_PUT)
53   @CollectionSize.Require(absent = ZERO)
testReplaceEntry_supportedPresentUnchanged()54   public void testReplaceEntry_supportedPresentUnchanged() {
55     assertTrue(getMap().replace(k0(), v0(), v0()));
56     expectUnchanged();
57   }
58 
59   @MapFeature.Require(SUPPORTS_PUT)
60   @CollectionSize.Require(absent = ZERO)
testReplaceEntry_supportedWrongValue()61   public void testReplaceEntry_supportedWrongValue() {
62     assertFalse(getMap().replace(k0(), v3(), v4()));
63     expectUnchanged();
64   }
65 
66   @MapFeature.Require(SUPPORTS_PUT)
testReplaceEntry_supportedAbsentKey()67   public void testReplaceEntry_supportedAbsentKey() {
68     assertFalse(getMap().replace(k3(), v3(), v4()));
69     expectUnchanged();
70   }
71 
72   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
73   @CollectionSize.Require(absent = ZERO)
testReplaceEntry_presentNullValueUnsupported()74   public void testReplaceEntry_presentNullValueUnsupported() {
75     try {
76       getMap().replace(k0(), v0(), null);
77       fail("Expected NullPointerException");
78     } catch (NullPointerException expected) {
79     }
80     expectUnchanged();
81   }
82 
83   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
84   @CollectionSize.Require(absent = ZERO)
testReplaceEntry_wrongValueNullValueUnsupported()85   public void testReplaceEntry_wrongValueNullValueUnsupported() {
86     try {
87       assertFalse(getMap().replace(k0(), v3(), null));
88     } catch (NullPointerException tolerated) {
89       // the operation would be a no-op, so exceptions are allowed but not required
90     }
91     expectUnchanged();
92   }
93 
94   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
testReplaceEntry_absentKeyNullValueUnsupported()95   public void testReplaceEntry_absentKeyNullValueUnsupported() {
96     try {
97       assertFalse(getMap().replace(k3(), v3(), null));
98     } catch (NullPointerException tolerated) {
99       // 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})
testReplaceEntry_nullDifferentFromAbsent()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)
testReplaceEntry_expectNullUnsupported()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)
testReplaceEntry_unsupportedPresent()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)
testReplaceEntry_unsupportedWrongValue()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)
testReplaceEntry_unsupportedAbsentKey()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 }
152