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.tasks;
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.codehaus.mojo.cobertura.configuration.ConfigCheck;
25  import org.codehaus.mojo.cobertura.configuration.Regex;
26  import org.codehaus.plexus.util.StringUtils;
27  
28  /**
29   * The Check Task.
30   *
31   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
32   */
33  public class CheckTask
34      extends AbstractTask
35  {
36      private ConfigCheck config;
37  
38      private String dataFile;
39  
40      /**
41       * Setup the check task.
42       */
43      public CheckTask()
44      {
45          super( net.sourceforge.cobertura.check.CheckCoverageMain.class.getName() );
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public void execute()
52          throws MojoExecutionException, MojoFailureException
53      {
54          if ( dataFile != null )
55          {
56              cmdLineArgs.addArg( "--datafile", dataFile );
57          }
58  
59          if ( StringUtils.isNotEmpty( config.getBranchRate() ) )
60          {
61              cmdLineArgs.addArg( "--branch", config.getBranchRate() );
62          }
63  
64          if ( StringUtils.isNotEmpty( config.getLineRate() ) )
65          {
66              cmdLineArgs.addArg( "--line", config.getLineRate() );
67          }
68  
69          if ( StringUtils.isNotEmpty( config.getTotalBranchRate() ) )
70          {
71              cmdLineArgs.addArg( "--totalbranch", config.getTotalBranchRate() );
72          }
73  
74          if ( StringUtils.isNotEmpty( config.getTotalLineRate() ) )
75          {
76              cmdLineArgs.addArg( "--totalline", config.getTotalLineRate() );
77          }
78  
79          if ( StringUtils.isNotEmpty( config.getPackageBranchRate() ) )
80          {
81              cmdLineArgs.addArg( "--packagebranch", config.getPackageBranchRate() );
82          }
83  
84          if ( StringUtils.isNotEmpty( config.getPackageLineRate() ) )
85          {
86              cmdLineArgs.addArg( "--packageline", config.getPackageLineRate() );
87          }
88  
89          if ( StringUtils.isNotEmpty( config.getMaxmem() ) )
90          {
91              this.setMaxmem( config.getMaxmem() );
92          }
93  
94          for ( Regex regex : config.getRegexes() )
95          {
96              cmdLineArgs.addArg( "--regex", regex.toString() );
97          }
98  
99          int returnCode = executeJava();
100 
101         // Check the return code and print a message
102         if ( returnCode == 0 )
103         {
104             getLog().info( "All checks passed." );
105         }
106         else
107         {
108             if ( config.isHaltOnFailure() )
109             {
110                 throw new MojoFailureException( "Coverage check failed. See messages above." );
111             }
112             else
113             {
114                 getLog().error( "Coverage check failed. See messages above." );
115             }
116         }
117     }
118 
119     /**
120      * @return the check configuration.
121      */
122     public ConfigCheck getConfig()
123     {
124         return config;
125     }
126 
127     /**
128      * @return Returns the dataFile.
129      */
130     public String getDataFile()
131     {
132         return dataFile;
133     }
134 
135     /**
136      * Set the check configuration.
137      *
138      * @param config the config.
139      */
140     public void setConfig( ConfigCheck config )
141     {
142         this.config = config;
143     }
144 
145     /**
146      * @param dataFile The dataFile to set.
147      */
148     public void setDataFile( String dataFile )
149     {
150         this.dataFile = dataFile;
151     }
152 
153 }