View Javadoc
1   /*
2    * Copyright (C) 2015 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.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.features.CollectionSize;
24  import com.google.common.collect.testing.features.ListFeature;
25  import java.util.Collections;
26  import java.util.List;
27  
28  /**
29   * A generic JUnit test which tests {@link List#replaceAll}. Can't be
30   * invoked directly; please see
31   * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
32   *
33   * @author Louis Wasserman
34   */
35  @GwtCompatible
36  public class ListReplaceAllTester<E> extends AbstractListTester<E> {
37    @ListFeature.Require(SUPPORTS_SET)
38    public void testReplaceAll() {
39      getList().replaceAll(e -> samples.e3());
40      expectContents(Collections.nCopies(getNumElements(), samples.e3()));
41    }
42  
43    @ListFeature.Require(SUPPORTS_SET)
44    public void testReplaceAll_changesSome() {
45      getList().replaceAll(e -> (e.equals(samples.e0())) ? samples.e3() : e);
46      E[] expected = createSamplesArray();
47      for (int i = 0; i < expected.length; i++) {
48        if (expected[i].equals(samples.e0())) {
49          expected[i] = samples.e3();
50        }
51      }
52      expectContents(expected);
53    }
54  
55    @CollectionSize.Require(absent = ZERO)
56    @ListFeature.Require(absent = SUPPORTS_SET)
57    public void testReplaceAll_unsupported() {
58      try {
59        getList().replaceAll(e -> e);
60        fail("replaceAll() should throw UnsupportedOperationException");
61      } catch (UnsupportedOperationException expected) {
62      }
63      expectUnchanged();
64    }
65  }