ListAddAllAtIndexTester.java revision 3c77433663281544363151bf284b0240dfd22a42
1/* 2 * Copyright (C) 2008 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.CollectionSize.ONE; 21import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 23import static java.util.Collections.singletonList; 24 25import com.google.common.annotations.GwtCompatible; 26import com.google.common.collect.testing.MinimalCollection; 27import com.google.common.collect.testing.features.CollectionFeature; 28import com.google.common.collect.testing.features.CollectionSize; 29import com.google.common.collect.testing.features.ListFeature; 30 31import java.util.List; 32 33/** 34 * A generic JUnit test which tests {@code addAll(int, Collection)} operations 35 * on a list. Can't be invoked directly; please see 36 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 37 * 38 * <p>This class is GWT compatible. 39 * 40 * @author Chris Povirk 41 */ 42@SuppressWarnings("unchecked") // too many "unchecked generic array creations" 43@GwtCompatible 44public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> { 45 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 46 @CollectionSize.Require(absent = ZERO) 47 public void testAddAllAtIndex_supportedAllPresent() { 48 assertTrue("addAll(n, allPresent) should return true", 49 getList().addAll(0, MinimalCollection.of(samples.e0))); 50 expectAdded(0, samples.e0); 51 } 52 53 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 54 @CollectionSize.Require(absent = ZERO) 55 public void testAddAllAtIndex_unsupportedAllPresent() { 56 try { 57 getList().addAll(0, MinimalCollection.of(samples.e0)); 58 fail("addAll(n, allPresent) should throw"); 59 } catch (UnsupportedOperationException expected) { 60 } 61 expectUnchanged(); 62 } 63 64 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 65 @CollectionSize.Require(absent = ZERO) 66 public void testAddAllAtIndex_supportedSomePresent() { 67 assertTrue("addAll(n, allPresent) should return true", 68 getList().addAll(0, MinimalCollection.of(samples.e0, samples.e3))); 69 expectAdded(0, samples.e0, samples.e3); 70 } 71 72 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 73 @CollectionSize.Require(absent = ZERO) 74 public void testAddAllAtIndex_unsupportedSomePresent() { 75 try { 76 getList().addAll(0, MinimalCollection.of(samples.e0, samples.e3)); 77 fail("addAll(n, allPresent) should throw"); 78 } catch (UnsupportedOperationException expected) { 79 } 80 expectUnchanged(); 81 expectMissing(samples.e3); 82 } 83 84 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 85 public void testAddAllAtIndex_supportedNothing() { 86 assertFalse("addAll(n, nothing) should return false", 87 getList().addAll(0, emptyCollection())); 88 expectUnchanged(); 89 } 90 91 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 92 public void testAddAllAtIndex_unsupportedNothing() { 93 try { 94 assertFalse("addAll(n, nothing) should return false or throw", 95 getList().addAll(0, emptyCollection())); 96 } catch (UnsupportedOperationException tolerated) { 97 } 98 expectUnchanged(); 99 } 100 101 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 102 public void testAddAllAtIndex_withDuplicates() { 103 MinimalCollection<E> elementsToAdd 104 = MinimalCollection.of(samples.e0, samples.e1, samples.e0, samples.e1); 105 assertTrue("addAll(n, hasDuplicates) should return true", 106 getList().addAll(0, elementsToAdd)); 107 expectAdded(0, samples.e0, samples.e1, samples.e0, samples.e1); 108 } 109 110 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 111 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 112 public void testAddAllAtIndex_nullSupported() { 113 List<E> containsNull = singletonList(null); 114 assertTrue("addAll(n, containsNull) should return true", 115 getList().addAll(0, containsNull)); 116 /* 117 * We need (E) to force interpretation of null as the single element of a 118 * varargs array, not the array itself 119 */ 120 expectAdded(0, (E) null); 121 } 122 123 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 124 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 125 public void testAddAllAtIndex_nullUnsupported() { 126 List<E> containsNull = singletonList(null); 127 try { 128 getList().addAll(0, containsNull); 129 fail("addAll(n, containsNull) should throw"); 130 } catch (NullPointerException expected) { 131 } 132 expectUnchanged(); 133 expectNullMissingWhenNullUnsupported( 134 "Should not contain null after unsupported addAll(n, containsNull)"); 135 } 136 137 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 138 @CollectionSize.Require(absent = {ZERO, ONE}) 139 public void testAddAllAtIndex_middle() { 140 assertTrue("addAll(middle, disjoint) should return true", 141 getList().addAll(getNumElements() / 2, createDisjointCollection())); 142 expectAdded(getNumElements() / 2, createDisjointCollection()); 143 } 144 145 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 146 @CollectionSize.Require(absent = ZERO) 147 public void testAddAllAtIndex_end() { 148 assertTrue("addAll(end, disjoint) should return true", 149 getList().addAll(getNumElements(), createDisjointCollection())); 150 expectAdded(getNumElements(), createDisjointCollection()); 151 } 152 153 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 154 public void testAddAllAtIndex_nullCollectionReference() { 155 try { 156 getList().addAll(0, null); 157 fail("addAll(n, null) should throw"); 158 } catch (NullPointerException expected) { 159 } 160 expectUnchanged(); 161 } 162 163 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 164 public void testAddAllAtIndex_negative() { 165 try { 166 getList().addAll(-1, MinimalCollection.of(samples.e3)); 167 fail("addAll(-1, e) should throw"); 168 } catch (IndexOutOfBoundsException expected) { 169 } 170 expectUnchanged(); 171 expectMissing(samples.e3); 172 } 173 174 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 175 public void testAddAllAtIndex_tooLarge() { 176 try { 177 getList().addAll(getNumElements() + 1, MinimalCollection.of(samples.e3)); 178 fail("addAll(size + 1, e) should throw"); 179 } catch (IndexOutOfBoundsException expected) { 180 } 181 expectUnchanged(); 182 expectMissing(samples.e3); 183 } 184} 185