Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 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.SafeTreeSet;
     20 import com.google.common.collect.testing.SetTestSuiteBuilder;
     21 import com.google.common.collect.testing.TestStringSetGenerator;
     22 import com.google.common.collect.testing.features.CollectionFeature;
     23 import com.google.common.collect.testing.features.CollectionSize;
     24 
     25 import junit.framework.Test;
     26 import junit.framework.TestSuite;
     27 
     28 import java.util.Arrays;
     29 import java.util.Collection;
     30 import java.util.Collections;
     31 import java.util.List;
     32 import java.util.NavigableSet;
     33 import java.util.Set;
     34 import java.util.SortedSet;
     35 
     36 /**
     37  * Tests for {@code ForwardingNavigableSet}.
     38  *
     39  * @author Louis Wasserman
     40  */
     41 public class ForwardingNavigableSetTest extends ForwardingSortedSetTest {
     42   static class StandardImplForwardingNavigableSet<T>
     43       extends ForwardingNavigableSet<T> {
     44     private final NavigableSet<T> backingSet;
     45 
     46     StandardImplForwardingNavigableSet(NavigableSet<T> backingSet) {
     47       this.backingSet = backingSet;
     48     }
     49 
     50     @Override protected NavigableSet<T> delegate() {
     51       return backingSet;
     52     }
     53 
     54     @Override public boolean equals(Object object) {
     55       return standardEquals(object);
     56     }
     57 
     58     @Override public int hashCode() {
     59       return standardHashCode();
     60     }
     61 
     62     @Override public boolean addAll(Collection<? extends T> collection) {
     63       return standardAddAll(collection);
     64     }
     65 
     66     @Override public void clear() {
     67       standardClear();
     68     }
     69 
     70     @Override public boolean contains(Object object) {
     71       return standardContains(object);
     72     }
     73 
     74     @Override public boolean containsAll(Collection<?> collection) {
     75       return standardContainsAll(collection);
     76     }
     77 
     78     @Override public boolean remove(Object object) {
     79       return standardRemove(object);
     80     }
     81 
     82     @Override public boolean removeAll(Collection<?> collection) {
     83       return standardRemoveAll(collection);
     84     }
     85 
     86     @Override public boolean retainAll(Collection<?> collection) {
     87       return standardRetainAll(collection);
     88     }
     89 
     90     @Override public Object[] toArray() {
     91       return standardToArray();
     92     }
     93 
     94     @Override public <T> T[] toArray(T[] array) {
     95       return standardToArray(array);
     96     }
     97 
     98     @Override public String toString() {
     99       return standardToString();
    100     }
    101 
    102     @Override public SortedSet<T> subSet(T fromElement, T toElement) {
    103       return standardSubSet(fromElement, toElement);
    104     }
    105 
    106     @Override
    107     public T lower(T e) {
    108       return standardLower(e);
    109     }
    110 
    111     @Override
    112     public T floor(T e) {
    113       return standardFloor(e);
    114     }
    115 
    116     @Override
    117     public T ceiling(T e) {
    118       return standardCeiling(e);
    119     }
    120 
    121     @Override
    122     public T higher(T e) {
    123       return standardHigher(e);
    124     }
    125 
    126     @Override
    127     public T pollFirst() {
    128       return standardPollFirst();
    129     }
    130 
    131     @Override
    132     public T pollLast() {
    133       return standardPollLast();
    134     }
    135 
    136     @Override
    137     public SortedSet<T> headSet(T toElement) {
    138       return standardHeadSet(toElement);
    139     }
    140 
    141     @Override
    142     public SortedSet<T> tailSet(T fromElement) {
    143       return standardTailSet(fromElement);
    144     }
    145   }
    146 
    147   public static Test suite() {
    148     TestSuite suite = new TestSuite();
    149 
    150     suite.addTestSuite(ForwardingNavigableSetTest.class);
    151     suite.addTest(
    152         SetTestSuiteBuilder.using(new TestStringSetGenerator() {
    153           @Override protected Set<String> create(String[] elements) {
    154             return new StandardImplForwardingNavigableSet<String>(
    155                 new SafeTreeSet<String>(Arrays.asList(elements)));
    156           }
    157 
    158           @Override public List<String> order(List<String> insertionOrder) {
    159             return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
    160           }
    161         }).named(
    162             "ForwardingNavigableSet[SafeTreeSet] with standard implementations")
    163             .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    164                 CollectionFeature.GENERAL_PURPOSE).createTestSuite());
    165     suite.addTest(
    166         SetTestSuiteBuilder.using(new TestStringSetGenerator() {
    167           @Override protected Set<String> create(String[] elements) {
    168             SafeTreeSet<String> set = new SafeTreeSet<String>(Ordering.natural().nullsFirst());
    169             Collections.addAll(set, elements);
    170             return new StandardImplForwardingNavigableSet<String>(set);
    171           }
    172 
    173           @Override public List<String> order(List<String> insertionOrder) {
    174             return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
    175           }
    176         }).named(
    177             "ForwardingNavigableSet[SafeTreeSet[Ordering.natural.nullsFirst]]"
    178                 + " with standard implementations")
    179             .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    180                 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES)
    181                 .createTestSuite());
    182 
    183     return suite;
    184   }
    185 
    186   @Override public void setUp() throws Exception {
    187     super.setUp();
    188     /*
    189      * Class parameters must be raw, so we can't create a proxy with generic
    190      * type arguments. The created proxy only records calls and returns null, so
    191      * the type is irrelevant at runtime.
    192      */
    193     @SuppressWarnings("unchecked")
    194     final NavigableSet<String> navigableSet
    195         = createProxyInstance(NavigableSet.class);
    196     forward = new ForwardingNavigableSet<String>() {
    197       @Override protected NavigableSet<String> delegate() {
    198         return navigableSet;
    199       }
    200     };
    201   }
    202 
    203   public void testLower() {
    204     forward().lower("a");
    205     assertEquals("[lower(Object)]", getCalls());
    206   }
    207 
    208   public void testFloor() {
    209     forward().floor("a");
    210     assertEquals("[floor(Object)]", getCalls());
    211   }
    212 
    213   public void testCeiling() {
    214     forward().ceiling("a");
    215     assertEquals("[ceiling(Object)]", getCalls());
    216   }
    217 
    218   public void testHigher() {
    219     forward().higher("a");
    220     assertEquals("[higher(Object)]", getCalls());
    221   }
    222 
    223   public void testPollFirst() {
    224     forward().pollFirst();
    225     assertEquals("[pollFirst]", getCalls());
    226   }
    227 
    228   public void testPollLast() {
    229     forward().pollLast();
    230     assertEquals("[pollLast]", getCalls());
    231   }
    232 
    233   public void testDescendingIterator() {
    234     forward().descendingIterator();
    235     assertEquals("[descendingIterator]", getCalls());
    236   }
    237 
    238   public void testHeadSet_K_Boolean() {
    239     forward().headSet("key", false);
    240     assertEquals("[headSet(Object,boolean)]", getCalls());
    241   }
    242 
    243   public void testSubSet_K_Boolean_K_Boolean() {
    244     forward().subSet("a", true, "b", false);
    245     assertEquals("[subSet(Object,boolean,Object,boolean)]", getCalls());
    246   }
    247 
    248   public void testTailSet_K_Boolean() {
    249     forward().tailSet("key", false);
    250     assertEquals("[tailSet(Object,boolean)]", getCalls());
    251   }
    252 
    253   @Override NavigableSet<String> forward() {
    254     return (NavigableSet<String>) super.forward();
    255   }
    256 }
    257