| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing; |
| 18 |
|
| 19 |
import com.google.common.annotations.GwtCompatible; |
| 20 |
import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 21 |
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.Collection; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
import org.junit.Ignore; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@GwtCompatible |
| 38 |
@Ignore |
| 39 |
public abstract class AbstractContainerTester<C, E> |
| 40 |
extends AbstractTester<OneSizeTestContainerGenerator<C, E>> { |
| 41 |
protected SampleElements<E> samples; |
| 42 |
protected C container; |
| 43 |
|
| 44 |
@Override |
| 45 |
@OverridingMethodsMustInvokeSuper |
| 46 |
public void setUp() throws Exception { |
| 47 |
super.setUp(); |
| 48 |
samples = this.getSubjectGenerator().samples(); |
| 49 |
resetContainer(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected abstract Collection<E> actualContents(); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@CanIgnoreReturnValue |
| 66 |
protected C resetContainer() { |
| 67 |
return resetContainer(getSubjectGenerator().createTestSubject()); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@CanIgnoreReturnValue |
| 81 |
protected C resetContainer(C newValue) { |
| 82 |
container = newValue; |
| 83 |
return container; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected final void expectContents(E... elements) { |
| 91 |
expectContents(Arrays.asList(elements)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected void expectContents(Collection<E> expected) { |
| 111 |
Helpers.assertEqualIgnoringOrder(expected, actualContents()); |
| 112 |
} |
| 113 |
|
| 114 |
protected void expectUnchanged() { |
| 115 |
expectContents(getOrderedElements()); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected final void expectAdded(E... elements) { |
| 138 |
List<E> expected = Helpers.copyToList(getSampleElements()); |
| 139 |
expected.addAll(Arrays.asList(elements)); |
| 140 |
expectContents(expected); |
| 141 |
} |
| 142 |
|
| 143 |
protected final void expectAdded(int index, E... elements) { |
| 144 |
expectAdded(index, Arrays.asList(elements)); |
| 145 |
} |
| 146 |
|
| 147 |
protected final void expectAdded(int index, Collection<E> elements) { |
| 148 |
List<E> expected = Helpers.copyToList(getSampleElements()); |
| 149 |
expected.addAll(index, elements); |
| 150 |
expectContents(expected); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected void expectMissing(E... elements) { |
| 160 |
for (E element : elements) { |
| 161 |
assertFalse("Should not contain " + element, actualContents().contains(element)); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
protected E[] createSamplesArray() { |
| 166 |
E[] array = getSubjectGenerator().createArray(getNumElements()); |
| 167 |
getSampleElements().toArray(array); |
| 168 |
return array; |
| 169 |
} |
| 170 |
|
| 171 |
protected E[] createOrderedArray() { |
| 172 |
E[] array = getSubjectGenerator().createArray(getNumElements()); |
| 173 |
getOrderedElements().toArray(array); |
| 174 |
return array; |
| 175 |
} |
| 176 |
|
| 177 |
public static class ArrayWithDuplicate<E> { |
| 178 |
public final E[] elements; |
| 179 |
public final E duplicate; |
| 180 |
|
| 181 |
private ArrayWithDuplicate(E[] elements, E duplicate) { |
| 182 |
this.elements = elements; |
| 183 |
this.duplicate = duplicate; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
protected ArrayWithDuplicate<E> createArrayWithDuplicateElement() { |
| 191 |
E[] elements = createSamplesArray(); |
| 192 |
E duplicate = elements[(elements.length / 2) - 1]; |
| 193 |
elements[(elements.length / 2) + 1] = duplicate; |
| 194 |
return new ArrayWithDuplicate<>(elements, duplicate); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
protected int getNumElements() { |
| 200 |
return getSubjectGenerator().getCollectionSize().getNumElements(); |
| 201 |
} |
| 202 |
|
| 203 |
protected Collection<E> getSampleElements(int howMany) { |
| 204 |
return getSubjectGenerator().getSampleElements(howMany); |
| 205 |
} |
| 206 |
|
| 207 |
protected Collection<E> getSampleElements() { |
| 208 |
return getSampleElements(getNumElements()); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
protected List<E> getOrderedElements() { |
| 217 |
List<E> list = new ArrayList<>(); |
| 218 |
for (E e : getSubjectGenerator().order(new ArrayList<E>(getSampleElements()))) { |
| 219 |
list.add(e); |
| 220 |
} |
| 221 |
return Collections.unmodifiableList(list); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
protected int getNullLocation() { |
| 229 |
return getNumElements() / 2; |
| 230 |
} |
| 231 |
|
| 232 |
@SuppressWarnings("unchecked") |
| 233 |
protected MinimalCollection<E> createDisjointCollection() { |
| 234 |
return MinimalCollection.of(e3(), e4()); |
| 235 |
} |
| 236 |
|
| 237 |
@SuppressWarnings("unchecked") |
| 238 |
protected MinimalCollection<E> emptyCollection() { |
| 239 |
return MinimalCollection.<E>of(); |
| 240 |
} |
| 241 |
|
| 242 |
protected final E e0() { |
| 243 |
return samples.e0(); |
| 244 |
} |
| 245 |
|
| 246 |
protected final E e1() { |
| 247 |
return samples.e1(); |
| 248 |
} |
| 249 |
|
| 250 |
protected final E e2() { |
| 251 |
return samples.e2(); |
| 252 |
} |
| 253 |
|
| 254 |
protected final E e3() { |
| 255 |
return samples.e3(); |
| 256 |
} |
| 257 |
|
| 258 |
protected final E e4() { |
| 259 |
return samples.e4(); |
| 260 |
} |
| 261 |
} |