public void testDeepClone() {
Collection c1 = new ArrayList();
Collection c2 = null;
// empty list
try {
c2 = ObjectUtilities.deepClone(c1);
assertTrue(c2.isEmpty());
}
catch (CloneNotSupportedException e) {
assertTrue(false);
}
// list containing Cloneable objects
c1 = new ArrayList();
c1.add(new Point(1, 2));
c1.add(new Point(3, 4));
try {
c2 = ObjectUtilities.deepClone(c1);
assertEquals(2, c2.size());
assertTrue(c2.contains(new Point(1, 2)));
assertTrue(c2.contains(new Point(3, 4)));
}
catch (CloneNotSupportedException e) {
assertTrue(false);
}
// list containing Cloneable and null objects
c1 = new ArrayList();
c1.add(new Point(1, 2));
c1.add(null);
c1.add(new Point(3, 4));
try {
c2 = ObjectUtilities.deepClone(c1);
assertEquals(3, c2.size());
assertTrue(c2.contains(new Point(1, 2)));
assertTrue(c2.contains(new Point(3, 4)));
}
catch (CloneNotSupportedException e) {
assertTrue(false);
}
// list containing non-Cloneable objects
c1.clear();
c1.add("S1");
c1.add("S2");
try {
c2 = ObjectUtilities.deepClone(c1);
assertTrue(false); // if we get to here, the test has failed
}
catch (CloneNotSupportedException e) {
assertTrue(true);
}
// null list
try {
c2 = ObjectUtilities.deepClone(null);
assertTrue(false); // if we get to here, the test has failed
}
catch (IllegalArgumentException e) {
assertTrue(true);
}
catch (CloneNotSupportedException e) {
assertTrue(false);
}
}
Some checks for the deepClone(Collection) method. |