1 /* 2 * Copyright (C) 2008 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.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; 20 import static com.google.common.collect.testing.features.CollectionSize.ONE; 21 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 22 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 23 import static com.google.common.truth.Truth.assertThat; 24 25 import com.google.common.annotations.GwtCompatible; 26 import com.google.common.collect.testing.MinimalCollection; 27 import com.google.common.collect.testing.features.CollectionFeature; 28 import com.google.common.collect.testing.features.CollectionSize; 29 30 import java.util.Arrays; 31 32 /** 33 * A generic JUnit test which tests {@code retainAll} operations on a list. 34 * Can't be invoked directly; please see 35 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 36 * 37 * @author Chris Povirk 38 */ 39 @GwtCompatible 40 public class ListRetainAllTester<E> extends AbstractListTester<E> { 41 @CollectionFeature.Require(SUPPORTS_REMOVE) 42 @CollectionSize.Require(absent = {ZERO, ONE}) testRetainAll_duplicatesKept()43 public void testRetainAll_duplicatesKept() { 44 E[] array = createSamplesArray(); 45 array[1] = samples.e0; 46 collection = getSubjectGenerator().create(array); 47 assertFalse("containsDuplicates.retainAll(superset) should return false", 48 collection.retainAll(MinimalCollection.of(createSamplesArray()))); 49 expectContents(array); 50 } 51 52 @SuppressWarnings("unchecked") 53 @CollectionFeature.Require(SUPPORTS_REMOVE) 54 @CollectionSize.Require(SEVERAL) testRetainAll_duplicatesRemoved()55 public void testRetainAll_duplicatesRemoved() { 56 E[] array = createSamplesArray(); 57 array[1] = samples.e0; 58 collection = getSubjectGenerator().create(array); 59 assertTrue("containsDuplicates.retainAll(subset) should return true", 60 collection.retainAll(MinimalCollection.of(samples.e2))); 61 expectContents(samples.e2); 62 } 63 64 @SuppressWarnings("unchecked") 65 @CollectionFeature.Require(SUPPORTS_REMOVE) 66 @CollectionSize.Require(SEVERAL) testRetainAll_countIgnored()67 public void testRetainAll_countIgnored() { 68 resetContainer(getSubjectGenerator().create(samples.e0, samples.e2, samples.e1, samples.e0)); 69 assertTrue(getList().retainAll(Arrays.asList(samples.e0, samples.e1))); 70 assertThat(getList()).has().exactly(samples.e0, samples.e1, samples.e0).inOrder(); 71 } 72 } 73