|
| 1 | +/* |
| 2 | + * Copyright 2005-2025 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 | + * https://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 | + |
| 17 | +import groovy.xml.XmlSlurper |
| 18 | +import groovy.xml.slurpersupport.GPathResult |
| 19 | +import groovy.xml.slurpersupport.NodeChild |
| 20 | + |
| 21 | +import java.nio.file.Files |
| 22 | +import java.nio.file.Path |
| 23 | + |
| 24 | +Path spotbugsHtml = basedir.toPath().resolve('target/site/spotbugs.html') |
| 25 | +assert Files.exists(spotbugsHtml) |
| 26 | + |
| 27 | +Path spotbugsXdoc = basedir.toPath().resolve('target/spotbugs.xml') |
| 28 | +assert Files.exists(spotbugsXdoc) |
| 29 | + |
| 30 | +Path spotbugsXml = basedir.toPath().resolve('target/spotbugsXml.xml') |
| 31 | +assert Files.exists(spotbugsXml) |
| 32 | + |
| 33 | +println '******************' |
| 34 | +println 'Checking HTML file' |
| 35 | +println '******************' |
| 36 | + |
| 37 | +String effortLevel = 'default' |
| 38 | + |
| 39 | +assert spotbugsHtml.text.contains('<i>' + effortLevel + '</i>') |
| 40 | + |
| 41 | +XmlSlurper xmlSlurper = new XmlSlurper() |
| 42 | +xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', true) |
| 43 | +xmlSlurper.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false) |
| 44 | + |
| 45 | +// Temporarily allow DOCTYPE for HTML parsing |
| 46 | +xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false) |
| 47 | +GPathResult path = xmlSlurper.parse(spotbugsHtml) |
| 48 | +xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', true) |
| 49 | + |
| 50 | +int spotbugsErrors = path.body.'**'.find { NodeChild main -> main.@id == 'bodyColumn' }.section[1].table.tr[1].td[1].toInteger() |
| 51 | +println "Error Count is ${spotbugsErrors}" |
| 52 | + |
| 53 | +println '*********************************' |
| 54 | +println 'Checking Spotbugs Native XML file' |
| 55 | +println '*********************************' |
| 56 | + |
| 57 | +path = xmlSlurper.parse(spotbugsXml) |
| 58 | + |
| 59 | +List<NodeChild> allNodes = path.depthFirst().toList() |
| 60 | +int spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' } |
| 61 | +println "BugInstance size is ${spotbugsXmlErrors}" |
| 62 | + |
| 63 | +assert spotbugsXmlErrors == spotbugsErrors |
| 64 | + |
| 65 | +spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'URF_UNREAD_FIELD' } |
| 66 | +spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'UUF_UNUSED_FIELD'} |
| 67 | +spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'DLS_DEAD_LOCAL_STORE'} |
| 68 | +println "BugInstance size from detectors removed is ${spotbugsXmlErrors}" |
| 69 | + |
| 70 | +assert 0 == spotbugsXmlErrors |
| 71 | + |
| 72 | +spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_METHOD_NAMING_CONVENTION'} |
| 73 | +spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'ICAST_INT_CAST_TO_DOUBLE_PASSED_TO_CEIL'} |
| 74 | +spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_FIELD_NAMING_CONVENTION'} |
| 75 | +println "BugInstance size from detectors removed is ${spotbugsXmlErrors}" |
| 76 | + |
| 77 | +assert 0 != spotbugsXmlErrors |
| 78 | + |
| 79 | +println '******************' |
| 80 | +println 'Checking xDoc file' |
| 81 | +println '******************' |
| 82 | + |
| 83 | +path = xmlSlurper.parse(spotbugsXdoc) |
| 84 | + |
| 85 | +allNodes = path.depthFirst().toList() |
| 86 | +int xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' } |
| 87 | +println "BugInstance size is ${xdocErrors}" |
| 88 | + |
| 89 | +assert xdocErrors == spotbugsErrors |
| 90 | + |
| 91 | +xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'URF_UNREAD_FIELD' } |
| 92 | +xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'UUF_UNUSED_FIELD'} |
| 93 | +xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'DLS_DEAD_LOCAL_STORE'} |
| 94 | +println "BugInstance size from detectors removed is ${xdocErrors}" |
| 95 | + |
| 96 | +assert 0 == xdocErrors |
| 97 | + |
| 98 | +xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_METHOD_NAMING_CONVENTION'} |
| 99 | +xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'ICAST_INT_CAST_TO_DOUBLE_PASSED_TO_CEIL'} |
| 100 | +xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_FIELD_NAMING_CONVENTION'} |
| 101 | +println "BugInstance size from detectors removed is ${xdocErrors}" |
| 102 | + |
| 103 | +assert 0 != spotbugsXmlErrors |
0 commit comments