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