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 static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 020import static com.google.common.collect.testing.Helpers.mapEntry; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.collect.BiMap; 024import com.google.common.collect.testing.AbstractMapTester; 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.List; 028import java.util.Map.Entry; 029import org.jspecify.annotations.NullMarked; 030import org.jspecify.annotations.Nullable; 031import org.junit.Ignore; 032 033/** Skeleton for a tester of a {@code BiMap}. */ 034@GwtCompatible 035@Ignore("test runners must not instantiate and run this directly, only via suites we build") 036// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 037@SuppressWarnings("JUnit4ClassUsedInJUnit3") 038@NullMarked 039public abstract class AbstractBiMapTester<K extends @Nullable Object, V extends @Nullable Object> 040 extends AbstractMapTester<K, V> { 041 042 @Override 043 protected BiMap<K, V> getMap() { 044 return (BiMap<K, V>) super.getMap(); 045 } 046 047 static <K extends @Nullable Object, V extends @Nullable Object> Entry<V, K> reverseEntry( 048 Entry<K, V> entry) { 049 return mapEntry(entry.getValue(), entry.getKey()); 050 } 051 052 @Override 053 protected void expectContents(Collection<Entry<K, V>> expected) { 054 super.expectContents(expected); 055 List<Entry<V, K>> reversedEntries = new ArrayList<>(); 056 for (Entry<K, V> entry : expected) { 057 reversedEntries.add(reverseEntry(entry)); 058 } 059 assertEqualIgnoringOrder(getMap().inverse().entrySet(), reversedEntries); 060 061 for (Entry<K, V> entry : expected) { 062 assertEquals( 063 "Wrong key for value " + entry.getValue(), 064 entry.getKey(), 065 getMap().inverse().get(entry.getValue())); 066 } 067 } 068 069 @Override 070 protected void expectMissing(Entry<K, V>... entries) { 071 super.expectMissing(entries); 072 for (Entry<K, V> entry : entries) { 073 Entry<V, K> reversed = reverseEntry(entry); 074 BiMap<V, K> inv = getMap().inverse(); 075 assertFalse( 076 "Inverse should not contain entry " + reversed, inv.entrySet().contains(reversed)); 077 assertFalse( 078 "Inverse should not contain key " + reversed.getKey(), 079 inv.containsKey(reversed.getKey())); 080 assertFalse( 081 "Inverse should not contain value " + reversed.getValue(), 082 inv.containsValue(reversed.getValue())); 083 /* 084 * TODO(cpovirk): This is a bit stronger than super.expectMissing(), which permits a <key, 085 * someOtherValue> pair. 086 */ 087 assertNull( 088 "Inverse should not return a mapping for key " + reversed.getKey(), 089 inv.get(reversed.getKey())); 090 } 091 } 092}