1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.codehaus.mojo.cobertura;
21
22 import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
23 import net.sourceforge.cobertura.coveragedata.PackageData;
24 import net.sourceforge.cobertura.coveragedata.ProjectData;
25 import net.sourceforge.cobertura.util.Header;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28
29 import java.text.NumberFormat;
30 import java.util.Date;
31 import java.util.Iterator;
32
33
34
35
36
37
38
39
40 public class CoberturaDatafileDumpMojo
41 extends AbstractCoberturaMojo
42 {
43
44
45
46 public void execute()
47 throws MojoExecutionException, MojoFailureException
48 {
49 if ( skipMojo() )
50 {
51 return;
52 }
53
54 if ( !getDataFile().exists() )
55 {
56 throw new MojoExecutionException( "Unable to dump nonexistent dataFile [" + getDataFile() + "]" );
57 }
58
59 ProjectData projectData = CoverageDataFileHandler.loadCoverageData( getDataFile() );
60 NumberFormat percentage = NumberFormat.getPercentInstance();
61 NumberFormat integer = NumberFormat.getIntegerInstance();
62
63 println( "<?xml version=\"1.0\"?>" );
64
65 printProject( projectData, percentage, integer );
66
67 Iterator it = projectData.getPackages().iterator();
68 while ( it.hasNext() )
69 {
70 PackageData packageData = (PackageData) it.next();
71 printPackage( percentage, integer, packageData );
72 }
73
74 println( "</coverage>" );
75 }
76
77
78
79
80
81
82
83
84 private void printProject( ProjectData projectData, NumberFormat percentage, NumberFormat integer )
85 {
86 println( "<coverage line-rate=\"" + percentage.format( projectData.getLineCoverageRate() ) + "\" branch-rate=\""
87 + percentage.format( projectData.getBranchCoverageRate() ) + "\" lines-covered=\""
88 + integer.format( projectData.getNumberOfCoveredLines() ) + "\" lines-valid=\"" + integer.format(
89 projectData.getNumberOfValidLines() ) + "\" branches-covered=\"" + integer.format(
90 projectData.getNumberOfCoveredBranches() ) + "\" branches-valid=\"" + integer.format(
91 projectData.getNumberOfValidBranches() ) + "\" version=\"" + Header.version() + "\" timestamp=\""
92 + new Date().getTime() + "\">" );
93 }
94
95
96
97
98
99
100
101
102 private void printPackage( NumberFormat percentage, NumberFormat integer, PackageData packageData )
103 {
104 println( "<package name=\"" + packageData.getName() + "\" line-rate=\"" + percentage.format(
105 packageData.getLineCoverageRate() ) + "\" branch-rate=\"" + percentage.format(
106 packageData.getBranchCoverageRate() ) + "\" lines-covered=\"" + integer.format(
107 packageData.getNumberOfCoveredLines() ) + "\" lines-valid=\"" + integer.format(
108 packageData.getNumberOfValidLines() ) + "\" branches-covered=\"" + integer.format(
109 packageData.getNumberOfCoveredBranches() ) + "\" branches-valid=\"" + integer.format(
110 packageData.getNumberOfValidBranches() ) + "\" />" );
111 }
112
113
114
115
116
117
118 private void println( String msg )
119 {
120 getLog().info( msg );
121 }
122 }