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 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 import java.util.Arrays; 33 import java.util.Collections; 34 import java.util.List; 35 import java.util.Map; 36 import java.util.NavigableSet; 37 import java.util.Set; 38 import java.util.SortedSet; 39 40 public class SafeTreeSetTest extends TestCase { suite()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") testViewSerialization()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") testEmpty_serialization()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") testSingle_serialization()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") testSeveral_serialization()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