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.features.CollectionFeature.SERIALIZABLE;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.J2ktIncompatible;
024import com.google.common.collect.BiMap;
025import com.google.common.collect.testing.Helpers;
026import com.google.common.collect.testing.features.CollectionFeature;
027import com.google.common.testing.SerializableTester;
028import java.io.Serializable;
029import java.lang.reflect.Method;
030import java.util.Collections;
031import java.util.List;
032import org.junit.Ignore;
033
034/**
035 * Tests for the {@code inverse} view of a BiMap.
036 *
037 * <p>This assumes that {@code bimap.inverse().inverse() == bimap}, which is not technically
038 * required but is fulfilled by all current implementations.
039 *
040 * @author Louis Wasserman
041 */
042@GwtCompatible(emulated = true)
043@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
044@SuppressWarnings("JUnit4ClassUsedInJUnit3")
045public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> {
046
047  public void testInverseSame() {
048    assertSame(getMap(), getMap().inverse().inverse());
049  }
050
051  @CollectionFeature.Require(SERIALIZABLE)
052  public void testInverseSerialization() {
053    BiMapPair<K, V> pair = new BiMapPair<>(getMap());
054    BiMapPair<K, V> copy = SerializableTester.reserialize(pair);
055    assertEquals(pair.forward, copy.forward);
056    assertEquals(pair.backward, copy.backward);
057    assertSame(copy.backward, copy.forward.inverse());
058    assertSame(copy.forward, copy.backward.inverse());
059  }
060
061  private static class BiMapPair<K, V> implements Serializable {
062    final BiMap<K, V> forward;
063    final BiMap<V, K> backward;
064
065    BiMapPair(BiMap<K, V> original) {
066      this.forward = original;
067      this.backward = original.inverse();
068    }
069
070    private static final long serialVersionUID = 0;
071  }
072
073  /**
074   * Returns {@link Method} instances for the tests that assume that the inverse will be the same
075   * after serialization.
076   */
077  @J2ktIncompatible
078  @GwtIncompatible // reflection
079  public static List<Method> getInverseSameAfterSerializingMethods() {
080    return Collections.singletonList(getMethod("testInverseSerialization"));
081  }
082
083  @J2ktIncompatible
084  @GwtIncompatible // reflection
085  private static Method getMethod(String methodName) {
086    return Helpers.getMethod(BiMapInverseTester.class, methodName);
087  }
088}