|
| 1 | +package org.cyclonedx.maven; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import static org.cyclonedx.maven.TestUtils.getComponentNode; |
| 6 | +import static org.cyclonedx.maven.TestUtils.getDependencyNode; |
| 7 | +import static org.cyclonedx.maven.TestUtils.readXML; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.junit.Assert.assertNotNull; |
| 11 | +import static org.junit.Assert.assertNull; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.w3c.dom.Document; |
| 16 | +import org.w3c.dom.Node; |
| 17 | +import org.w3c.dom.NodeList; |
| 18 | + |
| 19 | +import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; |
| 20 | +import io.takari.maven.testing.executor.MavenVersions; |
| 21 | +import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; |
| 22 | + |
| 23 | +/** |
| 24 | + * test for https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/289 |
| 25 | + * NPE when handling relocations |
| 26 | + */ |
| 27 | +@RunWith(MavenJUnitTestRunner.class) |
| 28 | +@MavenVersions({"3.6.3"}) |
| 29 | +public class Issue289Test extends BaseMavenVerifier { |
| 30 | + |
| 31 | + private static final String AXIS_AXIS_ANT_1_3 = "pkg:maven/axis/[email protected]?type=jar"; |
| 32 | + private static final String AXIS_AXIS_ANT_1_4 = "pkg:maven/axis/[email protected]?type=jar"; |
| 33 | + private static final String ORG_APACHE_AXIS_ANT_1_4 = "pkg:maven/org.apache.axis/[email protected]?type=jar"; |
| 34 | + |
| 35 | + public Issue289Test(MavenRuntimeBuilder runtimeBuilder) throws Exception { |
| 36 | + super(runtimeBuilder); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testForRelocations() throws Exception { |
| 41 | + final File projDir = cleanAndBuild("issue-289", null); |
| 42 | + |
| 43 | + checkRelocatedArtifact(projDir); |
| 44 | + checkConflictWithNonRelocatedArtifact(projDir); |
| 45 | + } |
| 46 | + |
| 47 | + private void checkRelocatedArtifact(final File projDir) throws Exception { |
| 48 | + final Document bom = readXML(new File(projDir, "dependency1/target/bom.xml")); |
| 49 | + |
| 50 | + final NodeList componentsList = bom.getElementsByTagName("components"); |
| 51 | + assertEquals("Expected a single components element", 1, componentsList.getLength()); |
| 52 | + final Node components = componentsList.item(0); |
| 53 | + |
| 54 | + final NodeList dependenciesList = bom.getElementsByTagName("dependencies"); |
| 55 | + assertEquals("Expected a single dependencies element", 1, dependenciesList.getLength()); |
| 56 | + final Node dependencies = dependenciesList.item(0); |
| 57 | + |
| 58 | + /* |
| 59 | + * BOM should contain a component and a dependency for pkg:maven/org.apache.axis/[email protected]?type=jar |
| 60 | + */ |
| 61 | + final Node orgApacheAxisAntNode = getComponentNode(components, ORG_APACHE_AXIS_ANT_1_4); |
| 62 | + assertNotNull( "Missing pkg:maven/org.apache.axis/[email protected] component", orgApacheAxisAntNode); |
| 63 | + |
| 64 | + final Node orgApacheAxisAntDependencyNode = getDependencyNode(dependencies, ORG_APACHE_AXIS_ANT_1_4); |
| 65 | + assertNotNull( "Missing pkg:maven/org.apache.axis/[email protected] dependency", orgApacheAxisAntDependencyNode); |
| 66 | + |
| 67 | + /* |
| 68 | + * BOM should not contain a component nor a dependency for pkg:maven/axis/[email protected]?type=jar |
| 69 | + */ |
| 70 | + final Node axisAxisAntNode = getComponentNode(components, AXIS_AXIS_ANT_1_4); |
| 71 | + assertNull( "Unexpected pkg:maven/axis/[email protected] component discovered in BOM", axisAxisAntNode); |
| 72 | + |
| 73 | + final Node axisAxisAntDependencyNode = getDependencyNode(dependencies, AXIS_AXIS_ANT_1_4); |
| 74 | + assertNull( "Unexpected pkg:maven/axis/[email protected] dependency discovered in BOM", axisAxisAntDependencyNode); |
| 75 | + } |
| 76 | + |
| 77 | + private void checkConflictWithNonRelocatedArtifact(final File projDir) throws Exception { |
| 78 | + final Document bom = readXML(new File(projDir, "dependency2/target/bom.xml")); |
| 79 | + |
| 80 | + final NodeList componentsList = bom.getElementsByTagName("components"); |
| 81 | + assertEquals("Expected a single components element", 1, componentsList.getLength()); |
| 82 | + final Node components = componentsList.item(0); |
| 83 | + |
| 84 | + final NodeList dependenciesList = bom.getElementsByTagName("dependencies"); |
| 85 | + assertEquals("Expected a single dependencies element", 1, dependenciesList.getLength()); |
| 86 | + final Node dependencies = dependenciesList.item(0); |
| 87 | + |
| 88 | + /* |
| 89 | + * BOM should contain a component and a dependency for pkg:maven/axis/[email protected]?type=jar |
| 90 | + */ |
| 91 | + final Node axisAxisAntNode = getComponentNode(components, AXIS_AXIS_ANT_1_3); |
| 92 | + assertNotNull( "Missing pkg:maven/axis/[email protected] component", axisAxisAntNode); |
| 93 | + |
| 94 | + final Node axisAxisAntDependencyNode = getDependencyNode(dependencies, AXIS_AXIS_ANT_1_3); |
| 95 | + assertNotNull( "Missing pkg:maven/axis/[email protected] dependency", axisAxisAntDependencyNode); |
| 96 | + |
| 97 | + /* |
| 98 | + * BOM should not contain a component nor a dependency for pkg:maven/org.apache.axis/[email protected]?type=jar |
| 99 | + */ |
| 100 | + final Node orgApacheAxisAntNode = getComponentNode(components, ORG_APACHE_AXIS_ANT_1_4); |
| 101 | + assertNull( "Unexpected pkg:maven/org.apache.axis/[email protected] component discovered in BOM", orgApacheAxisAntNode); |
| 102 | + |
| 103 | + final Node orgApacheAxisAntDependencyNode = getDependencyNode(dependencies, ORG_APACHE_AXIS_ANT_1_4); |
| 104 | + assertNull( "Unexpected pkg:maven/org.apache.axis/[email protected] dependency discovered in BOM", orgApacheAxisAntDependencyNode); |
| 105 | + } |
| 106 | +} |
0 commit comments