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.features.CollectionFeature.ALLOWS_NULL_VALUES; 020import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 021import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024import static java.util.Collections.singletonList; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.annotations.GwtIncompatible; 028import com.google.common.annotations.J2ktIncompatible; 029import com.google.common.collect.testing.AbstractCollectionTester; 030import com.google.common.collect.testing.Helpers; 031import com.google.common.collect.testing.MinimalCollection; 032import com.google.common.collect.testing.features.CollectionFeature; 033import com.google.common.collect.testing.features.CollectionSize; 034import java.lang.reflect.Method; 035import java.util.ConcurrentModificationException; 036import java.util.Iterator; 037import java.util.List; 038import org.checkerframework.checker.nullness.qual.Nullable; 039import org.junit.Ignore; 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(emulated = true) 049@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 050public class CollectionAddAllTester<E extends @Nullable Object> 051 extends AbstractCollectionTester<E> { 052 @CollectionFeature.Require(SUPPORTS_ADD) 053 public void testAddAll_supportedNothing() { 054 assertFalse("addAll(nothing) should return false", collection.addAll(emptyCollection())); 055 expectUnchanged(); 056 } 057 058 @CollectionFeature.Require(absent = SUPPORTS_ADD) 059 public void testAddAll_unsupportedNothing() { 060 try { 061 assertFalse( 062 "addAll(nothing) should return false or throw", collection.addAll(emptyCollection())); 063 } catch (UnsupportedOperationException tolerated) { 064 } 065 expectUnchanged(); 066 } 067 068 @CollectionFeature.Require(SUPPORTS_ADD) 069 public void testAddAll_supportedNonePresent() { 070 assertTrue( 071 "addAll(nonePresent) should return true", collection.addAll(createDisjointCollection())); 072 expectAdded(e3(), e4()); 073 } 074 075 @CollectionFeature.Require(absent = SUPPORTS_ADD) 076 public void testAddAll_unsupportedNonePresent() { 077 try { 078 collection.addAll(createDisjointCollection()); 079 fail("addAll(nonePresent) should throw"); 080 } catch (UnsupportedOperationException expected) { 081 } 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 try { 100 collection.addAll(MinimalCollection.of(e3(), e0())); 101 fail("addAll(somePresent) should throw"); 102 } catch (UnsupportedOperationException expected) { 103 } 104 expectUnchanged(); 105 } 106 107 @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 108 @CollectionSize.Require(absent = ZERO) 109 public void testAddAllConcurrentWithIteration() { 110 try { 111 Iterator<E> iterator = collection.iterator(); 112 assertTrue(collection.addAll(MinimalCollection.of(e3(), e0()))); 113 iterator.next(); 114 fail("Expected ConcurrentModificationException"); 115 } catch (ConcurrentModificationException expected) { 116 // success 117 } 118 } 119 120 @CollectionFeature.Require(absent = SUPPORTS_ADD) 121 @CollectionSize.Require(absent = ZERO) 122 public void testAddAll_unsupportedAllPresent() { 123 try { 124 assertFalse( 125 "addAll(allPresent) should return false or throw", 126 collection.addAll(MinimalCollection.of(e0()))); 127 } catch (UnsupportedOperationException tolerated) { 128 } 129 expectUnchanged(); 130 } 131 132 @CollectionFeature.Require( 133 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 134 absent = RESTRICTS_ELEMENTS) 135 public void testAddAll_nullSupported() { 136 List<E> containsNull = singletonList(null); 137 assertTrue("addAll(containsNull) should return true", collection.addAll(containsNull)); 138 /* 139 * We need (E) to force interpretation of null as the single element of a 140 * varargs array, not the array itself 141 */ 142 expectAdded((E) null); 143 } 144 145 @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES) 146 public void testAddAll_nullUnsupported() { 147 List<E> containsNull = singletonList(null); 148 try { 149 collection.addAll(containsNull); 150 fail("addAll(containsNull) should throw"); 151 } catch (NullPointerException expected) { 152 } 153 expectUnchanged(); 154 expectNullMissingWhenNullUnsupported( 155 "Should not contain null after unsupported addAll(containsNull)"); 156 } 157 158 @CollectionFeature.Require(SUPPORTS_ADD) 159 public void testAddAll_nullCollectionReference() { 160 try { 161 collection.addAll(null); 162 fail("addAll(null) should throw NullPointerException"); 163 } catch (NullPointerException expected) { 164 } 165 } 166 167 /** 168 * Returns the {@link Method} instance for {@link #testAddAll_nullUnsupported()} so that tests can 169 * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 170 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed. 171 */ 172 @J2ktIncompatible 173 @GwtIncompatible // reflection 174 public static Method getAddAllNullUnsupportedMethod() { 175 return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported"); 176 } 177 178 /** 179 * Returns the {@link Method} instance for {@link #testAddAll_unsupportedNonePresent()} so that 180 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we 181 * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for 182 * {@code entrySet().add()}</a>. 183 */ 184 @J2ktIncompatible 185 @GwtIncompatible // reflection 186 public static Method getAddAllUnsupportedNonePresentMethod() { 187 return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedNonePresent"); 188 } 189 190 /** 191 * Returns the {@link Method} instance for {@link #testAddAll_unsupportedSomePresent()} so that 192 * tests can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we 193 * figure out what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for 194 * {@code entrySet().add()}</a>. 195 */ 196 @J2ktIncompatible 197 @GwtIncompatible // reflection 198 public static Method getAddAllUnsupportedSomePresentMethod() { 199 return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_unsupportedSomePresent"); 200 } 201}