forked from pfeuffer/GitCover
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to check coverage on new code with jacoco reports. Reso…
…lves issue pfeuffer#2
- Loading branch information
Showing
4 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package de.pfeufferweb.gitcover; | ||
|
||
/** | ||
* | ||
*/ | ||
public enum CoverageTool { | ||
COBERTURA, | ||
JACOCO | ||
} |
107 changes: 107 additions & 0 deletions
107
core/src/main/java/de/pfeufferweb/gitcover/JacocoCoverageBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package de.pfeufferweb.gitcover; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.filefilter.IOFileFilter; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.EntityResolver; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.xpath.XPath; | ||
import javax.xml.xpath.XPathConstants; | ||
import javax.xml.xpath.XPathExpression; | ||
import javax.xml.xpath.XPathFactory; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Collection; | ||
|
||
import static java.lang.Integer.parseInt; | ||
|
||
public class JacocoCoverageBuilder | ||
{ | ||
public Coverage computeAll(File directory) throws Exception | ||
{ | ||
Coverage overallCoverage = new Coverage(); | ||
Collection<File> files = FileUtils.listFiles(directory, new IOFileFilter() | ||
{ | ||
|
||
@Override | ||
public boolean accept(File dir, String name) | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean accept(File file) | ||
{ | ||
return file.getName().equals("jacoco.xml"); | ||
} | ||
}, new IOFileFilter() | ||
{ | ||
|
||
@Override | ||
public boolean accept(File dir, String name) | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean accept(File file) | ||
{ | ||
return true; | ||
} | ||
}); | ||
for (File file : files) | ||
{ | ||
overallCoverage.addAll(compute(file)); | ||
} | ||
return overallCoverage; | ||
} | ||
|
||
public Coverage compute(File file) throws Exception | ||
{ | ||
Coverage result = new Coverage(); | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
builder.setEntityResolver(new EntityResolver() | ||
{ | ||
@Override | ||
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException | ||
{ | ||
if (systemId.contains("JACOCO")) | ||
{ | ||
return new InputSource(new StringReader("")); | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
}); | ||
Document doc = builder.parse(file); | ||
XPathFactory xPathfactory = XPathFactory.newInstance(); | ||
XPath xpath = xPathfactory.newXPath(); | ||
XPathExpression classExpr = xpath.compile("//sourcefile/@name"); | ||
NodeList foundFileNodes = (NodeList) classExpr.evaluate(doc, XPathConstants.NODESET); | ||
for (int i = 0; i < foundFileNodes.getLength(); ++i) | ||
{ | ||
String fileName = foundFileNodes.item(i).getNodeValue(); | ||
result.addFile(fileName); | ||
XPathExpression linesExpr = xpath.compile("//sourcefile[@name='" + fileName + "']//line"); | ||
NodeList foundLineNodes = (NodeList) linesExpr.evaluate(doc, XPathConstants.NODESET); | ||
for (int j = 0; j < foundLineNodes.getLength(); ++j) | ||
{ | ||
NamedNodeMap attributes = foundLineNodes.item(j).getAttributes(); | ||
int line = parseInt(attributes.getNamedItem("nr").getNodeValue()); | ||
int hits = parseInt(attributes.getNamedItem("ci").getNodeValue()); | ||
result.addLine(fileName, line, hits); | ||
} | ||
} | ||
return result; | ||
} | ||
} |