| 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.CollectionSize.ZERO; |
| 21 |
import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET; |
| 22 |
|
| 23 |
import com.google.common.annotations.GwtCompatible; |
| 24 |
import com.google.common.annotations.GwtIncompatible; |
| 25 |
import com.google.common.collect.testing.Helpers; |
| 26 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 27 |
import com.google.common.collect.testing.features.CollectionSize; |
| 28 |
import com.google.common.collect.testing.features.ListFeature; |
| 29 |
import java.lang.reflect.Method; |
| 30 |
import org.junit.Ignore; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@GwtCompatible(emulated = true) |
| 39 |
@Ignore |
| 40 |
public class ListSetTester<E> extends AbstractListTester<E> { |
| 41 |
@ListFeature.Require(SUPPORTS_SET) |
| 42 |
@CollectionSize.Require(absent = ZERO) |
| 43 |
public void testSet() { |
| 44 |
doTestSet(e3()); |
| 45 |
} |
| 46 |
|
| 47 |
@CollectionSize.Require(absent = ZERO) |
| 48 |
@CollectionFeature.Require(ALLOWS_NULL_VALUES) |
| 49 |
@ListFeature.Require(SUPPORTS_SET) |
| 50 |
public void testSet_null() { |
| 51 |
doTestSet(null); |
| 52 |
} |
| 53 |
|
| 54 |
@CollectionSize.Require(absent = ZERO) |
| 55 |
@CollectionFeature.Require(ALLOWS_NULL_VALUES) |
| 56 |
@ListFeature.Require(SUPPORTS_SET) |
| 57 |
public void testSet_replacingNull() { |
| 58 |
E[] elements = createSamplesArray(); |
| 59 |
int i = aValidIndex(); |
| 60 |
elements[i] = null; |
| 61 |
collection = getSubjectGenerator().create(elements); |
| 62 |
|
| 63 |
doTestSet(e3()); |
| 64 |
} |
| 65 |
|
| 66 |
private void doTestSet(E newValue) { |
| 67 |
int index = aValidIndex(); |
| 68 |
E initialValue = getList().get(index); |
| 69 |
assertEquals( |
| 70 |
"set(i, x) should return the old element at position i.", |
| 71 |
initialValue, |
| 72 |
getList().set(index, newValue)); |
| 73 |
assertEquals("After set(i, x), get(i) should return x", newValue, getList().get(index)); |
| 74 |
assertEquals("set() should not change the size of a list.", getNumElements(), getList().size()); |
| 75 |
} |
| 76 |
|
| 77 |
@ListFeature.Require(SUPPORTS_SET) |
| 78 |
public void testSet_indexTooLow() { |
| 79 |
try { |
| 80 |
getList().set(-1, e3()); |
| 81 |
fail("set(-1) should throw IndexOutOfBoundsException"); |
| 82 |
} catch (IndexOutOfBoundsException expected) { |
| 83 |
} |
| 84 |
expectUnchanged(); |
| 85 |
} |
| 86 |
|
| 87 |
@ListFeature.Require(SUPPORTS_SET) |
| 88 |
public void testSet_indexTooHigh() { |
| 89 |
int index = getNumElements(); |
| 90 |
try { |
| 91 |
getList().set(index, e3()); |
| 92 |
fail("set(size) should throw IndexOutOfBoundsException"); |
| 93 |
} catch (IndexOutOfBoundsException expected) { |
| 94 |
} |
| 95 |
expectUnchanged(); |
| 96 |
} |
| 97 |
|
| 98 |
@CollectionSize.Require(absent = ZERO) |
| 99 |
@ListFeature.Require(absent = SUPPORTS_SET) |
| 100 |
public void testSet_unsupported() { |
| 101 |
try { |
| 102 |
getList().set(aValidIndex(), e3()); |
| 103 |
fail("set() should throw UnsupportedOperationException"); |
| 104 |
} catch (UnsupportedOperationException expected) { |
| 105 |
} |
| 106 |
expectUnchanged(); |
| 107 |
} |
| 108 |
|
| 109 |
@CollectionSize.Require(ZERO) |
| 110 |
@ListFeature.Require(absent = SUPPORTS_SET) |
| 111 |
public void testSet_unsupportedByEmptyList() { |
| 112 |
try { |
| 113 |
getList().set(0, e3()); |
| 114 |
fail("set() should throw UnsupportedOperationException or IndexOutOfBoundsException"); |
| 115 |
} catch (UnsupportedOperationException | IndexOutOfBoundsException tolerated) { |
| 116 |
} |
| 117 |
expectUnchanged(); |
| 118 |
} |
| 119 |
|
| 120 |
@CollectionSize.Require(absent = ZERO) |
| 121 |
@ListFeature.Require(SUPPORTS_SET) |
| 122 |
@CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) |
| 123 |
public void testSet_nullUnsupported() { |
| 124 |
try { |
| 125 |
getList().set(aValidIndex(), null); |
| 126 |
fail("set(null) should throw NullPointerException"); |
| 127 |
} catch (NullPointerException expected) { |
| 128 |
} |
| 129 |
expectUnchanged(); |
| 130 |
} |
| 131 |
|
| 132 |
private int aValidIndex() { |
| 133 |
return getList().size() / 2; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@GwtIncompatible |
| 146 |
public static Method getSetNullSupportedMethod() { |
| 147 |
return Helpers.getMethod(ListSetTester.class, "testSet_null"); |
| 148 |
} |
| 149 |
} |