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.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 021import static com.google.common.collect.testing.features.CollectionSize.ONE; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.annotations.J2ktIncompatible; 028import com.google.common.collect.testing.Helpers; 029import com.google.common.collect.testing.features.CollectionFeature; 030import com.google.common.collect.testing.features.CollectionSize; 031import com.google.common.collect.testing.features.ListFeature; 032import java.lang.reflect.Method; 033import java.util.ConcurrentModificationException; 034import java.util.Iterator; 035import org.junit.Ignore; 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(emulated = true) 044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 046public class ListAddAtIndexTester<E> extends AbstractListTester<E> { 047 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 048 @CollectionSize.Require(absent = ZERO) 049 public void testAddAtIndex_supportedPresent() { 050 getList().add(0, e0()); 051 expectAdded(0, e0()); 052 } 053 054 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 055 @CollectionSize.Require(absent = ZERO) 056 /* 057 * absent = ZERO isn't required, since unmodList.add() must 058 * throw regardless, but it keeps the method name accurate. 059 */ 060 public void testAddAtIndex_unsupportedPresent() { 061 try { 062 getList().add(0, e0()); 063 fail("add(n, present) should throw"); 064 } catch (UnsupportedOperationException expected) { 065 } 066 expectUnchanged(); 067 } 068 069 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 070 public void testAddAtIndex_supportedNotPresent() { 071 getList().add(0, e3()); 072 expectAdded(0, e3()); 073 } 074 075 @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 076 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 077 public void testAddAtIndexConcurrentWithIteration() { 078 try { 079 Iterator<E> iterator = collection.iterator(); 080 getList().add(0, e3()); 081 iterator.next(); 082 fail("Expected ConcurrentModificationException"); 083 } catch (ConcurrentModificationException expected) { 084 // success 085 } 086 } 087 088 @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 089 public void testAddAtIndex_unsupportedNotPresent() { 090 try { 091 getList().add(0, e3()); 092 fail("add(n, notPresent) should throw"); 093 } catch (UnsupportedOperationException expected) { 094 } 095 expectUnchanged(); 096 expectMissing(e3()); 097 } 098 099 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 100 @CollectionSize.Require(absent = {ZERO, ONE}) 101 public void testAddAtIndex_middle() { 102 getList().add(getNumElements() / 2, e3()); 103 expectAdded(getNumElements() / 2, e3()); 104 } 105 106 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 107 @CollectionSize.Require(absent = ZERO) 108 public void testAddAtIndex_end() { 109 getList().add(getNumElements(), e3()); 110 expectAdded(getNumElements(), e3()); 111 } 112 113 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 114 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 115 public void testAddAtIndex_nullSupported() { 116 getList().add(0, null); 117 expectAdded(0, (E) null); 118 } 119 120 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 121 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 122 public void testAddAtIndex_nullUnsupported() { 123 try { 124 getList().add(0, null); 125 fail("add(n, null) should throw"); 126 } catch (NullPointerException expected) { 127 } 128 expectUnchanged(); 129 expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(n, null)"); 130 } 131 132 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 133 public void testAddAtIndex_negative() { 134 try { 135 getList().add(-1, e3()); 136 fail("add(-1, e) should throw"); 137 } catch (IndexOutOfBoundsException expected) { 138 } 139 expectUnchanged(); 140 expectMissing(e3()); 141 } 142 143 @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 144 public void testAddAtIndex_tooLarge() { 145 try { 146 getList().add(getNumElements() + 1, e3()); 147 fail("add(size + 1, e) should throw"); 148 } catch (IndexOutOfBoundsException expected) { 149 } 150 expectUnchanged(); 151 expectMissing(e3()); 152 } 153 154 /** 155 * Returns the {@link Method} instance for {@link #testAddAtIndex_nullSupported()} so that tests 156 * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 157 */ 158 @J2ktIncompatible 159 @GwtIncompatible // reflection 160 public static Method getAddNullSupportedMethod() { 161 return Helpers.getMethod(ListAddAtIndexTester.class, "testAddAtIndex_nullSupported"); 162 } 163}