/subprojects/code-quality/src/test/groovy/org/gradle/api/plugins/quality/FindBugsPluginTest.groovy

http://github.com/gradle/gradle · Groovy · 284 lines · 235 code · 34 blank · 15 comment · 84 complexity · 4ecdbebe32a4980d2a1d3c940baed39a MD5 · raw file

  1. /*
  2. * Copyright 2011 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.gradle.api.plugins.quality
  17. import org.gradle.api.plugins.JavaBasePlugin
  18. import org.gradle.api.plugins.JavaPlugin
  19. import org.gradle.api.plugins.ReportingBasePlugin
  20. import org.gradle.api.tasks.SourceSet
  21. import org.gradle.test.fixtures.AbstractProjectBuilderSpec
  22. import static org.gradle.api.tasks.TaskDependencyMatchers.dependsOn
  23. import static org.hamcrest.Matchers.*
  24. import static spock.util.matcher.HamcrestSupport.that
  25. class FindBugsPluginTest extends AbstractProjectBuilderSpec {
  26. def setup() {
  27. project.pluginManager.apply(FindBugsPlugin)
  28. }
  29. def "applies reporting-base plugin"() {
  30. expect:
  31. project.plugins.hasPlugin(ReportingBasePlugin)
  32. }
  33. def "configures FindBugs configuration"() {
  34. def config = project.configurations.findByName("findbugs")
  35. expect:
  36. config != null
  37. !config.visible
  38. config.transitive
  39. config.description == 'The FindBugs libraries to be used for this project.'
  40. }
  41. def "configures FindBugs extension"() {
  42. expect:
  43. FindBugsExtension extension = project.extensions.findbugs
  44. extension.reportsDir == project.file("build/reports/findbugs")
  45. !extension.ignoreFailures
  46. extension.effort == null
  47. extension.reportLevel == null
  48. extension.visitors == null
  49. extension.omitVisitors == null
  50. extension.includeFilterConfig == null
  51. extension.excludeFilterConfig == null
  52. extension.excludeBugsFilterConfig == null
  53. extension.includeFilter == null
  54. extension.excludeFilter == null
  55. extension.excludeBugsFilter == null
  56. }
  57. def "configures FindBugs task for each source set"() {
  58. project.pluginManager.apply(JavaBasePlugin)
  59. project.sourceSets {
  60. main
  61. test
  62. other
  63. }
  64. expect:
  65. configuresFindBugsTask("findbugsMain", project.sourceSets.main)
  66. configuresFindBugsTask("findbugsTest", project.sourceSets.test)
  67. configuresFindBugsTask("findbugsOther", project.sourceSets.other)
  68. }
  69. private void configuresFindBugsTask(String taskName, SourceSet sourceSet) {
  70. def task = project.tasks.findByName(taskName)
  71. assert task instanceof FindBugs
  72. with(task) {
  73. description == "Run FindBugs analysis for ${sourceSet.name} classes"
  74. source as List == sourceSet.allJava as List
  75. findbugsClasspath == project.configurations.findbugs
  76. classes.empty // no classes to analyze
  77. reports.xml.destination == project.file("build/reports/findbugs/${sourceSet.name}.xml")
  78. !ignoreFailures
  79. effort == null
  80. reportLevel == null
  81. visitors == null
  82. omitVisitors == null
  83. excludeFilterConfig == null
  84. includeFilterConfig == null
  85. excludeBugsFilterConfig == null
  86. excludeFilter == null
  87. includeFilter == null
  88. excludeBugsFilter == null
  89. extraArgs == null
  90. }
  91. }
  92. def "configures any additional FindBugs tasks"() {
  93. def task = project.tasks.create("findbugsCustom", FindBugs)
  94. expect:
  95. with(task) {
  96. description == null
  97. source.empty
  98. classes == null
  99. classpath == null
  100. findbugsClasspath == project.configurations.findbugs
  101. pluginClasspath == project.configurations.findbugsPlugins
  102. reports.xml.destination == project.file("build/reports/findbugs/custom.xml")
  103. !ignoreFailures
  104. effort == null
  105. reportLevel == null
  106. visitors == null
  107. omitVisitors == null
  108. excludeFilterConfig == null
  109. includeFilterConfig == null
  110. excludeBugsFilterConfig == null
  111. excludeFilter == null
  112. includeFilter == null
  113. excludeBugsFilter == null
  114. extraArgs == null
  115. }
  116. }
  117. def "adds FindBugs tasks to check lifecycle task"() {
  118. project.pluginManager.apply(JavaBasePlugin)
  119. project.sourceSets {
  120. main
  121. test
  122. other
  123. }
  124. expect:
  125. that(project.check, dependsOn(hasItems("findbugsMain", "findbugsTest", "findbugsOther")))
  126. }
  127. def "can customize settings via extension"() {
  128. project.pluginManager.apply(JavaBasePlugin)
  129. project.sourceSets {
  130. main
  131. test
  132. other
  133. }
  134. project.findbugs {
  135. sourceSets = [project.sourceSets.main]
  136. reportsDir = project.file("findbugs-reports")
  137. ignoreFailures = true
  138. effort = 'min'
  139. reportLevel = 'high'
  140. visitors = ['org.gradle.Class']
  141. omitVisitors = ['org.gradle.Interface']
  142. includeFilter = new File("include.txt")
  143. excludeFilter = new File("exclude.txt")
  144. excludeBugsFilter = new File("baselineBugs.txt")
  145. extraArgs = [ '-adjustPriority', 'DM_CONVERT_CASE=raise,DM_CONVERT_CASE=raise']
  146. }
  147. expect:
  148. hasCustomizedSettings("findbugsMain", project.sourceSets.main)
  149. hasCustomizedSettings("findbugsTest", project.sourceSets.test)
  150. hasCustomizedSettings("findbugsOther", project.sourceSets.other)
  151. that(project.check, dependsOn(hasItem("findbugsMain")))
  152. that(project.check, dependsOn(not(hasItems("findbugsTest", "findbugsOther"))))
  153. }
  154. private void hasCustomizedSettings(String taskName, SourceSet sourceSet) {
  155. def task = project.tasks.findByName(taskName)
  156. assert task instanceof FindBugs
  157. with(task) {
  158. description == "Run FindBugs analysis for ${sourceSet.name} classes"
  159. source as List == sourceSet.allJava as List
  160. findbugsClasspath == project.configurations.findbugs
  161. reports.xml.destination == project.file("findbugs-reports/${sourceSet.name}.xml")
  162. ignoreFailures
  163. effort == 'min'
  164. reportLevel == 'high'
  165. visitors == ['org.gradle.Class']
  166. omitVisitors == ['org.gradle.Interface']
  167. includeFilterConfig.inputFiles.singleFile == project.file("include.txt")
  168. excludeFilterConfig.inputFiles.singleFile == project.file("exclude.txt")
  169. excludeBugsFilterConfig.inputFiles.singleFile == project.file("baselineBugs.txt")
  170. includeFilter == project.file("include.txt")
  171. excludeFilter == project.file("exclude.txt")
  172. excludeBugsFilter == project.file("baselineBugs.txt")
  173. extraArgs == [ '-adjustPriority', 'DM_CONVERT_CASE=raise,DM_CONVERT_CASE=raise' ]
  174. }
  175. }
  176. def "can customize any additional FindBugs tasks via extension"() {
  177. def task = project.tasks.create("findbugsCustom", FindBugs)
  178. project.findbugs {
  179. reportsDir = project.file("findbugs-reports")
  180. ignoreFailures = true
  181. effort = 'min'
  182. reportLevel = 'high'
  183. visitors = ['org.gradle.Class']
  184. omitVisitors = ['org.gradle.Interface']
  185. includeFilterConfig = project.resources.text.fromFile("include.txt")
  186. excludeFilterConfig = project.resources.text.fromFile("exclude.txt")
  187. excludeBugsFilterConfig = project.resources.text.fromFile("baselineBugs.txt")
  188. extraArgs = [ '-adjustPriority', 'DM_CONVERT_CASE=raise,DM_CONVERT_CASE=raise' ]
  189. }
  190. expect:
  191. with(task) {
  192. description == null
  193. source.empty
  194. classes == null
  195. classpath == null
  196. findbugsClasspath == project.configurations.findbugs
  197. pluginClasspath == project.configurations.findbugsPlugins
  198. reports.xml.destination == project.file("findbugs-reports/custom.xml")
  199. ignoreFailures
  200. effort == 'min'
  201. reportLevel == 'high'
  202. visitors == ['org.gradle.Class']
  203. omitVisitors == ['org.gradle.Interface']
  204. includeFilterConfig.inputFiles.singleFile == project.file("include.txt")
  205. excludeFilterConfig.inputFiles.singleFile == project.file("exclude.txt")
  206. excludeBugsFilterConfig.inputFiles.singleFile == project.file("baselineBugs.txt")
  207. includeFilter == project.file("include.txt")
  208. excludeFilter == project.file("exclude.txt")
  209. excludeBugsFilter == project.file("baselineBugs.txt")
  210. extraArgs == [ '-adjustPriority', 'DM_CONVERT_CASE=raise,DM_CONVERT_CASE=raise' ]
  211. }
  212. }
  213. def "can configure reporting"() {
  214. given:
  215. project.pluginManager.apply(JavaBasePlugin)
  216. project.sourceSets {
  217. main
  218. }
  219. when:
  220. project.findbugsMain.reports {
  221. html {
  222. enabled true
  223. }
  224. xml.destination "foo"
  225. }
  226. then:
  227. noExceptionThrown()
  228. }
  229. def "can use legacy includeFilter extension property"() {
  230. project.pluginManager.apply(JavaPlugin)
  231. project.findbugs.includeFilter = project.file("filter.txt")
  232. expect:
  233. project.findbugs.includeFilter == project.file("filter.txt")
  234. project.findbugs.includeFilterConfig.inputFiles.singleFile == project.file("filter.txt")
  235. }
  236. def "can use legacy excludeFilter extension property"() {
  237. project.pluginManager.apply(JavaPlugin)
  238. project.findbugs.excludeFilter = project.file("filter.txt")
  239. expect:
  240. project.findbugs.excludeFilter == project.file("filter.txt")
  241. project.findbugs.excludeFilterConfig.inputFiles.singleFile == project.file("filter.txt")
  242. }
  243. def "can use legacy excludeBugsFilter extension property"() {
  244. project.pluginManager.apply(JavaPlugin)
  245. project.findbugs.excludeBugsFilter = project.file("filter.txt")
  246. expect:
  247. project.findbugs.excludeBugsFilter == project.file("filter.txt")
  248. project.findbugs.excludeBugsFilterConfig.inputFiles.singleFile == project.file("filter.txt")
  249. }
  250. }