📄 abstracttestcollection.java
字号:
resetFull();
int size = collection.size();
assertTrue("Collection shouldn't change from retainAll elements",
!collection.retainAll(elements));
verify();
assertEquals("Collection size shouldn't change", size,
collection.size());
if (getFullElements().length > 1) {
resetFull();
size = collection.size();
int min = (getFullElements().length < 2 ? 0 : 2);
int max = (getFullElements().length <= 5 ? getFullElements().length - 1 : 5);
assertTrue("Collection should changed by partial retainAll",
collection.retainAll(elements.subList(min, max)));
confirmed.retainAll(elements.subList(min, max));
verify();
Iterator iter = collection.iterator();
while (iter.hasNext()) {
assertTrue("Collection only contains retained element",
elements.subList(min, max).contains(iter.next()));
}
}
resetFull();
HashSet set = new HashSet(elements);
size = collection.size();
assertTrue("Collection shouldn't change from retainAll without " +
"duplicate elements", !collection.retainAll(set));
verify();
assertEquals("Collection size didn't change from nonduplicate " +
"retainAll", size, collection.size());
}
/**
* Tests {@link Collection#size()}.
*/
public void testCollectionSize() {
resetEmpty();
assertEquals("Size of new Collection is 0.", 0, collection.size());
resetFull();
assertTrue("Size of full collection should be greater than zero",
collection.size() > 0);
}
/**
* Tests {@link Collection#toArray()}.
*/
public void testCollectionToArray() {
resetEmpty();
assertEquals("Empty Collection should return empty array for toArray",
0, collection.toArray().length);
resetFull();
Object[] array = collection.toArray();
assertEquals("Full collection toArray should be same size as " +
"collection", array.length, collection.size());
Object[] confirmedArray = confirmed.toArray();
assertEquals("length of array from confirmed collection should " +
"match the length of the collection's array",
confirmedArray.length, array.length);
boolean[] matched = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
assertTrue("Collection should contain element in toArray",
collection.contains(array[i]));
boolean match = false;
// find a match in the confirmed array
for(int j = 0; j < array.length; j++) {
// skip already matched
if(matched[j]) continue;
if(array[i] == confirmedArray[j] ||
(array[i] != null && array[i].equals(confirmedArray[j]))) {
matched[j] = true;
match = true;
break;
}
}
if(!match) {
fail("element " + i + " in returned array should be found " +
"in the confirmed collection's array");
}
}
for(int i = 0; i < matched.length; i++) {
assertEquals("Collection should return all its elements in " +
"toArray", true, matched[i]);
}
}
/**
* Tests {@link Collection#toArray(Object[])}.
*/
public void testCollectionToArray2() {
resetEmpty();
Object[] a = new Object[] { new Object(), null, null };
Object[] array = collection.toArray(a);
assertEquals("Given array shouldn't shrink", array, a);
assertEquals("Last element should be set to null", a[0], null);
verify();
resetFull();
try {
array = collection.toArray(new Void[0]);
fail("toArray(new Void[0]) should raise ArrayStore");
} catch (ArrayStoreException e) {
// expected
}
verify();
try {
array = collection.toArray(null);
fail("toArray(null) should raise NPE");
} catch (NullPointerException e) {
// expected
}
verify();
array = collection.toArray(new Object[0]);
a = collection.toArray();
assertEquals("toArrays should be equal",
Arrays.asList(array), Arrays.asList(a));
// Figure out if they're all the same class
// TODO: It'd be nicer to detect a common superclass
HashSet classes = new HashSet();
for (int i = 0; i < array.length; i++) {
classes.add((array[i] == null) ? null : array[i].getClass());
}
if (classes.size() > 1) return;
Class cl = (Class)classes.iterator().next();
if (Map.Entry.class.isAssignableFrom(cl)) { // check needed for protective cases like Predicated/Unmod map entrySet
cl = Map.Entry.class;
}
a = (Object[])Array.newInstance(cl, 0);
array = collection.toArray(a);
assertEquals("toArray(Object[]) should return correct array type",
a.getClass(), array.getClass());
assertEquals("type-specific toArrays should be equal",
Arrays.asList(array),
Arrays.asList(collection.toArray()));
verify();
}
/**
* Tests <code>toString</code> on a collection.
*/
public void testCollectionToString() {
resetEmpty();
assertTrue("toString shouldn't return null",
collection.toString() != null);
resetFull();
assertTrue("toString shouldn't return null",
collection.toString() != null);
}
/**
* If isRemoveSupported() returns false, tests to see that remove
* operations raise an UnsupportedOperationException.
*/
public void testUnsupportedRemove() {
if (isRemoveSupported()) return;
resetEmpty();
try {
collection.clear();
fail("clear should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
verify();
try {
collection.remove(null);
fail("remove should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
verify();
try {
collection.removeAll(null);
fail("removeAll should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
verify();
try {
collection.retainAll(null);
fail("removeAll should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
verify();
resetFull();
try {
Iterator iterator = collection.iterator();
iterator.next();
iterator.remove();
fail("iterator.remove should raise UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// expected
}
verify();
}
/**
* Tests that the collection's iterator is fail-fast.
*/
public void testCollectionIteratorFailFast() {
if (!isFailFastSupported()) return;
if (isAddSupported()) {
resetFull();
try {
Iterator iter = collection.iterator();
Object o = getOtherElements()[0];
collection.add(o);
confirmed.add(o);
iter.next();
fail("next after add should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
}
verify();
resetFull();
try {
Iterator iter = collection.iterator();
collection.addAll(Arrays.asList(getOtherElements()));
confirmed.addAll(Arrays.asList(getOtherElements()));
iter.next();
fail("next after addAll should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
}
verify();
}
if (!isRemoveSupported()) return;
resetFull();
try {
Iterator iter = collection.iterator();
collection.clear();
iter.next();
fail("next after clear should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
} catch (NoSuchElementException e) {
// (also legal given spec)
}
resetFull();
try {
Iterator iter = collection.iterator();
collection.remove(getFullElements()[0]);
iter.next();
fail("next after remove should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
}
resetFull();
try {
Iterator iter = collection.iterator();
List sublist = Arrays.asList(getFullElements()).subList(2,5);
collection.removeAll(sublist);
iter.next();
fail("next after removeAll should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
}
resetFull();
try {
Iterator iter = collection.iterator();
List sublist = Arrays.asList(getFullElements()).subList(2,5);
collection.retainAll(sublist);
iter.next();
fail("next after retainAll should raise ConcurrentModification");
} catch (ConcurrentModificationException e) {
// expected
}
}
public void testSerializeDeserializeThenCompare() throws Exception {
Object obj = makeCollection();
if (obj instanceof Serializable && isTestSerialization()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(obj);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
Object dest = in.readObject();
in.close();
if (isEqualsCheckable()) {
assertEquals("obj != deserialize(serialize(obj)) - EMPTY Collection", obj, dest);
}
}
obj = makeFullCollection();
if (obj instanceof Serializable && isTestSerialization()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(obj);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
Object dest = in.readObject();
in.close();
if (isEqualsCheckable()) {
assertEquals("obj != deserialize(serialize(obj)) - FULL Collection", obj, dest);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -