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