1/*
2 * Copyright (C) 2008 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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.collect.testing.MapInterfaceTest;
21
22import junit.framework.TestCase;
23
24import java.util.Map;
25import java.util.Map.Entry;
26
27/**
28 * Map interface tests for bimaps.
29 *
30 * @author Jared Levy
31 */
32@GwtCompatible
33public class BiMapMapInterfaceTest extends TestCase {
34
35  private abstract static class AbstractMapInterfaceTest
36      extends MapInterfaceTest<String, Integer> {
37
38    protected AbstractMapInterfaceTest(boolean modifiable) {
39      super(true, true, modifiable, modifiable, modifiable);
40    }
41
42    @Override protected String getKeyNotInPopulatedMap() {
43      return "cat";
44    }
45
46    @Override protected Integer getValueNotInPopulatedMap() {
47      return 3;
48    }
49
50    @Override protected Map<String, Integer> makeEmptyMap() {
51      return HashBiMap.create();
52    }
53
54    @Override protected Map<String, Integer> makePopulatedMap() {
55      Map<String, Integer> map = makeEmptyMap();
56      map.put("foo", 1);
57      map.put("bar", 2);
58      return map;
59    }
60
61    @Override protected void assertMoreInvariants(Map<String, Integer> map) {
62      BiMap<String, Integer> bimap = (BiMap<String, Integer>) map;
63      BiMap<Integer, String> inverse = bimap.inverse();
64      assertEquals(bimap.size(), inverse.size());
65      for (Entry<String, Integer> entry : bimap.entrySet()) {
66        assertEquals(entry.getKey(), inverse.get(entry.getValue()));
67      }
68      for (Entry<Integer, String> entry : inverse.entrySet()) {
69        assertEquals(entry.getKey(), bimap.get(entry.getValue()));
70      }
71    }
72  }
73
74  public static class HashBiMapInterfaceTest extends AbstractMapInterfaceTest {
75    public HashBiMapInterfaceTest() {
76      super(true);
77    }
78    @Override protected Map<String, Integer> makeEmptyMap() {
79      return HashBiMap.create();
80    }
81  }
82
83  public static class InverseBiMapInterfaceTest
84      extends AbstractMapInterfaceTest {
85    public InverseBiMapInterfaceTest() {
86      super(true);
87    }
88    @Override protected Map<String, Integer> makeEmptyMap() {
89      return HashBiMap.<Integer, String>create().inverse();
90    }
91  }
92
93  public static class UnmodifiableBiMapInterfaceTest
94      extends AbstractMapInterfaceTest {
95    public UnmodifiableBiMapInterfaceTest() {
96      super(false);
97    }
98    @Override protected Map<String, Integer> makeEmptyMap() {
99      return Maps.unmodifiableBiMap(HashBiMap.<String, Integer>create());
100    }
101    @Override protected Map<String, Integer> makePopulatedMap() {
102      BiMap<String, Integer> bimap = HashBiMap.create();
103      bimap.put("foo", 1);
104      bimap.put("bar", 2);
105      return Maps.unmodifiableBiMap(bimap);
106    }
107  }
108
109  public static class SynchronizedBiMapInterfaceTest
110      extends AbstractMapInterfaceTest {
111    public SynchronizedBiMapInterfaceTest() {
112      super(true);
113    }
114    @Override protected Map<String, Integer> makeEmptyMap() {
115      return Maps.synchronizedBiMap(HashBiMap.<String, Integer>create());
116    }
117  }
118
119  public void testNothing() {
120    /*
121     * It's a warning if a TestCase subclass contains no tests, so we add one.
122     * Alternatively, we could stop extending TestCase, but I worry that someone
123     * will add a test in the future and not realize that it's being ignored.
124     */
125  }
126}
127