• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.collect;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.base.Joiner;
24 import com.google.common.collect.ImmutableBiMap.Builder;
25 import com.google.common.collect.testing.MapInterfaceTest;
26 import com.google.common.collect.testing.features.CollectionFeature;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import com.google.common.collect.testing.features.MapFeature;
29 import com.google.common.collect.testing.google.BiMapGenerators.ImmutableBiMapCopyOfEntriesGenerator;
30 import com.google.common.collect.testing.google.BiMapGenerators.ImmutableBiMapCopyOfGenerator;
31 import com.google.common.collect.testing.google.BiMapGenerators.ImmutableBiMapGenerator;
32 import com.google.common.collect.testing.google.BiMapInverseTester;
33 import com.google.common.collect.testing.google.BiMapTestSuiteBuilder;
34 import com.google.common.testing.SerializableTester;
35 import java.util.AbstractMap;
36 import java.util.Collections;
37 import java.util.LinkedHashMap;
38 import java.util.Map;
39 import java.util.Map.Entry;
40 import java.util.Set;
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
44 
45 /**
46  * Tests for {@link ImmutableBiMap}.
47  *
48  * @author Jared Levy
49  */
50 @GwtCompatible(emulated = true)
51 public class ImmutableBiMapTest extends TestCase {
52 
53   // TODO: Reduce duplication of ImmutableMapTest code
54 
55   @GwtIncompatible // suite
suite()56   public static Test suite() {
57     TestSuite suite = new TestSuite();
58 
59     suite.addTestSuite(MapTests.class);
60     suite.addTestSuite(InverseMapTests.class);
61     suite.addTestSuite(CreationTests.class);
62     suite.addTestSuite(BiMapSpecificTests.class);
63 
64     suite.addTest(
65         BiMapTestSuiteBuilder.using(new ImmutableBiMapGenerator())
66             .named("ImmutableBiMap")
67             .withFeatures(
68                 CollectionSize.ANY,
69                 CollectionFeature.SERIALIZABLE,
70                 CollectionFeature.KNOWN_ORDER,
71                 MapFeature.REJECTS_DUPLICATES_AT_CREATION,
72                 MapFeature.ALLOWS_ANY_NULL_QUERIES)
73             .suppressing(BiMapInverseTester.getInverseSameAfterSerializingMethods())
74             .createTestSuite());
75     suite.addTest(
76         BiMapTestSuiteBuilder.using(new ImmutableBiMapCopyOfGenerator())
77             .named("ImmutableBiMap.copyOf[Map]")
78             .withFeatures(
79                 CollectionSize.ANY,
80                 CollectionFeature.SERIALIZABLE,
81                 CollectionFeature.KNOWN_ORDER,
82                 MapFeature.ALLOWS_ANY_NULL_QUERIES)
83             .suppressing(BiMapInverseTester.getInverseSameAfterSerializingMethods())
84             .createTestSuite());
85     suite.addTest(
86         BiMapTestSuiteBuilder.using(new ImmutableBiMapCopyOfEntriesGenerator())
87             .named("ImmutableBiMap.copyOf[Iterable<Entry>]")
88             .withFeatures(
89                 CollectionSize.ANY,
90                 CollectionFeature.SERIALIZABLE,
91                 CollectionFeature.KNOWN_ORDER,
92                 MapFeature.REJECTS_DUPLICATES_AT_CREATION,
93                 MapFeature.ALLOWS_ANY_NULL_QUERIES)
94             .suppressing(BiMapInverseTester.getInverseSameAfterSerializingMethods())
95             .createTestSuite());
96 
97     return suite;
98   }
99 
100   public abstract static class AbstractMapTests<K, V> extends MapInterfaceTest<K, V> {
AbstractMapTests()101     public AbstractMapTests() {
102       super(false, false, false, false, false);
103     }
104 
105     @Override
makeEmptyMap()106     protected Map<K, V> makeEmptyMap() {
107       throw new UnsupportedOperationException();
108     }
109 
110     private static final Joiner joiner = Joiner.on(", ");
111 
112     @Override
assertMoreInvariants(Map<K, V> map)113     protected void assertMoreInvariants(Map<K, V> map) {
114 
115       BiMap<K, V> bimap = (BiMap<K, V>) map;
116 
117       for (Entry<K, V> entry : map.entrySet()) {
118         assertEquals(entry.getKey() + "=" + entry.getValue(), entry.toString());
119         assertEquals(entry.getKey(), bimap.inverse().get(entry.getValue()));
120       }
121 
122       assertEquals("{" + joiner.join(map.entrySet()) + "}", map.toString());
123       assertEquals("[" + joiner.join(map.entrySet()) + "]", map.entrySet().toString());
124       assertEquals("[" + joiner.join(map.keySet()) + "]", map.keySet().toString());
125       assertEquals("[" + joiner.join(map.values()) + "]", map.values().toString());
126 
127       assertEquals(Sets.newHashSet(map.entrySet()), map.entrySet());
128       assertEquals(Sets.newHashSet(map.keySet()), map.keySet());
129     }
130   }
131 
132   public static class MapTests extends AbstractMapTests<String, Integer> {
133     @Override
makeEmptyMap()134     protected Map<String, Integer> makeEmptyMap() {
135       return ImmutableBiMap.of();
136     }
137 
138     @Override
makePopulatedMap()139     protected Map<String, Integer> makePopulatedMap() {
140       return ImmutableBiMap.of("one", 1, "two", 2, "three", 3);
141     }
142 
143     @Override
getKeyNotInPopulatedMap()144     protected String getKeyNotInPopulatedMap() {
145       return "minus one";
146     }
147 
148     @Override
getValueNotInPopulatedMap()149     protected Integer getValueNotInPopulatedMap() {
150       return -1;
151     }
152   }
153 
154   public static class InverseMapTests extends AbstractMapTests<String, Integer> {
155     @Override
makeEmptyMap()156     protected Map<String, Integer> makeEmptyMap() {
157       return ImmutableBiMap.of();
158     }
159 
160     @Override
makePopulatedMap()161     protected Map<String, Integer> makePopulatedMap() {
162       return ImmutableBiMap.of(1, "one", 2, "two", 3, "three").inverse();
163     }
164 
165     @Override
getKeyNotInPopulatedMap()166     protected String getKeyNotInPopulatedMap() {
167       return "minus one";
168     }
169 
170     @Override
getValueNotInPopulatedMap()171     protected Integer getValueNotInPopulatedMap() {
172       return -1;
173     }
174   }
175 
176   public static class CreationTests extends TestCase {
testEmptyBuilder()177     public void testEmptyBuilder() {
178       ImmutableBiMap<String, Integer> map = new Builder<String, Integer>().build();
179       assertEquals(Collections.<String, Integer>emptyMap(), map);
180       assertEquals(Collections.<Integer, String>emptyMap(), map.inverse());
181       assertSame(ImmutableBiMap.of(), map);
182     }
183 
testSingletonBuilder()184     public void testSingletonBuilder() {
185       ImmutableBiMap<String, Integer> map = new Builder<String, Integer>().put("one", 1).build();
186       assertMapEquals(map, "one", 1);
187       assertMapEquals(map.inverse(), 1, "one");
188     }
189 
testBuilder_withImmutableEntry()190     public void testBuilder_withImmutableEntry() {
191       ImmutableBiMap<String, Integer> map =
192           new Builder<String, Integer>().put(Maps.immutableEntry("one", 1)).build();
193       assertMapEquals(map, "one", 1);
194     }
195 
testBuilder()196     public void testBuilder() {
197       ImmutableBiMap<String, Integer> map =
198           ImmutableBiMap.<String, Integer>builder()
199               .put("one", 1)
200               .put("two", 2)
201               .put("three", 3)
202               .put("four", 4)
203               .put("five", 5)
204               .build();
205       assertMapEquals(map, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
206       assertMapEquals(map.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five");
207     }
208 
209     @GwtIncompatible
testBuilderExactlySizedReusesArray()210     public void testBuilderExactlySizedReusesArray() {
211       ImmutableBiMap.Builder<Integer, Integer> builder = ImmutableBiMap.builderWithExpectedSize(10);
212       Object[] builderArray = builder.alternatingKeysAndValues;
213       for (int i = 0; i < 10; i++) {
214         builder.put(i, i);
215       }
216       Object[] builderArrayAfterPuts = builder.alternatingKeysAndValues;
217       RegularImmutableBiMap<Integer, Integer> map =
218           (RegularImmutableBiMap<Integer, Integer>) builder.build();
219       Object[] mapInternalArray = map.alternatingKeysAndValues;
220       assertSame(builderArray, builderArrayAfterPuts);
221       assertSame(builderArray, mapInternalArray);
222     }
223 
testBuilder_orderEntriesByValue()224     public void testBuilder_orderEntriesByValue() {
225       ImmutableBiMap<String, Integer> map =
226           ImmutableBiMap.<String, Integer>builder()
227               .orderEntriesByValue(Ordering.natural())
228               .put("three", 3)
229               .put("one", 1)
230               .put("five", 5)
231               .put("four", 4)
232               .put("two", 2)
233               .build();
234       assertMapEquals(map, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
235       assertMapEquals(map.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five");
236     }
237 
testBuilder_orderEntriesByValueAfterExactSizeBuild()238     public void testBuilder_orderEntriesByValueAfterExactSizeBuild() {
239       ImmutableBiMap.Builder<String, Integer> builder =
240           new ImmutableBiMap.Builder<String, Integer>(2).put("four", 4).put("one", 1);
241       ImmutableMap<String, Integer> keyOrdered = builder.build();
242       ImmutableMap<String, Integer> valueOrdered =
243           builder.orderEntriesByValue(Ordering.natural()).build();
244       assertMapEquals(keyOrdered, "four", 4, "one", 1);
245       assertMapEquals(valueOrdered, "one", 1, "four", 4);
246     }
247 
testBuilder_orderEntriesByValue_usedTwiceFails()248     public void testBuilder_orderEntriesByValue_usedTwiceFails() {
249       ImmutableBiMap.Builder<String, Integer> builder =
250           new Builder<String, Integer>().orderEntriesByValue(Ordering.natural());
251       try {
252         builder.orderEntriesByValue(Ordering.natural());
253         fail("Expected IllegalStateException");
254       } catch (IllegalStateException expected) {
255       }
256     }
257 
testBuilderPutAllWithEmptyMap()258     public void testBuilderPutAllWithEmptyMap() {
259       ImmutableBiMap<String, Integer> map =
260           new Builder<String, Integer>().putAll(Collections.<String, Integer>emptyMap()).build();
261       assertEquals(Collections.<String, Integer>emptyMap(), map);
262     }
263 
testBuilderPutAll()264     public void testBuilderPutAll() {
265       Map<String, Integer> toPut = new LinkedHashMap<>();
266       toPut.put("one", 1);
267       toPut.put("two", 2);
268       toPut.put("three", 3);
269       Map<String, Integer> moreToPut = new LinkedHashMap<>();
270       moreToPut.put("four", 4);
271       moreToPut.put("five", 5);
272 
273       ImmutableBiMap<String, Integer> map =
274           new Builder<String, Integer>().putAll(toPut).putAll(moreToPut).build();
275       assertMapEquals(map, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
276       assertMapEquals(map.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five");
277     }
278 
testBuilderReuse()279     public void testBuilderReuse() {
280       Builder<String, Integer> builder = new Builder<>();
281       ImmutableBiMap<String, Integer> mapOne = builder.put("one", 1).put("two", 2).build();
282       ImmutableBiMap<String, Integer> mapTwo = builder.put("three", 3).put("four", 4).build();
283 
284       assertMapEquals(mapOne, "one", 1, "two", 2);
285       assertMapEquals(mapOne.inverse(), 1, "one", 2, "two");
286       assertMapEquals(mapTwo, "one", 1, "two", 2, "three", 3, "four", 4);
287       assertMapEquals(mapTwo.inverse(), 1, "one", 2, "two", 3, "three", 4, "four");
288     }
289 
testBuilderPutNullKey()290     public void testBuilderPutNullKey() {
291       Builder<String, Integer> builder = new Builder<>();
292       try {
293         builder.put(null, 1);
294         fail();
295       } catch (NullPointerException expected) {
296       }
297     }
298 
testBuilderPutNullValue()299     public void testBuilderPutNullValue() {
300       Builder<String, Integer> builder = new Builder<>();
301       try {
302         builder.put("one", null);
303         fail();
304       } catch (NullPointerException expected) {
305       }
306     }
307 
testBuilderPutNullKeyViaPutAll()308     public void testBuilderPutNullKeyViaPutAll() {
309       Builder<String, Integer> builder = new Builder<>();
310       try {
311         builder.putAll(Collections.<String, Integer>singletonMap(null, 1));
312         fail();
313       } catch (NullPointerException expected) {
314       }
315     }
316 
testBuilderPutNullValueViaPutAll()317     public void testBuilderPutNullValueViaPutAll() {
318       Builder<String, Integer> builder = new Builder<>();
319       try {
320         builder.putAll(Collections.<String, Integer>singletonMap("one", null));
321         fail();
322       } catch (NullPointerException expected) {
323       }
324     }
325 
testPuttingTheSameKeyTwiceThrowsOnBuild()326     public void testPuttingTheSameKeyTwiceThrowsOnBuild() {
327       Builder<String, Integer> builder =
328           new Builder<String, Integer>()
329               .put("one", 1)
330               .put("one", 1); // throwing on this line would be even better
331 
332       try {
333         builder.build();
334         fail();
335       } catch (IllegalArgumentException expected) {
336         assertThat(expected.getMessage()).contains("one");
337       }
338     }
339 
testOf()340     public void testOf() {
341       assertMapEquals(ImmutableBiMap.of("one", 1), "one", 1);
342       assertMapEquals(ImmutableBiMap.of("one", 1).inverse(), 1, "one");
343       assertMapEquals(ImmutableBiMap.of("one", 1, "two", 2), "one", 1, "two", 2);
344       assertMapEquals(ImmutableBiMap.of("one", 1, "two", 2).inverse(), 1, "one", 2, "two");
345       assertMapEquals(
346           ImmutableBiMap.of("one", 1, "two", 2, "three", 3), "one", 1, "two", 2, "three", 3);
347       assertMapEquals(
348           ImmutableBiMap.of("one", 1, "two", 2, "three", 3).inverse(),
349           1,
350           "one",
351           2,
352           "two",
353           3,
354           "three");
355       assertMapEquals(
356           ImmutableBiMap.of("one", 1, "two", 2, "three", 3, "four", 4),
357           "one",
358           1,
359           "two",
360           2,
361           "three",
362           3,
363           "four",
364           4);
365       assertMapEquals(
366           ImmutableBiMap.of("one", 1, "two", 2, "three", 3, "four", 4).inverse(),
367           1,
368           "one",
369           2,
370           "two",
371           3,
372           "three",
373           4,
374           "four");
375       assertMapEquals(
376           ImmutableBiMap.of("one", 1, "two", 2, "three", 3, "four", 4, "five", 5),
377           "one",
378           1,
379           "two",
380           2,
381           "three",
382           3,
383           "four",
384           4,
385           "five",
386           5);
387       assertMapEquals(
388           ImmutableBiMap.of("one", 1, "two", 2, "three", 3, "four", 4, "five", 5).inverse(),
389           1,
390           "one",
391           2,
392           "two",
393           3,
394           "three",
395           4,
396           "four",
397           5,
398           "five");
399       assertMapEquals(
400           ImmutableBiMap.of(
401               "one", 1,
402               "two", 2,
403               "three", 3,
404               "four", 4,
405               "five", 5,
406               "six", 6),
407           "one",
408           1,
409           "two",
410           2,
411           "three",
412           3,
413           "four",
414           4,
415           "five",
416           5,
417           "six",
418           6);
419       assertMapEquals(
420           ImmutableBiMap.of(
421               "one", 1,
422               "two", 2,
423               "three", 3,
424               "four", 4,
425               "five", 5,
426               "six", 6,
427               "seven", 7),
428           "one",
429           1,
430           "two",
431           2,
432           "three",
433           3,
434           "four",
435           4,
436           "five",
437           5,
438           "six",
439           6,
440           "seven",
441           7);
442       assertMapEquals(
443           ImmutableBiMap.of(
444               "one", 1,
445               "two", 2,
446               "three", 3,
447               "four", 4,
448               "five", 5,
449               "six", 6,
450               "seven", 7,
451               "eight", 8),
452           "one",
453           1,
454           "two",
455           2,
456           "three",
457           3,
458           "four",
459           4,
460           "five",
461           5,
462           "six",
463           6,
464           "seven",
465           7,
466           "eight",
467           8);
468       assertMapEquals(
469           ImmutableBiMap.of(
470               "one", 1,
471               "two", 2,
472               "three", 3,
473               "four", 4,
474               "five", 5,
475               "six", 6,
476               "seven", 7,
477               "eight", 8,
478               "nine", 9),
479           "one",
480           1,
481           "two",
482           2,
483           "three",
484           3,
485           "four",
486           4,
487           "five",
488           5,
489           "six",
490           6,
491           "seven",
492           7,
493           "eight",
494           8,
495           "nine",
496           9);
497       assertMapEquals(
498           ImmutableBiMap.of(
499               "one", 1,
500               "two", 2,
501               "three", 3,
502               "four", 4,
503               "five", 5,
504               "six", 6,
505               "seven", 7,
506               "eight", 8,
507               "nine", 9,
508               "ten", 10),
509           "one",
510           1,
511           "two",
512           2,
513           "three",
514           3,
515           "four",
516           4,
517           "five",
518           5,
519           "six",
520           6,
521           "seven",
522           7,
523           "eight",
524           8,
525           "nine",
526           9,
527           "ten",
528           10);
529     }
530 
testOfNullKey()531     public void testOfNullKey() {
532       try {
533         ImmutableBiMap.of(null, 1);
534         fail();
535       } catch (NullPointerException expected) {
536       }
537 
538       try {
539         ImmutableBiMap.of("one", 1, null, 2);
540         fail();
541       } catch (NullPointerException expected) {
542       }
543     }
544 
testOfNullValue()545     public void testOfNullValue() {
546       try {
547         ImmutableBiMap.of("one", null);
548         fail();
549       } catch (NullPointerException expected) {
550       }
551 
552       try {
553         ImmutableBiMap.of("one", 1, "two", null);
554         fail();
555       } catch (NullPointerException expected) {
556       }
557     }
558 
testOfWithDuplicateKey()559     public void testOfWithDuplicateKey() {
560       try {
561         ImmutableBiMap.of("one", 1, "one", 1);
562         fail();
563       } catch (IllegalArgumentException expected) {
564         assertThat(expected.getMessage()).contains("one");
565       }
566     }
567 
testOfEntries()568     public void testOfEntries() {
569       assertMapEquals(
570           ImmutableBiMap.ofEntries(entry("one", 1), entry("two", 2)), "one", 1, "two", 2);
571     }
572 
testOfEntriesNull()573     public void testOfEntriesNull() {
574       Entry<Integer, Integer> nullKey = entry(null, 23);
575       try {
576         ImmutableBiMap.ofEntries(nullKey);
577         fail();
578       } catch (NullPointerException expected) {
579       }
580       Entry<Integer, Integer> nullValue = entry(23, null);
581       try {
582         ImmutableBiMap.ofEntries(nullValue);
583         fail();
584       } catch (NullPointerException expected) {
585       }
586     }
587 
entry(T key, T value)588     private static <T> Entry<T, T> entry(T key, T value) {
589       return new AbstractMap.SimpleImmutableEntry<>(key, value);
590     }
591 
testCopyOfEmptyMap()592     public void testCopyOfEmptyMap() {
593       ImmutableBiMap<String, Integer> copy =
594           ImmutableBiMap.copyOf(Collections.<String, Integer>emptyMap());
595       assertEquals(Collections.<String, Integer>emptyMap(), copy);
596       assertSame(copy, ImmutableBiMap.copyOf(copy));
597       assertSame(ImmutableBiMap.of(), copy);
598     }
599 
testCopyOfSingletonMap()600     public void testCopyOfSingletonMap() {
601       ImmutableBiMap<String, Integer> copy =
602           ImmutableBiMap.copyOf(Collections.singletonMap("one", 1));
603       assertMapEquals(copy, "one", 1);
604       assertSame(copy, ImmutableBiMap.copyOf(copy));
605     }
606 
testCopyOf()607     public void testCopyOf() {
608       Map<String, Integer> original = new LinkedHashMap<>();
609       original.put("one", 1);
610       original.put("two", 2);
611       original.put("three", 3);
612 
613       ImmutableBiMap<String, Integer> copy = ImmutableBiMap.copyOf(original);
614       assertMapEquals(copy, "one", 1, "two", 2, "three", 3);
615       assertSame(copy, ImmutableBiMap.copyOf(copy));
616     }
617 
testEmpty()618     public void testEmpty() {
619       ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.of();
620       assertEquals(Collections.<String, Integer>emptyMap(), bimap);
621       assertEquals(Collections.<String, Integer>emptyMap(), bimap.inverse());
622     }
623 
testFromHashMap()624     public void testFromHashMap() {
625       Map<String, Integer> hashMap = Maps.newLinkedHashMap();
626       hashMap.put("one", 1);
627       hashMap.put("two", 2);
628       ImmutableBiMap<String, Integer> bimap =
629           ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2));
630       assertMapEquals(bimap, "one", 1, "two", 2);
631       assertMapEquals(bimap.inverse(), 1, "one", 2, "two");
632     }
633 
testFromImmutableMap()634     public void testFromImmutableMap() {
635       ImmutableBiMap<String, Integer> bimap =
636           ImmutableBiMap.copyOf(
637               new ImmutableMap.Builder<String, Integer>()
638                   .put("one", 1)
639                   .put("two", 2)
640                   .put("three", 3)
641                   .put("four", 4)
642                   .put("five", 5)
643                   .build());
644       assertMapEquals(bimap, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
645       assertMapEquals(bimap.inverse(), 1, "one", 2, "two", 3, "three", 4, "four", 5, "five");
646     }
647 
testDuplicateValues()648     public void testDuplicateValues() {
649       ImmutableMap<String, Integer> map =
650           new ImmutableMap.Builder<String, Integer>()
651               .put("one", 1)
652               .put("two", 2)
653               .put("uno", 1)
654               .put("dos", 2)
655               .build();
656 
657       try {
658         ImmutableBiMap.copyOf(map);
659         fail();
660       } catch (IllegalArgumentException expected) {
661         assertThat(expected.getMessage()).contains("1");
662       }
663     }
664 
665     // TODO(b/172823566): Use mainline testToImmutableBiMap once CollectorTester is usable to java7.
testToImmutableBiMap_java7_combine()666     public void testToImmutableBiMap_java7_combine() {
667       ImmutableBiMap.Builder<String, Integer> zis =
668           ImmutableBiMap.<String, Integer>builder().put("one", 1);
669       ImmutableBiMap.Builder<String, Integer> zat =
670           ImmutableBiMap.<String, Integer>builder().put("two", 2).put("three", 3);
671       ImmutableBiMap<String, Integer> biMap = zis.combine(zat).build();
672       assertMapEquals(biMap, "one", 1, "two", 2, "three", 3);
673     }
674 
675     // TODO(b/172823566): Use mainline testToImmutableBiMap once CollectorTester is usable to java7.
testToImmutableBiMap_exceptionOnDuplicateKey_java7_combine()676     public void testToImmutableBiMap_exceptionOnDuplicateKey_java7_combine() {
677       ImmutableBiMap.Builder<String, Integer> zis =
678           ImmutableBiMap.<String, Integer>builder().put("one", 1).put("two", 2);
679       ImmutableBiMap.Builder<String, Integer> zat =
680           ImmutableBiMap.<String, Integer>builder().put("two", 22).put("three", 3);
681       try {
682         zis.combine(zat).build();
683         fail("Expected IllegalArgumentException");
684       } catch (IllegalArgumentException expected) {
685         // expected
686       }
687     }
688   }
689 
690   public static class BiMapSpecificTests extends TestCase {
691 
testForcePut()692     public void testForcePut() {
693       BiMap<String, Integer> bimap = ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2));
694       try {
695         bimap.forcePut("three", 3);
696         fail();
697       } catch (UnsupportedOperationException expected) {
698       }
699     }
700 
testKeySet()701     public void testKeySet() {
702       ImmutableBiMap<String, Integer> bimap =
703           ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4));
704       Set<String> keys = bimap.keySet();
705       assertEquals(Sets.newHashSet("one", "two", "three", "four"), keys);
706       assertThat(keys).containsExactly("one", "two", "three", "four").inOrder();
707     }
708 
testValues()709     public void testValues() {
710       ImmutableBiMap<String, Integer> bimap =
711           ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4));
712       Set<Integer> values = bimap.values();
713       assertEquals(Sets.newHashSet(1, 2, 3, 4), values);
714       assertThat(values).containsExactly(1, 2, 3, 4).inOrder();
715     }
716 
testDoubleInverse()717     public void testDoubleInverse() {
718       ImmutableBiMap<String, Integer> bimap =
719           ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2));
720       assertSame(bimap, bimap.inverse().inverse());
721     }
722 
723     @GwtIncompatible // SerializableTester
testEmptySerialization()724     public void testEmptySerialization() {
725       ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.of();
726       assertSame(bimap, SerializableTester.reserializeAndAssert(bimap));
727     }
728 
729     @GwtIncompatible // SerializableTester
testSerialization()730     public void testSerialization() {
731       ImmutableBiMap<String, Integer> bimap =
732           ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2));
733       ImmutableBiMap<String, Integer> copy = SerializableTester.reserializeAndAssert(bimap);
734       assertEquals(Integer.valueOf(1), copy.get("one"));
735       assertEquals("one", copy.inverse().get(1));
736       assertSame(copy, copy.inverse().inverse());
737     }
738 
739     @GwtIncompatible // SerializableTester
testInverseSerialization()740     public void testInverseSerialization() {
741       ImmutableBiMap<String, Integer> bimap =
742           ImmutableBiMap.copyOf(ImmutableMap.of(1, "one", 2, "two")).inverse();
743       ImmutableBiMap<String, Integer> copy = SerializableTester.reserializeAndAssert(bimap);
744       assertEquals(Integer.valueOf(1), copy.get("one"));
745       assertEquals("one", copy.inverse().get(1));
746       assertSame(copy, copy.inverse().inverse());
747     }
748   }
749 
assertMapEquals(Map<K, V> map, Object... alternatingKeysAndValues)750   private static <K, V> void assertMapEquals(Map<K, V> map, Object... alternatingKeysAndValues) {
751     int i = 0;
752     for (Entry<K, V> entry : map.entrySet()) {
753       assertEquals(alternatingKeysAndValues[i++], entry.getKey());
754       assertEquals(alternatingKeysAndValues[i++], entry.getValue());
755     }
756   }
757 
758   /** No-op test so that the class has at least one method, making Maven's test runner happy. */
testNoop()759   public void testNoop() {}
760 }
761