• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.collect.BiMap;
21 import com.google.common.collect.testing.AbstractMapTester;
22 import com.google.common.collect.testing.Helpers;
23 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map.Entry;
28 
29 /**
30  * Skeleton for a tester of a {@code BiMap}.
31  */
32 @GwtCompatible
33 public abstract class AbstractBiMapTester<K, V> extends AbstractMapTester<K, V> {
34 
35   @Override
getMap()36   protected BiMap<K, V> getMap() {
37     return (BiMap<K, V>) super.getMap();
38   }
39 
reverseEntry(Entry<K, V> entry)40   static <K, V> Entry<V, K> reverseEntry(Entry<K, V> entry) {
41     return Helpers.mapEntry(entry.getValue(), entry.getKey());
42   }
43 
44   @Override
expectContents(Collection<Entry<K, V>> expected)45   protected void expectContents(Collection<Entry<K, V>> expected) {
46     super.expectContents(expected);
47     List<Entry<V, K>> reversedEntries = new ArrayList<Entry<V, K>>();
48     for (Entry<K, V> entry : expected) {
49       reversedEntries.add(reverseEntry(entry));
50     }
51     Helpers.assertEqualIgnoringOrder(getMap().inverse().entrySet(), reversedEntries);
52 
53     for (Entry<K, V> entry : expected) {
54       assertEquals("Wrong key for value " + entry.getValue(), entry.getKey(), getMap()
55           .inverse()
56           .get(entry.getValue()));
57     }
58   }
59 
60   @Override
expectMissing(Entry<K, V>.... entries)61   protected void expectMissing(Entry<K, V>... entries) {
62     super.expectMissing(entries);
63     for (Entry<K, V> entry : entries) {
64       Entry<V, K> reversed = reverseEntry(entry);
65       BiMap<V, K> inv = getMap().inverse();
66       assertFalse("Inverse should not contain entry " + reversed,
67           inv.entrySet().contains(entry));
68       assertFalse("Inverse should not contain key " + entry.getValue(),
69           inv.containsKey(entry.getValue()));
70       assertFalse("Inverse should not contain value " + entry.getKey(),
71           inv.containsValue(entry.getKey()));
72       assertNull("Inverse should not return a mapping for key " + entry.getValue(),
73           getMap().get(entry.getValue()));
74     }
75   }
76 
77 }
78