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 static java.util.Arrays.asList;
     20 
     21 import com.google.common.collect.testing.MinimalSet;
     22 import com.google.common.collect.testing.SetTestSuiteBuilder;
     23 import com.google.common.collect.testing.TestStringSetGenerator;
     24 import com.google.common.collect.testing.features.CollectionFeature;
     25 import com.google.common.collect.testing.features.CollectionSize;
     26 
     27 import junit.framework.Test;
     28 import junit.framework.TestSuite;
     29 
     30 import java.util.Collection;
     31 import java.util.Collections;
     32 import java.util.List;
     33 import java.util.Set;
     34 
     35 /**
     36  * Tests for {@code ForwardingSet}.
     37  *
     38  * @author Robert Konigsberg
     39  * @author Louis Wasserman
     40  */
     41 public class ForwardingSetTest extends ForwardingTestCase {
     42   static class StandardImplForwardingSet<T> extends ForwardingSet<T> {
     43     private final Set<T> backingSet;
     44 
     45     StandardImplForwardingSet(Set<T> backingSet) {
     46       this.backingSet = backingSet;
     47     }
     48 
     49     @Override protected Set<T> delegate() {
     50       return backingSet;
     51     }
     52 
     53     @Override public boolean equals(Object object) {
     54       return standardEquals(object);
     55     }
     56 
     57     @Override public int hashCode() {
     58       return standardHashCode();
     59     }
     60 
     61     @Override public boolean addAll(Collection<? extends T> collection) {
     62       return standardAddAll(collection);
     63     }
     64 
     65     @Override public void clear() {
     66       standardClear();
     67     }
     68 
     69     @Override public boolean contains(Object object) {
     70       return standardContains(object);
     71     }
     72 
     73     @Override public boolean containsAll(Collection<?> collection) {
     74       return standardContainsAll(collection);
     75     }
     76 
     77     @Override public boolean remove(Object object) {
     78       return standardRemove(object);
     79     }
     80 
     81     @Override public boolean removeAll(Collection<?> collection) {
     82       return standardRemoveAll(collection);
     83     }
     84 
     85     @Override public boolean retainAll(Collection<?> collection) {
     86       return standardRetainAll(collection);
     87     }
     88 
     89     @Override public Object[] toArray() {
     90       return standardToArray();
     91     }
     92 
     93     @Override public <T> T[] toArray(T[] array) {
     94       return standardToArray(array);
     95     }
     96 
     97     @Override public String toString() {
     98       return standardToString();
     99     }
    100   }
    101 
    102   private static final List<String> EMPTY_LIST =
    103       Collections.<String>emptyList();
    104 
    105   Set<String> forward;
    106 
    107   public static Test suite() {
    108     TestSuite suite = new TestSuite();
    109 
    110     suite.addTestSuite(ForwardingSetTest.class);
    111     suite.addTest(
    112         SetTestSuiteBuilder.using(new TestStringSetGenerator() {
    113           @Override protected Set<String> create(String[] elements) {
    114             return new StandardImplForwardingSet<String>(
    115                 Sets.newLinkedHashSet(asList(elements)));
    116           }
    117         }).named(
    118             "ForwardingSet[LinkedHashSet] with standard implementations")
    119             .withFeatures(CollectionSize.ANY,
    120                 CollectionFeature.ALLOWS_NULL_VALUES,
    121                 CollectionFeature.GENERAL_PURPOSE).createTestSuite());
    122     suite.addTest(
    123         SetTestSuiteBuilder.using(new TestStringSetGenerator() {
    124           @Override protected Set<String> create(String[] elements) {
    125             return new StandardImplForwardingSet<String>(
    126                 MinimalSet.of(elements));
    127           }
    128         }).named(
    129             "ForwardingSet[MinimalSet] with standard implementations")
    130             .withFeatures(CollectionSize.ANY,
    131                 CollectionFeature.ALLOWS_NULL_VALUES).createTestSuite());
    132 
    133     return suite;
    134   }
    135 
    136   @Override public void setUp() throws Exception {
    137     super.setUp();
    138     /*
    139      * Class parameters must be raw, so we can't create a proxy with generic
    140      * type arguments. The created proxy only records calls and returns null, so
    141      * the type is irrelevant at runtime.
    142      */
    143     @SuppressWarnings("unchecked")
    144     final Set<String> set = createProxyInstance(Set.class);
    145     forward = new ForwardingSet<String>() {
    146       @Override protected Set<String> delegate() {
    147         return set;
    148       }
    149     };
    150   }
    151 
    152   public void testAdd_T() {
    153     forward().add("asdf");
    154     assertEquals("[add(Object)]", getCalls());
    155   }
    156 
    157   public void testAddAll_Collection() {
    158     forward().addAll(EMPTY_LIST);
    159     assertEquals("[addAll(Collection)]", getCalls());
    160   }
    161 
    162   public void testClear() {
    163     forward().clear();
    164     assertEquals("[clear]", getCalls());
    165   }
    166 
    167   public void testContains_Object() {
    168     forward().contains(null);
    169     assertEquals("[contains(Object)]", getCalls());
    170   }
    171 
    172   public void testContainsAll_Collection() {
    173     forward().containsAll(EMPTY_LIST);
    174     assertEquals("[containsAll(Collection)]", getCalls());
    175   }
    176 
    177   public void testIsEmpty() {
    178     forward().isEmpty();
    179     assertEquals("[isEmpty]", getCalls());
    180   }
    181 
    182   public void testIterator() {
    183     forward().iterator();
    184     assertEquals("[iterator]", getCalls());
    185   }
    186 
    187   public void testRemove_Object() {
    188     forward().remove(null);
    189     assertEquals("[remove(Object)]", getCalls());
    190   }
    191 
    192   public void testRemoveAll_Collection() {
    193     forward().removeAll(EMPTY_LIST);
    194     assertEquals("[removeAll(Collection)]", getCalls());
    195   }
    196 
    197   public void testRetainAll_Collection() {
    198     forward().retainAll(EMPTY_LIST);
    199     assertEquals("[retainAll(Collection)]", getCalls());
    200   }
    201 
    202   public void testSize() {
    203     forward().size();
    204     assertEquals("[size]", getCalls());
    205   }
    206 
    207   public void testToArray() {
    208     forward().toArray();
    209     assertEquals("[toArray]", getCalls());
    210   }
    211 
    212   public void testToArray_TArray() {
    213     forward().toArray(new String[0]);
    214     assertEquals("[toArray(Object[])]", getCalls());
    215   }
    216 
    217   public void testToString() {
    218     forward().toString();
    219     assertEquals("[toString]", getCalls());
    220   }
    221 
    222   Set<String> forward() {
    223     return forward;
    224   }
    225 }
    226