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_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  
30  /**
31   * A generic JUnit test which tests {@link Map#replace(Object, Object, Object)}. Can't 
32   * be invoked directly; please see
33   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
34   * 
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class MapReplaceEntryTester<K, V> extends AbstractMapTester<K, V> {
39    
40    @MapFeature.Require(SUPPORTS_PUT)
41    @CollectionSize.Require(absent = ZERO)
42    public void testReplaceEntry_supportedPresent() {
43      try {
44        assertTrue(getMap().replace(k0(), v0(), v3()));
45        expectReplacement(entry(k0(), v3()));
46      } catch (ClassCastException tolerated) { // for ClassToInstanceMap
47        expectUnchanged();
48      }
49    }
50    
51    @MapFeature.Require(SUPPORTS_PUT)
52    @CollectionSize.Require(absent = ZERO)
53    public void testReplaceEntry_supportedPresentUnchanged() {
54      assertTrue(getMap().replace(k0(), v0(), v0()));
55      expectUnchanged();
56    }
57    
58    @MapFeature.Require(SUPPORTS_PUT)
59    @CollectionSize.Require(absent = ZERO)
60    public void testReplaceEntry_supportedWrongValue() {
61      assertFalse(getMap().replace(k0(), v3(), v4()));
62      expectUnchanged();
63    }
64    
65    @MapFeature.Require(SUPPORTS_PUT)
66    public void testReplaceEntry_supportedAbsentKey() {
67      assertFalse(getMap().replace(k3(), v3(), v4()));
68      expectUnchanged();
69    }
70    
71    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
72    @CollectionSize.Require(absent = ZERO)
73    public void testReplaceEntry_presentNullValueUnsupported() {
74      try {
75        getMap().replace(k0(), v0(), null);
76        fail("Expected NullPointerException");
77      } catch (NullPointerException expected) {}
78      expectUnchanged();
79    }
80    
81    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 
82    @CollectionSize.Require(absent = ZERO)
83    public void testReplaceEntry_wrongValueNullValueUnsupported() {
84      try {
85        assertFalse(getMap().replace(k0(), v3(), null));
86      } catch (NullPointerException tolerated) {
87        // the operation would be a no-op, so exceptions are allowed but not required
88      }
89      expectUnchanged();
90    }
91    
92    @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) 
93    public void testReplaceEntry_absentKeyNullValueUnsupported() {
94      try {
95        assertFalse(getMap().replace(k3(), v3(), null));
96      } catch (NullPointerException tolerated) {
97        // the operation would be a no-op, so exceptions are allowed but not required
98      }
99      expectUnchanged();
100   }
101   
102   @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUE_QUERIES})
103   public void testReplaceEntry_nullDifferentFromAbsent() {
104     assertFalse(getMap().replace(k3(), null, v3()));
105     expectUnchanged();
106   }
107   
108   @MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES)
109   public void testReplaceEntry_expectNullUnsupported() {
110     try {
111       assertFalse(getMap().replace(k3(), null, v3())); 
112     } catch (NullPointerException tolerated) {
113       // the operation would be a no-op, so exceptions are allowed but not required
114     }
115     expectUnchanged();
116   }
117   
118   @MapFeature.Require(absent = SUPPORTS_PUT)
119   @CollectionSize.Require(absent = ZERO)
120   public void testReplaceEntry_unsupportedPresent() {
121     try {
122       getMap().replace(k0(), v0(), v3());
123       fail("Expected UnsupportedOperationException");
124     } catch (UnsupportedOperationException expected) {}
125     expectUnchanged();
126   }
127   
128   @MapFeature.Require(absent = SUPPORTS_PUT)
129   @CollectionSize.Require(absent = ZERO)
130   public void testReplaceEntry_unsupportedWrongValue() {
131     try {
132       getMap().replace(k0(), v3(), v4());
133     } catch (UnsupportedOperationException tolerated) {
134       // the operation would be a no-op, so exceptions are allowed but not required
135     }
136     expectUnchanged();
137   }
138   
139   @MapFeature.Require(absent = SUPPORTS_PUT)
140   public void testReplaceEntry_unsupportedAbsentKey() {
141     try {
142       getMap().replace(k3(), v3(), v4());
143     } catch (UnsupportedOperationException tolerated) {
144       // the operation would be a no-op, so exceptions are allowed but not required
145     }
146     expectUnchanged();
147   }
148 }