1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import java.util.IdentityHashMap;
18 import java.util.Iterator;
19 import java.util.Map.Entry;
20 import junit.framework.TestCase;
21 
22 /**
23  * Tests for {@code AbstractBiMap}.
24  *
25  * @author Mike Bostock
26  */
27 public class AbstractBiMapTest extends TestCase {
28 
29   // The next two tests verify that map entries are not accessed after they're
30   // removed, since IdentityHashMap throws an exception when that occurs.
31   @SuppressWarnings("IdentityHashMapBoxing") // explicitly testing IdentityHashMap
testIdentityKeySetIteratorRemove()32   public void testIdentityKeySetIteratorRemove() {
33     BiMap<Integer, String> bimap =
34         new AbstractBiMap<Integer, String>(
35             new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {};
36     bimap.put(1, "one");
37     bimap.put(2, "two");
38     bimap.put(3, "three");
39     Iterator<Integer> iterator = bimap.keySet().iterator();
40     iterator.next();
41     iterator.next();
42     iterator.remove();
43     iterator.next();
44     iterator.remove();
45     assertEquals(1, bimap.size());
46     assertEquals(1, bimap.inverse().size());
47   }
48 
49   @SuppressWarnings("IdentityHashMapBoxing") // explicitly testing IdentityHashMap
testIdentityEntrySetIteratorRemove()50   public void testIdentityEntrySetIteratorRemove() {
51     BiMap<Integer, String> bimap =
52         new AbstractBiMap<Integer, String>(
53             new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {};
54     bimap.put(1, "one");
55     bimap.put(2, "two");
56     bimap.put(3, "three");
57     Iterator<Entry<Integer, String>> iterator = bimap.entrySet().iterator();
58     iterator.next();
59     iterator.next();
60     iterator.remove();
61     iterator.next();
62     iterator.remove();
63     assertEquals(1, bimap.size());
64     assertEquals(1, bimap.inverse().size());
65   }
66 }
67