HtmlRenderer { public void mergeFiles(String entity, File sourceFolder) throws CommanderException { File allRecordsFile = new File(sourceFolder.getAbsolutePath() + "/" + "all_" + entity + ".xml"); DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance(); docFactory.setNamespaceAware(true); try { DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(entity); rootElement.setAttributeNS("http: "http: doc.appendChild(rootElement); File[] recordFileList = sourceFolder.listFiles(); Arrays.sort(recordFileList); for (File recordFile : recordFileList) { if (!recordFile.getAbsolutePath() .equals(allRecordsFile.getAbsolutePath())) { Document entryDoc = docBuilder.parse(recordFile); Node importNode = doc .importNode(entryDoc.getDocumentElement(), true); rootElement.appendChild(importNode); recordFile.delete(); } } TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(doc), new StreamResult(allRecordsFile)); } catch (ParserConfigurationException e) { throw new CommanderException( "Could not configure document builder for " + allRecordsFile.getAbsolutePath(), e); } catch (SAXException e) { throw new CommanderException( "Could not parse one of the xml files in " + sourceFolder.getAbsolutePath(), e); } catch (IOException e) { throw new CommanderException( "Could not open one of the xml files in " + sourceFolder.getAbsolutePath(), e); } catch (TransformerConfigurationException e) { throw new CommanderException( "Could not configure xml-transformer for " + allRecordsFile.getAbsolutePath(), e); } catch (TransformerException e) { new CommanderException("Could not transform xml-result into file " + allRecordsFile.getAbsolutePath(), e); } } HtmlRenderer(String aSystemName, String aRenderPath); void generateOutput(); final void render(final File xslFile, final File xmlFile,
            final File htmlFile); void mergeFiles(String entity, File sourceFolder); void generateFileList(final String entity, final File sourceFolder,
            final File targetFolder);  }
@Test public void mergeFilesTest() throws IOException, CommanderException, ParserConfigurationException, SAXException { HtmlRenderer renderer = new HtmlRenderer("SampleOrg", "/a/destination/folder"); File resourceFolder = new File("src/test/resources/roles"); File tmpFolder = new File("src/test/resources/mergeFilesTest"); File testFile1 = new File(tmpFolder.getAbsolutePath() + "/CEO.role"); File testFile2 = new File(tmpFolder.getAbsolutePath() + "/CFO.role"); tmpFolder.mkdir(); Files.copy(Paths.get(resourceFolder.getAbsolutePath() + "/CEO.role"), testFile1.toPath()); Files.copy(Paths.get(resourceFolder.getAbsolutePath() + "/CFO.role"), testFile2.toPath()); renderer.mergeFiles("roles", tmpFolder); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); File allRoles = new File( tmpFolder.getAbsolutePath() + "/all_roles.xml"); Document doc = dBuilder.parse(allRoles); Element rootElement = doc.getDocumentElement(); Assert.assertEquals("roles", rootElement.getNodeName()); Assert.assertEquals("http: rootElement.getNamespaceURI()); NodeList roles = rootElement.getChildNodes(); int j = 0; for (int i = 0; i < roles.getLength(); i++) { if (roles.item(i).getNodeType() == Node.ELEMENT_NODE) { Assert.assertEquals("Role", roles.item(i).getNodeName()); j++; } } Assert.assertEquals(2, j); allRoles.delete(); tmpFolder.delete(); Assert.assertFalse(tmpFolder.exists()); }