CollectionAddAllTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.collect.testing.testers; 18 19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 20import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 21import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 23import static com.google.common.collect.testing.features.CollectionSize.ZERO; 24import static java.util.Collections.singletonList; 25 26import com.google.common.annotations.GwtCompatible; 27import com.google.common.annotations.GwtIncompatible; 28import com.google.common.collect.testing.AbstractCollectionTester; 29import com.google.common.collect.testing.Helpers; 30import com.google.common.collect.testing.MinimalCollection; 31import com.google.common.collect.testing.features.CollectionFeature; 32import com.google.common.collect.testing.features.CollectionSize; 33 34import java.lang.reflect.Method; 35import java.util.ConcurrentModificationException; 36import java.util.Iterator; 37import java.util.List; 38 39/** 40 * A generic JUnit test which tests addAll operations on a collection. Can't be 41 * invoked directly; please see 42 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 43 * 44 * <p>This class is GWT compatible. 45 * 46 * @author Chris Povirk 47 * @author Kevin Bourrillion 48 */ 49@SuppressWarnings("unchecked") // too many "unchecked generic array creations" 50@GwtCompatible(emulated = true) 51public class CollectionAddAllTester<E> extends AbstractCollectionTester<E> { 52 @CollectionFeature.Require(SUPPORTS_ADD) 53 public void testAddAll_supportedNothing() { 54 assertFalse("addAll(nothing) should return false", 55 collection.addAll(emptyCollection())); 56 expectUnchanged(); 57 } 58 59 @CollectionFeature.Require(absent = SUPPORTS_ADD) 60 public void testAddAll_unsupportedNothing() { 61 try { 62 assertFalse("addAll(nothing) should return false or throw", 63 collection.addAll(emptyCollection())); 64 } catch (UnsupportedOperationException tolerated) { 65 } 66 expectUnchanged(); 67 } 68 69 @CollectionFeature.Require(SUPPORTS_ADD) 70 public void testAddAll_supportedNonePresent() { 71 assertTrue("addAll(nonePresent) should return true", 72 collection.addAll(createDisjointCollection())); 73 expectAdded(samples.e3, samples.e4); 74 } 75 76 @CollectionFeature.Require(absent = SUPPORTS_ADD) 77 public void testAddAll_unsupportedNonePresent() { 78 try { 79 collection.addAll(createDisjointCollection()); 80 fail("addAll(nonePresent) should throw"); 81 } catch (UnsupportedOperationException expected) { 82 } 83 expectUnchanged(); 84 expectMissing(samples.e3, samples.e4); 85 } 86 87 @CollectionFeature.Require(SUPPORTS_ADD) 88 @CollectionSize.Require(absent = ZERO) 89 public void testAddAll_supportedSomePresent() { 90 assertTrue("addAll(somePresent) should return true", 91 collection.addAll(MinimalCollection.of(samples.e3, samples.e0))); 92 assertTrue("should contain " + samples.e3, collection.contains(samples.e3)); 93 assertTrue("should contain " + samples.e0, collection.contains(samples.e0)); 94 } 95 96 @CollectionFeature.Require(absent = SUPPORTS_ADD) 97 @CollectionSize.Require(absent = ZERO) 98 public void testAddAll_unsupportedSomePresent() { 99 try { 100 collection.addAll(MinimalCollection.of(samples.e3, samples.e0)); 101 fail("addAll(somePresent) should throw"); 102 } catch (UnsupportedOperationException expected) { 103 } 104 expectUnchanged(); 105 } 106 107 @CollectionFeature.Require({SUPPORTS_ADD, 108 FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 109 @CollectionSize.Require(absent = ZERO) 110 public void testAddAllConcurrentWithIteration() { 111 try { 112 Iterator<E> iterator = collection.iterator(); 113 assertTrue(collection.addAll(MinimalCollection.of(samples.e3, samples.e0))); 114 iterator.next(); 115 fail("Expected ConcurrentModificationException"); 116 } catch (ConcurrentModificationException expected) { 117 // success 118 } 119 } 120 121 @CollectionFeature.Require(absent = SUPPORTS_ADD) 122 @CollectionSize.Require(absent = ZERO) 123 public void testAddAll_unsupportedAllPresent() { 124 try { 125 assertFalse("addAll(allPresent) should return false or throw", 126 collection.addAll(MinimalCollection.of(samples.e0))); 127 } catch (UnsupportedOperationException tolerated) { 128 } 129 expectUnchanged(); 130 } 131 132 @CollectionFeature.Require(value = {SUPPORTS_ADD, 133 ALLOWS_NULL_VALUES}, absent = RESTRICTS_ELEMENTS) 134 public void testAddAll_nullSupported() { 135 List<E> containsNull = singletonList(null); 136 assertTrue("addAll(containsNull) should return true", collection 137 .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, 146 absent = ALLOWS_NULL_VALUES) 147 public void testAddAll_nullUnsupported() { 148 List<E> containsNull = singletonList(null); 149 try { 150 collection.addAll(containsNull); 151 fail("addAll(containsNull) should throw"); 152 } catch (NullPointerException expected) { 153 } 154 expectUnchanged(); 155 expectNullMissingWhenNullUnsupported( 156 "Should not contain null after unsupported addAll(containsNull)"); 157 } 158 159 @CollectionFeature.Require(SUPPORTS_ADD) 160 public void testAddAll_nullCollectionReference() { 161 try { 162 collection.addAll(null); 163 fail("addAll(null) should throw NullPointerException"); 164 } catch (NullPointerException expected) { 165 } 166 } 167 168 /** 169 * Returns the {@link Method} instance for {@link 170 * #testAddAll_nullUnsupported()} so that tests can suppress it with {@code 171 * FeatureSpecificTestSuiteBuilder.suppressing()} until <a 172 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun 173 * bug 5045147</a> is fixed. 174 */ 175 @GwtIncompatible("reflection") 176 public static Method getAddAllNullUnsupportedMethod() { 177 return Helpers.getMethod(CollectionAddAllTester.class, "testAddAll_nullUnsupported"); 178 } 179} 180