001/* 002 * Copyright (C) 2008 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.CollectionSize.ONE; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 023import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 024import static java.util.Collections.singletonList; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.collect.testing.MinimalCollection; 028import com.google.common.collect.testing.features.CollectionFeature; 029import com.google.common.collect.testing.features.CollectionSize; 030import com.google.common.collect.testing.features.ListFeature; 031import java.util.List; 032import org.junit.Ignore; 033 034/** 035 * A generic JUnit test which tests {@code addAll(int, Collection)} operations on a list. Can't be 036 * invoked directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 037 * 038 * @author Chris Povirk 039 */ 040@GwtCompatible 041@Ignore("test runners must not instantiate and run this directly, only via suites we build") 042// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 043@SuppressWarnings("JUnit4ClassUsedInJUnit3") 044public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> { 045 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 046 @CollectionSize.Require(absent = ZERO) 047 public void testAddAllAtIndex_supportedAllPresent() { 048 assertTrue( 049 "addAll(n, allPresent) should return true", 050 getList().addAll(0, MinimalCollection.of(e0()))); 051 expectAdded(0, e0()); 052 } 053 054 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 055 @CollectionSize.Require(absent = ZERO) 056 public void testAddAllAtIndex_unsupportedAllPresent() { 057 assertThrows( 058 UnsupportedOperationException.class, () -> getList().addAll(0, MinimalCollection.of(e0()))); 059 expectUnchanged(); 060 } 061 062 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 063 @CollectionSize.Require(absent = ZERO) 064 public void testAddAllAtIndex_supportedSomePresent() { 065 assertTrue( 066 "addAll(n, allPresent) should return true", 067 getList().addAll(0, MinimalCollection.of(e0(), e3()))); 068 expectAdded(0, e0(), e3()); 069 } 070 071 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 072 @CollectionSize.Require(absent = ZERO) 073 public void testAddAllAtIndex_unsupportedSomePresent() { 074 assertThrows( 075 UnsupportedOperationException.class, 076 () -> getList().addAll(0, MinimalCollection.of(e0(), e3()))); 077 expectUnchanged(); 078 expectMissing(e3()); 079 } 080 081 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 082 public void testAddAllAtIndex_supportedNothing() { 083 assertFalse("addAll(n, nothing) should return false", getList().addAll(0, emptyCollection())); 084 expectUnchanged(); 085 } 086 087 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 088 public void testAddAllAtIndex_unsupportedNothing() { 089 try { 090 assertFalse( 091 "addAll(n, nothing) should return false or throw", 092 getList().addAll(0, emptyCollection())); 093 } catch (UnsupportedOperationException tolerated) { 094 } 095 expectUnchanged(); 096 } 097 098 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 099 public void testAddAllAtIndex_withDuplicates() { 100 MinimalCollection<E> elementsToAdd = MinimalCollection.of(e0(), e1(), e0(), e1()); 101 assertTrue("addAll(n, hasDuplicates) should return true", getList().addAll(0, elementsToAdd)); 102 expectAdded(0, e0(), e1(), e0(), e1()); 103 } 104 105 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 106 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 107 public void testAddAllAtIndex_nullSupported() { 108 List<E> containsNull = singletonList(null); 109 assertTrue("addAll(n, containsNull) should return true", getList().addAll(0, containsNull)); 110 /* 111 * We need (E) to force interpretation of null as the single element of a 112 * varargs array, not the array itself 113 */ 114 expectAdded(0, (E) null); 115 } 116 117 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 118 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 119 public void testAddAllAtIndex_nullUnsupported() { 120 List<E> containsNull = singletonList(null); 121 assertThrows(NullPointerException.class, () -> getList().addAll(0, containsNull)); 122 expectUnchanged(); 123 expectNullMissingWhenNullUnsupported( 124 "Should not contain null after unsupported addAll(n, containsNull)"); 125 } 126 127 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 128 @CollectionSize.Require(absent = {ZERO, ONE}) 129 public void testAddAllAtIndex_middle() { 130 assertTrue( 131 "addAll(middle, disjoint) should return true", 132 getList().addAll(getNumElements() / 2, createDisjointCollection())); 133 expectAdded(getNumElements() / 2, createDisjointCollection()); 134 } 135 136 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 137 @CollectionSize.Require(absent = ZERO) 138 public void testAddAllAtIndex_end() { 139 assertTrue( 140 "addAll(end, disjoint) should return true", 141 getList().addAll(getNumElements(), createDisjointCollection())); 142 expectAdded(getNumElements(), createDisjointCollection()); 143 } 144 145 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 146 public void testAddAllAtIndex_nullCollectionReference() { 147 assertThrows(NullPointerException.class, () -> getList().addAll(0, null)); 148 expectUnchanged(); 149 } 150 151 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 152 public void testAddAllAtIndex_negative() { 153 assertThrows( 154 IndexOutOfBoundsException.class, () -> getList().addAll(-1, MinimalCollection.of(e3()))); 155 expectUnchanged(); 156 expectMissing(e3()); 157 } 158 159 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 160 public void testAddAllAtIndex_tooLarge() { 161 assertThrows( 162 IndexOutOfBoundsException.class, 163 () -> getList().addAll(getNumElements() + 1, MinimalCollection.of(e3()))); 164 expectUnchanged(); 165 expectMissing(e3()); 166 } 167}