Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2010 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.testing;
     18 
     19 import static java.util.Collections.sort;
     20 
     21 import com.google.common.annotations.GwtIncompatible;
     22 import com.google.common.collect.ImmutableSortedMap;
     23 import com.google.common.collect.Lists;
     24 import com.google.common.collect.Ordering;
     25 import com.google.common.collect.testing.Helpers.NullsBeforeTwo;
     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.testing.SerializableTester;
     30 
     31 import junit.framework.Test;
     32 import junit.framework.TestCase;
     33 import junit.framework.TestSuite;
     34 
     35 import java.util.List;
     36 import java.util.Map;
     37 import java.util.Map.Entry;
     38 import java.util.NavigableMap;
     39 import java.util.SortedMap;
     40 
     41 /**
     42  * Tests for SafeTreeMap.
     43  *
     44  * @author Louis Wasserman
     45  */
     46 public class SafeTreeMapTest extends TestCase {
     47   public static Test suite() {
     48     TestSuite suite = new TestSuite();
     49     suite.addTestSuite(SafeTreeMapTest.class);
     50     suite.addTest(
     51         NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() {
     52           @Override protected SortedMap<String, String> create(
     53               Entry<String, String>[] entries) {
     54             NavigableMap<String, String> map =
     55                 new SafeTreeMap<String, String>(Ordering.natural());
     56             for (Entry<String, String> entry : entries) {
     57               map.put(entry.getKey(), entry.getValue());
     58             }
     59             return map;
     60           }
     61         }).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     62             CollectionFeature.SERIALIZABLE, MapFeature.ALLOWS_NULL_VALUES,
     63             CollectionFeature.SUPPORTS_ITERATOR_REMOVE, MapFeature.GENERAL_PURPOSE).named(
     64             "SafeTreeMap with natural comparator").createTestSuite());
     65     suite.addTest(NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() {
     66       @Override protected SortedMap<String, String> create(
     67           Entry<String, String>[] entries) {
     68         NavigableMap<String, String> map =
     69             new SafeTreeMap<String, String>(NullsBeforeTwo.INSTANCE);
     70         for (Entry<String, String> entry : entries) {
     71           map.put(entry.getKey(), entry.getValue());
     72         }
     73         return map;
     74       }
     75 
     76       @Override
     77       public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) {
     78         sort(insertionOrder, Helpers.<String, String>entryComparator(NullsBeforeTwo.INSTANCE));
     79         return insertionOrder;
     80       }
     81     }).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
     82         MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES,
     83         MapFeature.ALLOWS_ANY_NULL_QUERIES, MapFeature.GENERAL_PURPOSE,
     84         CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.SERIALIZABLE).named(
     85         "SafeTreeMap with null-friendly comparator").createTestSuite());
     86     return suite;
     87   }
     88 
     89   @GwtIncompatible("SerializableTester")
     90   public void testViewSerialization() {
     91     Map<String, Integer> map =
     92         ImmutableSortedMap.of("one", 1, "two", 2, "three", 3);
     93     SerializableTester.reserializeAndAssert(map.entrySet());
     94     SerializableTester.reserializeAndAssert(map.keySet());
     95     assertEquals(Lists.newArrayList(map.values()),
     96         Lists.newArrayList(SerializableTester.reserialize(map.values())));
     97   }
     98 
     99   @GwtIncompatible("SerializableTester")
    100   public static class ReserializedMapTests
    101       extends SortedMapInterfaceTest<String, Integer> {
    102     public ReserializedMapTests() {
    103       super(false, true, true, true, true);
    104     }
    105 
    106     @Override protected SortedMap<String, Integer> makePopulatedMap() {
    107       NavigableMap<String, Integer> map = new SafeTreeMap<String, Integer>();
    108       map.put("one", 1);
    109       map.put("two", 2);
    110       map.put("three", 3);
    111       return SerializableTester.reserialize(map);
    112     }
    113 
    114     @Override protected SortedMap<String, Integer> makeEmptyMap()
    115         throws UnsupportedOperationException {
    116       NavigableMap<String, Integer> map = new SafeTreeMap<String, Integer>();
    117       return SerializableTester.reserialize(map);
    118     }
    119 
    120     @Override protected String getKeyNotInPopulatedMap() {
    121       return "minus one";
    122     }
    123 
    124     @Override protected Integer getValueNotInPopulatedMap() {
    125       return -1;
    126     }
    127   }
    128 }
    129