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