View Javadoc
1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
18  import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
19  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ITERATOR_REMOVE;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.annotations.GwtIncompatible;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.IteratorTester;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import java.lang.reflect.Method;
28  import java.util.Arrays;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * Tester to make sure the {@code iterator().remove()} implementation of {@code Multiset} works when
34   * there are multiple occurrences of elements.
35   *
36   * @author Louis Wasserman
37   */
38  @GwtCompatible(emulated = true)
39  public class MultisetIteratorTester<E> extends AbstractMultisetTester<E> {
40    @SuppressWarnings("unchecked")
41    @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER})
42    public void testRemovingIteratorKnownOrder() {
43      new IteratorTester<E>(
44          4,
45          MODIFIABLE,
46          getSubjectGenerator().order(Arrays.asList(e0(), e1(), e1(), e2())),
47          IteratorTester.KnownOrder.KNOWN_ORDER) {
48        @Override
49        protected Iterator<E> newTargetIterator() {
50          return getSubjectGenerator().create(e0(), e1(), e1(), e2()).iterator();
51        }
52      }.test();
53    }
54  
55    @SuppressWarnings("unchecked")
56    @CollectionFeature.Require(value = SUPPORTS_ITERATOR_REMOVE, absent = KNOWN_ORDER)
57    public void testRemovingIteratorUnknownOrder() {
58      new IteratorTester<E>(
59          4,
60          MODIFIABLE,
61          Arrays.asList(e0(), e1(), e1(), e2()),
62          IteratorTester.KnownOrder.UNKNOWN_ORDER) {
63        @Override
64        protected Iterator<E> newTargetIterator() {
65          return getSubjectGenerator().create(e0(), e1(), e1(), e2()).iterator();
66        }
67      }.test();
68    }
69  
70    @SuppressWarnings("unchecked")
71    @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
72    public void testIteratorKnownOrder() {
73      new IteratorTester<E>(
74          4,
75          UNMODIFIABLE,
76          getSubjectGenerator().order(Arrays.asList(e0(), e1(), e1(), e2())),
77          IteratorTester.KnownOrder.KNOWN_ORDER) {
78        @Override
79        protected Iterator<E> newTargetIterator() {
80          return getSubjectGenerator().create(e0(), e1(), e1(), e2()).iterator();
81        }
82      }.test();
83    }
84  
85    @SuppressWarnings("unchecked")
86    @CollectionFeature.Require(absent = {SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER})
87    public void testIteratorUnknownOrder() {
88      new IteratorTester<E>(
89          4,
90          UNMODIFIABLE,
91          Arrays.asList(e0(), e1(), e1(), e2()),
92          IteratorTester.KnownOrder.UNKNOWN_ORDER) {
93        @Override
94        protected Iterator<E> newTargetIterator() {
95          return getSubjectGenerator().create(e0(), e1(), e1(), e2()).iterator();
96        }
97      }.test();
98    }
99  
100   /**
101    * Returns {@link Method} instances for the tests that assume multisets support duplicates so that
102    * the test of {@code Multisets.forSet()} can suppress them.
103    */
104   @GwtIncompatible // reflection
105   public static List<Method> getIteratorDuplicateInitializingMethods() {
106     return Arrays.asList(
107         Helpers.getMethod(MultisetIteratorTester.class, "testIteratorKnownOrder"),
108         Helpers.getMethod(MultisetIteratorTester.class, "testIteratorUnknownOrder"),
109         Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorKnownOrder"),
110         Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorUnknownOrder"));
111   }
112 }