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