📄 testcollection.java
字号:
verify();
assertTrue("Collection should shrink after removeAll",
collection.size() < size);
Iterator iter = all.iterator();
while (iter.hasNext()) {
assertTrue("Collection shouldn't contain removed element",
!collection.contains(iter.next()));
}
}
/**
* Tests {@link Collection#retainAll(Collection)}.
*/
public void testCollectionRetainAll() {
if (!isRemoveSupported()) return;
resetEmpty();
List elements = Arrays.asList(getFullElements());
List other = Arrays.asList(getOtherElements());
assertTrue("Empty retainAll() should return false",
!collection.retainAll(Collections.EMPTY_SET));
verify();
assertTrue("Empty retainAll() should return false",
!collection.retainAll(elements));
verify();
resetFull();
assertTrue("Collection should change from retainAll empty",
collection.retainAll(Collections.EMPTY_SET));
confirmed.retainAll(Collections.EMPTY_SET);
verify();
resetFull();
assertTrue("Collection changed from retainAll other",
collection.retainAll(other));
confirmed.retainAll(other);
verify();
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());
resetFull();
size = collection.size();
assertTrue("Collection should changed by partial retainAll",
collection.retainAll(elements.subList(2, 5)));
confirmed.retainAll(elements.subList(2, 5));
verify();
Iterator iter = collection.iterator();
while (iter.hasNext()) {
assertTrue("Collection only contains retained element",
elements.subList(2, 5).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();
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 (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
}
}
/**
* Returns a list of elements suitable for return by
* {@link getFullElements()}. The array returned by this method
* does not include null, but does include a variety of objects
* of different types. Override getFullElements to return
* the results of this method if your collection does not support
* the null element.
*/
public static Object[] getFullNonNullElements() {
return new Object[] {
new String(""),
new String("One"),
new Integer(2),
"Three",
new Integer(4),
"One",
new Double(5),
new Float(6),
"Seven",
"Eight",
new String("Nine"),
new Integer(10),
new Short((short)11),
new Long(12),
"Thirteen",
"14",
"15",
new Byte((byte)16)
};
}
/**
* Returns the default list of objects returned by
* {@link getOtherElements()}. Includes many objects
* of different types.
*/
public static Object[] getOtherNonNullElements() {
return new Object[] {
new Integer(0),
new Float(0),
new Double(0),
"Zero",
new Short((short)0),
new Byte((byte)0),
new Long(0),
new Character('\u0000'),
"0"
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getFullElements()}. Override getFullElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
public static Object[] getFullNonNullStringElements() {
return new Object[] {
"If","the","dull","substance","of","my","flesh","were","thought",
"Injurious","distance","could","not","stop","my","way",
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getOtherElements()}. Override getOtherElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
public static Object[] getOtherNonNullStringElements() {
return new Object[] {
"For","then","despite",/* of */"space","I","would","be","brought",
"From","limits","far","remote","where","thou","dost","stay"
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -