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.collect.MultimapBuilder.MultimapBuilderWithKeys; 22 import java.io.ByteArrayInputStream; 23 import java.io.ByteArrayOutputStream; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.math.RoundingMode; 27 import java.util.SortedMap; 28 import java.util.SortedSet; 29 import junit.framework.TestCase; 30 31 /** 32 * Tests for {@link MultimapBuilder}. 33 * 34 * @author Louis Wasserman 35 */ 36 @GwtCompatible(emulated = true) 37 public class MultimapBuilderTest extends TestCase { 38 39 @GwtIncompatible // doesn't build without explicit type parameters on build() methods testGenerics()40 public void testGenerics() { 41 ListMultimap<String, Integer> a = MultimapBuilder.hashKeys().arrayListValues().build(); 42 SortedSetMultimap<String, Integer> b = MultimapBuilder.linkedHashKeys().treeSetValues().build(); 43 SetMultimap<String, Integer> c = 44 MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER).hashSetValues().build(); 45 } 46 testGenerics_gwtCompatible()47 public void testGenerics_gwtCompatible() { 48 ListMultimap<String, Integer> a = 49 MultimapBuilder.hashKeys().arrayListValues().<String, Integer>build(); 50 SortedSetMultimap<String, Integer> b = 51 MultimapBuilder.linkedHashKeys().treeSetValues().<String, Integer>build(); 52 SetMultimap<String, Integer> c = 53 MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER) 54 .hashSetValues() 55 .<String, Integer>build(); 56 } 57 58 @GwtIncompatible // doesn't build without explicit type parameters on build() methods testTreeKeys()59 public void testTreeKeys() { 60 ListMultimap<String, Integer> multimap = MultimapBuilder.treeKeys().arrayListValues().build(); 61 assertTrue(multimap.keySet() instanceof SortedSet); 62 assertTrue(multimap.asMap() instanceof SortedMap); 63 } 64 testTreeKeys_gwtCompatible()65 public void testTreeKeys_gwtCompatible() { 66 ListMultimap<String, Integer> multimap = 67 MultimapBuilder.treeKeys().arrayListValues().<String, Integer>build(); 68 assertTrue(multimap.keySet() instanceof SortedSet); 69 assertTrue(multimap.asMap() instanceof SortedMap); 70 } 71 72 @GwtIncompatible // serialization testSerialization()73 public void testSerialization() throws Exception { 74 for (MultimapBuilderWithKeys<?> builderWithKeys : 75 ImmutableList.of( 76 MultimapBuilder.hashKeys(), 77 MultimapBuilder.linkedHashKeys(), 78 MultimapBuilder.treeKeys(), 79 MultimapBuilder.enumKeys(RoundingMode.class))) { 80 for (MultimapBuilder<?, ?> builder : 81 ImmutableList.of( 82 builderWithKeys.arrayListValues(), 83 builderWithKeys.linkedListValues(), 84 builderWithKeys.hashSetValues(), 85 builderWithKeys.linkedHashSetValues(), 86 builderWithKeys.treeSetValues(), 87 builderWithKeys.enumSetValues(RoundingMode.class))) { 88 /* 89 * Temporarily inlining SerializableTester here for obscure internal reasons. 90 */ 91 reserializeAndAssert(builder.build()); 92 } 93 } 94 } 95 96 @GwtIncompatible // serialization reserializeAndAssert(Object object)97 private static void reserializeAndAssert(Object object) throws Exception { 98 Object copy = reserialize(object); 99 assertEquals(object, copy); 100 assertEquals(object.getClass(), copy.getClass()); 101 } 102 103 @GwtIncompatible // serialization reserialize(Object object)104 private static Object reserialize(Object object) throws Exception { 105 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 106 new ObjectOutputStream(bytes).writeObject(object); 107 return new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())).readObject(); 108 } 109 } 110