View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.BiMap;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.features.CollectionFeature;
26  import com.google.common.testing.SerializableTester;
27  import java.io.Serializable;
28  import java.lang.reflect.Method;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * Tests for the {@code inverse} view of a BiMap.
34   *
35   * <p>This assumes that {@code bimap.inverse().inverse() == bimap}, which is not technically
36   * required but is fulfilled by all current implementations.
37   *
38   * @author Louis Wasserman
39   */
40  @GwtCompatible(emulated = true)
41  public class BiMapInverseTester<K, V> extends AbstractBiMapTester<K, V> {
42  
43    public void testInverseSame() {
44      assertSame(getMap(), getMap().inverse().inverse());
45    }
46  
47    @CollectionFeature.Require(SERIALIZABLE)
48    public void testInverseSerialization() {
49      BiMapPair<K, V> pair = new BiMapPair<>(getMap());
50      BiMapPair<K, V> copy = SerializableTester.reserialize(pair);
51      assertEquals(pair.forward, copy.forward);
52      assertEquals(pair.backward, copy.backward);
53      assertSame(copy.backward, copy.forward.inverse());
54      assertSame(copy.forward, copy.backward.inverse());
55    }
56  
57    private static class BiMapPair<K, V> implements Serializable {
58      final BiMap<K, V> forward;
59      final BiMap<V, K> backward;
60  
61      BiMapPair(BiMap<K, V> original) {
62        this.forward = original;
63        this.backward = original.inverse();
64      }
65  
66      private static final long serialVersionUID = 0;
67    }
68  
69    /**
70     * Returns {@link Method} instances for the tests that assume that the inverse will be the same
71     * after serialization.
72     */
73    @GwtIncompatible // reflection
74    public static List<Method> getInverseSameAfterSerializingMethods() {
75      return Collections.singletonList(getMethod("testInverseSerialization"));
76    }
77  
78    @GwtIncompatible // reflection
79    private static Method getMethod(String methodName) {
80      return Helpers.getMethod(BiMapInverseTester.class, methodName);
81    }
82  }