diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java index 281d0f56..0d040096 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ApplicationMonitorConfigXmlDocument.java @@ -17,40 +17,35 @@ import java.io.File; import java.io.IOException; +import java.util.HashMap; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; -import org.w3c.dom.Element; - public class ApplicationMonitorConfigXmlDocument extends XmlDocument { public static final String APPLICATION_CONFIG_XML_FILENAME = "liberty-plugin-app-monitor-config.xml"; - protected static final String HEADER = "# Generated by liberty-maven-plugin"; + String tool = null; - public ApplicationMonitorConfigXmlDocument() { - try { - createDocument("server"); - } catch (ParserConfigurationException e) { - // it should never occur - e.printStackTrace(); - } - } + HashMap attributes = new HashMap<>(); - public void createAppMonitorElement() { - Element appMonitor = doc.createElement("applicationMonitor"); - appMonitor.setAttribute("updateTrigger", "${io.openliberty.tools.update.trigger}"); - doc.getDocumentElement().appendChild(appMonitor); + public ApplicationMonitorConfigXmlDocument(String tool) { + this.tool = tool; + attributes.put("updateTrigger", "${io.openliberty.tools.update.trigger}"); } - public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException { + public void writeAppMonitorConfigXmlDocument(File serverDirectory) throws IOException, TransformerException, ParserConfigurationException { File appMonXml = getAppMonitorConfigXmlFile(serverDirectory); if (!appMonXml.getParentFile().exists()) { appMonXml.getParentFile().mkdirs(); } - writeXMLDocument(appMonXml); + + ServerConfigXmlDocument configDocument = ServerConfigXmlDocument.newInstance(); + configDocument.createGeneratedComment(tool); + configDocument.createServerElement("applicationMonitor", attributes);; + configDocument.writeXMLDocument(appMonXml); } - public static File getAppMonitorConfigXmlFile(File serverDirectory) { + public File getAppMonitorConfigXmlFile(File serverDirectory) { File f = new File(serverDirectory, "configDropins/overrides/" + APPLICATION_CONFIG_XML_FILENAME); return f; } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java index fda422b3..b60acc58 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlDocument.java @@ -17,23 +17,22 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Comment; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.w3c.dom.Text; import org.xml.sax.SAXException; -import org.w3c.dom.Element; -import org.w3c.dom.Comment; public class ServerConfigXmlDocument extends XmlDocument { // Formerly called src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDropinXmlDocument.java public static String DEFAULT_INDENTATION = " "; private Element featureManager = null; + protected static final String HEADER = "# Generated by liberty-%s-plugin"; private ServerConfigXmlDocument() { } @@ -68,6 +67,35 @@ public static ServerConfigXmlDocument newInstance(File f) throws ParserConfigura return configDocument; } + public void createComment(String comment) { + createComment(findServerElement(), comment); + } + + // add comment to the end of the children + public void createComment(Element elem, String comment) { + Comment commentElement = doc.createComment(comment); + appendBeforeBlanks(elem, commentElement); + } + + /** + * Creates comment "# Generated by liberty-{}-plugin", + * @param tool - Which tool, maven or gradle, is generating the comment + */ + public void createGeneratedComment(String tool) { + String comment = String.format(HEADER, tool); + createComment(comment); + } + + private void appendBeforeBlanks(Element elem, Node childElement) { + Node lastchild = elem.getLastChild(); + if (isWhitespace(lastchild)) { + // last child is the whitespace preceding the so insert before that + elem.insertBefore(childElement, lastchild); + } else { + elem.appendChild(childElement); + } + } + // Return true if the document was changed and false otherwise. public boolean createFMComment(String comment) { if (featureManager == null) { @@ -123,6 +151,19 @@ public boolean removeFMComment(String comment) { return false; } + /** + * Creates element of `elementName`, with attributes/values. + * @param elementName - name of the element, ie: + * @param attributes - HashMap of the + */ + public void createServerElement(String elementName, HashMap attributes) { + Element element = doc.createElement(elementName); + for (String attributeString : attributes.keySet()) { + element.setAttribute(attributeString, attributes.get(attributeString)); + } + findServerElement().appendChild(element); + } + public void createVariableWithValue(String varName, String varValue, boolean isDefaultValue) { createVariableWithValue(doc.getDocumentElement(), varName, varValue, isDefaultValue); } @@ -194,4 +235,7 @@ public Node findFirstLevelComment(String comment) { return null; } + public Element findServerElement() { + return doc.getDocumentElement(); // defined for this type of file + } } diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java index d79d16fa..d3a059d5 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/XmlDocument.java @@ -60,30 +60,6 @@ public void createDocument(File xmlFile) throws ParserConfigurationException, SA doc = builder.parse(xmlFile); } - public void createComment(String comment) { - createComment(findServerElement(), comment); - } - - // add comment to the end of the children - public void createComment(Element elem, String comment) { - Comment commentElement = doc.createComment(comment); - appendBeforeBlanks(elem, commentElement); - } - - private void appendBeforeBlanks(Element elem, Node childElement) { - Node lastchild = elem.getLastChild(); - if (isWhitespace(lastchild)) { - // last child is the whitespace preceding the so insert before that - elem.insertBefore(childElement, lastchild); - } else { - elem.appendChild(childElement); - } - } - - public Element findServerElement() { - return doc.getDocumentElement(); // defined for this type of file - } - public void writeXMLDocument(String fileName) throws IOException, TransformerException { File f = new File(fileName); writeXMLDocument(f); diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java new file mode 100644 index 00000000..7b39afaf --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/config/BaseConfigXmlTest.java @@ -0,0 +1,32 @@ +package io.openliberty.tools.common.plugins.config; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +public class BaseConfigXmlTest { + + public static final String RESOURCES_DIR = "src/test/resources"; + + private static final String RESOURCES_INSTALL_DIR = RESOURCES_DIR + "/serverConfig"; + + protected File serverConfigDir = new File(RESOURCES_INSTALL_DIR); + public File buildDir; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Before + public void setupInstallDir() throws IOException { + serverConfigDir = temp.newFolder(); + File src = new File(RESOURCES_INSTALL_DIR); + FileUtils.copyDirectory(src, serverConfigDir); + + buildDir = temp.newFolder(); + } + +} diff --git a/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java new file mode 100644 index 00000000..1ca450b0 --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/plugins/config/ServerConfigXmlTest.java @@ -0,0 +1,24 @@ +package io.openliberty.tools.common.plugins.config; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.nio.file.Files; + +import org.junit.Test; + +public class ServerConfigXmlTest extends BaseConfigXmlTest { + + @Test + public void createAppMonitorConfigTest() throws Exception { + File serverDirectory = new File(serverConfigDir, "liberty/wlp/usr/servers/defaultServer"); + + ApplicationMonitorConfigXmlDocument appMonXml = new ApplicationMonitorConfigXmlDocument("test"); + appMonXml.writeAppMonitorConfigXmlDocument(serverDirectory); + + File newFile = appMonXml.getAppMonitorConfigXmlFile(serverDirectory); + String content = Files.readString(newFile.toPath()); + assertTrue(content.contains("Generated by liberty-test-plugin")); + assertTrue(content.contains("")); + } +}