-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'knrc-maven_optionality'
- Loading branch information
Showing
18 changed files
with
382 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/test/java/org/cyclonedx/maven/Issue314OptionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.cyclonedx.maven; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.cyclonedx.maven.TestUtils.getComponentNode; | ||
import static org.cyclonedx.maven.TestUtils.getElement; | ||
import static org.cyclonedx.maven.TestUtils.readXML; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.cyclonedx.model.Component; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NodeList; | ||
|
||
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; | ||
import io.takari.maven.testing.executor.MavenVersions; | ||
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; | ||
|
||
/** | ||
* Test optional detection as Maven dependency optional vs bytecode analysis of unused. | ||
*/ | ||
@RunWith(MavenJUnitTestRunner.class) | ||
@MavenVersions({"3.6.3"}) | ||
public class Issue314OptionalTest extends BaseMavenVerifier { | ||
|
||
private static final String ISSUE_314_DEPENDENCY_B = "pkg:maven/com.example.issue_314/[email protected]?type=jar"; | ||
private static final String ISSUE_314_DEPENDENCY_C = "pkg:maven/com.example.issue_314/[email protected]?type=jar"; | ||
private static final String ISSUE_314_DEPENDENCY_D = "pkg:maven/com.example.issue_314/[email protected]?type=jar"; | ||
|
||
public Issue314OptionalTest(MavenRuntimeBuilder runtimeBuilder) throws Exception { | ||
super(runtimeBuilder); | ||
} | ||
|
||
/** | ||
* Validate the bytecode analysis components. | ||
* - No component should be marked as optional | ||
*/ | ||
@Test | ||
public void testBytecodeDependencyTree() throws Exception { | ||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put("detectUnusedForOptionalScope", "true"); | ||
final File projDir = mvnBuild("issue-314", null, null, null, properties); | ||
|
||
final String requiredName = Component.Scope.REQUIRED.getScopeName(); | ||
|
||
final Document bom = readXML(new File(projDir, "dependency_A/target/bom.xml")); | ||
|
||
final NodeList componentsList = bom.getElementsByTagName("components"); | ||
assertEquals("Expected a single components element", 1, componentsList.getLength()); | ||
final Element components = (Element)componentsList.item(0); | ||
|
||
final Element componentBNode = getComponentNode(components, ISSUE_314_DEPENDENCY_B); | ||
final Element componentBScope = getElement(componentBNode, "scope"); | ||
if (componentBScope != null) { | ||
assertEquals("dependency_B scope should be " + requiredName, requiredName, componentBScope.getTextContent()); | ||
} | ||
|
||
final Element componentCNode = getComponentNode(components, ISSUE_314_DEPENDENCY_C); | ||
final Element componentCScope = getElement(componentCNode, "scope"); | ||
if (componentCScope != null) { | ||
assertEquals("dependency_C scope should be " + requiredName, requiredName, componentCScope.getTextContent()); | ||
} | ||
|
||
final Element componentDNode = getComponentNode(components, ISSUE_314_DEPENDENCY_D); | ||
final Element componentDScope = getElement(componentDNode, "scope"); | ||
if (componentDScope != null) { | ||
assertEquals("dependency_D scope should be " + requiredName, requiredName, componentDScope.getTextContent()); | ||
} | ||
} | ||
|
||
/** | ||
* Validate the maven optional components. | ||
* - com.example.issue_314:dependency_C:1.0.0 and com.example.issue_314:dependency_D:1.0.0 *should* be marked as optional | ||
* because dependency_A declares dependency_C as optional, which depends on dependency_D | ||
*/ | ||
@Test | ||
public void testMavenOptionalDependencyTree() throws Exception { | ||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put("detectUnusedForOptionalScope", "false"); | ||
final File projDir = mvnBuild("issue-314", null, null, null, properties); | ||
|
||
final String requiredName = Component.Scope.REQUIRED.getScopeName(); | ||
final String optionalName = Component.Scope.OPTIONAL.getScopeName(); | ||
|
||
final Document bom = readXML(new File(projDir, "dependency_A/target/bom.xml")); | ||
|
||
final NodeList componentsList = bom.getElementsByTagName("components"); | ||
assertEquals("Expected a single components element", 1, componentsList.getLength()); | ||
final Element components = (Element)componentsList.item(0); | ||
|
||
final Element componentBNode = getComponentNode(components, ISSUE_314_DEPENDENCY_B); | ||
final Element componentBScope = getElement(componentBNode, "scope"); | ||
if (componentBScope != null) { | ||
assertEquals("dependency_B scope should be " + requiredName, requiredName, componentBScope.getTextContent()); | ||
} | ||
|
||
final Element componentCNode = getComponentNode(components, ISSUE_314_DEPENDENCY_C); | ||
final Element componentCScope = getElement(componentCNode, "scope"); | ||
assertNotNull("dependency_C is missing its scope", componentCScope); | ||
assertEquals("dependency_C scope should be " + optionalName, optionalName, componentCScope.getTextContent()); | ||
|
||
final Element componentDNode = getComponentNode(components, ISSUE_314_DEPENDENCY_D); | ||
final Element componentDScope = getElement(componentDNode, "scope"); | ||
assertNotNull("dependency_D is missing its scope", componentDScope); | ||
assertEquals("dependency_D scope should be " + optionalName, optionalName, componentDScope.getTextContent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>issue_314_parent</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>dependency_A</artifactId> | ||
|
||
<name>Dependency A</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>dependency_B</artifactId> | ||
<version>1.0.0</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>dependency_C</artifactId> | ||
<version>1.0.0</version> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.cyclonedx</groupId> | ||
<artifactId>cyclonedx-maven-plugin</artifactId> | ||
<version>${current.version}</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>makeBom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Oops, something went wrong.