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