001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.Helpers.assertContentsInOrder;
020import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
021import static com.google.common.collect.testing.features.CollectionSize.ONE;
022import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
023import static com.google.common.collect.testing.features.CollectionSize.ZERO;
024import static java.util.Arrays.asList;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.MinimalCollection;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030
031/**
032 * A generic JUnit test which tests {@code retainAll} operations on a list. Can't be invoked
033 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
034 *
035 * @author Chris Povirk
036 */
037@GwtCompatible
038public class ListRetainAllTester<E> extends AbstractListTester<E> {
039  @CollectionFeature.Require(SUPPORTS_REMOVE)
040  @CollectionSize.Require(absent = {ZERO, ONE})
041  public void testRetainAll_duplicatesKept() {
042    E[] array = createSamplesArray();
043    array[1] = e0();
044    collection = getSubjectGenerator().create(array);
045    assertFalse(
046        "containsDuplicates.retainAll(superset) should return false",
047        collection.retainAll(MinimalCollection.of(createSamplesArray())));
048    expectContents(array);
049  }
050
051  @CollectionFeature.Require(SUPPORTS_REMOVE)
052  @CollectionSize.Require(SEVERAL)
053  public void testRetainAll_duplicatesRemoved() {
054    E[] array = createSamplesArray();
055    array[1] = e0();
056    collection = getSubjectGenerator().create(array);
057    assertTrue(
058        "containsDuplicates.retainAll(subset) should return true",
059        collection.retainAll(MinimalCollection.of(e2())));
060    expectContents(e2());
061  }
062
063  @CollectionFeature.Require(SUPPORTS_REMOVE)
064  @CollectionSize.Require(SEVERAL)
065  public void testRetainAll_countIgnored() {
066    resetContainer(getSubjectGenerator().create(e0(), e2(), e1(), e0()));
067    assertTrue(getList().retainAll(asList(e0(), e1())));
068    assertContentsInOrder(getList(), e0(), e1(), e0());
069  }
070}