From 48725c3087cb013c66d84b39ff5e9ce18cc60fc6 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:12:14 +0200 Subject: [PATCH 01/10] Adding an XML Catalog file. Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Fix typo O instead of zero 0 Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- schema/xmlcatalog.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 schema/xmlcatalog.xml diff --git a/schema/xmlcatalog.xml b/schema/xmlcatalog.xml new file mode 100644 index 00000000..60665b45 --- /dev/null +++ b/schema/xmlcatalog.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 2c4ed7db9e44a522b50189025a598a8f9aad11b0 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Mon, 10 Jun 2024 16:51:33 +0200 Subject: [PATCH 02/10] Update xmlcatalog.xml Signed-off-by: Jan Kowalleck --- schema/xmlcatalog.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/schema/xmlcatalog.xml b/schema/xmlcatalog.xml index 60665b45..645f2ab7 100644 --- a/schema/xmlcatalog.xml +++ b/schema/xmlcatalog.xml @@ -1,6 +1,7 @@ + - + @@ -8,4 +9,4 @@ - \ No newline at end of file + From c6e0f037060c97a41b810d4051cbd03b5dda83db Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:54:52 +0200 Subject: [PATCH 03/10] Update to cyclonedx-core-java-9.0.2 and adding 1.6 in JSON and XML tests https://github.com/CycloneDX/cyclonedx-core-java/releases/tag/cyclonedx-core-java-9.0.2 Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- tools/pom.xml | 2 +- .../schema/BaseSchemaVerificationTest.java | 1 + .../schema/JsonSchemaVerificationTest.java | 23 ++++++----- .../schema/XmlSchemaVerificationTest.java | 41 +++++++++++++------ 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/tools/pom.xml b/tools/pom.xml index 519b8a9f..5eb0313f 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -55,7 +55,7 @@ 3.6 1.12.0 1.4.9 - 8.0.3 + 9.0.2 diff --git a/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java index faae872b..a67565ed 100644 --- a/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java @@ -29,6 +29,7 @@ List getAllResources() throws Exception { files.addAll(getResources("1.3/")); files.addAll(getResources("1.4/")); files.addAll(getResources("1.5/")); + files.addAll(getResources("1.6/")); return files; } diff --git a/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java index 3862bb14..1598c0ae 100644 --- a/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java @@ -13,15 +13,16 @@ */ package org.cyclonedx.schema; -import org.cyclonedx.CycloneDxSchema; -import org.cyclonedx.parsers.JsonParser; -import org.junit.jupiter.api.DynamicTest; -import org.junit.jupiter.api.TestFactory; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.cyclonedx.parsers.JsonParser; +import org.cyclonedx.Version; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; + import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -33,15 +34,17 @@ Collection dynamicTestsWithCollection() throws Exception { final List dynamicTests = new ArrayList<>(); for (final String file: files) { if (file.endsWith(".json")) { - final CycloneDxSchema.Version schemaVersion; + final Version schemaVersion; if (file.endsWith("-1.2.json")) { - schemaVersion = CycloneDxSchema.Version.VERSION_12; + schemaVersion = Version.VERSION_12; } else if (file.endsWith("-1.3.json")) { - schemaVersion = CycloneDxSchema.Version.VERSION_13; + schemaVersion = Version.VERSION_13; } else if (file.endsWith("-1.4.json")) { - schemaVersion = CycloneDxSchema.Version.VERSION_14; + schemaVersion = Version.VERSION_14; } else if (file.endsWith("-1.5.json")) { - schemaVersion = CycloneDxSchema.Version.VERSION_15; + schemaVersion = Version.VERSION_15; + } else if (file.endsWith("-1.6.json")) { + schemaVersion = Version.VERSION_16; } else { schemaVersion = null; } @@ -57,7 +60,7 @@ Collection dynamicTestsWithCollection() throws Exception { return dynamicTests; } - private boolean isValidJson(CycloneDxSchema.Version version, String resource) throws Exception { + private boolean isValidJson(Version version, String resource) throws Exception { final File file = new File(this.getClass().getResource(resource).getFile()); final JsonParser parser = new JsonParser(); return parser.isValid(file, version); diff --git a/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java index 01434b00..2d57dd8a 100644 --- a/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java @@ -13,39 +13,48 @@ */ package org.cyclonedx.schema; -import org.cyclonedx.CycloneDxSchema; -import org.cyclonedx.parsers.XmlParser; -import org.junit.jupiter.api.DynamicTest; -import org.junit.jupiter.api.TestFactory; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.cyclonedx.parsers.XmlParser; +import org.cyclonedx.Version; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; + import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; public class XmlSchemaVerificationTest extends BaseSchemaVerificationTest { @TestFactory + /** + * Generates a collection of dynamic tests based on the available XML files. + * + * @return Collection a collection of dynamic tests + * @throws Exception if an error occurs during the generation of the dynamic tests + */ Collection dynamicTestsWithCollection() throws Exception { final List files = getAllResources(); final List dynamicTests = new ArrayList<>(); for (final String file: files) { if (file.endsWith(".xml")) { - final CycloneDxSchema.Version schemaVersion; + final Version schemaVersion; if (file.endsWith("-1.0.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_10; + schemaVersion = Version.VERSION_10; } else if (file.endsWith("-1.1.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_11; + schemaVersion = Version.VERSION_11; } else if (file.endsWith("-1.2.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_12; + schemaVersion = Version.VERSION_12; } else if (file.endsWith("-1.3.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_13; + schemaVersion = Version.VERSION_13; } else if (file.endsWith("-1.4.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_14; + schemaVersion = Version.VERSION_14; } else if (file.endsWith("-1.5.xml")) { - schemaVersion = CycloneDxSchema.Version.VERSION_15; + schemaVersion = Version.VERSION_15; + } else if (file.endsWith("-1.6.xml")) { + schemaVersion = Version.VERSION_16; } else { schemaVersion = null; } @@ -61,7 +70,15 @@ Collection dynamicTestsWithCollection() throws Exception { return dynamicTests; } - private boolean isValid(CycloneDxSchema.Version version, String resource) throws Exception { + /** + * Validates the given XML file against the specified CycloneDX schema version. + * + * @param version the CycloneDX schema version to validate against + * @param resource the path to the XML file to be validated + * @return boolean true if the XML file is valid according to the specified schema version, false otherwise + * @throws Exception if an error occurs during the validation process + */ + private boolean isValid(Version version, String resource) throws Exception { final File file = new File(this.getClass().getResource(resource).getFile()); final XmlParser parser = new XmlParser(); return parser.isValid(file, version); From 3400f757c02753686863695d06282173951da8e6 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:57:32 +0200 Subject: [PATCH 04/10] Improve comments in the XML catalog Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- schema/xmlcatalog.xml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/schema/xmlcatalog.xml b/schema/xmlcatalog.xml index 60665b45..36289403 100644 --- a/schema/xmlcatalog.xml +++ b/schema/xmlcatalog.xml @@ -1,6 +1,16 @@ + + + + + @@ -8,4 +18,8 @@ - \ No newline at end of file + + + + + From 02a1a06c77706eb7c209cd3f270bd9e2e1621652 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:42:31 +0200 Subject: [PATCH 05/10] TODO: fix the XML catalog test. The issue lies in the location of the schema files Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- .../schema/XmlCatalogVerificationTest.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java diff --git a/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java new file mode 100644 index 00000000..258f50a2 --- /dev/null +++ b/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java @@ -0,0 +1,98 @@ +package org.cyclonedx.schema; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import java.io.InputStream; +import java.io.IOException; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.DynamicTest.dynamicTest; + +public class XmlCatalogVerificationTest { + + /** + * Tests the XML catalog by parsing the xmlcatalog.xml file and checking if the namespaces + * in the XSD schema files match the namespaces defined in the xmlcatalog.xml file. + * + * @return a list of dynamic tests for each URI in the xmlcatalog.xml file + * @throws IOException if an I/O error occurs while reading the XML catalog file + * @throws ParserConfigurationException if a parser configuration error occurs + * @throws SAXException if a SAX error occurs while parsing the XML catalog file + */ + @TestFactory + public List testXmlCatalog() throws IOException, ParserConfigurationException, SAXException { + // Define the path to the XML catalog file + String xmlCatalogFilename = "xmlcatalog.xml"; + + // Load the XML catalog file from the classpath + ClassLoader classLoader = getClass().getClassLoader(); + InputStream xmlCatalogStream = classLoader.getResourceAsStream(xmlCatalogFilename); + + Assertions.assertNotNull(xmlCatalogStream, "XML catalog file not found"); + + // Parse the xmlcatalog.xml file + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document xmlCatalogDocument = builder.parse(new InputSource(xmlCatalogStream)); + + // Get the XML catalog elements + NodeList xmlCatalogElements = xmlCatalogDocument.getDocumentElement().getChildNodes(); + + // List to hold dynamic tests + List dynamicTests = new ArrayList<>(); + + // Iterate through the XML catalog elements + for (int i = 0; i < xmlCatalogElements.getLength(); i++) { + Node xmlCatalogElement = xmlCatalogElements.item(i); + if (xmlCatalogElement.getNodeName().equals("uri")) { + String uriName = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent(); + String xsdLocalFilename = xmlCatalogElement.getAttributes().getNamedItem("uri").getTextContent(); + + // Create a dynamic test for each URI + dynamicTests.add(dynamicTest("Testing URI: " + uriName, () -> { + // Load the XSD schema file from the classpath + InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename); + Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename); + + // Read the XSD local file content + String xsdContent = new String(xsdSchemaFileStream.readAllBytes(), StandardCharsets.UTF_8); + + // Parse the XSD file content to a Document object + Document xsdDocument = builder.parse(new InputSource(new StringReader(xsdContent))); + + // Check if the XSD document contains the expected namespace + NodeList schemaNodes = xsdDocument.getElementsByTagNameNS("*", "schema"); + boolean namespaceFound = false; + for (int j = 0; j < schemaNodes.getLength(); j++) { + Node schemaNode = schemaNodes.item(j); + String targetNamespace = schemaNode.getAttributes().getNamedItem("targetNamespace").getTextContent(); + System.out.println("uriName.equals(targetNamespace)" + uriName.equals(targetNamespace)); + if (uriName.equals(targetNamespace)) { + namespaceFound = true; + break; + } + } + assertTrue(namespaceFound, "The namespace " + uriName + " is not present in file " + xsdLocalFilename); + })); + } + } + + return dynamicTests; + } +} From ba9a99482b643b8c7b28cd86b126b7e4a5a4a55b Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Tue, 11 Jun 2024 18:57:07 +0200 Subject: [PATCH 06/10] The XML catalog unit test that verifies if the local XSD files matches the content of the XML catalog is ready. Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- tools/pom.xml | 5 + .../schema/XmlCatalogVerificationTest.java | 95 ++++++++++++------- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/tools/pom.xml b/tools/pom.xml index 5eb0313f..e79cb71c 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -145,5 +145,10 @@ 3.2.5 + + + ${basedir}/../schema + + diff --git a/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java index 258f50a2..aa8ec29f 100644 --- a/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java @@ -10,44 +10,50 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import javax.xml.namespace.NamespaceContext; 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.XPathExpressionException; +import javax.xml.xpath.XPathFactory; -import java.io.InputStream; import java.io.IOException; -import java.io.StringReader; -import java.nio.charset.StandardCharsets; +import java.io.InputStream; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.DynamicTest.dynamicTest; public class XmlCatalogVerificationTest { /** - * Tests the XML catalog by parsing the xmlcatalog.xml file and checking if the namespaces - * in the XSD schema files match the namespaces defined in the xmlcatalog.xml file. + * Tests the XML catalog by parsing the xmlcatalog.xml file and verifying if the XML namespaces + * in the referenced XSD schema files match the XML namespaces defined in the xmlcatalog.xml file. * * @return a list of dynamic tests for each URI in the xmlcatalog.xml file * @throws IOException if an I/O error occurs while reading the XML catalog file * @throws ParserConfigurationException if a parser configuration error occurs * @throws SAXException if a SAX error occurs while parsing the XML catalog file + * @throws XPathExpressionException if an XPath expression error occurs */ @TestFactory - public List testXmlCatalog() throws IOException, ParserConfigurationException, SAXException { - // Define the path to the XML catalog file + public List testXmlCatalog() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException { + // Define the path to the XML catalog file. This is relative to "${basedir}/../schema" in the pom.xml. String xmlCatalogFilename = "xmlcatalog.xml"; // Load the XML catalog file from the classpath ClassLoader classLoader = getClass().getClassLoader(); InputStream xmlCatalogStream = classLoader.getResourceAsStream(xmlCatalogFilename); + // Ensure the XML catalog file is found Assertions.assertNotNull(xmlCatalogStream, "XML catalog file not found"); // Parse the xmlcatalog.xml file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); // Make the factory namespace-aware DocumentBuilder builder = factory.newDocumentBuilder(); Document xmlCatalogDocument = builder.parse(new InputSource(xmlCatalogStream)); @@ -60,39 +66,64 @@ public List testXmlCatalog() throws IOException, ParserConfiguratio // Iterate through the XML catalog elements for (int i = 0; i < xmlCatalogElements.getLength(); i++) { Node xmlCatalogElement = xmlCatalogElements.item(i); + + // Check if the element is a element if (xmlCatalogElement.getNodeName().equals("uri")) { - String uriName = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent(); + // Get the URI name and the local filename of the XSD schema + String uriNameXmlCtlg = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent(); String xsdLocalFilename = xmlCatalogElement.getAttributes().getNamedItem("uri").getTextContent(); - // Create a dynamic test for each URI - dynamicTests.add(dynamicTest("Testing URI: " + uriName, () -> { - // Load the XSD schema file from the classpath - InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename); - Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename); - - // Read the XSD local file content - String xsdContent = new String(xsdSchemaFileStream.readAllBytes(), StandardCharsets.UTF_8); - - // Parse the XSD file content to a Document object - Document xsdDocument = builder.parse(new InputSource(new StringReader(xsdContent))); - - // Check if the XSD document contains the expected namespace - NodeList schemaNodes = xsdDocument.getElementsByTagNameNS("*", "schema"); - boolean namespaceFound = false; - for (int j = 0; j < schemaNodes.getLength(); j++) { - Node schemaNode = schemaNodes.item(j); - String targetNamespace = schemaNode.getAttributes().getNamedItem("targetNamespace").getTextContent(); - System.out.println("uriName.equals(targetNamespace)" + uriName.equals(targetNamespace)); - if (uriName.equals(targetNamespace)) { - namespaceFound = true; - break; + // Load the XSD schema local file from the classpath + InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename); + Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename); + + // Parse the XSD schema local file + DocumentBuilderFactory factoryXsd = DocumentBuilderFactory.newInstance(); + factoryXsd.setNamespaceAware(true); // Make the factory namespace-aware + DocumentBuilder builderXsd = factoryXsd.newDocumentBuilder(); + Document xsdDocument = builderXsd.parse(new InputSource(xsdSchemaFileStream)); + + // Create an XPath instance to evaluate the targetNamespace field found in the XSD schema + XPath xPath = XPathFactory.newInstance().newXPath(); + xPath.setNamespaceContext(new NamespaceContext() { + @Override + public String getNamespaceURI(String prefix) { + // Define the namespace URI for the xs prefix + if ("xs".equals(prefix)) { + return "http://www.w3.org/2001/XMLSchema"; + } + return null; + } + + @Override + public String getPrefix(String namespaceURI) { + // Define the prefix for the namespace URI + if ("http://www.w3.org/2001/XMLSchema".equals(namespaceURI)) { + return "xs"; } + return null; } - assertTrue(namespaceFound, "The namespace " + uriName + " is not present in file " + xsdLocalFilename); + + @Override + public Iterator getPrefixes(String namespaceURI) { + return null; + } + }); + + // Evaluate the targetNamespace attribute from the XSD document + String targetNamespace = (String) xPath.evaluate("/xs:schema/@targetNamespace", xsdDocument, XPathConstants.STRING); + + // Assert if the targetNamespace from the XSD file matches the uriNameXmlCtlg from the XML catalog + Assertions.assertEquals(uriNameXmlCtlg, targetNamespace, "The namespace " + uriNameXmlCtlg + " does not match the targetNamespace in file " + xsdLocalFilename); + + // Create a dynamic test for each URI + dynamicTests.add(dynamicTest("Testing if URI namespace from the XML catalog: " + uriNameXmlCtlg + " matches the URI namespace from the local XSD file: " + targetNamespace, () -> { + // Dummy test, the namespace check is done before })); } } + // Return the list of dynamic tests return dynamicTests; } } From c1fe8a9781ee08d766292bdf269391e909712707 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:12:57 +0200 Subject: [PATCH 07/10] Explicitely add the default maven-surefire path to testResource because it was override by adding a line to the schema folder Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- tools/pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/pom.xml b/tools/pom.xml index e79cb71c..0e8c90c8 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -149,6 +149,9 @@ ${basedir}/../schema + + src/test/resources/ + From 64fd5e88819fe14e32d2c9eb8ce1212318c625d6 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:18:24 +0200 Subject: [PATCH 08/10] To better differentiate the 2 PRs https://github.com/CycloneDX/specification/pull/479 and https://github.com/CycloneDX/specification/pull/480, I remove here everything related to the XML catalog. Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- schema/xmlcatalog.xml | 41 ------ tools/pom.xml | 8 -- .../schema/XmlCatalogVerificationTest.java | 129 ------------------ 3 files changed, 178 deletions(-) delete mode 100644 schema/xmlcatalog.xml delete mode 100644 tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java diff --git a/schema/xmlcatalog.xml b/schema/xmlcatalog.xml deleted file mode 100644 index 7eae3fa9..00000000 --- a/schema/xmlcatalog.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/tools/pom.xml b/tools/pom.xml index 0e8c90c8..5eb0313f 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -145,13 +145,5 @@ 3.2.5 - - - ${basedir}/../schema - - - src/test/resources/ - - diff --git a/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java deleted file mode 100644 index aa8ec29f..00000000 --- a/tools/src/test/java/org/cyclonedx/schema/XmlCatalogVerificationTest.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.cyclonedx.schema; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DynamicTest; -import org.junit.jupiter.api.TestFactory; - -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.xml.namespace.NamespaceContext; -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.XPathExpressionException; -import javax.xml.xpath.XPathFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import static org.junit.jupiter.api.DynamicTest.dynamicTest; - -public class XmlCatalogVerificationTest { - - /** - * Tests the XML catalog by parsing the xmlcatalog.xml file and verifying if the XML namespaces - * in the referenced XSD schema files match the XML namespaces defined in the xmlcatalog.xml file. - * - * @return a list of dynamic tests for each URI in the xmlcatalog.xml file - * @throws IOException if an I/O error occurs while reading the XML catalog file - * @throws ParserConfigurationException if a parser configuration error occurs - * @throws SAXException if a SAX error occurs while parsing the XML catalog file - * @throws XPathExpressionException if an XPath expression error occurs - */ - @TestFactory - public List testXmlCatalog() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException { - // Define the path to the XML catalog file. This is relative to "${basedir}/../schema" in the pom.xml. - String xmlCatalogFilename = "xmlcatalog.xml"; - - // Load the XML catalog file from the classpath - ClassLoader classLoader = getClass().getClassLoader(); - InputStream xmlCatalogStream = classLoader.getResourceAsStream(xmlCatalogFilename); - - // Ensure the XML catalog file is found - Assertions.assertNotNull(xmlCatalogStream, "XML catalog file not found"); - - // Parse the xmlcatalog.xml file - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); // Make the factory namespace-aware - DocumentBuilder builder = factory.newDocumentBuilder(); - Document xmlCatalogDocument = builder.parse(new InputSource(xmlCatalogStream)); - - // Get the XML catalog elements - NodeList xmlCatalogElements = xmlCatalogDocument.getDocumentElement().getChildNodes(); - - // List to hold dynamic tests - List dynamicTests = new ArrayList<>(); - - // Iterate through the XML catalog elements - for (int i = 0; i < xmlCatalogElements.getLength(); i++) { - Node xmlCatalogElement = xmlCatalogElements.item(i); - - // Check if the element is a element - if (xmlCatalogElement.getNodeName().equals("uri")) { - // Get the URI name and the local filename of the XSD schema - String uriNameXmlCtlg = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent(); - String xsdLocalFilename = xmlCatalogElement.getAttributes().getNamedItem("uri").getTextContent(); - - // Load the XSD schema local file from the classpath - InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename); - Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename); - - // Parse the XSD schema local file - DocumentBuilderFactory factoryXsd = DocumentBuilderFactory.newInstance(); - factoryXsd.setNamespaceAware(true); // Make the factory namespace-aware - DocumentBuilder builderXsd = factoryXsd.newDocumentBuilder(); - Document xsdDocument = builderXsd.parse(new InputSource(xsdSchemaFileStream)); - - // Create an XPath instance to evaluate the targetNamespace field found in the XSD schema - XPath xPath = XPathFactory.newInstance().newXPath(); - xPath.setNamespaceContext(new NamespaceContext() { - @Override - public String getNamespaceURI(String prefix) { - // Define the namespace URI for the xs prefix - if ("xs".equals(prefix)) { - return "http://www.w3.org/2001/XMLSchema"; - } - return null; - } - - @Override - public String getPrefix(String namespaceURI) { - // Define the prefix for the namespace URI - if ("http://www.w3.org/2001/XMLSchema".equals(namespaceURI)) { - return "xs"; - } - return null; - } - - @Override - public Iterator getPrefixes(String namespaceURI) { - return null; - } - }); - - // Evaluate the targetNamespace attribute from the XSD document - String targetNamespace = (String) xPath.evaluate("/xs:schema/@targetNamespace", xsdDocument, XPathConstants.STRING); - - // Assert if the targetNamespace from the XSD file matches the uriNameXmlCtlg from the XML catalog - Assertions.assertEquals(uriNameXmlCtlg, targetNamespace, "The namespace " + uriNameXmlCtlg + " does not match the targetNamespace in file " + xsdLocalFilename); - - // Create a dynamic test for each URI - dynamicTests.add(dynamicTest("Testing if URI namespace from the XML catalog: " + uriNameXmlCtlg + " matches the URI namespace from the local XSD file: " + targetNamespace, () -> { - // Dummy test, the namespace check is done before - })); - } - } - - // Return the list of dynamic tests - return dynamicTests; - } -} From 7cc5b9682d40de2e6f304ba9c6e8af6d53b5e0a8 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:42:33 +0200 Subject: [PATCH 09/10] Remove test resources fo CDX 1.6 to create a separate PR. Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- .../java/org/cyclonedx/schema/BaseSchemaVerificationTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java index a67565ed..faae872b 100644 --- a/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java @@ -29,7 +29,6 @@ List getAllResources() throws Exception { files.addAll(getResources("1.3/")); files.addAll(getResources("1.4/")); files.addAll(getResources("1.5/")); - files.addAll(getResources("1.6/")); return files; } From fa502876568fd998d4637d2cb34157a9787f6727 Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:51:18 +0200 Subject: [PATCH 10/10] Removing 1.6 because we will do a new PR Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- .../java/org/cyclonedx/schema/JsonSchemaVerificationTest.java | 2 -- .../java/org/cyclonedx/schema/XmlSchemaVerificationTest.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java index 1598c0ae..c11b25ec 100644 --- a/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java @@ -43,8 +43,6 @@ Collection dynamicTestsWithCollection() throws Exception { schemaVersion = Version.VERSION_14; } else if (file.endsWith("-1.5.json")) { schemaVersion = Version.VERSION_15; - } else if (file.endsWith("-1.6.json")) { - schemaVersion = Version.VERSION_16; } else { schemaVersion = null; } diff --git a/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java b/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java index 2d57dd8a..a56e77e3 100644 --- a/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java +++ b/tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java @@ -53,8 +53,6 @@ Collection dynamicTestsWithCollection() throws Exception { schemaVersion = Version.VERSION_14; } else if (file.endsWith("-1.5.xml")) { schemaVersion = Version.VERSION_15; - } else if (file.endsWith("-1.6.xml")) { - schemaVersion = Version.VERSION_16; } else { schemaVersion = null; }