001/* 002 * Copyright (C) 2007 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.Helpers.getMethod; 020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 021import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 022import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 023import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 024import static com.google.common.collect.testing.features.CollectionSize.ZERO; 025import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 026import static java.util.Collections.singletonList; 027 028import com.google.common.annotations.GwtCompatible; 029import com.google.common.annotations.GwtIncompatible; 030import com.google.common.annotations.J2ktIncompatible; 031import com.google.common.collect.testing.AbstractCollectionTester; 032import com.google.common.collect.testing.MinimalCollection; 033import com.google.common.collect.testing.features.CollectionFeature; 034import com.google.common.collect.testing.features.CollectionSize; 035import java.lang.reflect.Method; 036import java.util.ConcurrentModificationException; 037import java.util.Iterator; 038import java.util.List; 039import org.checkerframework.checker.nullness.qual.Nullable; 040import org.junit.Ignore; 041 042/** 043 * A generic JUnit test which tests addAll operations on a collection. Can't be invoked directly; 044 * please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 045 * 046 * @author Chris Povirk 047 * @author Kevin Bourrillion 048 */ 049@GwtCompatible(emulated = true) 050@Ignore("test runners must not instantiate and run this directly, only via suites we build") 051// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 052@SuppressWarnings("JUnit4ClassUsedInJUnit3") 053public class CollectionAddAllTester<E extends @Nullable Object> 054 extends AbstractCollectionTester<E> { 055 @CollectionFeature.Require(SUPPORTS_ADD) 056 public void testAddAll_supportedNothing() { 057 assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection())); 058 expectUnchanged(); 059 } 060 061 @CollectionFeature.Require(absent = SUPPORTS_ADD) 062 public void testAddAll_unsupportedNothing() { 063 try { 064 assertFalse( 065 "addAll(nothing) should return false or throw", collection.addAll(emptyCollection())); 066 } catch (UnsupportedOperationException tolerated) { 067 } 068 expectUnchanged(); 069 } 070 071 @CollectionFeature.Require(SUPPORTS_ADD) 072 public void testAddAll_supportedNonePresent() { 073 assertTrue( 074 "addAll(nonePresent) should return true", collection.addAll(createDisjointCollection())); 075 expectAdded(e3(), e4()); 076 } 077 078 @CollectionFeature.Require(absent = SUPPORTS_ADD) 079 public void testAddAll_unsupportedNonePresent() { 080 assertThrows( 081 UnsupportedOperationException.class, () -> collection.addAll(createDisjointCollection())); 082 expectUnchanged(); 083 expectMissing(e3(), e4()); 084 } 085 086 @CollectionFeature.Require(SUPPORTS_ADD) 087 @CollectionSize.Require(absent = ZERO) 088 public void testAddAll_supportedSomePresent() { 089 assertTrue( 090 "addAll(somePresent) should return true", 091 collection.addAll(MinimalCollection.of(e3(), e0()))); 092 assertTrue("should contain " + e3(), collection.contains(e3())); 093 assertTrue("should contain " + e0(), collection.contains(e0())); 094 } 095 096 @CollectionFeature.Require(absent = SUPPORTS_ADD) 097 @CollectionSize.Require(absent = ZERO) 098 public void testAddAll_unsupportedSomePresent() { 099 assertThrows( 100 UnsupportedOperationException.class, 101 () -> collection.addAll(MinimalCollection.of(e3(), e0()))); 102 expectUnchanged(); 103 } 104 105 @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 106 @CollectionSize.Require(absent = ZERO) 107 public void testAddAllConcurrentWithIteration() { 108 assertThrows( 109 ConcurrentModificationException.class, 110 () -> { 111 Iterator<E> iterator = collection.iterator(); 112 assertTrue(collection.addAll(MinimalCollection.of(e3(), e0()))); 113 iterator.next(); 114 }); 115 } 116 117 @CollectionFeature.Require(absent = SUPPORTS_ADD) 118 @CollectionSize.Require(absent = ZERO) 119 public void testAddAll_unsupportedAllPresent() { 120 try { 121 assertFalse( 122 "addAll(allPresent) should return false or throw", 123 collection.addAll(MinimalCollection.of(e0()))); 124 } catch (UnsupportedOperationException tolerated) { 125 } 126 expectUnchanged(); 127 } 128 129 @CollectionFeature.Require( 130 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 131 absent = RESTRICTS_ELEMENTS) 132 public void testAddAll_nullSupported() { 133 List<E> containsNull = singletonList(null); 134 assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull)); 135 /* 136 * We need (E) to force interpretation of null as the single element of a 137 * varargs array, not the array itself 138 */ 139 expectAdded((E) null); 140 } 141 142 @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES) 143 public void testAddAll_nullUnsupported() { 144 List<E> containsNull = singletonList(null); 145 assertThrows(NullPointerException.class, () -> collection.addAll(containsNull)); 146 expectUnchanged(); 147 expectNullMissingWhenNullUnsupported( 148 "Should not contain null after unsupported addAll(containsNull)"); 149 } 150 151 @CollectionFeature.Require(SUPPORTS_ADD) 152 public void testAddAll_nullCollectionReference() { 153 assertThrows(NullPointerException.class, () -> collection.addAll(null)); 154 } 155 156 /** 157 * Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can 158 * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 159 * href="https://bugs.openjdk.org/browse/JDK-5045147">JDK-5045147</a> is fixed. 160 */ 161 @J2ktIncompatible 162 @GwtIncompatible // reflection 163 public static Method getAddAllNullUnsupportedMethod() { 164 return getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported"); 165 } 166 167 /** 168 * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that 169 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we 170 * figure out what to do with <a 171 * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code 172 * ConcurrentHashMap} support for {@code entrySet().add()}</a>. 173 */ 174 @J2ktIncompatible 175 @GwtIncompatible // reflection 176 public static Method getAddAllUnsupportedNonePresentMethod() { 177 return getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent"); 178 } 179 180 /** 181 * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that 182 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we 183 * figure out what to do with <a 184 * href="https://github.com/openjdk/jdk/blob/c25c4896ad9ef031e3cddec493aef66ff87c48a7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L4830">{@code 185 * ConcurrentHashMap} support for {@code entrySet().add()}</a>. 186 */ 187 @J2ktIncompatible 188 @GwtIncompatible // reflection 189 public static Method getAddAllUnsupportedSomePresentMethod() { 190 return getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent"); 191 } 192}