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