1 /* 2 * Copyright (C) 2009 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.SetTestSuiteBuilder; 20 import com.google.common.collect.testing.TestStringSetGenerator; 21 import com.google.common.collect.testing.features.CollectionFeature; 22 import com.google.common.collect.testing.features.CollectionSize; 23 import com.google.common.collect.testing.google.MultisetTestSuiteBuilder; 24 import com.google.common.collect.testing.google.TestStringMultisetGenerator; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 import java.util.Arrays; 30 import java.util.Collection; 31 import java.util.Collections; 32 import java.util.Iterator; 33 import java.util.Set; 34 35 /** 36 * Tests for {@link ForwardingMultiset}. 37 * 38 * @author Hayward Chan 39 * @author Louis Wasserman 40 */ 41 public class ForwardingMultisetTest extends ForwardingTestCase { 42 43 static final class StandardImplForwardingMultiset<T> 44 extends ForwardingMultiset<T> { 45 private final Multiset<T> backingCollection; 46 47 StandardImplForwardingMultiset(Multiset<T> backingMultiset) { 48 this.backingCollection = backingMultiset; 49 } 50 51 @Override protected Multiset<T> delegate() { 52 return backingCollection; 53 } 54 55 @Override public boolean addAll(Collection<? extends T> collection) { 56 return standardAddAll(collection); 57 } 58 59 @Override public boolean add(T element) { 60 return standardAdd(element); 61 } 62 63 @Override public void clear() { 64 standardClear(); 65 } 66 67 @Override public int count(Object element) { 68 return standardCount(element); 69 } 70 71 @Override public boolean contains(Object object) { 72 return standardContains(object); 73 } 74 75 @Override public boolean containsAll(Collection<?> collection) { 76 return standardContainsAll(collection); 77 } 78 79 @Override public boolean remove(Object object) { 80 return standardRemove(object); 81 } 82 83 @Override public boolean removeAll(Collection<?> collection) { 84 return standardRemoveAll(collection); 85 } 86 87 @Override public boolean retainAll(Collection<?> collection) { 88 return standardRetainAll(collection); 89 } 90 91 @Override public Object[] toArray() { 92 return standardToArray(); 93 } 94 95 @Override public <T> T[] toArray(T[] array) { 96 return standardToArray(array); 97 } 98 99 @Override public String toString() { 100 return standardToString(); 101 } 102 103 @Override public boolean equals(Object object) { 104 return standardEquals(object); 105 } 106 107 @Override public int hashCode() { 108 return standardHashCode(); 109 } 110 111 @Override public boolean setCount(T element, int oldCount, int newCount) { 112 return standardSetCount(element, oldCount, newCount); 113 } 114 115 @Override public int setCount(T element, int count) { 116 return standardSetCount(element, count); 117 } 118 119 @Override public Set<T> elementSet() { 120 return new StandardElementSet(); 121 } 122 123 @Override public Iterator<T> iterator() { 124 return standardIterator(); 125 } 126 127 @Override public boolean isEmpty() { 128 return standardIsEmpty(); 129 } 130 131 @Override public int size() { 132 return standardSize(); 133 } 134 } 135 136 private static final Collection<String> EMPTY_COLLECTION = 137 Collections.emptyList(); 138 139 protected Multiset<String> forward; 140 141 public static Test suite() { 142 TestSuite suite = new TestSuite(); 143 144 suite.addTestSuite(ForwardingMultisetTest.class); 145 suite.addTest( 146 MultisetTestSuiteBuilder.using(new TestStringMultisetGenerator() { 147 148 @Override protected Multiset<String> create(String[] elements) { 149 return new StandardImplForwardingMultiset<String>( 150 LinkedHashMultiset.create(Arrays.asList(elements))); 151 } 152 }).named("ForwardingMultiset[LinkedHashMultiset] with standard " 153 + "implementations").withFeatures(CollectionSize.ANY, 154 CollectionFeature.ALLOWS_NULL_VALUES, 155 CollectionFeature.GENERAL_PURPOSE).createTestSuite()); 156 suite.addTest( 157 MultisetTestSuiteBuilder.using(new TestStringMultisetGenerator() { 158 159 @Override protected Multiset<String> create(String[] elements) { 160 return new StandardImplForwardingMultiset<String>( 161 ImmutableMultiset.copyOf(elements)); 162 } 163 }).named("ForwardingMultiset[ImmutableMultiset] with standard " 164 + "implementations") 165 .withFeatures(CollectionSize.ANY, 166 CollectionFeature.ALLOWS_NULL_QUERIES) 167 .createTestSuite()); 168 suite.addTest(SetTestSuiteBuilder.using(new TestStringSetGenerator() { 169 170 /** 171 * Returns a Multiset that throws an exception on any attempt to use a 172 * method not specifically authorized by the elementSet() or hashCode() 173 * docs. 174 */ 175 @Override protected Set<String> create(String[] elements) { 176 final Multiset<String> inner = 177 LinkedHashMultiset.create(Arrays.asList(elements)); 178 return new ForwardingMultiset<String>() { 179 @Override protected Multiset<String> delegate() { 180 return inner; 181 } 182 183 @Override public Set<String> elementSet() { 184 return new StandardElementSet(); 185 } 186 187 @Override public int add(String element, int occurrences) { 188 throw new UnsupportedOperationException(); 189 } 190 @Override public Set<Entry<String>> entrySet() { 191 final Set<Entry<String>> backingSet = super.entrySet(); 192 return new ForwardingSet<Entry<String>>() { 193 @Override protected Set<Entry<String>> delegate() { 194 return backingSet; 195 } 196 @Override public boolean add(Entry<String> element) { 197 throw new UnsupportedOperationException(); 198 } 199 @Override public boolean addAll( 200 Collection<? extends Entry<String>> collection) { 201 throw new UnsupportedOperationException(); 202 } 203 @Override public void clear() { 204 throw new UnsupportedOperationException(); 205 } 206 @Override public boolean contains(Object object) { 207 throw new UnsupportedOperationException(); 208 } 209 @Override public boolean containsAll(Collection<?> collection) { 210 throw new UnsupportedOperationException(); 211 } 212 @Override public boolean isEmpty() { 213 throw new UnsupportedOperationException(); 214 } 215 @Override public boolean remove(Object object) { 216 throw new UnsupportedOperationException(); 217 } 218 @Override public boolean removeAll(Collection<?> collection) { 219 throw new UnsupportedOperationException(); 220 } 221 @Override public boolean retainAll(Collection<?> collection) { 222 throw new UnsupportedOperationException(); 223 } 224 }; 225 } 226 @Override public boolean equals(Object object) { 227 throw new UnsupportedOperationException(); 228 } 229 @Override public boolean remove(Object element) { 230 throw new UnsupportedOperationException(); 231 } 232 @Override public boolean setCount( 233 String element, int oldCount, int newCount) { 234 throw new UnsupportedOperationException(); 235 } 236 @Override public int setCount(String element, int count) { 237 throw new UnsupportedOperationException(); 238 } 239 @Override public boolean add(String element) { 240 throw new UnsupportedOperationException(); 241 } 242 @Override public boolean addAll( 243 Collection<? extends String> collection) { 244 throw new UnsupportedOperationException(); 245 } 246 @Override public Iterator<String> iterator() { 247 throw new UnsupportedOperationException(); 248 } 249 @Override public boolean removeAll(Collection<?> collection) { 250 throw new UnsupportedOperationException(); 251 } 252 @Override public boolean retainAll(Collection<?> collection) { 253 throw new UnsupportedOperationException(); 254 } 255 @Override public int size() { 256 throw new UnsupportedOperationException(); 257 } 258 }.elementSet(); 259 } 260 }).named("standardElementSet tripwire").withFeatures(CollectionSize.ANY, 261 CollectionFeature.ALLOWS_NULL_VALUES, 262 CollectionFeature.REMOVE_OPERATIONS).createTestSuite()); 263 264 return suite; 265 } 266 267 @Override public void setUp() throws Exception { 268 super.setUp(); 269 /* 270 * Class parameters must be raw, so we can't create a proxy with generic 271 * type arguments. The created proxy only records calls and returns null, so 272 * the type is irrelevant at runtime. 273 */ 274 @SuppressWarnings("unchecked") 275 final Multiset<String> multiset = createProxyInstance(Multiset.class); 276 forward = new ForwardingMultiset<String>() { 277 @Override protected Multiset<String> delegate() { 278 return multiset; 279 } 280 }; 281 } 282 283 public void testAdd_T() { 284 forward().add("asdf"); 285 assertEquals("[add(Object)]", getCalls()); 286 } 287 288 public void testAddAll_Collection() { 289 forward().addAll(EMPTY_COLLECTION); 290 assertEquals("[addAll(Collection)]", getCalls()); 291 } 292 293 public void testClear() { 294 forward().clear(); 295 assertEquals("[clear]", getCalls()); 296 } 297 298 public void testContains_Object() { 299 forward().contains(null); 300 assertEquals("[contains(Object)]", getCalls()); 301 } 302 303 public void testContainsAll_Collection() { 304 forward().containsAll(EMPTY_COLLECTION); 305 assertEquals("[containsAll(Collection)]", getCalls()); 306 } 307 308 public void testIsEmpty() { 309 forward().isEmpty(); 310 assertEquals("[isEmpty]", getCalls()); 311 } 312 313 public void testIterator() { 314 forward().iterator(); 315 assertEquals("[iterator]", getCalls()); 316 } 317 318 public void testRemove_Object() { 319 forward().remove(null); 320 assertEquals("[remove(Object)]", getCalls()); 321 } 322 323 public void testRemoveAll_Collection() { 324 forward().removeAll(EMPTY_COLLECTION); 325 assertEquals("[removeAll(Collection)]", getCalls()); 326 } 327 328 public void testRetainAll_Collection() { 329 forward().retainAll(EMPTY_COLLECTION); 330 assertEquals("[retainAll(Collection)]", getCalls()); 331 } 332 333 public void testSize() { 334 forward().size(); 335 assertEquals("[size]", getCalls()); 336 } 337 338 public void testToArray() { 339 forward().toArray(); 340 assertEquals("[toArray]", getCalls()); 341 } 342 343 public void testToArray_TArray() { 344 forward().toArray(new String[0]); 345 assertEquals("[toArray(Object[])]", getCalls()); 346 } 347 348 public void testToString() { 349 forward().toString(); 350 assertEquals("[toString]", getCalls()); 351 } 352 353 public void testEquals_Object() { 354 forward().equals("asdf"); 355 assertEquals("[equals(Object)]", getCalls()); 356 } 357 358 public void testHashCode() { 359 forward().hashCode(); 360 assertEquals("[hashCode]", getCalls()); 361 } 362 363 public void testCount_Object() { 364 forward().count(null); 365 assertEquals("[count(Object)]", getCalls()); 366 } 367 368 public void testAdd_Object_int() { 369 forward().add("asd", 23); 370 assertEquals("[add(Object,int)]", getCalls()); 371 } 372 373 public void testRemove_Object_int() { 374 forward().remove("asd", 23); 375 assertEquals("[remove(Object,int)]", getCalls()); 376 } 377 378 public void testSetCount_Object_int() { 379 forward().setCount("asdf", 233); 380 assertEquals("[setCount(Object,int)]", getCalls()); 381 } 382 383 public void testSetCount_Object_oldCount_newCount() { 384 forward().setCount("asdf", 4552, 1233); 385 assertEquals("[setCount(Object,int,int)]", getCalls()); 386 } 387 388 public void testElementSet() { 389 forward().elementSet(); 390 assertEquals("[elementSet]", getCalls()); 391 } 392 393 public void testEntrySet() { 394 forward().entrySet(); 395 assertEquals("[entrySet]", getCalls()); 396 } 397 398 protected Multiset<String> forward() { 399 return forward; 400 } 401 } 402