001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.google; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.common.collect.BiMap; 021import com.google.common.collect.testing.AbstractMapTester; 022import com.google.common.collect.testing.Helpers; 023import java.util.ArrayList; 024import java.util.Collection; 025import java.util.List; 026import java.util.Map.Entry; 027import org.junit.Ignore; 028 029/** Skeleton for a tester of a {@code BiMap}. */ 030@GwtCompatible 031@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 032public abstract class AbstractBiMapTester<K, V> extends AbstractMapTester<K, V> { 033 034 @Override 035 protected BiMap<K, V> getMap() { 036 return (BiMap<K, V>) super.getMap(); 037 } 038 039 static <K, V> Entry<V, K> reverseEntry(Entry<K, V> entry) { 040 return Helpers.mapEntry(entry.getValue(), entry.getKey()); 041 } 042 043 @Override 044 protected void expectContents(Collection<Entry<K, V>> expected) { 045 super.expectContents(expected); 046 List<Entry<V, K>> reversedEntries = new ArrayList<>(); 047 for (Entry<K, V> entry : expected) { 048 reversedEntries.add(reverseEntry(entry)); 049 } 050 Helpers.assertEqualIgnoringOrder(getMap().inverse().entrySet(), reversedEntries); 051 052 for (Entry<K, V> entry : expected) { 053 assertEquals( 054 "Wrong key for value " + entry.getValue(), 055 entry.getKey(), 056 getMap().inverse().get(entry.getValue())); 057 } 058 } 059 060 @Override 061 protected void expectMissing(Entry<K, V>... entries) { 062 super.expectMissing(entries); 063 for (Entry<K, V> entry : entries) { 064 Entry<V, K> reversed = reverseEntry(entry); 065 BiMap<V, K> inv = getMap().inverse(); 066 assertFalse( 067 "Inverse should not contain entry " + reversed, inv.entrySet().contains(reversed)); 068 assertFalse( 069 "Inverse should not contain key " + reversed.getKey(), 070 inv.containsKey(reversed.getKey())); 071 assertFalse( 072 "Inverse should not contain value " + reversed.getValue(), 073 inv.containsValue(reversed.getValue())); 074 /* 075 * TODO(cpovirk): This is a bit stronger than super.expectMissing(), which permits a <key, 076 * someOtherValue> pair. 077 */ 078 assertNull( 079 "Inverse should not return a mapping for key " + reversed.getKey(), 080 inv.get(reversed.getKey())); 081 } 082 } 083}