testElement.add( new TestC() );
testElement.add( new TestDerivedFromC() );
testElement.add( new TestA() );
testElement.add( new TestB() );
testElement.add( new TestA() );
testElement.add( new TestUElement() );
testElement.add( new TestUElementComposite() );
testIter = testElement.iterator();
try
{
testIter.remove();
fail("Attempting to call remove() on an iterator before next() has been called should throw an exception, but did not.");
}
catch ( IllegalStateException e )
{
}
catch ( Exception e )
{
fail("Attempting to call remove() on an iterator before next() has been called should only throw an IllegalStateException, but a(n) " + e.getClass().getName() + " was thrown.");
}
assertTrue(
"An new iterator for a UniverseElementComposite that has child elements should recognize that there is a next element when next has never been called for the iterator.",
testIter.hasNext() );
LinkedList<UniverseElement> testList = new LinkedList<UniverseElement>();
while ( testIter.hasNext() )
{
testList.add( (UniverseElement) testIter.next() );
}
assertEquals(
"After iterating through a UniverseElementComposite and adding each individual element returned from next() to an external list, the list and the UniverseElementComposite should have the same size",
testElement.size(), testList.size() );
assertTrue(
"After iterating through a UniverseElementComposite and adding each individual element returned from next() to an external list, the UniverseElementComposite should contain all the elements in the external list.",
testElement.containsAll( testList ) );
assertFalse(
"After iterating through the last child element of a UniverseElementComposite, an iterator should recognize that there is not next element.",
testIter.hasNext() );
try
{
testIter.next();
fail("Attempting to iterate to the next element when an iterator has already iterated past the last element in a UniverseElementComposite should throw an exception, but did not.");
}
catch ( NoSuchElementException e )
{
}
catch ( Exception e )
{
fail("Attempting to iterate to the next element when an iterator has already iterated past the last element in a UniverseElementComposite should only throw a NoSuchElementException, but a(n) " + e.getClass().getName() + " was thrown.");
}
int savedSize = testElement.size();
UniverseElement savedItem = null;
testIter = testElement.iterator();
savedItem = testIter.next();
assertTrue(
"Before a call to remove an item through an iterator, the item being removed should report itself as being contained.",
savedItem.isContained() );
testIter.remove();
assertFalse(
"After a call to remove an item through an iterator, the item being removed should not report itself as being contained.",
savedItem.isContained() );
assertTrue(
"An item saved from a call to an iterator's next() method should exist in a list of all items already saved from the related UniverseElementComposite, even after a call has been made through the iterator to remove that item.",
testList.contains(savedItem) );
assertFalse(
"An item saved from a call to an iterator's next() method should not exist in the related UniverseElementComposite after a call has been made through the iterator to remove that item.",
testElement.contains( savedItem ) );
assertEquals(
"After using an iterator to remove an item from a UniverseElementComposite, the size of the UniverseElementComposite should be one less than it was previously.",
savedSize - 1, testElement.size() );
testList.remove(savedItem);
assertTrue(
"After removing an item from UniverseElementComposite through a call to an iterator, and then removing that same item from a list of elements saved from that UniverseElementComposite before the original remove operation, the UniverseElementComposite should contain all the elements in the list.",
testElement.containsAll(testList) );
savedSize = testElement.size();
try
{
testIter.remove();
fail("Attempting to call remove for an iterator twice in a row, with no intervening call to that iterator to advance to the next element, should throw an exception, but did not.");
}
catch ( IllegalStateException e )
{
}
catch ( Exception e )
{
fail("Attempting to call remove for an iterator twice in a row, with no intervening call to that iterator to advance to the next element, should only throw an IllegalStateException, but a(n) " + e.getClass().getName() + " was thrown.");
}
assertEquals(
"After attempting (and failing) to call remove() on an iterator twice in a row with no intervening call to next(), the related UniverseElementComposite should not have changed size.",
testElement.size(), savedSize );
assertTrue(
"After attempting (and failing) to call remove() on an iterator twice in a row with no intervening call to next(), the related UniverseElementComposite should contain all the elements it did before the call.",
testElement.containsAll( testList ) );
assertTrue(
"After calling remove() (twice) on an iterator, the iterator should still recognize that there is another element in a UniverseElementComposite that does, in fact, have additional child elements.",
testIter.hasNext() );
savedItem = testIter.next();
assertTrue(
"After calling remove() (twice) on an iterator, calling next() should still return the next element in the UniverseElementComposite.",
testElement.contains( savedItem ) );
testIter.remove();
assertFalse(
"After calling remove() twice in a row on an iterator and handling the exception, then calling next, removing an element through the iterator should still work as expected and the UniverseElementComposite should not contain the removed element.",
testElement.contains( savedItem ) );
int numElements = 0;
while ( testIter.hasNext() )
{
testIter.next();
++numElements;
}
assertEquals(
"After calling remove any number of times on the logical beginning of a UniverseElementComposite, the number of elements you can get back from a UniverseElementComposite through an iterator using the next() method should still be the same as the size of the UniverseElementComposite.",
testElement.size(), numElements );
/* These tests are probably unnecessary. */
testElement.clear();
testElement.add( new TestUElement() );
testIter = testElement.iterator();
assertNotNull(
"The item returned from a call to an iterator's next() method, even when it returns the last element in a UniverseElementComposite, should not be null.",