/** * 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); }
resetEmpty(); elements = getFullElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Empty collection shouldn't contain element[" + i + "]", !collection.contains(elements[i])); verify(); elements = getOtherElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Empty collection shouldn't contain element[" + i + "]", !collection.contains(elements[i])); verify(); resetFull(); elements = getFullElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Full collection should contain element[" + i + "]", collection.contains(elements[i])); verify(); resetFull(); elements = getOtherElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Full collection shouldn't contain element", !collection.contains(elements[i]));
/** * 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(); }
/** * 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); }
/** * 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; }
if (!isRemoveSupported()) return; resetEmpty(); try { collection.iterator().remove(); fail("New iterator.remove should raise IllegalState"); } catch (IllegalStateException e) { verify(); iter.hasNext(); iter.remove(); fail("New iterator.remove should raise IllegalState " + "even after hasNext"); } catch (IllegalStateException e) { verify(); resetFull(); int size = collection.size(); Iterator iter = collection.iterator(); o = cloneMapEntry((Map.Entry) o); if(!areEqualElementsDistinguishable()) { confirmed.remove(o); verify(); assertEquals("Collection should shrink by one after " +
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()));
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) { 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) { verify(); if (!isRemoveSupported()) return; resetFull();
if (isAddSupported()) return; resetEmpty(); try { collection.add(new Object()); fail("Emtpy collection should not support add."); } catch (UnsupportedOperationException e) { verify(); collection.addAll(Arrays.asList(getFullElements())); fail("Emtpy collection should not support addAll."); } catch (UnsupportedOperationException e) { verify(); resetFull(); try { collection.add(new Object()); fail("Full collection should not support add."); } catch (UnsupportedOperationException e) { verify(); collection.addAll(Arrays.asList(getOtherElements())); fail("Full collection should not support addAll."); } catch (UnsupportedOperationException e) { verify();
/** * 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])); } }
if (isRemoveSupported()) return; resetEmpty(); try { collection.clear(); fail("clear should raise UnsupportedOperationException"); } catch (UnsupportedOperationException e) { verify(); fail("remove should raise UnsupportedOperationException"); } catch (UnsupportedOperationException e) { verify(); fail("removeAll should raise UnsupportedOperationException"); } catch (UnsupportedOperationException e) { verify(); fail("removeAll should raise UnsupportedOperationException"); } catch (UnsupportedOperationException e) { verify(); resetFull(); try { Iterator iterator = collection.iterator(); iterator.next();
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); } } }
/** * 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(); } }
assertEquals("Collection size should match confirmed collection's", confirmedSize, collection.size()); assertEquals("Collection isEmpty() result should match confirmed " + " collection's", confirmed.isEmpty(), collection.isEmpty()); fail("Collection should not contain a value that the " + "confirmed collection does not have: " + o + "\nTest: " + collection + "\nReal: " + confirmed); if(!matched[i]) { fail("Collection should contain all values that are in the confirmed collection" + "\nTest: " + collection + "\nReal: " + confirmed);
if (!isRemoveSupported()) return; resetEmpty(); Object[] elements = getFullElements(); for (int i = 0; i < elements.length; i++) { assertTrue("Shouldn't remove nonexistent element", !collection.remove(elements[i])); verify(); Object[] other = getOtherElements(); resetFull(); for (int i = 0; i < other.length; i++) { assertTrue("Shouldn't remove nonexistent other element", !collection.remove(other[i])); verify(); resetFull(); assertTrue("Collection should remove extant element: " + elements[i], collection.remove(elements[i])); if(!areEqualElementsDistinguishable()) { confirmed.remove(elements[i]); verify(); assertEquals("Collection should shrink after remove", size - 1, collection.size());
/** * Tests {@link Collection#isEmpty()}. */ public void testCollectionIsEmpty() { resetEmpty(); assertEquals("New Collection should be empty.", true, collection.isEmpty()); // make sure calls to "isEmpty() don't change anything verify(); resetFull(); assertEquals("Full collection shouldn't be empty", false, collection.isEmpty()); // make sure calls to "isEmpty() don't change anything verify(); }
resetEmpty(); Collection col = new HashSet(); assertTrue("Every Collection should contain all elements of an " + "empty Collection.", collection.containsAll(col)); col.addAll(Arrays.asList(getOtherElements())); assertTrue("Empty Collection shouldn't contain all elements of " + "a non-empty Collection.", !collection.containsAll(col)); verify(); resetFull(); assertTrue("Full collection shouldn't contain other elements", !collection.containsAll(col)); col.addAll(Arrays.asList(getFullElements())); assertTrue("Full collection should containAll full elements", collection.containsAll(col)); verify(); int min = (getFullElements().length < 2 ? 0 : 2); int max = (getFullElements().length == 1 ? 1 : (getFullElements().length <= 5 ? getFullElements().length - 1 : 5)); col = Arrays.asList(getFullElements()).subList(min, max); assertTrue("Full collection should containAll partial full " + "elements", collection.containsAll(col)); assertTrue("Full collection should containAll itself", collection.containsAll(collection));
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); assertTrue("Collection should contain element in toArray", collection.contains(array[i])); fail("element " + i + " in returned array should be found " + "in the confirmed collection's array"); assertEquals("Collection should return all its elements in " + "toArray", true, matched[i]);
if (!isRemoveSupported()) return; resetEmpty(); assertTrue("Emtpy collection removeAll should return false for " + "empty input", !collection.removeAll(Collections.EMPTY_SET)); verify(); assertTrue("Emtpy collection removeAll should return false for " + "nonempty input", !collection.removeAll(new ArrayList(collection))); verify(); resetFull(); assertTrue("Full collection removeAll should return false for " + "empty input", !collection.removeAll(Collections.EMPTY_SET)); verify(); assertTrue("Full collection removeAll should return false for other elements", !collection.removeAll(Arrays.asList(getOtherElements()))); verify(); assertTrue("Full collection removeAll should return true for full elements", collection.removeAll(new HashSet(collection))); confirmed.removeAll(new HashSet(confirmed)); verify(); resetFull();
resetEmpty(); Iterator it1 = collection.iterator(); assertEquals("Iterator for empty Collection shouldn't have next.", false, it1.hasNext()); try { it1.next(); fail("Iterator at end of Collection should throw " + "NoSuchElementException when next is called."); } catch(NoSuchElementException e) { verify(); resetFull(); it1 = collection.iterator(); for (int i = 0; i < collection.size(); i++) { assertTrue("Iterator for full collection should haveNext", it1.hasNext()); it1.next(); assertTrue("Iterator should be finished", !it1.hasNext()); for (int i = 0; i < collection.size(); i++) { Object next = it1.next(); assertTrue("Collection should contain element returned by " + "its iterator", collection.contains(next)); list.add(next); fail("iterator.next() should raise NoSuchElementException " + "after it finishes");