BiMapRemoveTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/*
2 * Copyright (C) 2012 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
17package com.google.common.collect.testing.google;
18
19import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
21
22import com.google.common.annotations.GwtCompatible;
23import com.google.common.annotations.GwtIncompatible;
24import com.google.common.collect.testing.Helpers;
25import com.google.common.collect.testing.features.CollectionSize;
26import com.google.common.collect.testing.features.MapFeature;
27
28import java.lang.reflect.Method;
29import java.util.Iterator;
30
31/**
32 * Tester for {@code BiMap.remove}.
33 *
34 * @author Louis Wasserman
35 */
36@GwtCompatible(emulated = true)
37public class BiMapRemoveTester<K, V> extends AbstractBiMapTester<K, V> {
38  @SuppressWarnings("unchecked")
39  @MapFeature.Require(SUPPORTS_REMOVE)
40  @CollectionSize.Require(absent = ZERO)
41  public void testRemoveKeyRemovesFromInverse() {
42    getMap().remove(samples.e0.getKey());
43    expectMissing(samples.e0);
44  }
45
46  @SuppressWarnings("unchecked")
47  @MapFeature.Require(SUPPORTS_REMOVE)
48  @CollectionSize.Require(absent = ZERO)
49  public void testRemoveKeyFromKeySetRemovesFromInverse() {
50    getMap().keySet().remove(samples.e0.getKey());
51    expectMissing(samples.e0);
52  }
53
54  @SuppressWarnings("unchecked")
55  @MapFeature.Require(SUPPORTS_REMOVE)
56  @CollectionSize.Require(absent = ZERO)
57  public void testRemoveFromValuesRemovesFromInverse() {
58    getMap().values().remove(samples.e0.getValue());
59    expectMissing(samples.e0);
60  }
61
62  @SuppressWarnings("unchecked")
63  @MapFeature.Require(SUPPORTS_REMOVE)
64  @CollectionSize.Require(absent = ZERO)
65  public void testRemoveFromInverseRemovesFromForward() {
66    getMap().inverse().remove(samples.e0.getValue());
67    expectMissing(samples.e0);
68  }
69
70  @SuppressWarnings("unchecked")
71  @MapFeature.Require(SUPPORTS_REMOVE)
72  @CollectionSize.Require(absent = ZERO)
73  public void testRemoveFromInverseKeySetRemovesFromForward() {
74    getMap().inverse().keySet().remove(samples.e0.getValue());
75    expectMissing(samples.e0);
76  }
77
78  @SuppressWarnings("unchecked")
79  @MapFeature.Require(SUPPORTS_REMOVE)
80  @CollectionSize.Require(absent = ZERO)
81  public void testRemoveFromInverseValuesRemovesFromInverse() {
82    getMap().inverse().values().remove(samples.e0.getKey());
83    expectMissing(samples.e0);
84  }
85
86  @MapFeature.Require(SUPPORTS_REMOVE)
87  @CollectionSize.Require(absent = ZERO)
88  public void testKeySetIteratorRemove() {
89    int initialSize = getNumElements();
90    Iterator<K> iterator = getMap().keySet().iterator();
91    iterator.next();
92    iterator.remove();
93    assertEquals(initialSize - 1, getMap().size());
94    assertEquals(initialSize - 1, getMap().inverse().size());
95  }
96
97  /**
98   * Returns the {@link Method} instance for
99   * {@link #testKeySetIteratorRemove()} so that tests of
100   * {@code Maps.filterEntries(BiMap, Predicate)} can suppress
101   * it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
102   */
103  @GwtIncompatible("reflection")
104  public static Method getKeySetIteratorRemoveMethod() {
105    return Helpers.getMethod(BiMapRemoveTester.class, "testKeySetIteratorRemove");
106  }
107}
108