View Javadoc
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  
31  /**
32   * A generic JUnit test which tests {@link Map#replace(Object, Object)}. Can't be
33   * invoked directly; please see
34   * {@link com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}.
35   * 
36   * @author Louis Wasserman
37   */
38  @GwtCompatible
39  public class MapReplaceTester<K, V> extends AbstractMapTester<K, V> {
40  
41    @MapFeature.Require(SUPPORTS_PUT)
42    @CollectionSize.Require(absent = ZERO)
43    public void testReplace_supportedPresent() {
44      try {
45        assertEquals(v0(), getMap().replace(k0(), 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)
54    public void testReplace_supportedPresentNoChange() {
55      assertEquals(v0(), getMap().replace(k0(), v0()));
56      expectUnchanged();
57    }
58  
59    @MapFeature.Require(SUPPORTS_PUT)
60    public void testReplace_supportedAbsent() {
61      assertNull(getMap().replace(k3(), v3()));
62      expectUnchanged();
63    }
64    
65    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
66    @CollectionSize.Require(absent = ZERO)
67    public void testReplace_presentNullValueUnsupported() {
68      try {
69        getMap().replace(k0(), null);
70        fail("Expected NullPointerException");
71      } catch (NullPointerException expected) {}
72      expectUnchanged();
73    }
74    
75    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
76    public void testReplace_absentNullValueUnsupported() {
77      try {
78        getMap().replace(k3(), null);
79      } catch (NullPointerException tolerated) {
80        // permitted not to throw because it would be a no-op
81      }
82      expectUnchanged();
83    }
84    
85    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEY_QUERIES)
86    public void testReplace_absentNullKeyUnsupported() {
87      try {
88        getMap().replace(null, v3());
89      } catch (NullPointerException tolerated) {
90        // permitted not to throw because it would be a no-op
91      }
92      expectUnchanged();
93    }
94    
95    @MapFeature.Require(absent = SUPPORTS_PUT)
96    @CollectionSize.Require(absent = ZERO)
97    public void testReplace_unsupportedPresent() {
98      try {
99        getMap().replace(k0(), v3());
100       fail("Expected UnsupportedOperationException");
101     } catch (UnsupportedOperationException expected) {
102     } catch (ClassCastException tolerated) {
103       // for ClassToInstanceMap
104     }
105     
106     expectUnchanged();
107   }
108 }