Home | History | Annotate | Download | only in google
      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.features.CollectionFeature.KNOWN_ORDER;
     18 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
     19 
     20 import com.google.common.annotations.GwtCompatible;
     21 import com.google.common.collect.testing.IteratorFeature;
     22 import com.google.common.collect.testing.IteratorTester;
     23 import com.google.common.collect.testing.features.CollectionFeature;
     24 
     25 import java.lang.reflect.Method;
     26 import java.util.Arrays;
     27 import java.util.Iterator;
     28 import java.util.List;
     29 
     30 /**
     31  * Tester to make sure the {@code iterator().remove()} implementation of {@code Multiset} works when
     32  * there are multiple occurrences of elements.
     33  *
     34  * @author Louis Wasserman
     35  */
     36 @GwtCompatible
     37 public class MultisetIteratorTester<E> extends AbstractMultisetTester<E> {
     38   @SuppressWarnings("unchecked")
     39   @CollectionFeature.Require({SUPPORTS_REMOVE, KNOWN_ORDER})
     40   public void testRemovingIteratorKnownOrder() {
     41     new IteratorTester<E>(4, IteratorFeature.MODIFIABLE, getSubjectGenerator().order(
     42         Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
     43         IteratorTester.KnownOrder.KNOWN_ORDER) {
     44       @Override
     45       protected Iterator<E> newTargetIterator() {
     46         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     47             .iterator();
     48       }
     49     }.test();
     50   }
     51 
     52   @SuppressWarnings("unchecked")
     53   @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = KNOWN_ORDER)
     54   public void testRemovingIteratorUnknownOrder() {
     55     new IteratorTester<E>(4, IteratorFeature.MODIFIABLE, Arrays.asList(samples.e0, samples.e1,
     56         samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
     57       @Override
     58       protected Iterator<E> newTargetIterator() {
     59         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     60             .iterator();
     61       }
     62     }.test();
     63   }
     64 
     65   @SuppressWarnings("unchecked")
     66   @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_REMOVE)
     67   public void testIteratorKnownOrder() {
     68     new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, getSubjectGenerator().order(
     69         Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
     70         IteratorTester.KnownOrder.KNOWN_ORDER) {
     71       @Override
     72       protected Iterator<E> newTargetIterator() {
     73         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     74             .iterator();
     75       }
     76     }.test();
     77   }
     78 
     79   @SuppressWarnings("unchecked")
     80   @CollectionFeature.Require(absent = {SUPPORTS_REMOVE, KNOWN_ORDER})
     81   public void testIteratorUnknownOrder() {
     82     new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, Arrays.asList(samples.e0, samples.e1,
     83         samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
     84       @Override
     85       protected Iterator<E> newTargetIterator() {
     86         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     87             .iterator();
     88       }
     89     }.test();
     90   }
     91 
     92   /**
     93    * Returns {@link Method} instances for the tests that assume multisets support duplicates so that
     94    * the test of {@code Multisets.forSet()} can suppress them.
     95    */
     96   public static List<Method> getIteratorDuplicateInitializingMethods() {
     97     return Arrays.asList(
     98         Platform.getMethod(MultisetIteratorTester.class, "testIteratorKnownOrder"),
     99         Platform.getMethod(MultisetIteratorTester.class, "testIteratorUnknownOrder"),
    100         Platform.getMethod(MultisetIteratorTester.class, "testRemovingIteratorKnownOrder"),
    101         Platform.getMethod(MultisetIteratorTester.class, "testRemovingIteratorUnknownOrder"));
    102   }
    103 }
    104