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.checkerframework.checker.nullness.qual.Nullable;
028import org.junit.Ignore;
029
030/** Skeleton for a tester of a {@code BiMap}. */
031@GwtCompatible
032@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
033@ElementTypesAreNonnullByDefault
034public abstract class AbstractBiMapTester<K extends @Nullable Object, V extends @Nullable Object>
035    extends AbstractMapTester<K, V> {
036
037  @Override
038  protected BiMap<K, V> getMap() {
039    return (BiMap<K, V>) super.getMap();
040  }
041
042  static <K extends @Nullable Object, V extends @Nullable Object> Entry<V, K> reverseEntry(
043      Entry<K, V> entry) {
044    return Helpers.mapEntry(entry.getValue(), entry.getKey());
045  }
046
047  @Override
048  protected void expectContents(Collection<Entry<K, V>> expected) {
049    super.expectContents(expected);
050    List<Entry<V, K>> reversedEntries = new ArrayList<>();
051    for (Entry<K, V> entry : expected) {
052      reversedEntries.add(reverseEntry(entry));
053    }
054    Helpers.assertEqualIgnoringOrder(getMap().inverse().entrySet(), reversedEntries);
055
056    for (Entry<K, V> entry : expected) {
057      assertEquals(
058          "Wrong key for value " + entry.getValue(),
059          entry.getKey(),
060          getMap().inverse().get(entry.getValue()));
061    }
062  }
063
064  @Override
065  protected void expectMissing(Entry<K, V>... entries) {
066    super.expectMissing(entries);
067    for (Entry<K, V> entry : entries) {
068      Entry<V, K> reversed = reverseEntry(entry);
069      BiMap<V, K> inv = getMap().inverse();
070      assertFalse(
071          "Inverse should not contain entry " + reversed, inv.entrySet().contains(reversed));
072      assertFalse(
073          "Inverse should not contain key " + reversed.getKey(),
074          inv.containsKey(reversed.getKey()));
075      assertFalse(
076          "Inverse should not contain value " + reversed.getValue(),
077          inv.containsValue(reversed.getValue()));
078      /*
079       * TODO(cpovirk): This is a bit stronger than super.expectMissing(), which permits a <key,
080       * someOtherValue> pair.
081       */
082      assertNull(
083          "Inverse should not return a mapping for key " + reversed.getKey(),
084          inv.get(reversed.getKey()));
085    }
086  }
087}