This page documents Clover-for-Ant's implementation of JSON data and how to make use of it.

このページの内容

Why JSON?

The JSON format is supported as an output type in Clover specifically to create integration opportunities with other applications. The JSON data from Clover is easy to manipulate programmatically, allowing innovative developers to use it for displaying or processing their coverage data in novel ways.

Clover-Ant code for JSON output

<target name="report.json" depends="with.clover">
<clover-report>
   <current outfile="clover_json">
    <format type="json"/>
      <columns>
        <lineCount/>
        <ncLineCount/>
      </columns>
   </current>
</clover-report>
</target>

Examples of JSON report usage

JSON for a file page:

processClover (  {
  "lines": ["", "", "10", "10", "10", "", "", "16", "1", "1", "1", "", "", "", "", "22", "22", "5678", "5678", "1", "1"],
  "stats": {
    "Complexity": 22,
    "TotalPercentageCovered": 100,
    "CoveredElements": 63,
    "UncoveredElements": 0
  },
  "id": "org_apache_commons_codec_net_BCodec_java"
}
 ); 

The "lines" array contains the hit counts for each line in the BCodec.java file.

JSON in an HTML page:

<!--  Define the callback function before including the java.js for the file you wish to process. -->
    <script type="text/javascript">
        function processClover(obj) {
            alert(obj.id);
            alert(obj.stats.CoveredElements);
            alert(obj.stats.Complexity);
            alert(obj.lines);
        }
    </script>
<!-- Now, include as many java.js files as you wish. Each will call your "processClover" callback function above. -->
    <script type="text/javascript"
            src="http://downloads.atlassian.com/software/clover/samples/codec/org/apache/commons/codec/StringEncoderAbstractTest.java.js">
    </script>
    <script type="text/javascript"
            src="http://downloads.atlassian.com/software/clover/samples/codec/org/apache/commons/codec/net/BCode.java.js">
    </script>



JSON live demo
This example uses JavaScript alerts to display the JSON data. Click here to run the live demo.

Clover Historical Data in JSONP

You can also access Clover historical data using JSON or JSONP. If your coverage data is available online, you could view your coverage inside a Google Gadget.

Using this data, you can create your own visualisations in Javascript or HTML. The data from Clover is compatible with the Google Visualisation API.

Basic example:

table: {
           cols: [{id: 'timestamp', label: 'Date', type: 'date'}, ...],
           rows: [{c:[{v: new Date(0), f: '1 January 1970 00:00'}, ...]}]
       }

This is the basic JSON object produced by Clover. The above JSON object may be passed directly to a google.visualization.DataTable constructor like so:

new google.visualization.DataTable(json.table[0], 0.5);

Example for use with Javascript:

var data = new google.visualization.DataTable(json.table[0], 0.5);
         var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
         chart.draw(data, {width: 800, height: 400, legend: 'bottom', title: 'Clover Historical Chart'});

Example Clover Gadget:

The screenshot below shows Clover historical data in a Gadget:

The XML source for this gadget is as follows:

http://labs.atlassian.com/gadgets/npellow/src/clover-gadget.xml

Screenshot: Clover in a Google Gadget

  • ラベルなし