• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.annotations.J2ktIncompatible;
22 import com.google.common.collect.MultimapBuilder.MultimapBuilderWithKeys;
23 import com.google.common.collect.MultimapBuilder.SortedSetMultimapBuilder;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.math.RoundingMode;
29 import java.util.SortedMap;
30 import java.util.SortedSet;
31 import junit.framework.TestCase;
32 import org.checkerframework.checker.nullness.qual.Nullable;
33 
34 /**
35  * Tests for {@link MultimapBuilder}.
36  *
37  * @author Louis Wasserman
38  */
39 @GwtCompatible(emulated = true)
40 @ElementTypesAreNonnullByDefault
41 public class MultimapBuilderTest extends TestCase {
42 
43   @J2ktIncompatible
44   @GwtIncompatible // doesn't build without explicit type parameters on build() methods
testGenerics()45   public void testGenerics() {
46     ListMultimap<String, Integer> unusedA = MultimapBuilder.hashKeys().arrayListValues().build();
47     SortedSetMultimap<String, Integer> unusedB =
48         MultimapBuilder.linkedHashKeys().treeSetValues().build();
49     SetMultimap<String, Integer> unusedC =
50         MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER).hashSetValues().build();
51   }
52 
testGenerics_gwtCompatible()53   public void testGenerics_gwtCompatible() {
54     ListMultimap<String, Integer> unusedA =
55         MultimapBuilder.hashKeys().arrayListValues().<String, Integer>build();
56     SortedSetMultimap<String, Integer> unusedB =
57         rawtypeToWildcard(MultimapBuilder.linkedHashKeys().treeSetValues())
58             .<String, Integer>build();
59     SetMultimap<String, Integer> unusedC =
60         MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
61             .hashSetValues()
62             .<String, Integer>build();
63   }
64 
65   @J2ktIncompatible
66   @GwtIncompatible // doesn't build without explicit type parameters on build() methods
testTreeKeys()67   public void testTreeKeys() {
68     ListMultimap<String, Integer> multimap = MultimapBuilder.treeKeys().arrayListValues().build();
69     assertTrue(multimap.keySet() instanceof SortedSet);
70     assertTrue(multimap.asMap() instanceof SortedMap);
71   }
72 
testTreeKeys_gwtCompatible()73   public void testTreeKeys_gwtCompatible() {
74     ListMultimap<String, Integer> multimap =
75         rawtypeToWildcard(MultimapBuilder.treeKeys()).arrayListValues().<String, Integer>build();
76     assertTrue(multimap.keySet() instanceof SortedSet);
77     assertTrue(multimap.asMap() instanceof SortedMap);
78   }
79 
80   // J2kt cannot translate the Comparable rawtype in a usable way (it becomes Comparable<Object>
81   // but types are typically only Comparable to themselves).
82   @SuppressWarnings({"rawtypes", "unchecked"})
rawtypeToWildcard( MultimapBuilderWithKeys<Comparable> treeKeys)83   private static MultimapBuilderWithKeys<Comparable<?>> rawtypeToWildcard(
84       MultimapBuilderWithKeys<Comparable> treeKeys) {
85     return (MultimapBuilderWithKeys) treeKeys;
86   }
87 
88   @SuppressWarnings({"rawtypes", "unchecked"})
89   private static <K extends @Nullable Object>
rawtypeToWildcard( SortedSetMultimapBuilder<K, Comparable> setMultimapBuilder)90       SortedSetMultimapBuilder<K, Comparable<?>> rawtypeToWildcard(
91           SortedSetMultimapBuilder<K, Comparable> setMultimapBuilder) {
92     return (SortedSetMultimapBuilder) setMultimapBuilder;
93   }
94 
95   @J2ktIncompatible
96   @GwtIncompatible // serialization
testSerialization()97   public void testSerialization() throws Exception {
98     for (MultimapBuilderWithKeys<?> builderWithKeys :
99         ImmutableList.of(
100             MultimapBuilder.hashKeys(),
101             MultimapBuilder.linkedHashKeys(),
102             MultimapBuilder.treeKeys(),
103             MultimapBuilder.enumKeys(RoundingMode.class))) {
104       for (MultimapBuilder<?, ?> builder :
105           ImmutableList.of(
106               builderWithKeys.arrayListValues(),
107               builderWithKeys.linkedListValues(),
108               builderWithKeys.hashSetValues(),
109               builderWithKeys.linkedHashSetValues(),
110               builderWithKeys.treeSetValues(),
111               builderWithKeys.enumSetValues(RoundingMode.class))) {
112         /*
113          * Temporarily inlining SerializableTester here for obscure internal reasons.
114          */
115         reserializeAndAssert(builder.build());
116       }
117     }
118   }
119 
120   @J2ktIncompatible
121   @GwtIncompatible // serialization
reserializeAndAssert(Object object)122   private static void reserializeAndAssert(Object object) throws Exception {
123     Object copy = reserialize(object);
124     assertEquals(object, copy);
125     assertEquals(object.getClass(), copy.getClass());
126   }
127 
128   @J2ktIncompatible
129   @GwtIncompatible // serialization
reserialize(Object object)130   private static Object reserialize(Object object) throws Exception {
131     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
132     new ObjectOutputStream(bytes).writeObject(object);
133     return new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())).readObject();
134   }
135 }
136