Skip to content

Commit 02a1a06

Browse files
TODO: fix the XML catalog test. The issue lies in the location of the schema files
Signed-off-by: Nicolas-Peiffer <[email protected]>
1 parent 3400f75 commit 02a1a06

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.cyclonedx.schema;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.DynamicTest;
5+
import org.junit.jupiter.api.TestFactory;
6+
7+
import org.w3c.dom.Document;
8+
import org.w3c.dom.Node;
9+
import org.w3c.dom.NodeList;
10+
import org.xml.sax.InputSource;
11+
import org.xml.sax.SAXException;
12+
13+
import javax.xml.parsers.DocumentBuilder;
14+
import javax.xml.parsers.DocumentBuilderFactory;
15+
import javax.xml.parsers.ParserConfigurationException;
16+
17+
import java.io.InputStream;
18+
import java.io.IOException;
19+
import java.io.StringReader;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
26+
27+
public class XmlCatalogVerificationTest {
28+
29+
/**
30+
* Tests the XML catalog by parsing the xmlcatalog.xml file and checking if the namespaces
31+
* in the XSD schema files match the namespaces defined in the xmlcatalog.xml file.
32+
*
33+
* @return a list of dynamic tests for each URI in the xmlcatalog.xml file
34+
* @throws IOException if an I/O error occurs while reading the XML catalog file
35+
* @throws ParserConfigurationException if a parser configuration error occurs
36+
* @throws SAXException if a SAX error occurs while parsing the XML catalog file
37+
*/
38+
@TestFactory
39+
public List<DynamicTest> testXmlCatalog() throws IOException, ParserConfigurationException, SAXException {
40+
// Define the path to the XML catalog file
41+
String xmlCatalogFilename = "xmlcatalog.xml";
42+
43+
// Load the XML catalog file from the classpath
44+
ClassLoader classLoader = getClass().getClassLoader();
45+
InputStream xmlCatalogStream = classLoader.getResourceAsStream(xmlCatalogFilename);
46+
47+
Assertions.assertNotNull(xmlCatalogStream, "XML catalog file not found");
48+
49+
// Parse the xmlcatalog.xml file
50+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51+
DocumentBuilder builder = factory.newDocumentBuilder();
52+
Document xmlCatalogDocument = builder.parse(new InputSource(xmlCatalogStream));
53+
54+
// Get the XML catalog elements
55+
NodeList xmlCatalogElements = xmlCatalogDocument.getDocumentElement().getChildNodes();
56+
57+
// List to hold dynamic tests
58+
List<DynamicTest> dynamicTests = new ArrayList<>();
59+
60+
// Iterate through the XML catalog elements
61+
for (int i = 0; i < xmlCatalogElements.getLength(); i++) {
62+
Node xmlCatalogElement = xmlCatalogElements.item(i);
63+
if (xmlCatalogElement.getNodeName().equals("uri")) {
64+
String uriName = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent();
65+
String xsdLocalFilename = xmlCatalogElement.getAttributes().getNamedItem("uri").getTextContent();
66+
67+
// Create a dynamic test for each URI
68+
dynamicTests.add(dynamicTest("Testing URI: " + uriName, () -> {
69+
// Load the XSD schema file from the classpath
70+
InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename);
71+
Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename);
72+
73+
// Read the XSD local file content
74+
String xsdContent = new String(xsdSchemaFileStream.readAllBytes(), StandardCharsets.UTF_8);
75+
76+
// Parse the XSD file content to a Document object
77+
Document xsdDocument = builder.parse(new InputSource(new StringReader(xsdContent)));
78+
79+
// Check if the XSD document contains the expected namespace
80+
NodeList schemaNodes = xsdDocument.getElementsByTagNameNS("*", "schema");
81+
boolean namespaceFound = false;
82+
for (int j = 0; j < schemaNodes.getLength(); j++) {
83+
Node schemaNode = schemaNodes.item(j);
84+
String targetNamespace = schemaNode.getAttributes().getNamedItem("targetNamespace").getTextContent();
85+
System.out.println("uriName.equals(targetNamespace)" + uriName.equals(targetNamespace));
86+
if (uriName.equals(targetNamespace)) {
87+
namespaceFound = true;
88+
break;
89+
}
90+
}
91+
assertTrue(namespaceFound, "The namespace " + uriName + " is not present in file " + xsdLocalFilename);
92+
}));
93+
}
94+
}
95+
96+
return dynamicTests;
97+
}
98+
}

0 commit comments

Comments
 (0)