• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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_KEYS;
21 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_QUERIES;
22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
23 
24 import com.google.common.collect.testing.AbstractMapTester;
25 import com.google.common.collect.testing.WrongType;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28 
29 /**
30  * A generic JUnit test which tests {@code remove} operations on a map. Can't be
31  * invoked directly; please see
32  * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
33  *
34  * <p>This class is GWT compatible.
35  *
36  * @author George van den Driessche
37  * @author Chris Povirk
38  */
39 @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
40 public class MapRemoveTester<K, V> extends AbstractMapTester<K, V> {
41   @MapFeature.Require(SUPPORTS_REMOVE)
42   @CollectionSize.Require(absent = ZERO)
testRemove_present()43   public void testRemove_present() {
44     int initialSize = getMap().size();
45     assertEquals("remove(present) should return the associated value",
46         samples.e0.getValue(), getMap().remove(samples.e0.getKey()));
47     assertEquals("remove(present) should decrease a map's size by one.",
48         initialSize - 1, getMap().size());
49     expectMissing(samples.e0);
50   }
51 
52   @MapFeature.Require(SUPPORTS_REMOVE)
testRemove_notPresent()53   public void testRemove_notPresent() {
54     assertNull("remove(notPresent) should return null",
55         getMap().remove(samples.e3.getKey()));
56     expectUnchanged();
57   }
58 
59   @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
60   @CollectionSize.Require(absent = ZERO)
testRemove_nullPresent()61   public void testRemove_nullPresent() {
62     initMapWithNullKey();
63 
64     int initialSize = getMap().size();
65     assertEquals("remove(null) should return the associated value",
66         getValueForNullKey(), getMap().remove(null));
67     assertEquals("remove(present) should decrease a map's size by one.",
68         initialSize - 1, getMap().size());
69     expectMissing(entry(null, getValueForNullKey()));
70   }
71 
72   @MapFeature.Require(absent = SUPPORTS_REMOVE)
73   @CollectionSize.Require(absent = ZERO)
testRemove_unsupported()74   public void testRemove_unsupported() {
75     try {
76       getMap().remove(samples.e0.getKey());
77       fail("remove(present) should throw UnsupportedOperationException");
78     } catch (UnsupportedOperationException expected) {
79     }
80     expectUnchanged();
81     assertEquals("remove(present) should not remove the element",
82         samples.e0.getValue(), get(samples.e0.getKey()));
83   }
84 
85   @MapFeature.Require(absent = SUPPORTS_REMOVE)
testRemove_unsupportedNotPresent()86   public void testRemove_unsupportedNotPresent() {
87     try {
88       assertNull("remove(notPresent) should return null or throw "
89           + "UnsupportedOperationException",
90           getMap().remove(samples.e3.getKey()));
91     } catch (UnsupportedOperationException tolerated) {
92     }
93     expectUnchanged();
94     expectMissing(samples.e3);
95   }
96 
97   @MapFeature.Require(
98       value = SUPPORTS_REMOVE,
99       absent = ALLOWS_NULL_QUERIES)
testRemove_nullQueriesNotSupported()100   public void testRemove_nullQueriesNotSupported() {
101     try {
102       assertNull("remove(null) should return null or throw "
103           + "NullPointerException",
104           getMap().remove(null));
105     } catch (NullPointerException tolerated) {
106     }
107     expectUnchanged();
108   }
109 
110   @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
testRemove_nullSupportedMissing()111   public void testRemove_nullSupportedMissing() {
112     assertNull("remove(null) should return null", getMap().remove(null));
113     expectUnchanged();
114   }
115 
116   @MapFeature.Require(SUPPORTS_REMOVE)
testRemove_wrongType()117   public void testRemove_wrongType() {
118     try {
119       assertNull(getMap().remove(WrongType.VALUE));
120     } catch (ClassCastException tolerated) {
121     }
122     expectUnchanged();
123   }
124 }
125