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