View Javadoc
1   package org.codehaus.mojo.clirr;
2   
3   /*
4    * Copyright 2006 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import net.sf.clirr.core.ApiDifference;
20  import net.sf.clirr.core.Severity;
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.codehaus.plexus.i18n.I18N;
24  
25  import java.util.Locale;
26  
27  /**
28   * Check for compatibility with previous version.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   */
32  public class AbstractClirrCheckMojo
33      extends AbstractClirrMojo
34  {
35      /**
36       * Whether to fail on errors.
37       *
38       * @parameter property="failOnError" default-value="true"
39       */
40      private boolean failOnError;
41  
42      /**
43       * Whether to fail on warnings.
44       *
45       * @parameter property="failOnWarning" default-value="false"
46       */
47      private boolean failOnWarning;
48  
49      /**
50       * Whether to fail on info.
51       *
52       * @parameter property="failOnInfo" default-value="false"
53       */
54      private boolean failOnInfo;
55  
56      /**
57       * @component
58       */
59      private I18N i18n;
60  
61      protected void doExecute()
62          throws MojoExecutionException, MojoFailureException
63      {
64          if ( !canGenerate() )
65          {
66              return;
67          }
68          Severity minSeverity = convertSeverity( this.minSeverity );
69          
70          
71          ClirrDiffListener listener;
72          try
73          {
74              listener = executeClirr( minSeverity );
75          }
76          catch ( MissingPreviousException e )
77          {
78              getLog().debug( e );
79              getLog().info( "No previous version was found. Use 'comparisonArtifacts'"
80                      + " for explicit configuration if you think this is wrong." );
81              return;
82          }
83  
84          Locale locale = Locale.getDefault();
85  
86          int errorCount = listener.getSeverityCount( Severity.ERROR );
87          if ( failOnError && errorCount > 0 )
88          {
89              log( listener, Severity.ERROR );
90              String message;
91              if ( errorCount > 1 )
92              {
93                  String[] args = new String[]{String.valueOf( errorCount )};
94                  message = i18n.format( "clirr-report", locale, "check.clirr.failure.errors", args );
95              }
96              else
97              {
98                  message = i18n.getString( "clirr-report", locale, "check.clirr.failure.error" );
99              }
100             throw new MojoFailureException( message );
101         }
102 
103         int warningCount = listener.getSeverityCount( Severity.WARNING );
104         if ( failOnWarning && warningCount > 0 )
105         {
106             log( listener, Severity.WARNING );
107             String message;
108             if ( warningCount > 1 )
109             {
110                 String[] args = new String[]{String.valueOf( warningCount )};
111                 message = i18n.format( "clirr-report", locale, "check.clirr.failure.warnings", args );
112             }
113             else
114             {
115                 message = i18n.getString( "clirr-report", locale, "check.clirr.failure.warning" );
116             }
117             throw new MojoFailureException( message );
118         }
119 
120         int infoCount = listener.getSeverityCount( Severity.INFO );
121         if ( failOnInfo && infoCount > 0 )
122         {
123             log( listener, Severity.INFO );
124             String message;
125             if ( infoCount > 1 )
126             {
127                 String[] args = new String[]{String.valueOf( infoCount )};
128                 message = i18n.format( "clirr-report", locale, "check.clirr.failure.infos", args );
129             }
130             else
131             {
132                 message = i18n.getString( "clirr-report", locale, "check.clirr.failure.info" );
133             }
134             throw new MojoFailureException( message );
135         }
136 
137         String[] args =
138             new String[]{String.valueOf( errorCount ), String.valueOf( warningCount ), String.valueOf( infoCount )};
139         getLog().info( i18n.format( "clirr-report", locale, "check.clirr.success", args ) );
140     }
141 
142     private void log( ClirrDiffListener listener, Severity severity )
143     {
144         if ( !logResults )
145         {
146             LogDiffListener l = new LogDiffListener( getLog() );
147             for ( ApiDifference difference : listener.getApiDifferences() )
148             {
149                 if ( difference.getMaximumSeverity().equals( severity ) )
150                 {
151                     l.reportDiff( difference );
152                 }
153             }
154         }
155     }
156 
157 }