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