Skip to content

Commit

Permalink
Refactor, create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evie-lau committed Jul 6, 2023
1 parent 33f99fa commit 325ca36
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down Expand Up @@ -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 </element> 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) {
Expand Down Expand Up @@ -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: <applicationMonitor>
* @param attributes - HashMap of the <attributes,values>
*/
public void createServerElement(String elementName, HashMap<String, String> 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);
}
Expand Down Expand Up @@ -194,4 +235,7 @@ public Node findFirstLevelComment(String comment) {
return null;
}

public Element findServerElement() {
return doc.getDocumentElement(); // defined for this type of file
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 </element> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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("<applicationMonitor updateTrigger=\"${io.openliberty.tools.update.trigger}\"/>"));
}
}

0 comments on commit 325ca36

Please sign in to comment.