Coverage Summary for Class: ListAddAllAtIndexTester (com.google.common.collect.testing.testers)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| ListAddAllAtIndexTester | 100% (1/1) | 93.3% (14/15) | 76% (57/75) |
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_WITH_INDEX; 23 import static java.util.Collections.singletonList; 24 25 import com.google.common.annotations.GwtCompatible; 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 import java.util.List; 31 import org.junit.Ignore; 32 33 /** 34 * A generic JUnit test which tests {@code addAll(int, Collection)} operations on a list. Can't be 35 * invoked directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 36 * 37 * @author Chris Povirk 38 */ 39 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 40 @GwtCompatible 41 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 42 public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> { 43 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 44 @CollectionSize.Require(absent = ZERO) 45 public void testAddAllAtIndex_supportedAllPresent() { 46 assertTrue( 47 "addAll(n, allPresent) should return true", 48 getList().addAll(0, MinimalCollection.of(e0()))); 49 expectAdded(0, e0()); 50 } 51 52 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 53 @CollectionSize.Require(absent = ZERO) 54 public void testAddAllAtIndex_unsupportedAllPresent() { 55 try { 56 getList().addAll(0, MinimalCollection.of(e0())); 57 fail("addAll(n, allPresent) should throw"); 58 } catch (UnsupportedOperationException expected) { 59 } 60 expectUnchanged(); 61 } 62 63 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 64 @CollectionSize.Require(absent = ZERO) 65 public void testAddAllAtIndex_supportedSomePresent() { 66 assertTrue( 67 "addAll(n, allPresent) should return true", 68 getList().addAll(0, MinimalCollection.of(e0(), e3()))); 69 expectAdded(0, e0(), 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(e0(), e3())); 77 fail("addAll(n, allPresent) should throw"); 78 } catch (UnsupportedOperationException expected) { 79 } 80 expectUnchanged(); 81 expectMissing(e3()); 82 } 83 84 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 85 public void testAddAllAtIndex_supportedNothing() { 86 assertFalse("addAll(n, nothing) should return false", getList().addAll(0, emptyCollection())); 87 expectUnchanged(); 88 } 89 90 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 91 public void testAddAllAtIndex_unsupportedNothing() { 92 try { 93 assertFalse( 94 "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 = MinimalCollection.of(e0(), e1(), e0(), e1()); 104 assertTrue("addAll(n, hasDuplicates) should return true", getList().addAll(0, elementsToAdd)); 105 expectAdded(0, e0(), e1(), e0(), e1()); 106 } 107 108 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 109 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 110 public void testAddAllAtIndex_nullSupported() { 111 List<E> containsNull = singletonList(null); 112 assertTrue("addAll(n, containsNull) should return true", getList().addAll(0, containsNull)); 113 /* 114 * We need (E) to force interpretation of null as the single element of a 115 * varargs array, not the array itself 116 */ 117 expectAdded(0, (E) null); 118 } 119 120 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 121 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 122 public void testAddAllAtIndex_nullUnsupported() { 123 List<E> containsNull = singletonList(null); 124 try { 125 getList().addAll(0, containsNull); 126 fail("addAll(n, containsNull) should throw"); 127 } catch (NullPointerException expected) { 128 } 129 expectUnchanged(); 130 expectNullMissingWhenNullUnsupported( 131 "Should not contain null after unsupported addAll(n, containsNull)"); 132 } 133 134 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 135 @CollectionSize.Require(absent = {ZERO, ONE}) 136 public void testAddAllAtIndex_middle() { 137 assertTrue( 138 "addAll(middle, disjoint) should return true", 139 getList().addAll(getNumElements() / 2, createDisjointCollection())); 140 expectAdded(getNumElements() / 2, createDisjointCollection()); 141 } 142 143 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 144 @CollectionSize.Require(absent = ZERO) 145 public void testAddAllAtIndex_end() { 146 assertTrue( 147 "addAll(end, disjoint) should return true", 148 getList().addAll(getNumElements(), createDisjointCollection())); 149 expectAdded(getNumElements(), createDisjointCollection()); 150 } 151 152 @ListFeature.Require(SUPPORTS_ADD_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(e3())); 166 fail("addAll(-1, e) should throw"); 167 } catch (IndexOutOfBoundsException expected) { 168 } 169 expectUnchanged(); 170 expectMissing(e3()); 171 } 172 173 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 174 public void testAddAllAtIndex_tooLarge() { 175 try { 176 getList().addAll(getNumElements() + 1, MinimalCollection.of(e3())); 177 fail("addAll(size + 1, e) should throw"); 178 } catch (IndexOutOfBoundsException expected) { 179 } 180 expectUnchanged(); 181 expectMissing(e3()); 182 } 183 }