From 936b2d7bd1149851a20d62858f786708576a32a2 Mon Sep 17 00:00:00 2001 From: thibault_falque Date: Tue, 8 Aug 2017 17:44:07 +0200 Subject: [PATCH] add debug information for #14 --- pom.xml | 2 +- .../sonargo/coverage/CoverageParser.java | 176 +++++++++--------- 2 files changed, 89 insertions(+), 89 deletions(-) diff --git a/pom.xml b/pom.xml index dd3215e..2c8ee16 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ sonar-golang-plugin - 1.2.9-gamma + 1.2.10-beta Golang diff --git a/src/main/java/fr/univartois/sonargo/coverage/CoverageParser.java b/src/main/java/fr/univartois/sonargo/coverage/CoverageParser.java index c5981e1..767f98c 100644 --- a/src/main/java/fr/univartois/sonargo/coverage/CoverageParser.java +++ b/src/main/java/fr/univartois/sonargo/coverage/CoverageParser.java @@ -48,105 +48,105 @@ public class CoverageParser implements Parser { - private final Map> coverageByFile = new HashMap<>(); - private static final String FILE_NAME_ATTR = "filename"; - private static final String LINE_NUMBER_ATTR = "number"; - private static final String HITS_ATTR = "hits"; - private static final String METHOD_TAG = "method"; - private static final String CLASS_TAG = "class"; - private static final String LINE_TAG = "line"; - private static final Logger LOGGER = Loggers.get(CoverageParser.class); - - private final boolean checkDtd; - - public CoverageParser(SensorContext context) { - checkDtd = context.settings().getBoolean(GoProperties.DTD_VERIFICATION_KEY); - } - - private DocumentBuilder constructDocumentBuilder() throws ParserConfigurationException { - final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - - final DocumentBuilder db = dbf.newDocumentBuilder(); - - if (!checkDtd) { - db.setEntityResolver((publicId, systemId) -> { - if (systemId.contains("http://cobertura.sourceforge.net/xml/coverage-03.dtd")) { - return new InputSource(new StringReader("")); - } else { - return null; - } - }); + private final Map> coverageByFile = new HashMap<>(); + private static final String FILE_NAME_ATTR = "filename"; + private static final String LINE_NUMBER_ATTR = "number"; + private static final String HITS_ATTR = "hits"; + private static final String METHOD_TAG = "method"; + private static final String CLASS_TAG = "class"; + private static final String LINE_TAG = "line"; + private static final Logger LOGGER = Loggers.get(CoverageParser.class); + + private final boolean checkDtd; + + public CoverageParser(SensorContext context) { + checkDtd = context.settings().getBoolean(GoProperties.DTD_VERIFICATION_KEY); } - return db; - } - - /** - * {@link http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work} - * - * @param reportPath - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - */ - @Override - public void parse(String reportPath) throws ParserConfigurationException, SAXException, IOException { - final DocumentBuilder db = constructDocumentBuilder(); - final Document doc = db.parse(new File(reportPath)); + private DocumentBuilder constructDocumentBuilder() throws ParserConfigurationException { + final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - doc.getDocumentElement().normalize(); + final DocumentBuilder db = dbf.newDocumentBuilder(); - final NodeList classList = doc.getElementsByTagName(CLASS_TAG); + if (!checkDtd) { + db.setEntityResolver((publicId, systemId) -> { + if (systemId.contains("http://cobertura.sourceforge.net/xml/coverage-03.dtd")) { + return new InputSource(new StringReader("")); + } else { + return null; + } + }); + } - for (int i = 0; i < classList.getLength(); i++) { - final Node nNode = classList.item(i); - if (nNode.getNodeType() == Node.ELEMENT_NODE) { + return db; + } - final Element eElement = (Element) nNode; - final String filepath = eElement.getAttribute(FILE_NAME_ATTR); + /** + * {@link http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work} + * + * @param reportPath + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + */ + @Override + public void parse(String reportPath) throws ParserConfigurationException, SAXException, IOException { + final DocumentBuilder db = constructDocumentBuilder(); + final Document doc = db.parse(new File(reportPath)); + + doc.getDocumentElement().normalize(); + + final NodeList classList = doc.getElementsByTagName(CLASS_TAG); + + for (int i = 0; i < classList.getLength(); i++) { + final Node nNode = classList.item(i); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + + final Element eElement = (Element) nNode; + final String filepath = eElement.getAttribute(FILE_NAME_ATTR); + LOGGER.debug("filepath from coverage file " + filepath); + parseMethodTag(eElement.getElementsByTagName(METHOD_TAG), getListForFile(filepath)); + } + } - parseMethodTag(eElement.getElementsByTagName(METHOD_TAG), getListForFile(filepath)); - } } - } - - public Map> getCoveragePerFile() { - for (Map.Entry> entry : coverageByFile.entrySet()) { - String fileName = entry.getKey(); - List list = entry.getValue(); - LOGGER.debug(list.size() + "line coverage for file " + fileName); + public Map> getCoveragePerFile() { + for (Map.Entry> entry : coverageByFile.entrySet()) { + String fileName = entry.getKey(); + List list = entry.getValue(); + LOGGER.debug(list.size() + "line coverage for file " + fileName); + } + return coverageByFile; } - return coverageByFile; - } - - private List getListForFile(String filepath) { - List list = coverageByFile.get(filepath); - if (list == null) { - list = new ArrayList<>(); - coverageByFile.put(filepath, list); + + private List getListForFile(String filepath) { + List list = coverageByFile.get(filepath); + if (list == null) { + list = new ArrayList<>(); + coverageByFile.put(filepath, list); + } + return list; } - return list; - } - - private void parseMethodTag(NodeList methodsList, List listOfCoverage) { - for (int j = 0; j < methodsList.getLength(); j++) { - final Node nNode = methodsList.item(j); - if (nNode.getNodeType() == Node.ELEMENT_NODE) { - final Element eElement = (Element) nNode; - parseLineTag(eElement.getElementsByTagName(LINE_TAG), listOfCoverage); - } + + private void parseMethodTag(NodeList methodsList, List listOfCoverage) { + for (int j = 0; j < methodsList.getLength(); j++) { + final Node nNode = methodsList.item(j); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + final Element eElement = (Element) nNode; + parseLineTag(eElement.getElementsByTagName(LINE_TAG), listOfCoverage); + } + } } - } - - private void parseLineTag(NodeList lineList, List listOfCoverage) { - for (int j = 0; j < lineList.getLength(); j++) { - final Node nNode = lineList.item(j); - if (nNode.getNodeType() == Node.ELEMENT_NODE) { - final Element eElement = (Element) nNode; - listOfCoverage.add(new LineCoverage(Integer.parseInt(eElement.getAttribute(LINE_NUMBER_ATTR)), - Integer.parseInt(eElement.getAttribute(HITS_ATTR)))); - } + + private void parseLineTag(NodeList lineList, List listOfCoverage) { + for (int j = 0; j < lineList.getLength(); j++) { + final Node nNode = lineList.item(j); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + final Element eElement = (Element) nNode; + listOfCoverage.add(new LineCoverage(Integer.parseInt(eElement.getAttribute(LINE_NUMBER_ATTR)), + Integer.parseInt(eElement.getAttribute(HITS_ATTR)))); + } + } } - } }