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