@Test(timeout = TIMEOUT) public void testDeepCopyNestedCollection() { ArrayList<ArrayList<?>> elmMatrix = new ArrayList<ArrayList<?>>(); CloneableCopy elm1 = new CloneableCopy("elm1"); CloneableCopy elm2 = new CloneableCopy("elm2"); CloneableCopy elm3 = null; ArrayList<CloneableCopy> nested = new ArrayList<CloneableCopy>(2); Collections.addAll(nested, elm1, elm2, elm3); elmMatrix.add(nested); // try adding itself as element // elmMatrix.add(elmMatrix); ArrayList<ArrayList<?>> copy = MiscUtils.deepCollectionCopy(elmMatrix); assertNotNull(copy); assertNotSame(elmMatrix, copy); assertEquals(elmMatrix, copy); assertNotSame(elmMatrix.get(0), copy.get(0)); // assertNotSame(elmMatrix.get(1),copy.get(1)); // make sure nested was also copied ArrayList<?> nestedCopy = copy.get(0); for (int i = 0; i < nestedCopy.size(); ++i) { if (nested.get(i) != null) assertNotSame(nestedCopy.get(i), nested.get(i)); else assertNull(nestedCopy.get(i)); // assertEquals(nestedCopy.get(i),nested.get(i)); } }
/** Tests {@link MiscUtils#deepCollectionCopy(Collection)}. */ @Test public void testDeepCopyCollection() { ArrayList<CloneableCopy> elms = new ArrayList<CloneableCopy>(3); CloneableCopy elm1 = new CloneableCopy("elm1"); CloneableCopy elm2 = new CloneableCopy("elm2"); elms.add(elm1); elms.add(elm2); elms.add(null); ArrayList<CloneableCopy> copy = MiscUtils.deepCollectionCopy(elms); assertNotNull(copy); assertNotSame(elms, copy); assertEquals(elms, copy); assertNotSame(elms.get(0), copy.get(0)); assertNotSame(elms.get(1), copy.get(1)); assertSame(elms.get(2), copy.get(2)); assertNull(copy.get(2)); }