/**
* EtherStrategy: A simulation of space and interstellar civilization.
*
* Copyright * (C) 2007 Wayne Witzke
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* (COPYING.txt) along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* To contact the author of EtherStrategy, send e-mail to wwitzke at
* users.sourceforge.net
*/
package etherstrategy.simulation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.LinkedList;
import java.lang.NullPointerException;
import java.lang.IllegalArgumentException;
import java.lang.reflect.InvocationTargetException;
import java.util.NoSuchElementException;
import etherstrategy.simulation.TestUElement;
import etherstrategy.simulation.TestUElementComposite;
import java.util.Arrays;
/**
* TestUElementCompositeTest is a set of tests that is designed to test the
* UniverseElementComposite methods. UniverseElementComposite is abstract (to
* prevent type information from being lost for used components through
* carelessness, or idiocy), so this set of tests uses the derived class
* TestUElementComposite, which has no (well, few, only required) members of its
* own.
*
* There is potential for confusion here, but all tests on TestUElementComposite
* are, in fact, tests on UniverseElementComposite. The terms could almost be used
* interchangeably in this set of tests (and probably are several times).
*
* TODO: Lots of redundancy here(?) Should reduce it eventually.
*
* @author Wayne Witzke
*/
public class TestUElementCompositeTest
{
TestUElementComposite testElement;
/**
* TestA is an empty class that will be used to insure that the type of the
* UniverseElement is being considered where appropriate in the various
* UniverseElementComposite methods.
*
* @author Wayne Witzke
*
*/
public static class TestA extends UniverseElementComposite
{
public TestA()
{
}
public TestA( TestA toBeCopied )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( toBeCopied );
}
}
/**
* TestB is like TestA, but will present as a different type.
* @author Wayne Witzke
*
*/
public static class TestB extends UniverseElement
{
public TestB()
{
}
public TestB( TestB toBeCopied )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( toBeCopied );
}
}
/**
* TestC is like TestA, but will present as a different type.
* @author Wayne Witzke
*
*/
public static class TestC extends UniverseElementComposite
{
public TestC()
{
}
public TestC( TestC toBeCopied )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( toBeCopied );
}
}
/**
* TestDerivedFromC is like TestA, but is derived from TestC and will present
* as a different type with a different supertype.
* @author Wayne Witzke
*
*/
public static class TestDerivedFromC extends TestC
{
public TestDerivedFromC()
{
}
public TestDerivedFromC( TestDerivedFromC toBeCopied )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( toBeCopied );
}
}
/**
* It would be a drag to create a new test element for every single test, so
* we'll do it once here. In theory, this should take the place of any
* constructor test for TestUElementComposite.
*
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
{
testElement = new TestUElementComposite();
assertNotNull( "A newly constructed test element should not be null.",
testElement );
}
/**
* This method will nullify testElement, which is probably overly cautious.
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception
{
testElement = null;
}
/**
* In order to facilitate further testing, we need to implement a deep
* comparison method for UniverseElements. It's going to be horribly
* inefficient, probably, but since the Universes we'll be testing with here
* should be relatively small, usually, and since this comparison operation
* will probably have very limited (if any) actual usage in the application
* itself, this shouldn't be a problem. This test will insure that the
* equality method functions properly and returns correct results for empty
* UniverseElementComposites.
*
* @throws Exception
*/
@Test
public void compareBlankUniverseElementCompositess() throws Exception
{
TestUElementComposite testElement2 = new TestUElementComposite();
assertTrue(
"Two UniverseElementComposites created with a default constructor should be similar, but are not",
testElement.similar( testElement2 ) );
}
/**
* Here we just want to make sure that we can get an iterator back from a
* UniverseElementComposite. If this doesn't work, then the constructor for
* the iterator, or the iterator constructor function, is broken.
*
* @throws Exception
*/
@Test
public void constructUniverseElementCompositeIterator() throws Exception
{
UniverseElement.Iterator testIterator = null;
try
{
testIterator = testElement.iterator();
}
catch ( Exception e )
{
fail("Creating a UniverseElementComposite iterator should not throw an exception.");
}
assertNotNull(
"After constructing an iterator, the variable storing the iterator should not be null.",
testIterator );
}
/**
* We need to be able to determine if a UniverseElementComposite is empty, and
* how many elements a UniverseElementComposite contains. These are the
* isEmpty() and size() methods.
*/
@Test
public void accessElementsInEmptyUniverseElementComposite() throws Exception
{
assertEquals(
"A newly created UniverseElementComposite should have a zero count of elements.",
testElement.size(), 0 );
assertTrue( "A newly created UniverseElementComposite should be empty.",
testElement.isEmpty() );
}
/**
* Next, we get to test adding items to a UniverseElementComposite.
*
* While we're here, we might as well insure that the other UniverseElementComposite
* testing methods are functioning properly after the add.
*/
@Test
public void addUniverseElementsToUniverseElementComposite() throws Exception
{
TestUElementComposite testElementToAdd = new TestUElementComposite();
TestUElementComposite emptyTestElement = new TestUElementComposite();
TestUElement testElementToAdd2 = new TestUElement();
assertFalse(
"Prior to adding a UniverseElementComposite to another UniverseElementComposite, the potential child UniverseElementComposite should not report itself as being contained within another UniverseElementComposite",
testElementToAdd.isContained() );
assertFalse(
"Prior to adding a UniverseElement to a UniverseElementComposite, the UniverseElement should report itself as not being contained.",
testElementToAdd2.isContained() );
assertTrue(
"Adding a UniverseElementComposite to an empty UniverseElementComposite should return true",
testElement.add( testElementToAdd ) );
assertFalse(
"A UniverseElementComposite that has had an element added should not be similar to a UniverseElementComposite that has no nodes.",
testElement.similar( emptyTestElement ) );
assertFalse(
"A UniverseElementComposite that has an element should not be marked as empty.",
testElement.isEmpty() );
assertEquals(
"A UniverseElementComposite that has exactly one element should report a size of one.",
testElement.size(), 1 );
assertFalse(
"Trying to add a UniverseElementComposite twice to a UniverseElementComposite should return false",
testElement.add( testElementToAdd ) );
assertEquals(
"After trying to add a UniverseElementComposite twice to a UniverseElementComposite should leave the original UniverseElementComposite (the parent) with a size of 1.",
testElement.size(), 1 );
assertFalse(
"Trying to add a UniverseElementComposite to two different UniverseElementComposites should return false.",
emptyTestElement.add( testElementToAdd ) );
assertTrue(
"After trying to add a UniverseElementComposite to two different UniverseElementComposites (the second being initially empty), the second UniverseElementComposite should still be empty.",
emptyTestElement.isEmpty() );
assertTrue(
"We should be able to add an element to a different UniverseElementComposite after having already added a different UniverseElement to a different UniverseElementComposite.",
emptyTestElement.add( testElementToAdd2 ) );
assertFalse(
"Having one UniverseElementComposite in another, then adding a different UniverseElement to a different UniverseElementComposite should leave the second parent UniverseElementComposite in a non-empty state.",
emptyTestElement.isEmpty() );
assertTrue(
"Adding a second unique element to a UniverseElementComposite should return true.",
testElement.add( emptyTestElement ) );
assertEquals(
"Adding a second element to a UniverseElementComposite should leave the UniverseElementComposite with a size of 2.",
testElement.size(), 2 );
assertTrue(
"Having added one UniverseElementComposite to another, the added element should be reported itself as being contained by another UniverseElementComposite class.",
testElementToAdd.isContained() );
assertTrue(
"Having added a UniverseElement to a UniverseElementComposite, the UniverseElement should report itself as being contained.",
testElementToAdd2.isContained() );
}
/**
* This method insures that it is impossible to add a UniverseElementComposite to
* itself using add().
* @throws Exception
*/
@Test
public void addSelfToUniverseElementComposite() throws Exception
{
try
{
testElement.add(testElement);
fail("Adding a UniverseElementComposite to itself should throw an exception, but no exception was thrown.");
}
catch ( IllegalArgumentException e )
{
}
catch ( Exception e )
{
fail("Adding a UniverseElementComposite to itself should only throw an IllegalArgumentException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add an empty UniverseElementComposite to itself, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
assertFalse(
"After failing to add an non-contained UniverseElementComposite to itself, the UniverseElementComposite should still report itself as not being contained.",
testElement.isContained() );
TestUElement element1 = new TestUElement();
testElement.add( element1 );
try
{
testElement.add(testElement);
fail("Adding a UniverseElementComposite to itself should throw an exception, even if the UniverseElementComposite was not empty to begin with, but no exception was thrown.");
}
catch ( IllegalArgumentException e )
{
}
catch ( Exception e )
{
fail("Adding a UniverseElementComposite to itself should only throw an IllegalArgumentException, even if the UniverseElementComposite was not empty, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a UniverseElementComposite to itself, the UniverseElementComposite should still only contain the elements that it contained initially.",
( testElement.size() == 1 )
&& testElement.contains( element1 ) );
assertFalse(
"After failing to add an non-contained UniverseElementComposite to itself, even when the UniverseElementComposite was not empty to begin with, the UniverseElementComposite should still report itself as not being contained.",
testElement.isContained() );
}
/**
* The UniverseElementComposite.add() method should not allow a null reference
* to be added to the UniverseElementComposite.
*
* @throws Exception
*/
@Test
public void addNullToUniverseElementComposite() throws Exception
{
try
{
testElement.add(null);
fail("Adding a null reference to a UniverseElementComposite should throw an exception, but no exception was thrown.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a null reference to a UniverseElementComposite should only throw a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a null reference to an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
TestUElement element1 = new TestUElement();
testElement.add( element1 );
try
{
testElement.add(null);
fail("Adding a null reference to a UniverseElementComposite should throw an exception, even if the UniverseElementComposite was not empty to begin with, but no exception was thrown.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a null reference to a UniverseElementComposite should only throw a NullPointerException, even if the UniverseElementComposite was not empty, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a null reference to a UniverseElementComposite, the UniverseElementComposite should still only contain the elements that it contained initially.",
( testElement.size() == 1 )
&& testElement.contains( element1 ) );
}
/**
* This test will check to see if any particular UniverseElementComposite can be found
* in a UniverseElementComposite collection after several different UniverseElementComposites
* have already been added.
*
* @throws Exception
*/
@Test
public void checkIfUniverseElementInUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement elementNotAdded = new TestUElement();
testElement.add(element1);
testElement.add(element2);
testElement.add(element3);
assertEquals("Having added three elements to a UniverseElementComposite, the size of the UniverseElementComposite should be 3",
testElement.size(), 3 );
assertTrue("If we've added an element to a UniverseElementComposite, then we should get \"true\" when we ask UniverseElementComposite if it contains that element.",
testElement.contains( element1 ) );
assertTrue("A UniverseElementComposite should be able to return true for any element it contains when asked if it contains that element.",
testElement.contains( element2 )
&& testElement.contains( element3 ) );
assertFalse("A UniverseElementComposite should return false when asked if it contains an element that was never added to the UniverseElementComposite.",
testElement.contains( elementNotAdded ) );
}
/**
* This test will check to insure that NullPointerException is thrown when
* a null reference is passed into contains().
* @throws Exception
*/
/*
@Test ( expected = NullPointerException.class )
public void checkIfNullInUniverseElementComposite() throws Exception
{
testElement.contains( null );
}
*/
/**
* This is a change of behavior for contains(). It should no longer throw
* an exception when an attempt is made to check for the existence of null in
* the UniverseElementComposite. Instead, since a UniverseElementComposite cannot contain a
* null reference, the method will simply return false when asked about a null
* reference.
* @throws Exception
*/
@Test
public void checkIfNullInUniverseElementComposite() throws Exception
{
assertFalse(
"A UniverseElementComposite can never contain a null, and when asked if it does, it should always return false.",
testElement.contains( null ) );
}
/**
* This test will check to insure that ordering is not considered when two
* UniverseElementComposites are checked for similarity.
* @throws Exception
*/
@Test
public void similarityOnDifferentOrderings() throws Exception
{
TestUElementComposite testElement2 = new TestUElementComposite();
TestA element1 = new TestA();
TestB element2 = new TestB();
TestA element3 = new TestA();
TestB element4 = new TestB();
testElement.add( element1 );
testElement.add( element2 );
testElement2.add( element4 );
testElement2.add( element3 );
assertTrue(
"Two UniverseElementComposites should be similar if they contain similar child elements, no matter the order in which those elements were added.",
testElement.similar( testElement2 ) );
}
/**
* This test will insure that two UniverseElementComposites don't compare as similar
* when the data members of the two elements are not, in fact, the same.
* @throws Exception
*/
@Test
public void similarityForUniverseElementCompositeWithVariousSetsOfMemberData() throws Exception
{
TestUElementComposite elementToCompare = new TestUElementComposite();
testElement.setCenter( 1, 1, 1 );
assertFalse(
"A newly created UniverseElementComposite with default member data should not be similar to an empty UniverseElementComposite where the center has been changed so that it is not (0, 0, 0).",
testElement.similar( elementToCompare ) );
elementToCompare.setCenter( 2, 2, 2 );
assertFalse(
"Two empty UniverseElementComposites that have had their centers set to different values should not be similar.",
testElement.similar( elementToCompare ) );
elementToCompare.setCenter( 1, 1, 1 );
assertTrue(
"Two empty UniverseElementComposites that have had their centers set to the same value, and have all other members the same, should be similar.",
testElement.similar( elementToCompare ) );
}
/**
* There are a few instances where we have not actually checked to insure that
* two UniverseElementComposites that are known to not be similar may actually be said
* to be similar by the similar() method. This is bad, so we're checking for
* it here.
* @throws Exception
*/
@Test
public void similarityBetweenNonSimilarUniverseElementComposites() throws Exception
{
testElement.add( new TestA() );
testElement.add( new TestB() );
testElement.add( new TestA() );
TestUElementComposite nonsimilarElement = new TestUElementComposite();
nonsimilarElement.add( new TestB() );
nonsimilarElement.add( new TestA() );
nonsimilarElement.add( new TestB() );
assertFalse(
"Two UniverseElementComposites that are not, in fact, similar because they contain different numbers of different kinds of elements should not be said to be similar when the similar() method is applied to them.",
testElement.similar(nonsimilarElement) );
}
/**
* We need to make sure that attempting to check the similarity between a
* UniverseElementComposite and a null pointer throws a null pointer exception.
*
* @throws Exception
*/
@Test ( expected = NullPointerException.class )
public void similartyBetweenNullAndUniverseElementComposite() throws Exception
{
assertFalse(
"A UniverseElementComposite should not be similar to a null reference.",
testElement.similar( null ) );
}
/**
* Here, we check to make sure that a remove operation on a UniverseElementComposite
* has the expected results. This not only includes removing the child from
* the parent UniverseElementComposite, but also removing the child's parent
* reference (thereby causing it to report that it is no longer contained).
*
* @throws Exception
*/
@Test
public void removeFromUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
testElement.add(element1);
testElement.add(element2);
assertFalse(
"Removing an non-existent child from a UniverseElementComposite should return false.",
testElement.remove( element3 ) );
assertTrue(
"Removing a child element from a UniverseElementComposite should return true.",
testElement.remove( element1 ) );
assertEquals(
"Having removed a child element from a UniverseElementComposite containing two children, the UniverseElementComposite should now have a count of 1.",
testElement.size(), 1 );
assertFalse(
"Having removed a child element from a UniverseElementComposite, that UniverseElementComposite should not report containing that child.",
testElement.contains(element1) );
assertFalse(
"A UniverseElement that has been removed from a UniverseElementComposite should not report itself as being contained.",
element1.isContained() );
testElement.remove( element2 );
assertTrue(
"Having removed all elements from a UniverseElementComposite, the UniverseElementComposite should now report that it is empty.",
testElement.isEmpty() );
assertFalse(
"A UniverseElementComposite that has been removed from a UniverseElementComposite should not report itself as being contained.",
element2.isContained() );
assertTrue(
"Having removed a child from a UniverseElementComposite, it should be possible to add that child back to the UniverseElementComposite.",
testElement.add(element1) );
assertTrue(
"Having added a previously removed child back to a UniverseElementComposite, the UniverseElementComposite should report that it contains that child.",
testElement.contains( element1 ) );
}
/**
* We want to make sure that trying to remove a null reference from a
* UniverseElementComposite collection throws an exception.
*
* @throws Exception
*/
@Test
public void removeNullFromUniverseElementComposite() throws Exception
{
try
{
testElement.remove(null);
fail("Removing a null reference from a UniverseElementComposite should throw an exception, but no exception was thrown.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a null reference from a UniverseElementComposite should only throw a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a null reference from an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
TestUElement element1 = new TestUElement();
testElement.add( element1 );
try
{
testElement.remove(null);
fail("Removing a null reference from a UniverseElementComposite should throw an exception, even if the UniverseElementComposite was not empty to begin with, but no exception was thrown.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a null reference from a UniverseElementComposite should only throw a NullPointerException, even if the UniverseElementComposite was not empty, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a null reference from a UniverseElementComposite, the UniverseElementComposite should still only contain the elements that it contained initially.",
( testElement.size() == 1 )
&& testElement.contains( element1 ) );
}
/**
* We also want to make sure that attempting to remove a non-UniverseElement
* object from a UniverseElementComposite throws a ClassCastException.
*
* @throws Exception
*/
@Test
public void removeIntegerFromUniverseElementComposite() throws Exception
{
try
{
testElement.remove( new Integer(1) );
fail("Removing a non-UniverseElement object from a UniverseElementComposite should throw an exception, but no exception was thrown.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Removing a non-UniverseElement object from a UniverseElementComposite should only throw a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a non-UniverseElement object from an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
TestUElement element1 = new TestUElement();
testElement.add( element1 );
try
{
testElement.remove( new Integer(1) );
fail("Removing a non-UniverseElement object from a UniverseElementComposite should throw an exception, even if the UniverseElementComposite was not empty to begin with, but no exception was thrown.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Removing a non-UniverseElement object from a UniverseElementComposite should only throw a ClassCastException, even if the UniverseElementComposite was not empty, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a non-UniverseElement object from a UniverseElementComposite, the UniverseElementComposite should still only contain the elements that it contained initially.",
( testElement.size() == 1 )
&& testElement.contains( element1 ) );
}
/**
* We need to be able to clear a UniverseElementComposite of all its children. This
* should also prevent those children from being reported as contained by
* a UniverseElementComposite.
*
* @throws Exception
*/
@Test
public void clearingUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
testElement.clear();
assertTrue(
"Clearing a newly created UniverseElementComposite should still leave it in the empty state.",
testElement.isEmpty() );
testElement.add(element1);
testElement.add(element2);
testElement.add(element3);
testElement.clear();
assertEquals(
"Clearing a UniverseElementComposite should leave it with a size of zero.",
testElement.size(), 0 );
assertFalse(
"Having cleared the UniverseElementComposite, it should not report any of the elements that it previously contained as being contained currently.",
testElement.contains( element1 )
|| testElement.contains( element2 )
|| testElement.contains( element3 ) );
assertFalse(
"Elements that were contained in a UniverseElementComposite prior to a call to clear() should not reported themselves as being contained after the call to clear().",
element1.isContained()
|| element2.isContained()
|| element3.isContained() );
assertTrue(
"After clearing a UniverseElementComposite, the UniverseElementComposite should report itself as being empty, even if it had elements previously.",
testElement.isEmpty() );
assertTrue(
"After clearing a UniverseElementComposite, it should be possible to add previous children back to the collection.",
testElement.add(element1) );
assertTrue(
"Having added a previously removed child (due to a clear operation) back to a UniverseElementComposite, the UniverseElementComposite should report that it contains that child.",
testElement.contains( element1 ) );
}
/**
* This method will test the addAll() method of UniverseElementComposite, a required
* component of the Collections interface. addAll() should add a collection
* of UniverseElements to the root of the UniverseElementComposite upon which it is
* called.
*
* @throws Exception
*/
@Test
public void addingACollectionOfUniverseElementsToAUniverseElementComposite() throws Exception
{
LinkedList<UniverseElement> testCollection =
new LinkedList<UniverseElement>();
TestUElementComposite testElement2 = new TestUElementComposite();
assertFalse(
"Adding an empty collection to an empty UniverseElementComposite should return false, since the UniverseElementComposite was not modified.",
testElement.addAll( testCollection ) );
assertTrue(
"Having added an empty collection to an empty UniverseElementComposite, the UniverseElementComposite should report itself as being empty.",
testElement.isEmpty() );
testCollection.add( new TestA() );
testCollection.add( new TestB() );
testCollection.add( new TestA() );
assertTrue(
"Adding a non-empty collection of UniverseElements to an empty UniverseElementComposite should return true.",
testElement.addAll( testCollection ) );
assertFalse(
"Having added a non-empty collection of UniverseElements to an empty UniverseElementComposite, the UniverseElementComposite should not longer report itself as being empty.",
testElement.isEmpty() );
assertEquals(
"Having added a non-empty collection to an empty UniverseElementComposite, the collection and the UniverseElementComposite should have the same size.",
testElement.size(), testCollection.size() );
assertTrue(
"UniverseElements added through addAll should report themselves as being contained.",
testCollection.get(0).isContained()
&& testCollection.get(1).isContained()
&& testCollection.get( 2 ).isContained() );
/* Saved for a later test. */
int size = testElement.size();
assertFalse(
"Trying to add a non-empty collection of UniverseElements to a UniverseElementComposite twice should return false the second time.",
testElement.addAll( testCollection ) );
assertEquals(
"Having attempted to add a non-empty collection to an empty UniverseElementComposite twice, the UniverseElementComposite size should not have changed after the second add attempt.",
testElement.size(), size );
testCollection.clear();
testCollection.add( new TestA() );
testCollection.add( new TestA() );
testElement2.addAll( testCollection );
assertFalse(
"Two UniverseElementComposites should not be similar if they were populated with collections of UniverseElements that were, themselves, not similar.",
testElement.similar( testElement2 ) );
testElement2.add( new TestB() );
assertTrue(
"Two UniverseElementComposites should be similar if they have similar elements, regardless of mixing add and addAll statements in building the UniverseElementComposites.",
testElement.similar( testElement2 ) );
assertFalse(
"Attempting to add to a UniverseElementComposite a collection of UniverseElements that have already been added elsewhere should return false.",
testElement.addAll( testCollection ) );
assertFalse(
"Attempting to add to a UniverseElementComposite a collection of UniverseElements that have already been added to a different UniverseElementComposite should cause the potential container UniverseElementComposite to report that it does not contain those elements.",
testElement.contains( testCollection.get( 0 ) )
|| testElement.contains( testCollection.get( 1 ) ) );
testCollection.add( new TestB() );
assertTrue(
"Adding a collection of UniverseElements to a UniverseElementComposite, where the container UniverseElementComposite already contains some, but not all, of the potential child UniverseElements in the collection, should return true.",
testElement2.addAll( testCollection ) );
assertTrue(
"Having added a collection of UniverseElements to a UniverseElementComposite, where the container UniverseElementComposite already contained some, but not all, of the potential child UniverseElements in the collection, the container UniverseElementComposite should report containing all the elements in the added collection.",
testElement2.contains( testCollection.get( 0 ) )
&& testElement2.contains( testCollection.get( 1 ) )
&& testElement2.contains( testCollection.get( 2 ) ) );
assertEquals(
"After a complex series of adding collections of, and individual elements to a UniverseElementComposite, the reported size of the UniverseElementComposite should still be correct (in this case, it should be 4).",
testElement2.size(), 4);
}
/**
* This test insures that it is impossible to add a UniverseElementComposite to itself
* even indirectly by adding a collection with the UniverseElementComposite as a
* member of the collection being added. It also insures that no changes are
* made to the UniverseElementComposite if an exception is thrown.
* @throws Exception
*/
@Test
public void addingSelfToUniverseElementCompositeThroughAddingACollection() throws Exception
{
TestUElement element1 = new TestUElement();
testElement.add(element1);
LinkedList<UniverseElement> testCollection =
new LinkedList<UniverseElement>();
testCollection.add( new TestUElement() );
testCollection.add(testElement);
testCollection.add( new TestUElementComposite() );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with a reference to the UniverseElementComposite it is being added to, and other valid elements, should throw an IllegalArgumentException due to the self reference, but did not.");
}
catch ( IllegalArgumentException e )
{
}
catch ( Exception e )
{
fail("Adding a collection with a reference to the UniverseElementComposite it is being added to should throw only an IllegalArgumentException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection containing a reference to the UniverseElementComposite to which it was being added, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
testCollection.clear();
testCollection.add( new TestUElementComposite() );
testCollection.add( new TestUElement() );
testCollection.add( testElement );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with valid elements and a reference to the UniverseElementComposite to which it is being added as the last element should throw an IllegalArgumentException due to the self reference, but did not.");
}
catch ( IllegalArgumentException e )
{
}
catch ( Exception e )
{
fail("Adding a collection with a reference to the UniverseElementComposite it is being added to as the last element in the collection should throw only an IllegalArgumentException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection containing a reference to the UniverseElementComposite to which it was being added, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains(element1) );
testCollection.clear();
testCollection.add( testElement );
testCollection.add( new TestUElement() );
testCollection.add( new TestUElementComposite() );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with valid elements and a reference to the UniverseElementComposite to which it is being added as the first element should throw an IllegalArgumentException due to the self reference, but did not.");
}
catch ( IllegalArgumentException e )
{
}
catch ( Exception e )
{
fail("Adding a collection with a reference to the UniverseElementComposite to which it was being added as the first element should throw only an IllegalArgumentException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection containing a reference to the UniverseElementComposite to which it was being added as its first element, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
}
/**
* This test will insure that addAll() throws an exception when the collection
* passed to it is actually a null reference.
* @throws Exception
*/
@Test
public void addingANullCollectionToAUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
testElement.add(element1);
try
{
testElement.addAll( null );
fail("Adding a null collection to a UniverseElementComposite should throw an exception, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a null collection to a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a null collection to a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
}
/**
* This method will check that addAll() throws an exception when it is run
* on a collection that contains a null value. It also will check for null
* at various points in the collection.
*
* @throws Exception
*/
@Test
public void addingACollectionWithNullToAUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
testElement.add (element1 );
LinkedList<UniverseElement> testCollection =
new LinkedList<UniverseElement>();
testCollection.add( null );
try
{
testElement.addAll( testCollection );
fail("Adding a collection containing only nulls to a UniverseElementComposite should throw an exception, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a collection containing only nulls to a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection containing only nulls to a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
testCollection.clear();
testCollection.add( new TestUElement() );
testCollection.add( null );
testCollection.add( new TestUElementComposite() );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with a null reference and other valid elements should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a collection containing a null to a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection with a null reference to a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
testCollection.clear();
testCollection.add( new TestUElementComposite() );
testCollection.add( new TestUElement() );
testCollection.add( null );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with valid elements and a null reference as the last element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a collection containing a null reference as the last element to a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection with a null reference to a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
testCollection.clear();
testCollection.add( null );
testCollection.add( new TestUElementComposite() );
testCollection.add( new TestUElement() );
try
{
testElement.addAll( testCollection );
fail("Adding a collection with valid elements and a null reference as the first element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Adding a collection containing a null reference as the first element to a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to add a collection with a null reference as its first element to a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
}
/**
* Here we are insuring that containsAll() properly checks for the existence
* of all objects in the passed container in the UniverseElementComposite on which the
* method was called.
* @throws Exception
*/
@Test
public void checkingUniverseElementCompositeContentsUsingContainersOfUniverseElements() throws Exception
{
LinkedList<Object> testCollection =
new LinkedList<Object>();
LinkedList<Object> emptyCollection =
new LinkedList<Object>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
testCollection.add(element1);
testCollection.add(element2);
/*
* This is the expected behavior of collections, apparently. Any
* collection contains all the elements of an empty collection.
*/
assertTrue(testCollection.containsAll(emptyCollection));
assertTrue(emptyCollection.containsAll(emptyCollection));
assertTrue(
"Checking an empty UniverseElementComposite to see if it contains the elements of an empty collection should return true.",
testElement.containsAll( emptyCollection ) );
assertFalse(
"Checking to see if an empty UniverseElementComposite contains the elements of a non-empty collection should return false.",
testElement.containsAll( testCollection ) );
testElement.add(element3);
assertFalse(
"Checking to see if a non-empty UniverseElementComposite contains the elements of a non-empty collection, when the two have no elements in common, should return false.",
testElement.containsAll( testCollection ) );
testElement.add( element2 );
assertFalse(
"Checking to see if a non-empty UniverseElementComposite contains the elements of a non-empty collection should return false when the UniverseElementComposite does not contain all the elements in the collection against which it is being compared.",
testElement.containsAll( testCollection ) );
testElement.add( element1 );
assertTrue(
"Checking to see if a non-empty UniverseElementComposite contains the elements of a non-empty collection should return true when the UniverseElementComposite does contain all the elements of the collection, even when the UniverseElementComposite contains additional elements that are not in the Collection.",
testElement.containsAll( testCollection ) );
assertTrue(
"Checking to see if a non-empty UniverseElementComposite contains the elements of an empty collection should always return true.",
testElement.containsAll( emptyCollection ) );
testCollection.add(element1);
testCollection.add(element1);
testCollection.add(element1);
assertTrue(
"Checking to see if a non-empty UniverseElementComposite contains the elements of a non-empty collection should return true when the UniverseElementComposite does contain all the elements of the collection, even when the collection has several of its elements duplicated.",
testElement.containsAll( testCollection ) );
testCollection.add( null );
assertFalse(
"Checking to see if a non-empty UniverseElementComposite contains the elements of a non-empty collection should return false if any of the elements in the collection are null, even when the rest of the elements are in the UniverseElementComposite.",
testElement.containsAll( testCollection ) );
}
/**
* This test insures that trying to compare a collection that is actually a
* null reference to a UniverseElementComposite throws an exception.
* @throws Exception
*/
@Test ( expected = NullPointerException.class )
public void checkingUniverseElementCompositeContentsUsingNullCollection() throws Exception
{
testElement.containsAll( null );
}
/**
* This test covers most functionality associated with the removeAll() method.
*
* @throws Exception
*/
@Test
public void removingCollectionsOfElementsFromUniverseElementComposite() throws Exception
{
LinkedList<Object> testCollection =
new LinkedList<Object>();
LinkedList<Object> emptyCollection =
new LinkedList<Object>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
testCollection.add(element1);
testCollection.add(element2);
assertFalse(
"Removing an empty collection from an empty UniverseElementComposite should return false (nothing changed).",
testElement.removeAll( emptyCollection ) );
assertTrue(
"After removing an empty collection from an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
assertFalse(
"Removing a non-empty collection from an empty UniverseElementComposite should return false (nothing changed).",
testElement.removeAll( testCollection ) );
assertTrue(
"After removing a non-empty collection from an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
testElement.add(element3);
assertFalse(
"Removing a non-empty collection from a non-empty UniverseElementComposite, when the collection and the UniverseElementComposite have no children in common, should return false (nothing changed).",
testElement.removeAll( testCollection ) );
assertTrue(
"After removing a non-empty collection from a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite have no children in common, the UniverseElementComposite should not have changed.",
testElement.contains( element3 ) && (testElement.size() == 1));
testElement.add( element2 );
testElement.add( element1 );
testCollection.add( element4 );
assertTrue(
"Before removal through removeAll, elements should report that they are contained.",
element1.isContained()
&& element2.isContained()
&& element3.isContained() );
assertTrue(
"Removing a non-empty collection from a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite have children in common, should return true (something changed).",
testElement.removeAll( testCollection ) );
assertFalse(
"After removing a non-empty collection from a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had children in common, the children that were common to both collections should no longer be present in the UniverseElementComposite.",
testElement.contains( element1 )
|| testElement.contains( element2 ) );
assertFalse(
"After removing elements using removeAll, the removed elements should not report that they are contained.",
element1.isContained()
|| element2.isContained() );
assertTrue(
"After removing a non-empty collection from a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had children in common, the children that were in the UniverseElementComposite and not common to both collections should still be present in the UniverseElementComposite.",
testElement.contains(element3) );
assertTrue(
"After removing elements using removeAll, elements that were not removed (but were originally contained) should still report that they are contained.",
element3.isContained() );
assertFalse(
"After removing a non-empty collection from a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had children in common, the children that were in the collection and not common to both the collection and the UniverseElementComposite should not be present in the UniverseElementComposite.",
testElement.contains(element4) );
assertFalse(
"Removing an empty collection from a non-empty UniverseElementComposite should return false (nothing changed).",
testElement.removeAll( emptyCollection ) );
assertTrue(
"Having removed an empty collection from a non-empty UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.contains( element3 )
&& ( testElement.size() == 1 ) );
}
/**
* This test makes sure that trying to remove the elements of a null
* collection from a UniverseElementComposite throws an exception.
* @throws Exception
*/
@Test
public void removingANullCollectionFromUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
testElement.add(element1);
try
{
testElement.removeAll( null );
fail("Removing a null collection from a UniverseElementComposite should throw an exception, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a null collection from a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a null collection from a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
}
/**
* This test insures that attempting to remove a non-UniverseElement object
* from a UniverseElementComposite will throw an exception, even using the
* removeAll method.
* @throws Exception
*/
@Test
public void removingCollectionsOfNonUniverseElementsFromUniverseElementComposites() throws Exception
{
LinkedList<Object> testCollection =
new LinkedList<Object>();
LinkedList<UniverseElement> checkCollection =
new LinkedList<UniverseElement>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
checkCollection.add( element1 );
checkCollection.add( element2 );
checkCollection.add( element3 );
checkCollection.add( element4 );
testElement.addAll( checkCollection );
testCollection.add( element1 );
testCollection.add( new Integer(0) );
testCollection.add( element2 );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with a non-UniverseElement object and other valid elements should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with a non-UniverseElement object and other valid elements from a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a non-UniverseElement from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection) );
testCollection.clear();
testCollection.add( element1 );
testCollection.add( element2 );
testCollection.add( new Integer(0) );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with valid elements and a non-UniverseElement object as the last element should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with valid elements and a non-UniverseElement object as the last element from a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a non-UniverseElement object from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
testCollection.clear();
testCollection.add( new Integer(0) );
testCollection.add( element1 );
testCollection.add( element2 );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with valid elements and a non-UniverseElement object as the first element should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with valid elements and a non-UniverseElement object as the first element from a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a non-UniverseElement object as its first element from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
}
/**
* This test will insure that nulls are caught as invalid elements to attempt
* to remove from a UniverseElementComposite. This is probably being overly cautious,
* and places a burden (small though it may be) on the caller to insure that
* there are no null references in the collection passed to the removeAll()
* method.
*
* @throws Exception
*/
@Test
public void removingCollectionsWithNullsFromUniverseElementComposites() throws Exception
{
LinkedList<UniverseElement> testCollection =
new LinkedList<UniverseElement>();
LinkedList<UniverseElement> checkCollection =
new LinkedList<UniverseElement>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
checkCollection.add( element1 );
checkCollection.add( element2 );
checkCollection.add( element3 );
checkCollection.add( element4 );
testElement.addAll( checkCollection );
testCollection.add( element1 );
testCollection.add( null );
testCollection.add( element2 );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with a null reference and other valid elements should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with a null reference and other valid elements from a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a null reference from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection) );
testCollection.clear();
testCollection.add( element1 );
testCollection.add( element2 );
testCollection.add( null );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with valid elements and a null reference as the last element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with valid elements and a null reference as the last element from a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a null reference from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
testCollection.clear();
testCollection.add( null );
testCollection.add( element1 );
testCollection.add( element2 );
try
{
testElement.removeAll( testCollection );
fail("Removing a collection with valid elements and a null reference as the first element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Removing a collection with valid elements and a null reference as the first element from a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to remove a collection with a null reference as its first element from a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
}
/**
* This test covers most functionality associated with the retainAll() method.
*
* @throws Exception
*/
@Test
public void retainingCollectionsOfElementsInUniverseElementComposite() throws Exception
{
LinkedList<Object> testCollection =
new LinkedList<Object>();
LinkedList<Object> emptyCollection =
new LinkedList<Object>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
testCollection.add(element1);
testCollection.add(element2);
assertFalse(
"Retaining an empty collection in an empty UniverseElementComposite should return false (nothing changed).",
testElement.retainAll( emptyCollection ) );
assertTrue(
"After retaining an empty collection in an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
assertFalse(
"Retaining a non-empty collection in an empty UniverseElementComposite should return false (nothing changed).",
testElement.retainAll( testCollection ) );
assertTrue(
"After retaining a non-empty collection in an empty UniverseElementComposite, the UniverseElementComposite should still be empty.",
testElement.isEmpty() );
testElement.add(element3);
assertTrue(
"Retaining a non-empty collection in a non-empty UniverseElementComposite, when the collection and the UniverseElementComposite have no children in common, should return true.",
testElement.retainAll( testCollection ) );
assertTrue(
"After retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite have no children in common, the UniverseElementComposite should be empty.",
testElement.isEmpty() );
testElement.add( element2 );
testElement.add( element1 );
assertFalse(
"Retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite have only children in common, should return false (nothing changed).",
testElement.retainAll( testCollection ) );
assertTrue(
"After retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had only children in common, the children that were common to both collections should still be present in the UniverseElementComposite.",
testElement.contains( element1 )
&& testElement.contains( element2 ) );
testElement.add( element3 );
testCollection.add( element4 );
assertTrue(
"Before removal through retainAll, elements should report that they are contained.",
element1.isContained()
&& element2.isContained()
&& element3.isContained() );
assertTrue(
"Retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite have some children in common, but the UniverseElementComposite has elements that are not common to the collection, should return true.",
testElement.retainAll( testCollection ) );
assertTrue(
"After retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had some children in common, the children that were common to both collections should still be present in the UniverseElementComposite.",
testElement.contains( element1 )
&& testElement.contains( element2 ) );
assertTrue(
"After removing elements using retainAll, elements that were not removed (but were originally contained) should still report that they are contained.",
element1.isContained()
&& element2.isContained() );
assertFalse(
"After retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had children in common, the children that were in the UniverseElementComposite and not common to both collections should still not present in the UniverseElementComposite.",
testElement.contains(element3) );
assertFalse(
"After removing elements using retainAll, the removed elements should not report that they are contained.",
element3.isContained() );
assertFalse(
"After retaining a non-empty collection in a non-empty UniverseElementComposite, where the collection and the UniverseElementComposite had some children in common, the children that were in the collection and not common to both the collection and the UniverseElementComposite should not be present in the UniverseElementComposite.",
testElement.contains(element4) );
assertTrue(
"Retaining an empty collection in a non-empty UniverseElementComposite should return true.",
testElement.retainAll( emptyCollection ) );
assertTrue(
"Having retained an empty collection in a non-empty UniverseElementComposite, the UniverseElementComposite should now be empty.",
testElement.isEmpty() );
}
/**
* This test makes sure that trying to remove the elements of a null
* collection from a UniverseElementComposite throws an exception.
* @throws Exception
*/
@Test
public void retainingANullCollectionInUniverseElementComposite() throws Exception
{
TestUElement element1 = new TestUElement();
testElement.add(element1);
try
{
testElement.retainAll( null );
fail("Retaining a null collection in a UniverseElementComposite should throw an exception, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Retaining a null collection in a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a null collection in a UniverseElementComposite, the UniverseElementComposite should not have changed.",
testElement.size() == 1
&& testElement.contains( element1 ) );
}
/**
* This test insures that attempting to remove a non-UniverseElement object
* from a UniverseElementComposite through retainAll will throw an exception.
* @throws Exception
*/
@Test
public void retainingCollectionsOfNonUniverseElementsInUniverseElementComposites() throws Exception
{
LinkedList<Object> testCollection =
new LinkedList<Object>();
LinkedList<UniverseElement> checkCollection =
new LinkedList<UniverseElement>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
checkCollection.add( element1 );
checkCollection.add( element2 );
checkCollection.add( element3 );
checkCollection.add( element4 );
testElement.addAll( checkCollection );
testCollection.add( element1 );
testCollection.add( new Integer(0) );
testCollection.add( element2 );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with a non-UniverseElement object and other valid elements should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a non-UniverseElement object and other valid elements in a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a non-UniverseElement object to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection) );
testCollection.clear();
testCollection.add( element1 );
testCollection.add( element2 );
testCollection.add( new Integer(0) );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with valid elements and a non-UniverseElement object as the last element should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a non-UniverseElement object as the last element and other valid elements in a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a non-UniverseElement object to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
testCollection.clear();
testCollection.add( new Integer(0) );
testCollection.add( element1 );
testCollection.add( element2 );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with valid elements and a non-UniverseElement object as the first element should throw an exception, but did not.");
}
catch ( ClassCastException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a non-UniverseElement object as the first element and other valid elements in a UniverseElementComposite should throw only a ClassCastException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a non-UniverseElement object as its first element to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
}
/**
* This test will insure that nulls are caught as invalid elements to attempt
* to remove from a UniverseElementComposite through the retainAll method. This is probably being overly cautious,
* and places a burden (small though it may be) on the caller to insure that
* there are no null references in the collection passed to the retainAll()
* method.
*
* @throws Exception
*/
@Test
public void retainingCollectionsWithNullsInUniverseElementComposites() throws Exception
{
LinkedList<UniverseElement> testCollection =
new LinkedList<UniverseElement>();
LinkedList<UniverseElement> checkCollection =
new LinkedList<UniverseElement>();
TestUElement element1 = new TestUElement();
TestUElementComposite element2 = new TestUElementComposite();
TestUElement element3 = new TestUElement();
TestUElement element4 = new TestUElement();
checkCollection.add( element1 );
checkCollection.add( element2 );
checkCollection.add( element3 );
checkCollection.add( element4 );
testElement.addAll( checkCollection );
testCollection.add( element1 );
testCollection.add( null );
testCollection.add( element2 );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with a null reference and other valid elements should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a null reference and other valid elements in a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a null reference to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection) );
testCollection.clear();
testCollection.add( element1 );
testCollection.add( element2 );
testCollection.add( null );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with valid elements and a null reference as the last element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a null reference as the last element and other valid elements in a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a null reference to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
testCollection.clear();
testCollection.add( null );
testCollection.add( element1 );
testCollection.add( element2 );
try
{
testElement.retainAll( testCollection );
fail("Retaining a collection with valid elements and a null reference as the first element should throw a NullPointerException due to the null reference, but did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Retaining a collection with a null reference as the first element and other valid elements in a UniverseElementComposite should throw only a NullPointerException, but a(n) " + e.getClass().getName() + " exception was thrown.");
}
assertTrue(
"After failing to retain a collection with a null reference as its first element to a UniverseElementComposite with elements in common with the collection, the UniverseElementComposite should not have changed.",
testElement.containsAll(checkCollection));
}
/**
* Here we are testing both flavors of UniverseElementComposite.toArray(). toArray()
* should return arrays containing the actual elements contained in the
* collection, not copies.
* @throws Exception
*/
@Test
public void creatingArraysFromUniverseElementComposites() throws Exception
{
TestA aElement = new TestA();
TestB bElement = new TestB();
TestC cElement = new TestC();
TestC cElement2 = new TestC();
UniverseElement[] tooSmallUEArray =
{
new TestUElement(), new TestUElement(), new TestUElement(),
};
UniverseElement[] bigEnoughUEArray =
{ new TestUElement(), new TestUElement(), new TestUElement(),
new TestUElement(), new TestUElement(), new TestUElement(),
new TestUElement(), new TestUElement(),
};
UniverseElement[] returnedUEArray;
returnedUEArray = testElement.toArray(tooSmallUEArray);
assertEquals(
"After creating a UniverseElement array from an empty UniverseElementComposite, the returned array should be the same size as the UniverseElement (empty).",
returnedUEArray.length, 0 );
assertNull(
"The array that was passed to the array creation method for an empty UniverseElementComposite should have null as the first element of the array.",
tooSmallUEArray[0]);
tooSmallUEArray = new UniverseElement[3];
testElement.add( aElement );
testElement.add( cElement2 );
testElement.add( bElement );
testElement.add( cElement );
returnedUEArray = testElement.toArray(tooSmallUEArray);
boolean containsOnlyNull = true;
for ( UniverseElement e : tooSmallUEArray )
{
if ( e != null )
{
containsOnlyNull = false;
break;
}
}
assertTrue(
"An array that is too small to contain all the elements in a UniverseElementComposite should, after being passed to toArray(), be unchanged.",
containsOnlyNull );
assertEquals(
"After creating a UniverseElement array from a UniverseElementComposite, the returned array should be the same size as the UniverseElementComposite.",
returnedUEArray.length, testElement.size() );
assertTrue(
"After creating a UniverseElement array from a UniverseElementComposite, every element in the returned array should be contained in the UniverseElementComposite.",
testElement.containsAll( Arrays.asList( returnedUEArray ) ) );
returnedUEArray = testElement.toArray( bigEnoughUEArray );
assertNull(
"An array that was big enough to contain all the elements in a UniverseElementComposite should, after being passed to toArray(), contain null at the size() array index.",
bigEnoughUEArray[testElement.size()]);
assertTrue(
"An array that was big enough to contain all the elements in a UniverseElementComposite should, after being passed to toArray(), contain all the elements in the UniverseElementComposite.",
testElement.containsAll(
Arrays.asList(
Arrays.copyOfRange(
bigEnoughUEArray, 0,
testElement.size() ) ) ) );
assertEquals(
"After creating a UniverseElement array from a UniverseElementComposite, the returned array should be the same size as the UniverseElementComposite, even when the passed array was large enough to hold all the elements in the UniverseElementComposite.",
returnedUEArray.length, testElement.size() );
assertTrue(
"After creating a UniverseElement array from a UniverseElementComposite, every element in the returned array should be contained in the UniverseElementComposite, even when the passed array was large enough to hold all the elements in the UniverseElementComposite.",
testElement.containsAll( Arrays.asList( returnedUEArray ) ) );
try
{
testElement.toArray( null );
fail("Passing a null reference to toArray() should throw an exception, but it did not.");
}
catch ( NullPointerException e )
{
}
catch ( Exception e )
{
fail("Passing a null reference to toArray() should only throw a NullPointerException, but a(n) " + e.getClass().getName() + " was thrown.");
}
try
{
testElement.toArray( new Integer[7] );
fail("Attempting to call toArray() by passing it an array that is not a UniverseElement array (or some derivation thereof) should throw an exception, but did not.");
}
catch ( ArrayStoreException e )
{
}
catch ( Exception e )
{
fail("Attempting to call toArray() by passing it an array that is not a UniverseElement array (or some derivation thereof) should only throw an ArrayStoreException, but a(n) " + e.getClass().getName() + " was thrown.");
}
Object[] objectArray = testElement.toArray();
assertEquals(
"After creating an Object array from a UniverseElementComposite, the resulting object array should be the same size as the UniverseElementComposite.",
objectArray.length, testElement.size() );
assertTrue(
"After creating an Object array from a UniverseElementComposite, every element of the object array should be contained in the UniverseElementComposite.",
testElement.containsAll( Arrays.asList( objectArray ) ) );
testElement.clear();
testElement.add( new TestC() );
testElement.add( new TestC() );
testElement.add( new TestDerivedFromC() );
testElement.add( new TestDerivedFromC() );
testElement.add( new TestC() );
TestC[] returnedTestC = null;
TestC[] passedTestC = new TestC[7];
returnedTestC = testElement.toArray(passedTestC);
assertNull(
"An array that was big enough to contain all the elements in a UniverseElementComposite should, after being passed to toArray(), contain null at the size() array index, even if the type of the elements in the array is a subclass of UniverseElement, but a supertype of all the elements in the UniverseElementComposite.",
passedTestC[testElement.size()]);
assertTrue(
"An array that was big enough to contain all the elements in a UniverseElementComposite should, after being passed to toArray(), contain all the elements in the UniverseElementComposite, even if the type of the elements in the array is a subclass of UniverseElement, but a supertype of all the elements in the UniverseElementComposite.",
testElement.containsAll(
Arrays.asList(
Arrays.copyOfRange(
passedTestC, 0,
testElement.size() ) ) ) );
assertEquals(
"After creating a UniverseElement array from a UniverseElementComposite, the returned array should be the same size as the UniverseElementComposite, even when the passed array was large enough to hold all the elements in the UniverseElementComposite, and even if the type of the elements in the passed array is a subclass of UniverseElement, but a supertype of all the elements in the UniverseElementComposite.",
returnedTestC.length, testElement.size() );
assertTrue(
"After creating a UniverseElement array from a UniverseElementComposite, every element in the returned array should be contained in the UniverseElementComposite, even when the passed array was large enough to hold all the elements in the UniverseElementComposite, and even if the type of the elements in the passed array is a subclass of UniverseElement, but a supertype of all the elements in the UniverseElementComposite.",
testElement.containsAll( Arrays.asList( returnedTestC ) ) );
testElement.add( new TestB() );
try
{
testElement.toArray( passedTestC );
fail("Attempting to call toArray() by passing it an array with a type that is not a supertype of all the elements in the UniverseElementComposite should throw an exception, but did not.");
}
catch ( ArrayStoreException e )
{
}
catch ( Exception e )
{
fail("Attempting to call toArray() by passing it an array with a type that is not a supertype of all the elements in the UniverseElementComposite should only throw an ArrayStoreException, but a(n) " + e.getClass().getName() + " was thrown.");
}
}
/**
* This method will test that the standard Iterator that is available from
* UniverseElementComposite functions correctly.
* @throws Exception
*/
@Test
public void creatingAndUsingUniverseElementCompositeIterators() throws Exception
{
UniverseElement.Iterator testIter = testElement.iterator();
assertFalse(
"An iterator created for an empty UniverseElementComposite should recognize immediately that there is no next element.",
testIter.hasNext() );
try
{
testIter.remove();
fail("Attempting to call remove() on an iterator for an empty UniverseElementComposite should always throw an exception, but did not.");
}
catch ( IllegalStateException e )
{
}
catch ( Exception e )
{
fail("Attempting to call remove() on an iterator for an empty UniverseElementComposite should only throw an IllegalStateException, but a(n) " + e.getClass().getName() + " was thrown.");
}
try
{
testIter.next();
fail("Attempting to iterate to the next element in an empty UniverseElementComposite should throw an exception, but did not.");
}
catch ( NoSuchElementException e )
{
}
catch ( Exception e )
{
fail("Attempting to iterate to the next element in an empty UniverseElementComposite should only throw a NoSuchElementException, but a(n) " + e.getClass().getName() + " was thrown.");
}
testElement.add( new TestUElementComposite() );
testElement.add( new TestA() );
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.",
testIter.next() );
}
/**
* This method will test the copy constructor for UniverseElementComposite. There is
* really only one condition that the copy constructor has to obey: the
* resulting copy should be similar() to the original.
* @throws Exception
*/
@Test
public void copyingUniverseElementComposites() throws Exception
{
TestA element1 = new TestA();
TestA element2 = new TestA();
TestB element3 = new TestB();
TestB element4 = new TestB();
TestB element5 = new TestB();
TestC element6 = new TestC();
TestC element7 = new TestC();
TestDerivedFromC element8 = new TestDerivedFromC();
TestDerivedFromC element9 = new TestDerivedFromC();
TestUElement element10 = new TestUElement();
TestUElementComposite element11 = new TestUElementComposite();
element1.add( element2 );
element1.add( element7 );
element1.add( element11 );
element1.setCenter( 1, 1, 1 );
element2.add( element8 );
element2.setCenter( 2, 2, 2 );
element3.setCenter( 3, 3, 3 );
element4.setCenter( 4, 4, 4 );
element4.add( element10 );
element5.setCenter( 5, 5, 5 );
element6.add( element3 );
element6.add( element5 );
element6.add( element9 );
element6.setCenter( 6, 6, 6 );
element7.setCenter( 7, 7, 7 );
element8.setCenter( 8, 8, 8 );
element9.setCenter( 9, 9, 9 );
TestUElementComposite copiedElement =
new TestUElementComposite(testElement);
assertNotNull(
"Constructing a copy of an empty UniverseElementComposite should not result in a null UniverseElementComposite.",
copiedElement );
assertTrue(
"A copy of an empty UniverseElementComposite should, itself, be empty.",
copiedElement.isEmpty() );
testElement.add( element1 );
testElement.add( element4 );
testElement.add( element6 );
copiedElement = new TestUElementComposite(testElement);
assertNotNull(
"After creating a new UniverseElementComposite through a copy constructor, the new UniverseElementComposite should not be null.",
copiedElement );
assertTrue(
"After creating a new UniverseElementComposite through a copy constructor, the copy should be similar to the original.",
testElement.similar( copiedElement ) );
assertEquals(
"After creating a new UniverseElementComposite through a copy constructor, the centers of the two element should be equal.",
testElement.getCenter(), copiedElement.getCenter() );
element8.add( new TestC() );
assertFalse(
"After creating a new UniverseElementComposite through a copy constructor, and then adding a new UniverseElement child deep in the structure of the original UniverseElementComposite, the two UniverseElementComposites should no longer be similar.",
testElement.similar( copiedElement ) );
element8.clear();
assertTrue(
"Having created a new UniverseElementComposite through a copy constructor and then added a child deep inside the structure of the original UniverseElementComposite (thereby making the two UniverseElementComposites dissimilar), removing that added child should make the two UniverseElementComposites similar again.",
testElement.similar( copiedElement ) );
element8.setCenter( 20, 20, 20 );
assertFalse(
"After creating a new UniverseElementComposite through a copy constructor and then changing the center of one of the children deep in the structure of the original UniverseElementComposite, the two elements should not longer be similar.",
testElement.similar( copiedElement ) );
/* This is almost certainly redundant. */
testElement.setCenter( 1, 1, 1 );
assertFalse(
"After creating a new UniverseElementComposite through a copy constructor and then changing the center of the original UniverseElementComposite, the centers of the two elements should not be equal.",
testElement.getCenter() == copiedElement.getCenter() );
}
/* The following classes are used in the badCopyConstructionOfUniverseElementComposite
* test.
*/
public static class NoConstructorTest extends UniverseElement {}
public static abstract class AbstractConstructorTest extends UniverseElement
{
public AbstractConstructorTest()
{
}
public AbstractConstructorTest( AbstractConstructorTest t )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( t );
}
}
public static class DerivedAbstractConstructorTest
extends AbstractConstructorTest {}
public static class ProtectedConstructorTest extends UniverseElement
{
public ProtectedConstructorTest()
{
}
protected ProtectedConstructorTest( ProtectedConstructorTest t )
throws NoSuchMethodException, IllegalAccessException,
InstantiationException, InvocationTargetException
{
super( t );
}
}
/**
* Because the copy constructor uses reflection to get at the types of the
* elements that are contained in the element being constructed, to insure that
* an appropriate constructor is called for those elements, there are things
* that can go wrong with the constructor now, and we need to make sure that
* those bad things throw appropriate exceptions.
*
* The bad things that can happen include:
* <ul>
* <li>The element being copied doesn't have a copy constructor.
* </li>
* <li>The element being copied doesn't have a public copy
* constructor.</li>
* <li>The element being copied ends up attempting to call a public
* constructor of an abstract superclass (or something along
* these lines).</li>
* <li>One of the above errors occurs for some child element of the
* element being copied.</li>
*
* It actually looks like it's only going to be possible to get one of the
* exceptions if they arise because of the attempted copying of a child
* element, so that is what we'll be testing here.
*
* I'm actually not sure how to make the second (non-public) and third
* (abstract) exceptions happen in our specific case. I hate to leave those
* untested, but it looks like the class to be copied and its children
* either have the appropriate public copy constructor (which is what the
* underlying reflection code is checking for), or they have No Such Method.
* Of course, if a child's child (or child's child's child, or so on) hasn't
* got an appropriate copy constructor, then you get an Invocation Target
* Exception, but that's just one or more layers of wrapping paper around the
* NoSuchMethodException. But, to get the other two. . . I'm mystified. It
* may actually just not be possible with the code that I've written, since
* I'm asking for the copy constructor of an object that must have, at some
* point, already been instantiated (and so therefore can never have been
* abstract), and the call that I make to find the copy constructor implicitly
* checks for only public constructors. It'd be nice to be able to
* just simplify "throws" down to just the NoSuchMethodException. In fact:
*
* TODO: Pear down the copy constructor exceptions to just the
* NoSuchMethodException, if it's safe.
*
* @throws Exception
*/
@Test
public void badConstructionOfUniverseElementComposites() throws Exception
{
NoConstructorTest elementNCT = new NoConstructorTest();
DerivedAbstractConstructorTest elementDACT = new DerivedAbstractConstructorTest();
ProtectedConstructorTest elementPCT = new ProtectedConstructorTest();
TestA goodElement = new TestA();
testElement.add(elementNCT);
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that does not have a copy constructor should throw an exception, but did not.");
}
catch ( NoSuchMethodException e )
{
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that does not have a copy constructor should only throw a NoSuchMethodException, but a(n) " + e.getClass().getName() + " was thrown.");
}
testElement.clear();
testElement.add( elementDACT );
/* Hmm, doesn't work this way. */
/*
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that only has access to the public copy constructor of an abstract superclass should throw an exception, but did not.");
}
catch ( InstantiationException e )
{
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that only has access to the public copy constructor of an abstract superclass should only throw an InstantiationException, but a(n) " + e.getClass().getName() + "was thrown.");
}
*/
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that only has access to the public copy constructor of an abstract superclass should throw an exception, but did not.");
}
catch ( NoSuchMethodException e )
{
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that only has access to the public copy constructor of an abstract superclass should only throw a NoSuchMethodException, but a(n) " + e.getClass().getName() + "was thrown.");
}
testElement.clear();
testElement.add( elementPCT );
/* ARG! This also doesn't work this way! */
/*
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that only has non-public copy constructors should throw an exception, but did not.");
}
catch ( IllegalAccessException e )
{
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that only has non-public copy constructors should only throw an IllegalAccessException, but a(n) " + e.getClass().getName() + " was thrown.");
}
*/
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that only has non-public copy constructors should throw an exception, but did not.");
}
catch ( NoSuchMethodException e )
{
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that only has non-public copy constructors should only throw a NoSuchMethodException, but a(n) " + e.getClass().getName() + " was thrown.");
}
testElement.clear();
goodElement.add(elementNCT);
testElement.add(goodElement);
try
{
new TestUElementComposite(testElement);
fail("Attempting to copy a child UniverseElement that, itself, has a copy constructor-less child should throw an exception, but did not.");
}
catch ( InvocationTargetException e )
{
if ( e.getCause().getClass() != NoSuchMethodException.class )
{
fail("When an attempted copy of a child UniverseElement's copy constructor-less child element (that's two layers deep) throws an InvocationTargetException, the cause of that exception should only be a NoSuchMethodEsception, but the cause was " + e.getCause().getClass().getName() + ".");
}
}
catch ( Exception e )
{
fail("Attempting to copy a child UniverseElement that, itself, has a copy constructor-less child should only throw an InvocationTargetException, but a(n) " + e.getClass().getName() + " was thrown.");
}
}
/**
* Okay, this test is over-the-top, crazy unnecessary, but I'm putting it in
* anyway, just in case.
* @throws Exception
*/
@Test
public void checkingForUniverseElementLeafState() throws Exception
{
assertFalse(
"UniverseElementComposites that do not override isLeaf() should always return false when that method is called.",
(new TestUElementComposite()).isLeaf()
|| (new TestA()).isLeaf()
|| (new TestC()).isLeaf()
|| (new TestDerivedFromC()).isLeaf() );
}
}