1 package org.codehaus.mojo.clirr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29
30
31
32 public class AbstractClirrCheckMojo
33 extends AbstractClirrMojo
34 {
35
36
37
38
39
40 private boolean failOnError;
41
42
43
44
45
46
47 private boolean failOnWarning;
48
49
50
51
52
53
54 private boolean failOnInfo;
55
56
57
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 }