Home | History | Annotate | Download | only in collect
      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;
     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.List;
     31 import java.util.Set;
     32 import java.util.SortedSet;
     33 
     34 /**
     35  * Tests for {@code ForwardingSortedSet}.
     36  *
     37  * @author Louis Wasserman
     38  */
     39 public class ForwardingSortedSetTest extends ForwardingSetTest {
     40   static class StandardImplForwardingSortedSet<T>
     41       extends ForwardingSortedSet<T> {
     42     private final SortedSet<T> backingSet;
     43 
     44     StandardImplForwardingSortedSet(SortedSet<T> backingSet) {
     45       this.backingSet = backingSet;
     46     }
     47 
     48     @Override protected SortedSet<T> delegate() {
     49       return backingSet;
     50     }
     51 
     52     @Override public boolean equals(Object object) {
     53       return standardEquals(object);
     54     }
     55 
     56     @Override public int hashCode() {
     57       return standardHashCode();
     58     }
     59 
     60     @Override public boolean addAll(Collection<? extends T> collection) {
     61       return standardAddAll(collection);
     62     }
     63 
     64     @Override public void clear() {
     65       standardClear();
     66     }
     67 
     68     @Override public boolean contains(Object object) {
     69       return standardContains(object);
     70     }
     71 
     72     @Override public boolean containsAll(Collection<?> collection) {
     73       return standardContainsAll(collection);
     74     }
     75 
     76     @Override public boolean remove(Object object) {
     77       return standardRemove(object);
     78     }
     79 
     80     @Override public boolean removeAll(Collection<?> collection) {
     81       return standardRemoveAll(collection);
     82     }
     83 
     84     @Override public boolean retainAll(Collection<?> collection) {
     85       return standardRetainAll(collection);
     86     }
     87 
     88     @Override public Object[] toArray() {
     89       return standardToArray();
     90     }
     91 
     92     @Override public <T> T[] toArray(T[] array) {
     93       return standardToArray(array);
     94     }
     95 
     96     @Override public String toString() {
     97       return standardToString();
     98     }
     99 
    100     @Override public SortedSet<T> subSet(T fromElement, T toElement) {
    101       return standardSubSet(fromElement, toElement);
    102     }
    103   }
    104 
    105   public static Test suite() {
    106     TestSuite suite = new TestSuite();
    107 
    108     suite.addTestSuite(ForwardingSortedSetTest.class);
    109     suite.addTest(
    110         SetTestSuiteBuilder.using(new TestStringSetGenerator() {
    111           @Override protected Set<String> create(String[] elements) {
    112             return new StandardImplForwardingSortedSet<String>(
    113                 new SafeTreeSet<String>(Arrays.asList(elements)));
    114           }
    115 
    116           @Override public List<String> order(List<String> insertionOrder) {
    117             return Lists.newArrayList(Sets.newTreeSet(insertionOrder));
    118           }
    119         }).named(
    120             "ForwardingSortedSet[SafeTreeSet] with standard implementations")
    121             .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
    122                 CollectionFeature.GENERAL_PURPOSE).createTestSuite());
    123 
    124     return suite;
    125   }
    126 
    127   @Override public void setUp() throws Exception {
    128     super.setUp();
    129     /*
    130      * Class parameters must be raw, so we can't create a proxy with generic
    131      * type arguments. The created proxy only records calls and returns null, so
    132      * the type is irrelevant at runtime.
    133      */
    134     @SuppressWarnings("unchecked")
    135     final SortedSet<String> sortedSet
    136         = createProxyInstance(SortedSet.class);
    137     forward = new ForwardingSortedSet<String>() {
    138       @Override protected SortedSet<String> delegate() {
    139         return sortedSet;
    140       }
    141     };
    142   }
    143 
    144   public void testComparator() {
    145     forward().comparator();
    146     assertEquals("[comparator]", getCalls());
    147   }
    148 
    149   public void testFirst() {
    150     forward().first();
    151     assertEquals("[first]", getCalls());
    152   }
    153 
    154   public void testHeadSet_K() {
    155     forward().headSet("asdf");
    156     assertEquals("[headSet(Object)]", getCalls());
    157   }
    158 
    159   public void testLast() {
    160     forward().last();
    161     assertEquals("[last]", getCalls());
    162   }
    163 
    164   public void testSubSet_K_K() {
    165     forward().subSet("first", "last");
    166     assertEquals("[subSet(Object,Object)]", getCalls());
    167   }
    168 
    169   public void testTailSet_K() {
    170     forward().tailSet("last");
    171     assertEquals("[tailSet(Object)]", getCalls());
    172   }
    173 
    174   @Override SortedSet<String> forward() {
    175     return (SortedSet<String>) super.forward();
    176   }
    177 }
    178