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_VALUE_QUERIES;
22  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
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   * Tester for {@link Map#remove(Object, Object)}.  Can't be invoked directly; please see 
32   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}. 
33   *
34   * @author Louis Wasserman
35   */
36  @GwtCompatible
37  public class MapRemoveEntryTester<K, V> extends AbstractMapTester<K, V> {
38    @MapFeature.Require(SUPPORTS_REMOVE)
39    @CollectionSize.Require(absent = ZERO)
40    public void testRemove_supportedPresent() {
41      assertTrue(getMap().remove(k0(), v0()));
42      expectMissing(e0());
43    }
44  
45    @MapFeature.Require(SUPPORTS_REMOVE)
46    public void testRemove_supportedPresentKeyWrongValue() {
47      assertFalse(getMap().remove(k0(), v3()));
48      expectUnchanged();
49    }
50  
51    @MapFeature.Require(SUPPORTS_REMOVE)
52    public void testRemove_supportedWrongKeyPresentValue() {
53      assertFalse(getMap().remove(k3(), v0()));
54      expectUnchanged();
55    }
56  
57    @MapFeature.Require(SUPPORTS_REMOVE)
58    public void testRemove_supportedAbsentKeyAbsentValue() {
59      assertFalse(getMap().remove(k3(), v3()));
60      expectUnchanged();
61    }
62    
63    @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_KEY_QUERIES)
64    public void testRemove_nullKeyQueriesUnsupported() {
65      try {
66        assertFalse(getMap().remove(null, v3()));
67      } catch (NullPointerException tolerated) {
68        // since the operation would be a no-op, the exception is not required
69      }
70      expectUnchanged();    
71    }
72    
73    @MapFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_VALUE_QUERIES)
74    public void testRemove_nullValueQueriesUnsupported() {
75      try {
76        assertFalse(getMap().remove(k3(), null));
77      } catch (NullPointerException tolerated) {
78        // since the operation would be a no-op, the exception is not required
79      }
80      expectUnchanged();    
81    }
82    
83    @MapFeature.Require(absent = SUPPORTS_REMOVE)
84    @CollectionSize.Require(absent = ZERO)
85    public void testRemove_unsupportedPresent() {
86      try {
87        getMap().remove(k0(), v0());
88        fail("Expected UnsupportedOperationException");
89      } catch (UnsupportedOperationException expected) {}
90      expectUnchanged();
91    }
92    
93    @MapFeature.Require(absent = SUPPORTS_REMOVE)
94    public void testRemove_unsupportedAbsent() {
95      try {
96        assertFalse(getMap().remove(k0(), v3()));
97      } catch (UnsupportedOperationException tolerated) {
98        // since the operation would be a no-op, the exception is not required
99      }
100     expectUnchanged();
101   }
102 }