001/*
002 * Copyright (C) 2015 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.features.CollectionSize.ZERO;
020import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
021import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
022import static java.util.Collections.nCopies;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.features.CollectionSize;
026import com.google.common.collect.testing.features.ListFeature;
027import java.util.List;
028import org.junit.Ignore;
029
030/**
031 * A generic JUnit test which tests {@link List#replaceAll}. Can't be invoked directly; please see
032 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
033 *
034 * @author Louis Wasserman
035 */
036@GwtCompatible
037@Ignore("test runners must not instantiate and run this directly, only via suites we build")
038// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039@SuppressWarnings("JUnit4ClassUsedInJUnit3")
040public class ListReplaceAllTester<E> extends AbstractListTester<E> {
041  @ListFeature.Require(SUPPORTS_SET)
042  public void testReplaceAll() {
043    getList().replaceAll(e -> samples.e3());
044    expectContents(nCopies(getNumElements(), samples.e3()));
045  }
046
047  @ListFeature.Require(SUPPORTS_SET)
048  public void testReplaceAll_changesSome() {
049    getList().replaceAll(e -> e.equals(samples.e0()) ? samples.e3() : e);
050    E[] expected = createSamplesArray();
051    for (int i = 0; i < expected.length; i++) {
052      if (expected[i].equals(samples.e0())) {
053        expected[i] = samples.e3();
054      }
055    }
056    expectContents(expected);
057  }
058
059  @CollectionSize.Require(absent = ZERO)
060  @ListFeature.Require(absent = SUPPORTS_SET)
061  public void testReplaceAll_unsupported() {
062    assertThrows(UnsupportedOperationException.class, () -> getList().replaceAll(e -> e));
063    expectUnchanged();
064  }
065}