| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing; |
| 18 |
|
| 19 |
import static java.util.Collections.sort; |
| 20 |
|
| 21 |
import com.google.common.annotations.GwtIncompatible; |
| 22 |
import com.google.common.collect.ImmutableSortedMap; |
| 23 |
import com.google.common.collect.Lists; |
| 24 |
import com.google.common.collect.Ordering; |
| 25 |
import com.google.common.collect.testing.Helpers.NullsBeforeTwo; |
| 26 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 27 |
import com.google.common.collect.testing.features.CollectionSize; |
| 28 |
import com.google.common.collect.testing.features.MapFeature; |
| 29 |
import com.google.common.testing.SerializableTester; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Map; |
| 32 |
import java.util.Map.Entry; |
| 33 |
import java.util.NavigableMap; |
| 34 |
import java.util.SortedMap; |
| 35 |
import junit.framework.Test; |
| 36 |
import junit.framework.TestCase; |
| 37 |
import junit.framework.TestSuite; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class SafeTreeMapTest extends TestCase { |
| 45 |
public static Test suite() { |
| 46 |
TestSuite suite = new TestSuite(); |
| 47 |
suite.addTestSuite(SafeTreeMapTest.class); |
| 48 |
suite.addTest( |
| 49 |
NavigableMapTestSuiteBuilder.using( |
| 50 |
new TestStringSortedMapGenerator() { |
| 51 |
@Override |
| 52 |
protected SortedMap<String, String> create(Entry<String, String>[] entries) { |
| 53 |
NavigableMap<String, String> map = new SafeTreeMap<>(Ordering.natural()); |
| 54 |
for (Entry<String, String> entry : entries) { |
| 55 |
map.put(entry.getKey(), entry.getValue()); |
| 56 |
} |
| 57 |
return map; |
| 58 |
} |
| 59 |
}) |
| 60 |
.withFeatures( |
| 61 |
CollectionSize.ANY, |
| 62 |
CollectionFeature.KNOWN_ORDER, |
| 63 |
CollectionFeature.SERIALIZABLE, |
| 64 |
MapFeature.ALLOWS_NULL_VALUES, |
| 65 |
CollectionFeature.SUPPORTS_ITERATOR_REMOVE, |
| 66 |
MapFeature.GENERAL_PURPOSE) |
| 67 |
.named("SafeTreeMap with natural comparator") |
| 68 |
.createTestSuite()); |
| 69 |
suite.addTest( |
| 70 |
NavigableMapTestSuiteBuilder.using( |
| 71 |
new TestStringSortedMapGenerator() { |
| 72 |
@Override |
| 73 |
protected SortedMap<String, String> create(Entry<String, String>[] entries) { |
| 74 |
NavigableMap<String, String> map = new SafeTreeMap<>(NullsBeforeTwo.INSTANCE); |
| 75 |
for (Entry<String, String> entry : entries) { |
| 76 |
map.put(entry.getKey(), entry.getValue()); |
| 77 |
} |
| 78 |
return map; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Iterable<Entry<String, String>> order( |
| 83 |
List<Entry<String, String>> insertionOrder) { |
| 84 |
sort( |
| 85 |
insertionOrder, |
| 86 |
Helpers.<String, String>entryComparator(NullsBeforeTwo.INSTANCE)); |
| 87 |
return insertionOrder; |
| 88 |
} |
| 89 |
}) |
| 90 |
.withFeatures( |
| 91 |
CollectionSize.ANY, |
| 92 |
CollectionFeature.KNOWN_ORDER, |
| 93 |
MapFeature.ALLOWS_NULL_KEYS, |
| 94 |
MapFeature.ALLOWS_NULL_VALUES, |
| 95 |
MapFeature.ALLOWS_ANY_NULL_QUERIES, |
| 96 |
MapFeature.GENERAL_PURPOSE, |
| 97 |
CollectionFeature.SUPPORTS_ITERATOR_REMOVE, |
| 98 |
CollectionFeature.SERIALIZABLE) |
| 99 |
.named("SafeTreeMap with null-friendly comparator") |
| 100 |
.createTestSuite()); |
| 101 |
return suite; |
| 102 |
} |
| 103 |
|
| 104 |
@GwtIncompatible |
| 105 |
public void testViewSerialization() { |
| 106 |
Map<String, Integer> map = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3); |
| 107 |
SerializableTester.reserializeAndAssert(map.entrySet()); |
| 108 |
SerializableTester.reserializeAndAssert(map.keySet()); |
| 109 |
assertEquals( |
| 110 |
Lists.newArrayList(map.values()), |
| 111 |
Lists.newArrayList(SerializableTester.reserialize(map.values()))); |
| 112 |
} |
| 113 |
} |