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