Home | History | Annotate | Download | only in collect
      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 com.google.common.testing.SerializableTester;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import java.math.RoundingMode;
     27 import java.util.SortedMap;
     28 import java.util.SortedSet;
     29 
     30 /**
     31  * Tests for {@link MultimapBuilder}.
     32  *
     33  * @author Louis Wasserman
     34  */
     35 @GwtCompatible(emulated = true)
     36 public class MultimapBuilderTest extends TestCase {
     37 
     38   @GwtIncompatible("doesn't build without explicit type parameters on build() methods")
     39   public void testGenerics() {
     40     ListMultimap<String, Integer> a = MultimapBuilder.hashKeys().arrayListValues().build();
     41     SortedSetMultimap<String, Integer> b = MultimapBuilder.linkedHashKeys().treeSetValues().build();
     42     SetMultimap<String, Integer> c =
     43         MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER).hashSetValues().build();
     44   }
     45 
     46   public void testGenerics_gwtCompatible() {
     47     ListMultimap<String, Integer> a =
     48         MultimapBuilder.hashKeys().arrayListValues().<String, Integer>build();
     49     SortedSetMultimap<String, Integer> b =
     50         MultimapBuilder.linkedHashKeys().treeSetValues().<String, Integer>build();
     51     SetMultimap<String, Integer> c = MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
     52         .hashSetValues().<String, Integer>build();
     53   }
     54 
     55   @GwtIncompatible("doesn't build without explicit type parameters on build() methods")
     56   public void testTreeKeys() {
     57     ListMultimap<String, Integer> multimap = MultimapBuilder.treeKeys().arrayListValues().build();
     58     assertTrue(multimap.keySet() instanceof SortedSet);
     59     assertTrue(multimap.asMap() instanceof SortedMap);
     60   }
     61 
     62   public void testTreeKeys_gwtCompatible() {
     63     ListMultimap<String, Integer> multimap =
     64         MultimapBuilder.treeKeys().arrayListValues().<String, Integer>build();
     65     assertTrue(multimap.keySet() instanceof SortedSet);
     66     assertTrue(multimap.asMap() instanceof SortedMap);
     67   }
     68 
     69   public void testSerialization() {
     70     for (MultimapBuilderWithKeys<?> builderWithKeys : ImmutableList.of(
     71         MultimapBuilder.hashKeys(), MultimapBuilder.linkedHashKeys(), MultimapBuilder.treeKeys(),
     72         MultimapBuilder.enumKeys(RoundingMode.class))) {
     73       for (MultimapBuilder<?, ?> builder : ImmutableList.of(
     74           builderWithKeys.arrayListValues(),
     75           builderWithKeys.linkedListValues(),
     76           builderWithKeys.hashSetValues(),
     77           builderWithKeys.linkedHashSetValues(),
     78           builderWithKeys.treeSetValues(),
     79           builderWithKeys.enumSetValues(RoundingMode.class))) {
     80         SerializableTester.reserializeAndAssert(builder.build());
     81       }
     82     }
     83   }
     84 }
     85