• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.NavigableSet;
32 import java.util.Set;
33 import java.util.SortedSet;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37 
38 public class SafeTreeSetTest extends TestCase {
suite()39   public static Test suite() {
40     TestSuite suite = new TestSuite();
41     suite.addTestSuite(SafeTreeSetTest.class);
42     suite.addTest(
43         NavigableSetTestSuiteBuilder.using(
44                 new TestStringSetGenerator() {
45                   @Override
46                   protected Set<String> create(String[] elements) {
47                     return new SafeTreeSet<>(Arrays.asList(elements));
48                   }
49 
50                   @Override
51                   public List<String> order(List<String> insertionOrder) {
52                     return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
53                   }
54                 })
55             .withFeatures(
56                 CollectionSize.ANY,
57                 CollectionFeature.KNOWN_ORDER,
58                 CollectionFeature.GENERAL_PURPOSE)
59             .named("SafeTreeSet with natural comparator")
60             .createTestSuite());
61     suite.addTest(
62         SetTestSuiteBuilder.using(
63                 new TestStringSetGenerator() {
64                   @Override
65                   protected Set<String> create(String[] elements) {
66                     NavigableSet<String> set = new SafeTreeSet<>(Ordering.natural().nullsFirst());
67                     Collections.addAll(set, elements);
68                     return set;
69                   }
70 
71                   @Override
72                   public List<String> order(List<String> insertionOrder) {
73                     return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
74                   }
75                 })
76             .withFeatures(
77                 CollectionSize.ANY,
78                 CollectionFeature.KNOWN_ORDER,
79                 CollectionFeature.GENERAL_PURPOSE,
80                 CollectionFeature.ALLOWS_NULL_VALUES)
81             .named("SafeTreeSet with null-friendly comparator")
82             .createTestSuite());
83     return suite;
84   }
85 
86   @GwtIncompatible // SerializableTester
testViewSerialization()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());
91     assertEquals(
92         Lists.newArrayList(map.values()),
93         Lists.newArrayList(SerializableTester.reserialize(map.values())));
94   }
95 
96   @GwtIncompatible // SerializableTester
testEmpty_serialization()97   public void testEmpty_serialization() {
98     SortedSet<String> set = new SafeTreeSet<>();
99     SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
100     assertEquals(set.comparator(), copy.comparator());
101   }
102 
103   @GwtIncompatible // SerializableTester
testSingle_serialization()104   public void testSingle_serialization() {
105     SortedSet<String> set = new SafeTreeSet<>();
106     set.add("e");
107     SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
108     assertEquals(set.comparator(), copy.comparator());
109   }
110 
111   @GwtIncompatible // SerializableTester
testSeveral_serialization()112   public void testSeveral_serialization() {
113     SortedSet<String> set = new SafeTreeSet<>();
114     set.add("a");
115     set.add("b");
116     set.add("c");
117     SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
118     assertEquals(set.comparator(), copy.comparator());
119   }
120 }
121