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