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