001: /*
002: * User: Michael Rettig
003: * Date: Sep 16, 2002
004: * Time: 9:11:03 PM
005: */
006: package net.sourceforge.jaxor.tests.clover;
007:
008: import net.sourceforge.jaxor.parser.XmlProxy;
009: import net.sourceforge.jaxor.util.SystemException;
010: import org.apache.tools.ant.BuildException;
011: import org.xml.sax.*;
012: import org.xml.sax.helpers.DefaultHandler;
013: import org.xml.sax.helpers.LocatorImpl;
014:
015: import javax.xml.parsers.SAXParser;
016: import javax.xml.parsers.SAXParserFactory;
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.io.FileNotFoundException;
020: import java.util.*;
021:
022: public class Task extends org.apache.tools.ant.Task {
023: private static SAXParserFactory _factory = SAXParserFactory
024: .newInstance();
025: private File _xmlFile;
026: private double _percentage;
027:
028: public void setPercentage(double percentage) {
029: _percentage = percentage;
030: }
031:
032: public void setFile(File xmlFile) {
033: _xmlFile = xmlFile;
034: }
035:
036: public void execute() throws BuildException {
037: try {
038: Handler handler = getCoverage(new InputSource(
039: new FileInputStream(_xmlFile)));
040: double newPercentage = handler.metrics
041: .getCoveragePercentage();
042:
043: if (newPercentage < _percentage) {
044: String worst = getWorst(handler.classMetricsList);
045: throw new BuildException("Coverage Failure!" + worst
046: + "\nCode Coverage: " + newPercentage + " < "
047: + _percentage);
048: } else
049: log("Coverage Percentage = " + newPercentage);
050: } catch (FileNotFoundException e) {
051: throw new SystemException(e);
052: }
053: }
054:
055: private String getWorst(List classMetricsList) {
056: Collections.sort(classMetricsList, new Comparator() {
057: public int compare(Object o1, Object o2) {
058: return ((ClassMetric) o1).metrics.uncoveredElements()
059: - ((ClassMetric) o2).metrics
060: .uncoveredElements();
061: }
062: });
063: StringBuffer buffer = new StringBuffer();
064: for (Iterator iterator = classMetricsList.iterator(); iterator
065: .hasNext();) {
066: ClassMetric classMetric = (ClassMetric) iterator.next();
067: buffer.append("\nUncovered Elements: "
068: + classMetric.metrics.uncoveredElements()
069: + " name: " + classMetric.name);
070: }
071: return buffer.toString();
072: }
073:
074: public Handler getCoverage(InputSource inputSource) {
075: try {
076: SAXParser saxParser = _factory.newSAXParser();
077: XMLReader _parser = saxParser.getXMLReader();
078: Handler handler = new Handler();
079: _parser.setContentHandler(handler);
080: _parser.parse(inputSource);
081: return handler;
082: } catch (SAXParseException exc) {
083: throw new SystemException(_xmlFile + ":"
084: + exc.getLineNumber() + ": " + exc.getMessage(),
085: exc);
086: } catch (Exception exc) {
087: throw new SystemException(_xmlFile.getPath(), exc);
088: }
089: }
090:
091: private static class ClassMetric {
092: public String name;
093: public Metrics metrics;
094: }
095:
096: public static class Handler extends DefaultHandler {
097: List classMetricsList = new ArrayList();
098: public Metrics metrics = null;
099: ClassMetric _current;
100: private boolean processingClass = false;
101: private Locator _locator = new LocatorImpl();
102:
103: public Handler() {
104: }
105:
106: public void setDocumentLocator(Locator locator) {
107: super .setDocumentLocator(locator);
108: _locator = locator;
109: }
110:
111: public void startElement(String namespace, String local,
112: String name, Attributes attributes) throws SAXException {
113: if (name.equalsIgnoreCase("metrics") && metrics == null) {
114: metrics = new Metrics();
115: XmlProxy proxy = new XmlProxy(metrics, _locator);
116: proxy.setAll(attributes);
117: }
118: if (name.equalsIgnoreCase("metrics") && processingClass) {
119: Metrics classMetrics = new Metrics();
120: XmlProxy proxy = new XmlProxy(classMetrics, _locator);
121: proxy.setAll(attributes);
122: _current.metrics = classMetrics;
123: if (classMetrics.uncoveredElements() > 0)
124: classMetricsList.add(_current);
125: processingClass = false;
126: }
127: if (name.equalsIgnoreCase("class")) {
128: _current = new ClassMetric();
129: _current.name = attributes.getValue("name");
130: processingClass = true;
131: }
132:
133: }
134: }
135: }
|