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