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 StandardImplForwardingSet(Set<T> backingSet)45 StandardImplForwardingSet(Set<T> backingSet) { 46 this.backingSet = backingSet; 47 } 48 delegate()49 @Override protected Set<T> delegate() { 50 return backingSet; 51 } 52 equals(Object object)53 @Override public boolean equals(Object object) { 54 return standardEquals(object); 55 } 56 hashCode()57 @Override public int hashCode() { 58 return standardHashCode(); 59 } 60 addAll(Collection<? extends T> collection)61 @Override public boolean addAll(Collection<? extends T> collection) { 62 return standardAddAll(collection); 63 } 64 clear()65 @Override public void clear() { 66 standardClear(); 67 } 68 contains(Object object)69 @Override public boolean contains(Object object) { 70 return standardContains(object); 71 } 72 containsAll(Collection<?> collection)73 @Override public boolean containsAll(Collection<?> collection) { 74 return standardContainsAll(collection); 75 } 76 remove(Object object)77 @Override public boolean remove(Object object) { 78 return standardRemove(object); 79 } 80 removeAll(Collection<?> collection)81 @Override public boolean removeAll(Collection<?> collection) { 82 return standardRemoveAll(collection); 83 } 84 retainAll(Collection<?> collection)85 @Override public boolean retainAll(Collection<?> collection) { 86 return standardRetainAll(collection); 87 } 88 toArray()89 @Override public Object[] toArray() { 90 return standardToArray(); 91 } 92 toArray(T[] array)93 @Override public <T> T[] toArray(T[] array) { 94 return standardToArray(array); 95 } 96 toString()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 suite()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 setUp()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 testAdd_T()152 public void testAdd_T() { 153 forward().add("asdf"); 154 assertEquals("[add(Object)]", getCalls()); 155 } 156 testAddAll_Collection()157 public void testAddAll_Collection() { 158 forward().addAll(EMPTY_LIST); 159 assertEquals("[addAll(Collection)]", getCalls()); 160 } 161 testClear()162 public void testClear() { 163 forward().clear(); 164 assertEquals("[clear]", getCalls()); 165 } 166 testContains_Object()167 public void testContains_Object() { 168 forward().contains(null); 169 assertEquals("[contains(Object)]", getCalls()); 170 } 171 testContainsAll_Collection()172 public void testContainsAll_Collection() { 173 forward().containsAll(EMPTY_LIST); 174 assertEquals("[containsAll(Collection)]", getCalls()); 175 } 176 testIsEmpty()177 public void testIsEmpty() { 178 forward().isEmpty(); 179 assertEquals("[isEmpty]", getCalls()); 180 } 181 testIterator()182 public void testIterator() { 183 forward().iterator(); 184 assertEquals("[iterator]", getCalls()); 185 } 186 testRemove_Object()187 public void testRemove_Object() { 188 forward().remove(null); 189 assertEquals("[remove(Object)]", getCalls()); 190 } 191 testRemoveAll_Collection()192 public void testRemoveAll_Collection() { 193 forward().removeAll(EMPTY_LIST); 194 assertEquals("[removeAll(Collection)]", getCalls()); 195 } 196 testRetainAll_Collection()197 public void testRetainAll_Collection() { 198 forward().retainAll(EMPTY_LIST); 199 assertEquals("[retainAll(Collection)]", getCalls()); 200 } 201 testSize()202 public void testSize() { 203 forward().size(); 204 assertEquals("[size]", getCalls()); 205 } 206 testToArray()207 public void testToArray() { 208 forward().toArray(); 209 assertEquals("[toArray]", getCalls()); 210 } 211 testToArray_TArray()212 public void testToArray_TArray() { 213 forward().toArray(new String[0]); 214 assertEquals("[toArray(Object[])]", getCalls()); 215 } 216 testToString()217 public void testToString() { 218 forward().toString(); 219 assertEquals("[toString]", getCalls()); 220 } 221 forward()222 Set<String> forward() { 223 return forward; 224 } 225 } 226