Coverage Summary for Class: ListAddAtIndexTester (com.google.common.collect.testing.testers)

Class Class, % Method, % Line, %
ListAddAtIndexTester 100% (1/1) 92.3% (12/13) 70.7% (41/58)


1 /* 2  * Copyright (C) 2008 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect.testing.testers; 18  19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 20 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 21 import static com.google.common.collect.testing.features.CollectionSize.ONE; 22 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 23 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX; 24  25 import com.google.common.annotations.GwtCompatible; 26 import com.google.common.annotations.GwtIncompatible; 27 import com.google.common.collect.testing.Helpers; 28 import com.google.common.collect.testing.features.CollectionFeature; 29 import com.google.common.collect.testing.features.CollectionSize; 30 import com.google.common.collect.testing.features.ListFeature; 31 import java.lang.reflect.Method; 32 import java.util.ConcurrentModificationException; 33 import java.util.Iterator; 34 import org.junit.Ignore; 35  36 /** 37  * A generic JUnit test which tests {@code add(int, Object)} operations on a list. Can't be invoked 38  * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 39  * 40  * @author Chris Povirk 41  */ 42 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 43 @GwtCompatible(emulated = true) 44 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 45 public class ListAddAtIndexTester<E> extends AbstractListTester<E> { 46  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 47  @CollectionSize.Require(absent = ZERO) 48  public void testAddAtIndex_supportedPresent() { 49  getList().add(0, e0()); 50  expectAdded(0, e0()); 51  } 52  53  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 54  @CollectionSize.Require(absent = ZERO) 55  /* 56  * absent = ZERO isn't required, since unmodList.add() must 57  * throw regardless, but it keeps the method name accurate. 58  */ 59  public void testAddAtIndex_unsupportedPresent() { 60  try { 61  getList().add(0, e0()); 62  fail("add(n, present) should throw"); 63  } catch (UnsupportedOperationException expected) { 64  } 65  expectUnchanged(); 66  } 67  68  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 69  public void testAddAtIndex_supportedNotPresent() { 70  getList().add(0, e3()); 71  expectAdded(0, e3()); 72  } 73  74  @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 75  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 76  public void testAddAtIndexConcurrentWithIteration() { 77  try { 78  Iterator<E> iterator = collection.iterator(); 79  getList().add(0, e3()); 80  iterator.next(); 81  fail("Expected ConcurrentModificationException"); 82  } catch (ConcurrentModificationException expected) { 83  // success 84  } 85  } 86  87  @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) 88  public void testAddAtIndex_unsupportedNotPresent() { 89  try { 90  getList().add(0, e3()); 91  fail("add(n, notPresent) should throw"); 92  } catch (UnsupportedOperationException expected) { 93  } 94  expectUnchanged(); 95  expectMissing(e3()); 96  } 97  98  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 99  @CollectionSize.Require(absent = {ZERO, ONE}) 100  public void testAddAtIndex_middle() { 101  getList().add(getNumElements() / 2, e3()); 102  expectAdded(getNumElements() / 2, e3()); 103  } 104  105  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 106  @CollectionSize.Require(absent = ZERO) 107  public void testAddAtIndex_end() { 108  getList().add(getNumElements(), e3()); 109  expectAdded(getNumElements(), e3()); 110  } 111  112  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 113  @CollectionFeature.Require(ALLOWS_NULL_VALUES) 114  public void testAddAtIndex_nullSupported() { 115  getList().add(0, null); 116  expectAdded(0, (E) null); 117  } 118  119  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 120  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 121  public void testAddAtIndex_nullUnsupported() { 122  try { 123  getList().add(0, null); 124  fail("add(n, null) should throw"); 125  } catch (NullPointerException expected) { 126  } 127  expectUnchanged(); 128  expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(n, null)"); 129  } 130  131  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 132  public void testAddAtIndex_negative() { 133  try { 134  getList().add(-1, e3()); 135  fail("add(-1, e) should throw"); 136  } catch (IndexOutOfBoundsException expected) { 137  } 138  expectUnchanged(); 139  expectMissing(e3()); 140  } 141  142  @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX) 143  public void testAddAtIndex_tooLarge() { 144  try { 145  getList().add(getNumElements() + 1, e3()); 146  fail("add(size + 1, e) should throw"); 147  } catch (IndexOutOfBoundsException expected) { 148  } 149  expectUnchanged(); 150  expectMissing(e3()); 151  } 152  153  /** 154  * Returns the {@link Method} instance for {@link #testAddAtIndex_nullSupported()} so that tests 155  * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 156  */ 157  @GwtIncompatible // reflection 158  public static Method getAddNullSupportedMethod() { 159  return Helpers.getMethod(ListAddAtIndexTester.class, "testAddAtIndex_nullSupported"); 160  } 161 }