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.
044public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> {
045
046  public void testInverseSame() {
047    assertSame(getMap(), getMap().inverse().inverse());
048  }
049
050  @CollectionFeature.Require(SERIALIZABLE)
051  public void testInverseSerialization() {
052    BiMapPair<K, V> pair = new BiMapPair<>(getMap());
053    BiMapPair<K, V> copy = SerializableTester.reserialize(pair);
054    assertEquals(pair.forward, copy.forward);
055    assertEquals(pair.backward, copy.backward);
056    assertSame(copy.backward, copy.forward.inverse());
057    assertSame(copy.forward, copy.backward.inverse());
058  }
059
060  private static class BiMapPair<K, V> implements Serializable {
061    final BiMap<K, V> forward;
062    final BiMap<V, K> backward;
063
064    BiMapPair(BiMap<K, V> original) {
065      this.forward = original;
066      this.backward = original.inverse();
067    }
068
069    private static final long serialVersionUID = 0;
070  }
071
072  /**
073   * Returns {@link Method} instances for the tests that assume that the inverse will be the same
074   * after serialization.
075   */
076  @J2ktIncompatible
077  @GwtIncompatible // reflection
078  public static List<Method> getInverseSameAfterSerializingMethods() {
079    return Collections.singletonList(getMethod("testInverseSerialization"));
080  }
081
082  @J2ktIncompatible
083  @GwtIncompatible // reflection
084  private static Method getMethod(String methodName) {
085    return Helpers.getMethod(BiMapInverseTester.class, methodName);
086  }
087}