Utils { public static Element reconstruct(Element elem, String... childOrder) { return reconstruct(elem, removeChildrenToMap(elem), childOrder); }  static List<File> buildFileListFromStringList(List<String> sources); static List<File> buildFileList(List<File> sources); static List<String> extractClassesFromJar(File jarFile); static Map<String,Element> removeChildrenToMap(Element elem); static Element reconstruct(Element elem, String... childOrder); static Element reconstruct(Element elem, Map<String,Element> children, String... childOrder);  }
@Test public void testReconstructBasicOperation() throws Exception { Document dom = element("root", element("c2"), element("c1"), element("c3")) .toDOM(); Element root = dom.getDocumentElement(); Utils.reconstruct(root, "c3", "c1", "c2"); assertEquals("elements have been reordered", Arrays.asList("c3", "c1", "c2"), getChildNames(root)); }
@Test public void testReconstructWithExtraElements() throws Exception { Document dom = element("root", element("c4"), element("c2"), element("c1"), element("c3"), element("c5")) .toDOM(); Element root = dom.getDocumentElement(); Utils.reconstruct(root, "c3", "c2"); assertEquals("unspecified elements at end, in original order", Arrays.asList("c3", "c2", "c4", "c1", "c5"), getChildNames(root)); }
@Test public void testReconstructPreservesRootAttributes() throws Exception { Document dom = element("root", attribute("argle", "bargle"), element("c2"), element("c1"), element("c3")) .toDOM(); Element root = dom.getDocumentElement(); Utils.reconstruct(root, "c3", "c1", "c2"); assertEquals("attribute remains", "bargle", root.getAttribute("argle")); }
@Test public void testReconstructPreservesComments() throws Exception { Document dom = element("root", element("c2"), comment("test"), element("c1"), element("c3")) .toDOM(); Element root = dom.getDocumentElement(); Utils.reconstruct(root, "c3", "c1", "c2"); boolean foundComment = false; for (Node node : new NodeListIterable(root.getChildNodes())) { if (node.getNodeType() == Node.COMMENT_NODE) foundComment = true; } assertTrue("found comment node", foundComment); }