1 /* 2 * Copyright (C) 2007 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; 18 19 import com.google.common.collect.testing.Helpers.NullsBeforeTwo; 20 import com.google.common.collect.testing.MapTestSuiteBuilder; 21 import com.google.common.collect.testing.SafeTreeMap; 22 import com.google.common.collect.testing.SortedMapTestSuiteBuilder; 23 import com.google.common.collect.testing.TestStringSortedMapGenerator; 24 import com.google.common.collect.testing.features.CollectionFeature; 25 import com.google.common.collect.testing.features.CollectionSize; 26 import com.google.common.collect.testing.features.MapFeature; 27 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 import java.util.Collection; 32 import java.util.Comparator; 33 import java.util.Iterator; 34 import java.util.Map; 35 import java.util.Map.Entry; 36 import java.util.Set; 37 import java.util.SortedMap; 38 39 /** 40 * Tests for {@code ForwardingSortedMap}. 41 * 42 * @author Robert Konigsberg 43 */ 44 public class ForwardingSortedMapTest extends ForwardingMapTest { 45 static class StandardImplForwardingSortedMap<K, V> 46 extends ForwardingSortedMap<K, V> { 47 private final SortedMap<K, V> backingMap; 48 49 StandardImplForwardingSortedMap(SortedMap<K, V> backingMap) { 50 this.backingMap = backingMap; 51 } 52 53 @Override protected SortedMap<K, V> delegate() { 54 return backingMap; 55 } 56 57 @Override public boolean containsKey(Object key) { 58 return standardContainsKey(key); 59 } 60 61 @Override public boolean containsValue(Object value) { 62 return standardContainsValue(value); 63 } 64 65 @Override public void putAll(Map<? extends K, ? extends V> map) { 66 standardPutAll(map); 67 } 68 69 /* This test would fail due to a bug fixed in JDK7 70 @Override public V remove(Object object) { 71 return standardRemove(object); 72 } 73 */ 74 75 @Override public boolean equals(Object object) { 76 return standardEquals(object); 77 } 78 79 @Override public int hashCode() { 80 return standardHashCode(); 81 } 82 83 @Override public Set<K> keySet() { 84 return new StandardKeySet(); 85 } 86 87 @Override public Collection<V> values() { 88 return new StandardValues(); 89 } 90 91 @Override public String toString() { 92 return standardToString(); 93 } 94 95 @Override public Set<Entry<K, V>> entrySet() { 96 return new StandardEntrySet() { 97 @Override 98 public Iterator<Entry<K, V>> iterator() { 99 return backingMap.entrySet().iterator(); 100 } 101 }; 102 } 103 104 @Override public void clear() { 105 standardClear(); 106 } 107 108 @Override public boolean isEmpty() { 109 return standardIsEmpty(); 110 } 111 112 @Override public SortedMap<K, V> subMap(K fromKey, K toKey) { 113 return standardSubMap(fromKey, toKey); 114 } 115 } 116 117 public static Test suite() { 118 TestSuite suite = new TestSuite(); 119 120 suite.addTestSuite(ForwardingSortedMapTest.class); 121 suite.addTest(SortedMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() { 122 @Override protected SortedMap<String, String> create( 123 Entry<String, String>[] entries) { 124 SortedMap<String, String> map = new SafeTreeMap<String, String>(); 125 for (Entry<String, String> entry : entries) { 126 map.put(entry.getKey(), entry.getValue()); 127 } 128 return new StandardImplForwardingSortedMap<String, String>(map); 129 } 130 }).named("ForwardingSortedMap[SafeTreeMap] with no comparator and standard " 131 + "implementations").withFeatures(CollectionSize.ANY, 132 CollectionFeature.KNOWN_ORDER, MapFeature.ALLOWS_NULL_VALUES, 133 MapFeature.GENERAL_PURPOSE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE) 134 .createTestSuite()); 135 suite.addTest(MapTestSuiteBuilder.using(new TestStringSortedMapGenerator() { 136 private final Comparator<String> comparator = NullsBeforeTwo.INSTANCE; 137 138 @Override protected SortedMap<String, String> create( 139 Entry<String, String>[] entries) { 140 SortedMap<String, String> map = 141 new SafeTreeMap<String, String>(comparator); 142 for (Entry<String, String> entry : entries) { 143 map.put(entry.getKey(), entry.getValue()); 144 } 145 return new StandardImplForwardingSortedMap<String, String>(map); 146 } 147 }).named("ForwardingSortedMap[SafeTreeMap] with natural comparator and " 148 + "standard implementations").withFeatures(CollectionSize.ANY, 149 CollectionFeature.KNOWN_ORDER, MapFeature.ALLOWS_NULL_VALUES, 150 MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_ANY_NULL_QUERIES, 151 MapFeature.GENERAL_PURPOSE, 152 CollectionFeature.SUPPORTS_ITERATOR_REMOVE).createTestSuite()); 153 suite.addTest(SortedMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() { 154 @Override protected SortedMap<String, String> create( 155 Entry<String, String>[] entries) { 156 ImmutableSortedMap.Builder<String, String> builder = 157 ImmutableSortedMap.naturalOrder(); 158 for (Entry<String, String> entry : entries) { 159 builder.put(entry.getKey(), entry.getValue()); 160 } 161 return new StandardImplForwardingSortedMap<String, String>( 162 builder.build()); 163 } 164 }).named("ForwardingSortedMap[ImmutableSortedMap] with standard " 165 + "implementations").withFeatures( 166 CollectionSize.ANY, MapFeature.REJECTS_DUPLICATES_AT_CREATION, 167 MapFeature.ALLOWS_ANY_NULL_QUERIES) 168 .createTestSuite()); 169 170 return suite; 171 } 172 173 @Override public void setUp() throws Exception { 174 super.setUp(); 175 /* 176 * Class parameters must be raw, so we can't create a proxy with generic 177 * type arguments. The created proxy only records calls and returns null, so 178 * the type is irrelevant at runtime. 179 */ 180 @SuppressWarnings("unchecked") 181 final SortedMap<String, Boolean> sortedMap = 182 createProxyInstance(SortedMap.class); 183 forward = new ForwardingSortedMap<String, Boolean>() { 184 @Override protected SortedMap<String, Boolean> delegate() { 185 return sortedMap; 186 } 187 }; 188 } 189 190 public void testComparator() { 191 forward().comparator(); 192 assertEquals("[comparator]", getCalls()); 193 } 194 195 public void testFirstKey() { 196 forward().firstKey(); 197 assertEquals("[firstKey]", getCalls()); 198 } 199 200 public void testHeadMap_K() { 201 forward().headMap("asdf"); 202 assertEquals("[headMap(Object)]", getCalls()); 203 } 204 205 public void testLastKey() { 206 forward().lastKey(); 207 assertEquals("[lastKey]", getCalls()); 208 } 209 210 public void testSubMap_K_K() { 211 forward().subMap("first", "last"); 212 assertEquals("[subMap(Object,Object)]", getCalls()); 213 } 214 215 public void testTailMap_K() { 216 forward().tailMap("last"); 217 assertEquals("[tailMap(Object)]", getCalls()); 218 } 219 220 @Override SortedMap<String, Boolean> forward() { 221 return (SortedMap<String, Boolean>) super.forward(); 222 } 223 } 224