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_ITERATOR_REMOVE;
     19 
     20 import com.google.common.annotations.GwtCompatible;
     21 import com.google.common.annotations.GwtIncompatible;
     22 import com.google.common.collect.testing.Helpers;
     23 import com.google.common.collect.testing.IteratorFeature;
     24 import com.google.common.collect.testing.IteratorTester;
     25 import com.google.common.collect.testing.features.CollectionFeature;
     26 
     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>(4, IteratorFeature.MODIFIABLE, getSubjectGenerator().order(
     44         Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
     45         IteratorTester.KnownOrder.KNOWN_ORDER) {
     46       @Override
     47       protected Iterator<E> newTargetIterator() {
     48         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     49             .iterator();
     50       }
     51     }.test();
     52   }
     53 
     54   @SuppressWarnings("unchecked")
     55   @CollectionFeature.Require(value = SUPPORTS_ITERATOR_REMOVE, absent = KNOWN_ORDER)
     56   public void testRemovingIteratorUnknownOrder() {
     57     new IteratorTester<E>(4, IteratorFeature.MODIFIABLE, Arrays.asList(samples.e0, samples.e1,
     58         samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
     59       @Override
     60       protected Iterator<E> newTargetIterator() {
     61         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     62             .iterator();
     63       }
     64     }.test();
     65   }
     66 
     67   @SuppressWarnings("unchecked")
     68   @CollectionFeature.Require(value = KNOWN_ORDER, absent = SUPPORTS_ITERATOR_REMOVE)
     69   public void testIteratorKnownOrder() {
     70     new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, getSubjectGenerator().order(
     71         Arrays.asList(samples.e0, samples.e1, samples.e1, samples.e2)),
     72         IteratorTester.KnownOrder.KNOWN_ORDER) {
     73       @Override
     74       protected Iterator<E> newTargetIterator() {
     75         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     76             .iterator();
     77       }
     78     }.test();
     79   }
     80 
     81   @SuppressWarnings("unchecked")
     82   @CollectionFeature.Require(absent = {SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER})
     83   public void testIteratorUnknownOrder() {
     84     new IteratorTester<E>(4, IteratorFeature.UNMODIFIABLE, Arrays.asList(samples.e0, samples.e1,
     85         samples.e1, samples.e2), IteratorTester.KnownOrder.UNKNOWN_ORDER) {
     86       @Override
     87       protected Iterator<E> newTargetIterator() {
     88         return getSubjectGenerator().create(samples.e0, samples.e1, samples.e1, samples.e2)
     89             .iterator();
     90       }
     91     }.test();
     92   }
     93 
     94   /**
     95    * Returns {@link Method} instances for the tests that assume multisets support duplicates so that
     96    * the test of {@code Multisets.forSet()} can suppress them.
     97    */
     98   @GwtIncompatible("reflection")
     99   public static List<Method> getIteratorDuplicateInitializingMethods() {
    100     return Arrays.asList(
    101         Helpers.getMethod(MultisetIteratorTester.class, "testIteratorKnownOrder"),
    102         Helpers.getMethod(MultisetIteratorTester.class, "testIteratorUnknownOrder"),
    103         Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorKnownOrder"),
    104         Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorUnknownOrder"));
    105   }
    106 }
    107