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.Set; 37 import java.util.SortedSet; 38 39 public class SafeTreeSetTest extends TestCase { suite()40 public static Test suite() { 41 TestSuite suite = new TestSuite(); 42 suite.addTestSuite(SafeTreeSetTest.class); 43 suite.addTest(SetTestSuiteBuilder.using(new TestStringSetGenerator() { 44 @Override protected Set<String> create(String[] elements) { 45 SortedSet<String> set = 46 new SafeTreeSet<String>(Ordering.natural().nullsFirst()); 47 Collections.addAll(set, elements); 48 return set; 49 } 50 51 @Override public List<String> order(List<String> insertionOrder) { 52 return Lists.newArrayList(Sets.newTreeSet(insertionOrder)); 53 } 54 }).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER, 55 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES) 56 .named("SafeTreeSet with null-friendly comparator").createTestSuite()); 57 return suite; 58 } 59 60 @GwtIncompatible("SerializableTester") testViewSerialization()61 public void testViewSerialization() { 62 Map<String, Integer> map = 63 ImmutableSortedMap.of("one", 1, "two", 2, "three", 3); 64 SerializableTester.reserializeAndAssert(map.entrySet()); 65 SerializableTester.reserializeAndAssert(map.keySet()); 66 assertEquals(Lists.newArrayList(map.values()), 67 Lists.newArrayList(SerializableTester.reserialize(map.values()))); 68 } 69 70 @GwtIncompatible("SerializableTester") testEmpty_serialization()71 public void testEmpty_serialization() { 72 SortedSet<String> set = new SafeTreeSet<String>(); 73 SortedSet<String> copy = SerializableTester.reserializeAndAssert(set); 74 assertEquals(set.comparator(), copy.comparator()); 75 } 76 77 @GwtIncompatible("SerializableTester") testSingle_serialization()78 public void testSingle_serialization() { 79 SortedSet<String> set = new SafeTreeSet<String>(); 80 set.add("e"); 81 SortedSet<String> copy = SerializableTester.reserializeAndAssert(set); 82 assertEquals(set.comparator(), copy.comparator()); 83 } 84 85 @GwtIncompatible("SerializableTester") testSeveral_serialization()86 public void testSeveral_serialization() { 87 SortedSet<String> set = new SafeTreeSet<String>(); 88 set.add("a"); 89 set.add("b"); 90 set.add("c"); 91 SortedSet<String> copy = SerializableTester.reserializeAndAssert(set); 92 assertEquals(set.comparator(), copy.comparator()); 93 } 94 } 95