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