Skip to content

Commit

Permalink
Add XMLTool testing-util for XML-doc parsing and XPath evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Oct 14, 2023
1 parent f0b800c commit 157c7a2
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2021 SAP AG and others.
* Copyright (c) 2010, 2023 SAP AG and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -25,37 +25,19 @@
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.eclipse.tycho.test.util.XMLTool;
import org.junit.Test;
import org.osgi.framework.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Tycho192SourceBundleTest extends AbstractTychoIntegrationTest {

private final DocumentBuilder docBuilder = createDocBuilder();
private final XPath xpath = XPathFactory.newInstance().newXPath();

private DocumentBuilder createDocBuilder() {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}

@Test
public void testDefaultSourceBundleSuffix() throws Exception {
Verifier verifier = getVerifier("/TYCHO192sourceBundles", false);
Expand All @@ -69,27 +51,25 @@ public void testDefaultSourceBundleSuffix() throws Exception {

private void checkP2ContentXml(File p2Content) throws Exception {
assertTrue(p2Content.isFile());
Document p2ContentDOM = docBuilder.parse(p2Content);
XPathExpression sourceBundleUnitExpression = xpath.compile("/units/unit[@id = 'helloworld.source']");
Element sourceBundleUnitNode = (Element) sourceBundleUnitExpression.evaluate(p2ContentDOM.getDocumentElement(),
XPathConstants.NODE);
Document p2ContentDOM = XMLTool.parseXMLDocument(p2Content);
Element sourceBundleUnitNode = (Element) XMLTool.getFirstMatchingNode(p2ContentDOM,
"/units/unit[@id = 'helloworld.source']");
assertNotNull("unit with id 'helloworld.source' not found", sourceBundleUnitNode);
assertHasMavenClassifierProperty(sourceBundleUnitNode);
}

private void assertHasMavenClassifierProperty(Element node) throws XPathExpressionException {
XPathExpression classifierNodeExpression = xpath.compile("properties/property[@name = 'maven-classifier']");
Element classifierNode = (Element) classifierNodeExpression.evaluate(node, XPathConstants.NODE);
Element classifierNode = (Element) XMLTool.getFirstMatchingNode(node,
"properties/property[@name = 'maven-classifier']");
assertNotNull("property node with name 'maven-classifier' not found", classifierNode);
assertEquals("sources", classifierNode.getAttribute("value"));
}

private void checkP2ArtifactsXml(File p2Artifacts) throws SAXException, IOException, XPathExpressionException {
private void checkP2ArtifactsXml(File p2Artifacts) throws Exception {
assertTrue(p2Artifacts.isFile());
Document p2ArtifactsDOM = docBuilder.parse(p2Artifacts);
XPathExpression sourceBundleNodeExpression = xpath.compile("/artifacts/artifact[@id = 'helloworld.source']");
Element sourceBundleArtifactNode = (Element) sourceBundleNodeExpression
.evaluate(p2ArtifactsDOM.getDocumentElement(), XPathConstants.NODE);
Document p2ArtifactsDOM = XMLTool.parseXMLDocument(p2Artifacts);
Element sourceBundleArtifactNode = (Element) XMLTool.getFirstMatchingNode(p2ArtifactsDOM,
"/artifacts/artifact[@id = 'helloworld.source']");
assertNotNull("artifact with id 'helloworld.source' not found", sourceBundleArtifactNode);
assertHasMavenClassifierProperty(sourceBundleArtifactNode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2010, 2022 SAP AG and others.
* Copyright (c) 2010, 2023 SAP AG and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -23,15 +23,10 @@
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.apache.maven.it.Verifier;
import org.eclipse.jetty.server.NetworkTrafficServerConnector;
Expand All @@ -40,6 +35,7 @@
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.eclipse.tycho.test.util.XMLTool;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -182,28 +178,22 @@ private void replaceSettingsArg(Verifier verifier) throws IOException {
}

private void configureProxyInSettingsXml(boolean isProxyActive, String user, String password) throws Exception {
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(settings);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression proxyExpr = xpath.compile("/settings/proxies/proxy");
Element proxyNode = (Element) proxyExpr.evaluate(dom.getDocumentElement(), XPathConstants.NODE);
Document dom = XMLTool.parseXMLDocument(settings);
Element proxyNode = (Element) XMLTool.getFirstMatchingNode(dom, "/settings/proxies/proxy");
{
XPathExpression portExpr = xpath.compile("/settings/proxies/proxy/port");
Element node = (Element) portExpr.evaluate(dom.getDocumentElement(), XPathConstants.NODE);
Element node = (Element) XMLTool.getFirstMatchingNode(dom, "/settings/proxies/proxy/port");
node.setTextContent(String.valueOf(proxyPort));
}
{
XPathExpression activeExpr = xpath.compile("/settings/proxies/proxy/active");
Element activeNode = (Element) activeExpr.evaluate(dom.getDocumentElement(), XPathConstants.NODE);
Element activeNode = (Element) XMLTool.getFirstMatchingNode(dom, "/settings/proxies/proxy/active");
activeNode.setTextContent(String.valueOf(isProxyActive));
}
{
XPathExpression userExpr = xpath.compile("/settings/proxies/proxy/username");
Element userNode = (Element) userExpr.evaluate(dom.getDocumentElement(), XPathConstants.NODE);
Element userNode = (Element) XMLTool.getFirstMatchingNode(dom, "/settings/proxies/proxy/username");
updateNodeValue("username", userNode, user, dom, proxyNode);
}
{
XPathExpression passwordExpr = xpath.compile("/settings/proxies/proxy/password");
Element passwordNode = (Element) passwordExpr.evaluate(dom.getDocumentElement(), XPathConstants.NODE);
Element passwordNode = (Element) XMLTool.getFirstMatchingNode(dom, "/settings/proxies/proxy/password");
updateNodeValue("password", passwordNode, password, dom, proxyNode);
}
Transformer xslTransformer = TransformerFactory.newInstance().newTransformer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2021 SAP AG
* Copyright (c) 2011, 2023 SAP AG
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,24 +12,18 @@
*******************************************************************************/
package org.eclipse.tycho.test.jarsigning;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.util.List;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.eclipse.tycho.test.util.XMLTool;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class JarSigningTest extends AbstractTychoIntegrationTest {

Expand Down Expand Up @@ -57,30 +51,12 @@ private void checkSha256SumsArePresent(Verifier verifier) throws Exception {
File repoDir = new File(verifier.getBasedir(), "rcp/target/repository");
File artifacts = new File(repoDir, "artifacts.jar");
assertTrue(artifacts.isFile());
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = null;
try (ZipFile artifactsJar = new ZipFile(artifacts)) {
ZipEntry artifactsXmlEntry = artifactsJar.getEntry("artifacts.xml");
document = parser.parse(artifactsJar.getInputStream(artifactsXmlEntry));
}
Element repository = document.getDocumentElement();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xpath.evaluate("/repository/artifacts/artifact", repository,
XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Element artifactNode = (Element) nodeList.item(i);
NodeList properties = (NodeList) xpath.evaluate("properties/property", artifactNode,
XPathConstants.NODESET);
boolean hasSha256 = false;
for (int j = 0; j < properties.getLength(); j++) {
Element property = (Element) properties.item(j);
String propName = property.getAttribute("name");
if ("download.checksum.sha-256".equals(propName)) {
hasSha256 = true;
break;
}
}
assertTrue("artifact does not have a 'download.checksum.sha-256' attribute", hasSha256);
Document document = XMLTool.parseXMLDocumentFromJar(artifacts, "artifacts.xml");
List<Node> artifactNodes = XMLTool.getMatchingNodes(document, "/repository/artifacts/artifact");
for (Node artifact : artifactNodes) {
List<Node> checksumProperties = XMLTool.getMatchingNodes(artifact,
"properties/property[@name='download.checksum.sha-256']");
assertFalse("artifact does not have a 'download.checksum.sha-256' attribute", checksumProperties.isEmpty());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2021 SAP AG and others.
* Copyright (c) 2012, 2023 SAP AG and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -18,24 +18,18 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.eclipse.tycho.test.util.XMLTool;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.w3c.dom.Node;

public class ParallelTestExecutionTest extends AbstractTychoIntegrationTest {

Expand All @@ -56,20 +50,15 @@ public void testParallelExecution() throws Exception {
assertEquals(expectedTests, actualTests);
}

private Set<String> extractExecutedTests(File[] xmlReports)
throws FileNotFoundException, XPathExpressionException, IOException {
XPath xpath = XPathFactory.newInstance().newXPath();
private Set<String> extractExecutedTests(File[] xmlReports) throws Exception {
Set<String> actualTests = new HashSet<>();
for (File xmlReportFile : xmlReports) {
NodeList testCaseNodes;
try (FileInputStream xmlStream = new FileInputStream(xmlReportFile)) {
testCaseNodes = (NodeList) xpath.evaluate("/testsuite/testcase", new InputSource(xmlStream),
XPathConstants.NODESET);
}
for (int i = 0; i < testCaseNodes.getLength(); i++) {
Element node = (Element) testCaseNodes.item(i);
String testClassName = node.getAttribute("classname");
String method = node.getAttribute("name");
Document document = XMLTool.parseXMLDocument(xmlReportFile);
List<Node> matchingNodes = XMLTool.getMatchingNodes(document, "/testsuite/testcase");
for (Node node : matchingNodes) {
Element element = (Element) node;
String testClassName = element.getAttribute("classname");
String method = element.getAttribute("name");
actualTests.add(testClassName + "#" + method);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2018 SAP AG and others.
* Copyright (c) 2012, 2023 SAP AG and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -16,15 +16,10 @@
import static org.junit.Assert.assertTrue;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class SurefireUtil {

Expand All @@ -42,23 +37,22 @@ public static void assertTestMethodWasSuccessfullyExecuted(String baseDir, Strin
int iterations) throws Exception {
File resultFile = getTestResultFile(baseDir, className);
Document document = readDocument(resultFile);
XPath xpath = XPathFactory.newInstance().newXPath();
// surefire-test-report XML schema:
// https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd
String testCaseXPath = String.format("/testsuite/testcase[@classname='%s' and @name='%s']", className,
methodName);
NodeList testCaseNodes = (NodeList) xpath.evaluate(testCaseXPath, document, XPathConstants.NODESET);
List<Node> testCaseNodes2 = XMLTool.getMatchingNodes(document, testCaseXPath);
assertEquals(resultFile.getAbsolutePath() + " with xpath " + testCaseXPath
+ " does not match the number of iterations", iterations, testCaseNodes.getLength());
+ " does not match the number of iterations", iterations, testCaseNodes2.size());

NodeList failureNodes = (NodeList) xpath.evaluate(testCaseXPath + "/failure", document, XPathConstants.NODESET);
assertEquals(0, failureNodes.getLength());
List<Node> failureNodes = XMLTool.getMatchingNodes(document, testCaseXPath + "/failure");
assertEquals(0, failureNodes.size());

NodeList errorNodes = (NodeList) xpath.evaluate(testCaseXPath + "/error", document, XPathConstants.NODESET);
assertEquals(0, errorNodes.getLength());
List<Node> errorNodes = XMLTool.getMatchingNodes(document, testCaseXPath + "/error");
assertEquals(0, errorNodes.size());

NodeList skippedNodes = (NodeList) xpath.evaluate(testCaseXPath + "/skipped", document, XPathConstants.NODESET);
assertEquals(0, skippedNodes.getLength());
List<Node> skippedNodes = XMLTool.getMatchingNodes(document, testCaseXPath + "/skipped");
assertEquals(0, skippedNodes.size());
}

public static void assertTestMethodWasSuccessfullyExecuted(String baseDir, String className, String methodName)
Expand Down Expand Up @@ -88,22 +82,14 @@ public static void assertNumberOfSkippedTests(String baseDir, String className,

private static int extractNumericAttribute(String baseDir, String className, String attributeXPath)
throws Exception {
Document document = readDocument(baseDir, className);
XPath xpath = XPathFactory.newInstance().newXPath();
String numberOfTests = (String) xpath.evaluate(attributeXPath, document, XPathConstants.STRING);
return Integer.parseInt(numberOfTests);
}

private static Document readDocument(String baseDir, String className) throws Exception {
return readDocument(getTestResultFile(baseDir, className));
Document document = readDocument(getTestResultFile(baseDir, className));
Node numberOfTests = XMLTool.getFirstMatchingNode(document, attributeXPath);
return Integer.parseInt(numberOfTests.getNodeValue());
}

private static Document readDocument(File sureFireTestReport) throws Exception {
assertTrue(sureFireTestReport.isFile());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(sureFireTestReport);
return document;
return XMLTool.parseXMLDocument(sureFireTestReport);
}

private static File getTestResultFile(String baseDir, String className) {
Expand Down
Loading

0 comments on commit 157c7a2

Please sign in to comment.