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