Skip to content

Commit

Permalink
CheckMojo: introduce failThreshold
Browse files Browse the repository at this point in the history
  • Loading branch information
famod committed May 25, 2020
1 parent ef22be2 commit 0cea25a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/it/check-fail/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2006-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[ERROR] High: Found reliance on default encoding in UserMistakes' )
4 changes: 4 additions & 0 deletions src/it/check-failThreshold/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invoker.goals = clean compile spotbugs:check

# The expected result of the build, possible values are "success" (default) and "failure"
invoker.buildResult = failure
44 changes: 44 additions & 0 deletions src/it/check-failThreshold/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2006-2020 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>spotbugs-maven-plugin.it</groupId>
<artifactId>common</artifactId>
<version>testing</version>
<relativePath>../common.xml</relativePath>
</parent>
<artifactId>check</artifactId>
<name>check</name>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<xmlOutput>true</xmlOutput>
<failOnError>true</failOnError>
<debug>@spotbugsTestDebug@</debug>
<failThreshold>High</failThreshold>
</configuration>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions src/it/check-failThreshold/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2006-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.text.contains( '[WARNING] Medium: Unused public or protected field:' )
assert buildLog.text.contains( '[ERROR] High: Found reliance on default encoding in UserMistakes' )
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ class SpotbugsViolationCheckMojo extends AbstractMojo {
@Parameter(property = "spotbugs.failOnError", defaultValue = "true")
boolean failOnError

/**
* Prioritiy threshold which bugs have to reach to cause a failure. Valid values are High, Medium or Low.
* Bugs below this threshold will just issue a warning log entry.
*
* @since 4.0.1
*/
@Parameter(property = "spotbugs.failThreshold")
String failThreshold

/**
* Fork a VM for Spotbugs analysis. This will allow you to set timeouts and heap size.
*
Expand Down Expand Up @@ -522,15 +531,31 @@ class SpotbugsViolationCheckMojo extends AbstractMojo {
}

log.info('Total bugs: ' + bugCount)
def bugCountAboveThreshold = 0
def priorityThresholdNum = failThreshold ? SpotBugsInfo.spotbugsPriority.indexOf(failThreshold) : Integer.MAX_VALUE
if (priorityThresholdNum == -1) {
throw new MojoExecutionException("Invalid value for failThreshold: ${failThreshold}")
}

for (i in 0..bugCount-1) {
def bug = bugs[i]
log.error( bug.LongMessage.text() + SpotBugsInfo.BLANK + bug.SourceLine.'@classname' + SpotBugsInfo.BLANK + bug.SourceLine.Message.text() + SpotBugsInfo.BLANK + bug.'@type')
def priorityNum = bug.'@priority' as int
def priorityName = SpotBugsInfo.spotbugsPriority[priorityNum]
def logMsg = priorityName + ': ' + bug.LongMessage.text() + SpotBugsInfo.BLANK + bug.SourceLine.'@classname' + SpotBugsInfo.BLANK +
bug.SourceLine.Message.text() + SpotBugsInfo.BLANK + bug.'@type'

if (priorityNum <= priorityThresholdNum) { // lower is more severe
bugCountAboveThreshold += 1
log.error(logMsg)
} else {
log.warn(logMsg)
}
}

log.info('\n\n\nTo see bug detail using the Spotbugs GUI, use the following command "mvn spotbugs:gui"\n\n\n')

if ( (bugCount || errorCount) && failOnError ) {
throw new MojoExecutionException("failed with ${bugCount} bugs and ${errorCount} errors ")
if ( (bugCountAboveThreshold || errorCount) && failOnError ) {
throw new MojoExecutionException("failed with ${bugCountAboveThreshold} bugs and ${errorCount} errors ")
}
}
}
Expand Down

0 comments on commit 0cea25a

Please sign in to comment.