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; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.collect.testing.MinimalCollection; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import com.google.common.collect.testing.features.ListFeature; 030import java.util.List; 031import org.junit.Ignore; 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 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 041public class ListAddAllAtIndexTester<E> extends AbstractListTester<E> { 042 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 043 @CollectionSize.Require(absent = ZERO) 044 public void testAddAllAtIndex_supportedAllPresent() { 045 assertTrue( 046 "addAll(n, allPresent) should return true", 047 getList().addAll(0, MinimalCollection.of(e0()))); 048 expectAdded(0, e0()); 049 } 050 051 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 052 @CollectionSize.Require(absent = ZERO) 053 public void testAddAllAtIndex_unsupportedAllPresent() { 054 try { 055 getList().addAll(0, MinimalCollection.of(e0())); 056 fail("addAll(n, allPresent) should throw"); 057 } catch (UnsupportedOperationException expected) { 058 } 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 try { 075 getList().addAll(0, MinimalCollection.of(e0(), e3())); 076 fail("addAll(n, allPresent) should throw"); 077 } catch (UnsupportedOperationException expected) { 078 } 079 expectUnchanged(); 080 expectMissing(e3()); 081 } 082 083 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 084 public void testAddAllAtIndex_supportedNothing() { 085 assertFalse("addAll(n, nothing) should return false", getList().addAll(0, emptyCollection())); 086 expectUnchanged(); 087 } 088 089 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 090 public void testAddAllAtIndex_unsupportedNothing() { 091 try { 092 assertFalse( 093 "addAll(n, nothing) should return false or throw", 094 getList().addAll(0, emptyCollection())); 095 } catch (UnsupportedOperationException tolerated) { 096 } 097 expectUnchanged(); 098 } 099 100 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 101 public void testAddAllAtIndex_withDuplicates() { 102 MinimalCollection<E> elementsToAdd = MinimalCollection.of(e0(), e1(), e0(), e1()); 103 assertTrue("addAll(n, hasDuplicates) should return true", getList().addAll(0, elementsToAdd)); 104 expectAdded(0, e0(), e1(), e0(), e1()); 105 } 106 107 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 108 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 109 public void testAddAllAtIndex_nullSupported() { 110 List<E> containsNull = singletonList(null); 111 assertTrue("addAll(n, containsNull) should return true", getList().addAll(0, containsNull)); 112 /* 113 * We need (E) to force interpretation of null as the single element of a 114 * varargs array, not the array itself 115 */ 116 expectAdded(0, (E) null); 117 } 118 119 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 120 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 121 public void testAddAllAtIndex_nullUnsupported() { 122 List<E> containsNull = singletonList(null); 123 try { 124 getList().addAll(0, containsNull); 125 fail("addAll(n, containsNull) should throw"); 126 } catch (NullPointerException expected) { 127 } 128 expectUnchanged(); 129 expectNullMissingWhenNullUnsupported( 130 "Should not contain null after unsupported addAll(n, containsNull)"); 131 } 132 133 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 134 @CollectionSize.Require(absent = {ZERO, ONE}) 135 public void testAddAllAtIndex_middle() { 136 assertTrue( 137 "addAll(middle, disjoint) should return true", 138 getList().addAll(getNumElements() / 2, createDisjointCollection())); 139 expectAdded(getNumElements() / 2, createDisjointCollection()); 140 } 141 142 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 143 @CollectionSize.Require(absent = ZERO) 144 public void testAddAllAtIndex_end() { 145 assertTrue( 146 "addAll(end, disjoint) should return true", 147 getList().addAll(getNumElements(), createDisjointCollection())); 148 expectAdded(getNumElements(), createDisjointCollection()); 149 } 150 151 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 152 public void testAddAllAtIndex_nullCollectionReference() { 153 try { 154 getList().addAll(0, null); 155 fail("addAll(n, null) should throw"); 156 } catch (NullPointerException expected) { 157 } 158 expectUnchanged(); 159 } 160 161 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 162 public void testAddAllAtIndex_negative() { 163 try { 164 getList().addAll(-1, MinimalCollection.of(e3())); 165 fail("addAll(-1, e) should throw"); 166 } catch (IndexOutOfBoundsException expected) { 167 } 168 expectUnchanged(); 169 expectMissing(e3()); 170 } 171 172 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 173 public void testAddAllAtIndex_tooLarge() { 174 try { 175 getList().addAll(getNumElements() + 1, MinimalCollection.of(e3())); 176 fail("addAll(size + 1, e) should throw"); 177 } catch (IndexOutOfBoundsException expected) { 178 } 179 expectUnchanged(); 180 expectMissing(e3()); 181 } 182}