1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright (C) 2010 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect.testing;
import static java.util.Collections.sort;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.common.collect.testing.Helpers.NullsBeforeTwo;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import com.google.common.testing.SerializableTester;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.SortedMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Tests for SafeTreeMap.
*
* @author Louis Wasserman
*/
public class SafeTreeMapTest extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(SafeTreeMapTest.class);
suite.addTest(
NavigableMapTestSuiteBuilder.using(
new TestStringSortedMapGenerator() {
@Override
protected SortedMap<String, String> create(Entry<String, String>[] entries) {
NavigableMap<String, String> map = new SafeTreeMap<>(Ordering.natural());
for (Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
})
.withFeatures(
CollectionSize.ANY,
CollectionFeature.KNOWN_ORDER,
CollectionFeature.SERIALIZABLE,
MapFeature.ALLOWS_NULL_VALUES,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
MapFeature.GENERAL_PURPOSE)
.named("SafeTreeMap with natural comparator")
.createTestSuite());
suite.addTest(
NavigableMapTestSuiteBuilder.using(
new TestStringSortedMapGenerator() {
@Override
protected SortedMap<String, String> create(Entry<String, String>[] entries) {
NavigableMap<String, String> map = new SafeTreeMap<>(NullsBeforeTwo.INSTANCE);
for (Entry<String, String> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
@Override
public Iterable<Entry<String, String>> order(
List<Entry<String, String>> insertionOrder) {
sort(
insertionOrder,
Helpers.<String, String>entryComparator(NullsBeforeTwo.INSTANCE));
return insertionOrder;
}
})
.withFeatures(
CollectionSize.ANY,
CollectionFeature.KNOWN_ORDER,
MapFeature.ALLOWS_NULL_KEYS,
MapFeature.ALLOWS_NULL_VALUES,
MapFeature.ALLOWS_ANY_NULL_QUERIES,
MapFeature.GENERAL_PURPOSE,
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
CollectionFeature.SERIALIZABLE)
.named("SafeTreeMap with null-friendly comparator")
.createTestSuite());
return suite;
}
@GwtIncompatible // SerializableTester
public void testViewSerialization() {
Map<String, Integer> map = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3);
SerializableTester.reserializeAndAssert(map.entrySet());
SerializableTester.reserializeAndAssert(map.keySet());
assertEquals(
Lists.newArrayList(map.values()),
Lists.newArrayList(SerializableTester.reserialize(map.values())));
}
@GwtIncompatible // SerializableTester
public static class ReserializedMapTests extends SortedMapInterfaceTest<String, Integer> {
public ReserializedMapTests() {
super(false, true, true, true, true);
}
@Override
protected SortedMap<String, Integer> makePopulatedMap() {
NavigableMap<String, Integer> map = new SafeTreeMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
return SerializableTester.reserialize(map);
}
@Override
protected SortedMap<String, Integer> makeEmptyMap() throws UnsupportedOperationException {
NavigableMap<String, Integer> map = new SafeTreeMap<>();
return SerializableTester.reserialize(map);
}
@Override
protected String getKeyNotInPopulatedMap() {
return "minus one";
}
@Override
protected Integer getValueNotInPopulatedMap() {
return -1;
}
}
}
|