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