View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Output the contents of Cobertura's data file to the command line.
35   *
36   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
37   * @version $Id$
38   * @goal dump-datafile
39   */
40  public class CoberturaDatafileDumpMojo
41      extends AbstractCoberturaMojo
42  {
43      /**
44       * {@inheritDoc}
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       * print project info to the log.
79       *
80       * @param projectData
81       * @param percentage
82       * @param integer
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       * print package info to the log.
97       *
98       * @param percentage
99       * @param integer
100      * @param packageData
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      * Write the message to the logger
115      *
116      * @param msg
117      */
118     private void println( String msg )
119     {
120         getLog().info( msg );
121     }
122 }