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 java.util.Collections.nCopies; 022import static org.junit.Assert.assertThrows; 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; 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 036public class ListReplaceAllTester<E> extends AbstractListTester<E> { 037 @ListFeature.Require(SUPPORTS_SET) 038 public void testReplaceAll() { 039 getList().replaceAll(e -> samples.e3()); 040 expectContents(nCopies(getNumElements(), samples.e3())); 041 } 042 043 @ListFeature.Require(SUPPORTS_SET) 044 public void testReplaceAll_changesSome() { 045 getList().replaceAll(e -> e.equals(samples.e0()) ? samples.e3() : e); 046 E[] expected = createSamplesArray(); 047 for (int i = 0; i < expected.length; i++) { 048 if (expected[i].equals(samples.e0())) { 049 expected[i] = samples.e3(); 050 } 051 } 052 expectContents(expected); 053 } 054 055 @CollectionSize.Require(absent = ZERO) 056 @ListFeature.Require(absent = SUPPORTS_SET) 057 public void testReplaceAll_unsupported() { 058 assertThrows(UnsupportedOperationException.class, () -> getList().replaceAll(e -> e)); 059 expectUnchanged(); 060 } 061}