📄 abstracttestcollection.java
字号:
//-----------------------------------------------------------------------
/**
* Returns a confirmed empty collection.
* For instance, an {@link java.util.ArrayList} for lists or a
* {@link java.util.HashSet} for sets.
*
* @return a confirmed empty collection
*/
public abstract Collection makeConfirmedCollection();
/**
* Returns a confirmed full collection.
* For instance, an {@link java.util.ArrayList} for lists or a
* {@link java.util.HashSet} for sets. The returned collection
* should contain the elements returned by {@link #getFullElements()}.
*
* @return a confirmed full collection
*/
public abstract Collection makeConfirmedFullCollection();
/**
* Return a new, empty {@link Collection} to be used for testing.
*/
public abstract Collection makeCollection();
/**
* Returns a full collection to be used for testing. The collection
* returned by this method should contain every element returned by
* {@link #getFullElements()}. The default implementation, in fact,
* simply invokes <code>addAll</code> on an empty collection with
* the results of {@link #getFullElements()}. Override this default
* if your collection doesn't support addAll.
*/
public Collection makeFullCollection() {
Collection c = makeCollection();
c.addAll(Arrays.asList(getFullElements()));
return c;
}
/**
* Returns an empty collection for Object tests.
*/
public Object makeObject() {
return makeCollection();
}
/**
* Creates a new Map Entry that is independent of the first and the map.
*/
public Map.Entry cloneMapEntry(Map.Entry entry) {
HashMap map = new HashMap();
map.put(entry.getKey(), entry.getValue());
return (Map.Entry) map.entrySet().iterator().next();
}
//-----------------------------------------------------------------------
/**
* Returns an array of objects that are contained in a collection
* produced by {@link #makeFullCollection()}. Every element in the
* returned array <I>must</I> be an element in a full collection.<P>
* The default implementation returns a heterogenous array of
* objects with some duplicates. null is added if allowed.
* Override if you require specific testing elements. Note that if you
* override {@link #makeFullCollection()}, you <I>must</I> override
* this method to reflect the contents of a full collection.
*/
public Object[] getFullElements() {
if (isNullSupported()) {
ArrayList list = new ArrayList();
list.addAll(Arrays.asList(getFullNonNullElements()));
list.add(4, null);
return list.toArray();
} else {
return (Object[]) getFullNonNullElements().clone();
}
}
/**
* Returns an array of elements that are <I>not</I> contained in a
* full collection. Every element in the returned array must
* not exist in a collection returned by {@link #makeFullCollection()}.
* The default implementation returns a heterogenous array of elements
* without null. Note that some of the tests add these elements
* to an empty or full collection, so if your collection restricts
* certain kinds of elements, you should override this method.
*/
public Object[] getOtherElements() {
return getOtherNonNullElements();
}
//-----------------------------------------------------------------------
/**
* 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 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 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 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 Object[] getOtherNonNullStringElements() {
return new Object[] {
"For","then","despite",/* of */"space","I","would","be","brought",
"From","limits","far","remote","where","thou","dost","stay"
};
}
// Tests
//-----------------------------------------------------------------------
/**
* Tests {@link Collection#add(Object)}.
*/
public void testCollectionAdd() {
if (!isAddSupported()) return;
Object[] elements = getFullElements();
for (int i = 0; i < elements.length; i++) {
resetEmpty();
boolean r = collection.add(elements[i]);
confirmed.add(elements[i]);
verify();
assertTrue("Empty collection changed after add", r);
assertEquals("Collection size is 1 after first add", 1, collection.size());
}
resetEmpty();
int size = 0;
for (int i = 0; i < elements.length; i++) {
boolean r = collection.add(elements[i]);
confirmed.add(elements[i]);
verify();
if (r) size++;
assertEquals("Collection size should grow after add",
size, collection.size());
assertTrue("Collection should contain added element",
collection.contains(elements[i]));
}
}
/**
* Tests {@link Collection#addAll(Collection)}.
*/
public void testCollectionAddAll() {
if (!isAddSupported()) return;
resetEmpty();
Object[] elements = getFullElements();
boolean r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Empty collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
assertTrue("Collection should contain added element",
collection.contains(elements[i]));
}
resetFull();
int size = collection.size();
elements = getOtherElements();
r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Full collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
assertTrue("Full collection should contain added element",
collection.contains(elements[i]));
}
assertEquals("Size should increase after addAll",
size + elements.length, collection.size());
resetFull();
size = collection.size();
r = collection.addAll(Arrays.asList(getFullElements()));
confirmed.addAll(Arrays.asList(getFullElements()));
verify();
if (r) {
assertTrue("Size should increase if addAll returns true",
size < collection.size());
} else {
assertEquals("Size should not change if addAll returns false",
size, collection.size());
}
}
/**
* If {@link #isAddSupported()} returns false, tests that add operations
* raise <code>UnsupportedOperationException.
*/
public void testUnsupportedAdd() {
if (isAddSupported()) return;
resetEmpty();
try {
collection.add(new Object());
fail("Emtpy collection should not support add.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
collection.addAll(Arrays.asList(getFullElements()));
fail("Emtpy collection should not support addAll.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
resetFull();
try {
collection.add(new Object());
fail("Full collection should not support add.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
collection.addAll(Arrays.asList(getOtherElements()));
fail("Full collection should not support addAll.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
}
/**
* Test {@link Collection#clear()}.
*/
public void testCollectionClear() {
if (!isRemoveSupported()) return;
resetEmpty();
collection.clear(); // just to make sure it doesn't raise anything
verify();
resetFull();
collection.clear();
confirmed.clear();
verify();
}
/**
* Tests {@link Collection#contains(Object)}.
*/
public void testCollectionContains() {
Object[] elements;
resetEmpty();
elements = getFullElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Empty collection shouldn't contain element[" + i + "]",
!collection.contains(elements[i]));
}
// make sure calls to "contains" don't change anything
verify();
elements = getOtherElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Empty collection shouldn't contain element[" + i + "]",
!collection.contains(elements[i]));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -