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 com.google.common.annotations.GwtIncompatible;
20
import com.google.common.collect.ImmutableSortedMap;
21
import com.google.common.collect.Lists;
22
import com.google.common.collect.Ordering;
23
import com.google.common.collect.Sets;
24
import com.google.common.collect.testing.features.CollectionFeature;
25
import com.google.common.collect.testing.features.CollectionSize;
26
import com.google.common.testing.SerializableTester;
27
import java.util.Arrays;
28
import java.util.Collections;
31
import java.util.NavigableSet;
33
import java.util.SortedSet;
34
import junit.framework.Test;
35
import junit.framework.TestCase;
36
import junit.framework.TestSuite;
38
public class SafeTreeSetTest extends TestCase {
39
public static Test suite() {
40
TestSuite suite = new TestSuite();
41
suite.addTestSuite(SafeTreeSetTest.class);
43
NavigableSetTestSuiteBuilder.using(
44
new TestStringSetGenerator() {
46
protected Set<String> create(String[] elements) {
47
return new SafeTreeSet<>(Arrays.asList(elements));
51
public List<String> order(List<String> insertionOrder) {
52
return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
57
CollectionFeature.KNOWN_ORDER,
58
CollectionFeature.GENERAL_PURPOSE)
59
.named("SafeTreeSet with natural comparator")
62
SetTestSuiteBuilder.using(
63
new TestStringSetGenerator() {
65
protected Set<String> create(String[] elements) {
66
NavigableSet<String> set = new SafeTreeSet<>(Ordering.natural().nullsFirst());
67
Collections.addAll(set, elements);
72
public List<String> order(List<String> insertionOrder) {
73
return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
78
CollectionFeature.KNOWN_ORDER,
79
CollectionFeature.GENERAL_PURPOSE,
80
CollectionFeature.ALLOWS_NULL_VALUES)
81
.named("SafeTreeSet with null-friendly comparator")
86
@GwtIncompatible // SerializableTester
87
public void testViewSerialization() {
88
Map<String, Integer> map = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3);
89
SerializableTester.reserializeAndAssert(map.entrySet());
90
SerializableTester.reserializeAndAssert(map.keySet());
92
Lists.newArrayList(map.values()),
93
Lists.newArrayList(SerializableTester.reserialize(map.values())));
96
@GwtIncompatible // SerializableTester
97
public void testEmpty_serialization() {
98
SortedSet<String> set = new SafeTreeSet<>();
99
SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
100
assertEquals(set.comparator(), copy.comparator());
103
@GwtIncompatible // SerializableTester
104
public void testSingle_serialization() {
105
SortedSet<String> set = new SafeTreeSet<>();
107
SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
108
assertEquals(set.comparator(), copy.comparator());
111
@GwtIncompatible // SerializableTester
112
public void testSeveral_serialization() {
113
SortedSet<String> set = new SafeTreeSet<>();
117
SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
118
assertEquals(set.comparator(), copy.comparator());