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