View Javadoc
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  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.Helpers;
24  import com.google.common.collect.testing.features.CollectionSize;
25  import com.google.common.collect.testing.features.MapFeature;
26  
27  /**
28   * Tester for {@code BiMap.put} and {@code BiMap.forcePut}.
29   */
30  @GwtCompatible
31  public class BiMapPutTester<K, V> extends AbstractBiMapTester<K, V> {
32  
33    @SuppressWarnings("unchecked")
34    @MapFeature.Require(SUPPORTS_PUT)
35    @CollectionSize.Require(ZERO)
36    public void testPutWithSameValueFails() {
37      getMap().put(k0(), v0());
38      try {
39        getMap().put(k1(), v0());
40        fail("Expected IllegalArgumentException");
41      } catch (IllegalArgumentException expected) {
42        // success
43      }
44      // verify that the bimap is unchanged
45      expectAdded(e0());
46    }
47  
48    @SuppressWarnings("unchecked")
49    @MapFeature.Require(SUPPORTS_PUT)
50    @CollectionSize.Require(ZERO)
51    public void testPutPresentKeyDifferentValue() {
52      getMap().put(k0(), v0());
53      getMap().put(k0(), v1());
54      // verify that the bimap is changed, and that the old inverse mapping
55      // from v1 -> v0 is deleted
56      expectContents(Helpers.mapEntry(k0(), v1()));
57    }
58  
59    @SuppressWarnings("unchecked")
60    @MapFeature.Require(SUPPORTS_PUT)
61    @CollectionSize.Require(ZERO)
62    public void putDistinctKeysDistinctValues() {
63      getMap().put(k0(), v0());
64      getMap().put(k1(), v1());
65      expectAdded(e0(), e1());
66    }
67  
68    @SuppressWarnings("unchecked")
69    @MapFeature.Require(SUPPORTS_PUT)
70    @CollectionSize.Require(ZERO)
71    public void testForcePutOverwritesOldValueEntry() {
72      getMap().put(k0(), v0());
73      getMap().forcePut(k1(), v0());
74      // verify that the bimap is unchanged
75      expectAdded(Helpers.mapEntry(k1(), v0()));
76    }
77  
78    @SuppressWarnings("unchecked")
79    @MapFeature.Require(SUPPORTS_PUT)
80    @CollectionSize.Require(ZERO)
81    public void testInversePut() {
82      getMap().put(k0(), v0());
83      getMap().inverse().put(v1(), k1());
84      expectAdded(e0(), e1());
85    }
86  }