Skip to content

Commit 0deaeec

Browse files
authored
Merge pull request #360 from knrc/issue-289
Add a test to ensure we handle relocations, closes #289
2 parents 14533c9 + a4ef9d6 commit 0deaeec

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<groupId>com.example</groupId>
10+
<artifactId>issue289_parent</artifactId>
11+
<version>1.0.0</version>
12+
</parent>
13+
14+
<artifactId>issue289_dependency1</artifactId>
15+
16+
<name>Dependency 1</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>axis</groupId>
21+
<artifactId>axis-ant</artifactId>
22+
<version>1.4</version>
23+
<scope>compile</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>axis</groupId>
27+
<artifactId>axis</artifactId>
28+
<version>1.4</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<groupId>com.example</groupId>
10+
<artifactId>issue289_parent</artifactId>
11+
<version>1.0.0</version>
12+
</parent>
13+
14+
<artifactId>issue289_dependency2</artifactId>
15+
16+
<name>Dependency 2</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>axis</groupId>
21+
<artifactId>axis-ant</artifactId>
22+
<version>1.3</version>
23+
<scope>compile</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.example</groupId>
27+
<artifactId>issue289_dependency1</artifactId>
28+
<version>1.0.0</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
</dependencies>
32+
</project>

src/test/resources/issue-289/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>issue289_parent</artifactId>
10+
<packaging>pom</packaging>
11+
<version>1.0.0</version>
12+
13+
<name>Issue 289 Parent</name>
14+
15+
<modules>
16+
<module>dependency1</module>
17+
<module>dependency2</module>
18+
</modules>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.cyclonedx</groupId>
24+
<artifactId>cyclonedx-maven-plugin</artifactId>
25+
<version>${current.version}</version>
26+
<executions>
27+
<execution>
28+
<phase>package</phase>
29+
<goals>
30+
<goal>makeBom</goal>
31+
</goals>
32+
</execution>
33+
</executions>
34+
<configuration>
35+
<projectType>library</projectType>
36+
<schemaVersion>1.4</schemaVersion>
37+
<includeBomSerialNumber>true</includeBomSerialNumber>
38+
<includeCompileScope>true</includeCompileScope>
39+
<includeProvidedScope>false</includeProvidedScope>
40+
<includeRuntimeScope>false</includeRuntimeScope>
41+
<includeSystemScope>false</includeSystemScope>
42+
<includeTestScope>false</includeTestScope>
43+
<includeLicenseText>false</includeLicenseText>
44+
<outputFormat>xml</outputFormat>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
<properties>
51+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
52+
</properties>
53+
</project>

0 commit comments

Comments
 (0)