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