|
| 1 | +package org.cyclonedx.maven; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import org.junit.runner.RunWith; |
| 6 | + |
| 7 | + |
| 8 | +import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; |
| 9 | +import io.takari.maven.testing.executor.MavenVersions; |
| 10 | +import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | +import org.w3c.dom.Document; |
| 14 | +import org.w3c.dom.Element; |
| 15 | +import org.w3c.dom.NodeList; |
| 16 | +import static org.cyclonedx.maven.TestUtils.readXML; |
| 17 | + |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +@RunWith(MavenJUnitTestRunner.class) |
| 27 | +@MavenVersions({"3.6.3"}) |
| 28 | + |
| 29 | +public class Issue521Test extends BaseMavenVerifier { |
| 30 | + |
| 31 | + public Issue521Test(MavenRuntimeBuilder runtimeBuilder) throws Exception { |
| 32 | + super(runtimeBuilder); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + @Test |
| 38 | +public void testFilePresence() throws Exception { |
| 39 | + File projDir = cleanAndBuild("issue-521", null); |
| 40 | + assertFileExists(projDir, "target/bom.json"); |
| 41 | + assertFileExists(projDir, "target/bom.xml"); |
| 42 | +} |
| 43 | + |
| 44 | +@Test |
| 45 | +public void testBomJsonContent() throws Exception { |
| 46 | + File projDir = cleanAndBuild("issue-521", null); |
| 47 | + verifyBomJsonContains(projDir, "target/bom.json", "issue-521-module1", "application"); |
| 48 | +} |
| 49 | + |
| 50 | +@Test |
| 51 | +public void testBomXmlContent() throws Exception { |
| 52 | + File projDir = cleanAndBuild("issue-521", null); |
| 53 | + checkBomXml(projDir); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + private static void assertFileExists(File basedir, String filePath) { |
| 60 | + File file = new File(basedir, filePath); |
| 61 | + assertTrue(file.exists(), String.format("File %s should exist", filePath)); |
| 62 | + } |
| 63 | + |
| 64 | + private void verifyBomJsonContains(File basedir, String filePath, String expectedName, String expectedType) throws IOException { |
| 65 | + File bomJsonFile = new File(basedir, filePath); |
| 66 | + assertTrue(bomJsonFile.exists(), String.format("File %s should exist", filePath)); |
| 67 | + |
| 68 | + // Leggi il contenuto del file bom.json e verifica la presenza dei campi desiderati |
| 69 | + String bomContents = fileRead(bomJsonFile, true); |
| 70 | + assertTrue(bomContents.contains("\"name\" : \"" + expectedName + "\""), |
| 71 | + String.format("bom.json should contain a module with name '%s'", expectedName)); |
| 72 | + assertTrue(bomContents.contains("\"type\" : \"" + expectedType + "\""), |
| 73 | + String.format("bom.json should contain a module with type '%s'", expectedType)); |
| 74 | + } |
| 75 | + |
| 76 | + private void checkBomXml(final File projDir) throws Exception { |
| 77 | + final Document bom = readXML(new File(projDir, "target/bom.xml")); |
| 78 | + |
| 79 | + final NodeList componentsList = bom.getElementsByTagName("component"); |
| 80 | + assertTrue(componentsList.getLength() > 0, "Expected at least one component element"); |
| 81 | + |
| 82 | + boolean found=false; |
| 83 | + for (int i = 0; i < componentsList.getLength(); i++) { |
| 84 | + Element component = (Element) componentsList.item(i); |
| 85 | + String name = component.getElementsByTagName("name").item(0).getTextContent(); |
| 86 | + String type = component.getAttribute("type"); |
| 87 | + |
| 88 | + if ("issue-521-module1".equals(name) && "application".equals(type)) { |
| 89 | + found = true; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + assertTrue(found, "Expected to find a component with name 'module1' and type 'application'"); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +} |
0 commit comments