@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
44import org.cyclonedx.gradle.utils.CycloneDxUtils
55import org.cyclonedx.model.Bom
66import org.cyclonedx.model.Component
7+ import org.cyclonedx.model.Tool
78import org.gradle.testkit.runner.GradleRunner
89import org.gradle.testkit.runner.TaskOutcome
910import spock.lang.Specification
@@ -619,4 +620,76 @@ class PluginConfigurationSpec extends Specification {
619620 File jsonBom = new File (testDir, " build/reports/bom.json" )
620621 assert ! jsonBom. text. contains(" \" id\" : \" Apache-2.0\" " )
621622 }
623+
624+ def " should not use depecrated tool section if schema is 1.5 or higher" () {
625+ given :
626+ File testDir = TestUtils . createFromString("""
627+ plugins {
628+ id 'org.cyclonedx.bom'
629+ id 'java'
630+ }
631+ repositories {
632+ mavenCentral()
633+ }
634+ group = 'com.example'
635+ version = '1.0.0'
636+ cyclonedxBom {
637+ schemaVersion = "1.6"
638+ }
639+ dependencies {
640+ implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0'
641+ }""" , " rootProject.name = 'hello-world'" )
642+
643+ when :
644+ def result = GradleRunner . create()
645+ .withProjectDir(testDir)
646+ .withArguments(" cyclonedxBom" , " --configuration-cache" )
647+ .withPluginClasspath()
648+ .build()
649+
650+ then :
651+ result. task(" :cyclonedxBom" ). outcome == TaskOutcome . SUCCESS
652+ File jsonBom = new File (testDir, " build/reports/bom.json" )
653+ Bom bom = new ObjectMapper (). readValue(jsonBom, Bom . class)
654+ assert bom. getMetadata(). getToolChoice(). getComponents(). size() == 1
655+ Component cycloneDxTool = bom. getMetadata(). getToolChoice(). getComponents(). get(0 )
656+ assert cycloneDxTool. getName() == " cyclonedx-gradle-plugin"
657+ assert cycloneDxTool. getAuthor() == " CycloneDX"
658+ }
659+
660+ def " should use legacy tools section if schema is below 1.5" () {
661+ given :
662+ File testDir = TestUtils . createFromString("""
663+ plugins {
664+ id 'org.cyclonedx.bom'
665+ id 'java'
666+ }
667+ repositories {
668+ mavenCentral()
669+ }
670+ group = 'com.example'
671+ version = '1.0.0'
672+ cyclonedxBom {
673+ schemaVersion = "1.4"
674+ }
675+ dependencies {
676+ implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0'
677+ }""" , " rootProject.name = 'hello-world'" )
678+
679+ when :
680+ def result = GradleRunner . create()
681+ .withProjectDir(testDir)
682+ .withArguments(" cyclonedxBom" , " --configuration-cache" )
683+ .withPluginClasspath()
684+ .build()
685+
686+ then :
687+ result. task(" :cyclonedxBom" ). outcome == TaskOutcome . SUCCESS
688+ File jsonBom = new File (testDir, " build/reports/bom.json" )
689+ Bom bom = new ObjectMapper (). readValue(jsonBom, Bom . class)
690+ assert bom. getMetadata(). getTools(). size() == 1
691+ Tool tool = bom. getMetadata(). getTools(). get(0 );
692+ assert tool. getName() == " cyclonedx-gradle-plugin"
693+ assert tool. getVendor() == " CycloneDX"
694+ }
622695}
0 commit comments