2
* Copyright (C) 2010 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.collect.testing;
19
import static java.util.Collections.sort;
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;
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;
40
* Tests for SafeTreeMap.
42
* @author Louis Wasserman
44
public class SafeTreeMapTest extends TestCase {
45
public static Test suite() {
46
TestSuite suite = new TestSuite();
47
suite.addTestSuite(SafeTreeMapTest.class);
49
NavigableMapTestSuiteBuilder.using(
50
new TestStringSortedMapGenerator() {
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());
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")
70
NavigableMapTestSuiteBuilder.using(
71
new TestStringSortedMapGenerator() {
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());
82
public Iterable<Entry<String, String>> order(
83
List<Entry<String, String>> insertionOrder) {
86
Helpers.<String, String>entryComparator(NullsBeforeTwo.INSTANCE));
87
return insertionOrder;
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")
104
@GwtIncompatible // SerializableTester
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());
110
Lists.newArrayList(map.values()),
111
Lists.newArrayList(SerializableTester.reserialize(map.values())));