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.Helpers.getMethod; 020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 021import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 022import static com.google.common.collect.testing.features.CollectionSize.ONE; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 025import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 026 027import com.google.common.annotations.GwtCompatible; 028import com.google.common.annotations.GwtIncompatible; 029import com.google.common.annotations.J2ktIncompatible; 030import com.google.common.collect.testing.features.CollectionFeature; 031import com.google.common.collect.testing.features.CollectionSize; 032import com.google.common.collect.testing.features.ListFeature; 033import java.lang.reflect.Method; 034import java.util.ConcurrentModificationException; 035import java.util.Iterator; 036import org.junit.Ignore; 037 038/** 039 * A generic JUnit test which tests {@code add(int, Object)} operations on a list. Can't be invoked 040 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 041 * 042 * @author Chris Povirk 043 */ 044@GwtCompatible(emulated = true) 045@Ignore("test runners must not instantiate and run this directly, only via suites we build") 046// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 047@SuppressWarnings("JUnit4ClassUsedInJUnit3") 048public class ListAddAtIndexTester<E> extends AbstractListTester<E> { 049 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 050 @CollectionSize.Require(absent = ZERO) 051 public void testAddAtIndex_supportedPresent() { 052 getList().add(0, e0()); 053 expectAdded(0, e0()); 054 } 055 056 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 057 @CollectionSize.Require(absent = ZERO) 058 /* 059 * absent = ZERO isn't required, since unmodList.add() must 060 * throw regardless, but it keeps the method name accurate. 061 */ 062 public void testAddAtIndex_unsupportedPresent() { 063 assertThrows(UnsupportedOperationException.class, () -> getList().add(0, e0())); 064 expectUnchanged(); 065 } 066 067 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 068 public void testAddAtIndex_supportedNotPresent() { 069 getList().add(0, e3()); 070 expectAdded(0, e3()); 071 } 072 073 @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 074 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 075 public void testAddAtIndexConcurrentWithIteration() { 076 assertThrows( 077 ConcurrentModificationException.class, 078 () -> { 079 Iterator<E> iterator = collection.iterator(); 080 getList().add(0, e3()); 081 iterator.next(); 082 }); 083 } 084 085 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 086 public void testAddAtIndex_unsupportedNotPresent() { 087 assertThrows(UnsupportedOperationException.class, () -> getList().add(0, e3())); 088 expectUnchanged(); 089 expectMissing(e3()); 090 } 091 092 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 093 @CollectionSize.Require(absent = {ZERO, ONE}) 094 public void testAddAtIndex_middle() { 095 getList().add(getNumElements() / 2, e3()); 096 expectAdded(getNumElements() / 2, e3()); 097 } 098 099 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 100 @CollectionSize.Require(absent = ZERO) 101 public void testAddAtIndex_end() { 102 getList().add(getNumElements(), e3()); 103 expectAdded(getNumElements(), e3()); 104 } 105 106 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 107 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 108 public void testAddAtIndex_nullSupported() { 109 getList().add(0, null); 110 expectAdded(0, (E) null); 111 } 112 113 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 114 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 115 public void testAddAtIndex_nullUnsupported() { 116 assertThrows(NullPointerException.class, () -> getList().add(0, null)); 117 expectUnchanged(); 118 expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(n, null)"); 119 } 120 121 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 122 public void testAddAtIndex_negative() { 123 assertThrows(IndexOutOfBoundsException.class, () -> getList().add(-1, e3())); 124 expectUnchanged(); 125 expectMissing(e3()); 126 } 127 128 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 129 public void testAddAtIndex_tooLarge() { 130 assertThrows(IndexOutOfBoundsException.class, () -> getList().add(getNumElements() + 1, e3())); 131 expectUnchanged(); 132 expectMissing(e3()); 133 } 134 135 /** 136 * Returns the {@link Method} instance for {@link #testAddAtIndex_nullSupported()} so that tests 137 * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 138 */ 139 @J2ktIncompatible 140 @GwtIncompatible // reflection 141 public static Method getAddNullSupportedMethod() { 142 return getMethod(ListAddAtIndexTester.class, "testAddAtIndex_nullSupported"); 143 } 144}