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
17package com.google.common.collect.testing;
18
19import com.google.common.annotations.GwtIncompatible;
20import com.google.common.collect.ImmutableSortedMap;
21import com.google.common.collect.Lists;
22import com.google.common.collect.Ordering;
23import com.google.common.collect.Sets;
24import com.google.common.collect.testing.features.CollectionFeature;
25import com.google.common.collect.testing.features.CollectionSize;
26import com.google.common.testing.SerializableTester;
27
28import junit.framework.Test;
29import junit.framework.TestCase;
30import junit.framework.TestSuite;
31
32import java.util.Arrays;
33import java.util.Collections;
34import java.util.List;
35import java.util.Map;
36import java.util.NavigableSet;
37import java.util.Set;
38import java.util.SortedSet;
39
40public class SafeTreeSetTest extends TestCase {
41  public static Test suite() {
42    TestSuite suite = new TestSuite();
43    suite.addTestSuite(SafeTreeSetTest.class);
44    suite.addTest(
45        NavigableSetTestSuiteBuilder.using(new TestStringSetGenerator() {
46          @Override protected Set<String> create(String[] elements) {
47            return new SafeTreeSet<String>(Arrays.asList(elements));
48          }
49
50          @Override public List<String> order(List<String> insertionOrder) {
51            return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
52          }
53        }).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
54            CollectionFeature.GENERAL_PURPOSE).named(
55            "SafeTreeSet with natural comparator").createTestSuite());
56    suite.addTest(SetTestSuiteBuilder.using(new TestStringSetGenerator() {
57      @Override protected Set<String> create(String[] elements) {
58        NavigableSet<String> set =
59            new SafeTreeSet<String>(Ordering.natural().nullsFirst());
60        Collections.addAll(set, elements);
61        return set;
62      }
63
64      @Override public List<String> order(List<String> insertionOrder) {
65        return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
66      }
67    }).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
68        CollectionFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES)
69        .named("SafeTreeSet with null-friendly comparator").createTestSuite());
70    return suite;
71  }
72
73  @GwtIncompatible("SerializableTester")
74  public void testViewSerialization() {
75    Map<String, Integer> map =
76        ImmutableSortedMap.of("one", 1, "two", 2, "three", 3);
77    SerializableTester.reserializeAndAssert(map.entrySet());
78    SerializableTester.reserializeAndAssert(map.keySet());
79    assertEquals(Lists.newArrayList(map.values()),
80        Lists.newArrayList(SerializableTester.reserialize(map.values())));
81  }
82
83  @GwtIncompatible("SerializableTester")
84  public void testEmpty_serialization() {
85    SortedSet<String> set = new SafeTreeSet<String>();
86    SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
87    assertEquals(set.comparator(), copy.comparator());
88  }
89
90  @GwtIncompatible("SerializableTester")
91  public void testSingle_serialization() {
92    SortedSet<String> set = new SafeTreeSet<String>();
93    set.add("e");
94    SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
95    assertEquals(set.comparator(), copy.comparator());
96  }
97
98  @GwtIncompatible("SerializableTester")
99  public void testSeveral_serialization() {
100    SortedSet<String> set = new SafeTreeSet<String>();
101    set.add("a");
102    set.add("b");
103    set.add("c");
104    SortedSet<String> copy = SerializableTester.reserializeAndAssert(set);
105    assertEquals(set.comparator(), copy.comparator());
106  }
107}
108