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. 037@SuppressWarnings("JUnit4ClassUsedInJUnit3") 038public class ListReplaceAllTester<E> extends AbstractListTester<E> { 039 @ListFeature.Require(SUPPORTS_SET) 040 public void testReplaceAll() { 041 getList().replaceAll(e -> samples.e3()); 042 expectContents(Collections.nCopies(getNumElements(), samples.e3())); 043 } 044 045 @ListFeature.Require(SUPPORTS_SET) 046 public void testReplaceAll_changesSome() { 047 getList().replaceAll(e -> e.equals(samples.e0()) ? samples.e3() : e); 048 E[] expected = createSamplesArray(); 049 for (int i = 0; i < expected.length; i++) { 050 if (expected[i].equals(samples.e0())) { 051 expected[i] = samples.e3(); 052 } 053 } 054 expectContents(expected); 055 } 056 057 @CollectionSize.Require(absent = ZERO) 058 @ListFeature.Require(absent = SUPPORTS_SET) 059 public void testReplaceAll_unsupported() { 060 try { 061 getList().replaceAll(e -> e); 062 fail("replaceAll() should throw UnsupportedOperationException"); 063 } catch (UnsupportedOperationException expected) { 064 } 065 expectUnchanged(); 066 } 067}