| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.CollectionFeature.RESTRICTS_ELEMENTS; |
| 22 |
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; |
| 23 |
import static com.google.common.collect.testing.features.CollectionSize.ZERO; |
| 24 |
|
| 25 |
import com.google.common.annotations.GwtCompatible; |
| 26 |
import com.google.common.annotations.GwtIncompatible; |
| 27 |
import com.google.common.collect.testing.AbstractCollectionTester; |
| 28 |
import com.google.common.collect.testing.Helpers; |
| 29 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 30 |
import com.google.common.collect.testing.features.CollectionSize; |
| 31 |
import java.lang.reflect.Method; |
| 32 |
import java.util.ConcurrentModificationException; |
| 33 |
import java.util.Iterator; |
| 34 |
import org.junit.Ignore; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@SuppressWarnings("unchecked") |
| 44 |
@GwtCompatible(emulated = true) |
| 45 |
@Ignore |
| 46 |
public class CollectionAddTester<E> extends AbstractCollectionTester<E> { |
| 47 |
@CollectionFeature.Require(SUPPORTS_ADD) |
| 48 |
public void testAdd_supportedNotPresent() { |
| 49 |
assertTrue("add(notPresent) should return true", collection.add(e3())); |
| 50 |
expectAdded(e3()); |
| 51 |
} |
| 52 |
|
| 53 |
@CollectionFeature.Require(absent = SUPPORTS_ADD) |
| 54 |
public void testAdd_unsupportedNotPresent() { |
| 55 |
try { |
| 56 |
collection.add(e3()); |
| 57 |
fail("add(notPresent) should throw"); |
| 58 |
} catch (UnsupportedOperationException expected) { |
| 59 |
} |
| 60 |
expectUnchanged(); |
| 61 |
expectMissing(e3()); |
| 62 |
} |
| 63 |
|
| 64 |
@CollectionFeature.Require(absent = SUPPORTS_ADD) |
| 65 |
@CollectionSize.Require(absent = ZERO) |
| 66 |
public void testAdd_unsupportedPresent() { |
| 67 |
try { |
| 68 |
assertFalse("add(present) should return false or throw", collection.add(e0())); |
| 69 |
} catch (UnsupportedOperationException tolerated) { |
| 70 |
} |
| 71 |
expectUnchanged(); |
| 72 |
} |
| 73 |
|
| 74 |
@CollectionFeature.Require( |
| 75 |
value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, |
| 76 |
absent = RESTRICTS_ELEMENTS) |
| 77 |
public void testAdd_nullSupported() { |
| 78 |
assertTrue("add(null) should return true", collection.add(null)); |
| 79 |
expectAdded((E) null); |
| 80 |
} |
| 81 |
|
| 82 |
@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES) |
| 83 |
public void testAdd_nullUnsupported() { |
| 84 |
try { |
| 85 |
collection.add(null); |
| 86 |
fail("add(null) should throw"); |
| 87 |
} catch (NullPointerException expected) { |
| 88 |
} |
| 89 |
expectUnchanged(); |
| 90 |
expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(null)"); |
| 91 |
} |
| 92 |
|
| 93 |
@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) |
| 94 |
@CollectionSize.Require(absent = ZERO) |
| 95 |
public void testAddConcurrentWithIteration() { |
| 96 |
try { |
| 97 |
Iterator<E> iterator = collection.iterator(); |
| 98 |
assertTrue(collection.add(e3())); |
| 99 |
iterator.next(); |
| 100 |
fail("Expected ConcurrentModificationException"); |
| 101 |
} catch (ConcurrentModificationException expected) { |
| 102 |
|
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@GwtIncompatible |
| 116 |
public static Method getAddNullSupportedMethod() { |
| 117 |
return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported"); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@GwtIncompatible |
| 127 |
public static Method getAddNullUnsupportedMethod() { |
| 128 |
return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported"); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@GwtIncompatible |
| 138 |
public static Method getAddUnsupportedNotPresentMethod() { |
| 139 |
return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent"); |
| 140 |
} |
| 141 |
} |