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; 020import static com.google.common.testing.SerializableTester.reserialize; 021import static java.util.Collections.singletonList; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.annotations.J2ktIncompatible; 026import com.google.common.collect.BiMap; 027import com.google.common.collect.testing.Helpers; 028import com.google.common.collect.testing.features.CollectionFeature; 029import java.io.Serializable; 030import java.lang.reflect.Method; 031import java.util.List; 032 033/** 034 * Tests for the {@code inverse} view of a BiMap. 035 * 036 * @author Louis Wasserman 037 */ 038@GwtCompatible 039public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> { 040 041 // View caching is not required but was historically provided by some of our implementations. 042 public void testInverseSame() { 043 assertSame(getMap(), getMap().inverse().inverse()); 044 } 045 046 // View caching is not required but was historically provided by some of our implementations. 047 @CollectionFeature.Require(SERIALIZABLE) 048 public void testInverseSerialization() { 049 BiMapPair<K, V> pair = new BiMapPair<>(getMap()); 050 BiMapPair<K, V> copy = reserialize(pair); 051 assertEquals(pair.forward, copy.forward); 052 assertEquals(pair.backward, copy.backward); 053 assertSame(copy.backward, copy.forward.inverse()); 054 assertSame(copy.forward, copy.backward.inverse()); 055 } 056 057 /** 058 * @since 33.7.0 059 */ 060 public void testInverseEquals() { 061 assertEquals(getMap(), getMap().inverse().inverse()); 062 } 063 064 /** 065 * @since 33.7.0 066 */ 067 @CollectionFeature.Require(SERIALIZABLE) 068 public void testInverseSerializationEquals() { 069 BiMapPair<K, V> pair = new BiMapPair<>(getMap()); 070 BiMapPair<K, V> copy = reserialize(pair); 071 assertEquals(pair.forward, copy.forward); 072 assertEquals(pair.backward, copy.backward); 073 assertEquals(copy.backward, copy.forward.inverse()); 074 assertEquals(copy.forward, copy.backward.inverse()); 075 } 076 077 private static final class BiMapPair<K, V> implements Serializable { 078 final BiMap<K, V> forward; 079 final BiMap<V, K> backward; 080 081 BiMapPair(BiMap<K, V> original) { 082 this.forward = original; 083 this.backward = original.inverse(); 084 } 085 086 @GwtIncompatible @J2ktIncompatible private static final long serialVersionUID = 0; 087 } 088 089 /** 090 * Returns {@link Method} instance for {@link #testInverseSame()} so that tests can suppress it 091 * with {@code FeatureSpecificTestSuiteBuilder.suppressing()}. 092 * 093 * @since 33.7.0 094 */ 095 @J2ktIncompatible 096 @GwtIncompatible // reflection 097 public static Method getInverseSameMethod() { 098 return getMethod("testInverseSame"); 099 } 100 101 /** 102 * Returns {@link Method} instances for the tests that assume that the inverse will be the same 103 * after serialization. 104 */ 105 @J2ktIncompatible 106 @GwtIncompatible // reflection 107 public static List<Method> getInverseSameAfterSerializingMethods() { 108 return singletonList(getMethod("testInverseSerialization")); 109 } 110 111 @J2ktIncompatible 112 @GwtIncompatible // reflection 113 private static Method getMethod(String methodName) { 114 return Helpers.getMethod(BiMapInverseTester.class, methodName); 115 } 116}