1 package org.apache.maven.plugin.verifier;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.verifier.model.Verifications;
25 import org.apache.maven.plugin.verifier.model.io.xpp3.VerificationsXpp3Reader;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.codehaus.plexus.util.FileUtils;
30 import org.codehaus.plexus.util.IOUtil;
31
32 import java.io.File;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.Reader;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39
40
41
42
43
44
45
46 @Mojo( name = "verify", defaultPhase = LifecyclePhase.INTEGRATION_TEST )
47 public class VerifierMojo
48 extends AbstractMojo
49 {
50
51
52
53 @Parameter( property = "basedir", required = true )
54 private File basedir;
55
56
57
58
59
60 @Parameter( property = "verifier.verificationFile", defaultValue = "${basedir}/src/test/verifier/verifications.xml", required = true )
61 private File verificationFile;
62
63
64
65
66
67 @Parameter( property = "verifier.failOnError", defaultValue = "true", required = true )
68 private boolean failOnError;
69
70 private VerificationResultPrinter resultPrinter = new ConsoleVerificationResultPrinter( getLog() );
71
72
73
74
75 public void execute()
76 throws MojoExecutionException
77 {
78 VerificationResult results = verify();
79 this.resultPrinter.print( results );
80
81
82 if ( this.failOnError && results.hasFailures() )
83 {
84 throw new MojoExecutionException( "There are test failures" );
85 }
86 }
87
88
89
90
91
92
93 protected File getAbsoluteFileToCheck( File file )
94 {
95 File result = file;
96 if ( !file.isAbsolute() )
97 {
98 result = new File( basedir, file.getPath() );
99 }
100 return result;
101 }
102
103 private VerificationResult verify()
104 throws MojoExecutionException
105 {
106 VerificationResult results = new VerificationResult();
107
108 Reader reader = null;
109 try
110 {
111 reader = new FileReader( this.verificationFile );
112
113 VerificationsXpp3Reader xppReader = new VerificationsXpp3Reader();
114 Verifications verifications = xppReader.read( reader );
115
116 for ( org.apache.maven.plugin.verifier.model.File file : verifications.getFiles() )
117 {
118
119
120 if ( file.getLocation() != null )
121 {
122 file.setLocation( getAbsoluteFileToCheck( new File( file.getLocation() ) ).getPath() );
123 verifyFile( file, results );
124 }
125 else
126 {
127 throw new MojoExecutionException( "Missing <location> element" );
128 }
129 }
130 }
131 catch ( org.codehaus.plexus.util.xml.pull.XmlPullParserException e )
132 {
133 throw new MojoExecutionException( "Error while verifying files", e );
134 }
135 catch ( IOException e )
136 {
137 throw new MojoExecutionException( "Error while verifying files", e );
138 }
139 finally
140 {
141 IOUtil.close( reader );
142 }
143
144 return results;
145 }
146
147 private boolean verifyFile( org.apache.maven.plugin.verifier.model.File fileCheck, VerificationResult results )
148 throws IOException
149 {
150 boolean result;
151
152 result = verifyFileExistence( fileCheck, results );
153 if ( result && fileCheck.getContains() != null )
154 {
155 result = result && verifyFileContent( fileCheck, results );
156 }
157
158 return result;
159 }
160
161
162 private boolean verifyFileContent( org.apache.maven.plugin.verifier.model.File fileCheck, VerificationResult results )
163 throws IOException
164 {
165 boolean result = false;
166
167 getLog().debug( "Verifying contents of " + fileCheck.getLocation() );
168
169 Pattern pattern = Pattern.compile( fileCheck.getContains() );
170
171
172
173 Matcher matcher = pattern.matcher( FileUtils.fileRead( new File( fileCheck.getLocation() ) ) );
174
175 if ( matcher.find() )
176 {
177 result = true;
178 }
179 else
180 {
181 results.addContentFailure( fileCheck );
182 }
183
184 return result;
185 }
186
187
188 private boolean verifyFileExistence( org.apache.maven.plugin.verifier.model.File fileCheck,
189 VerificationResult results )
190 {
191 boolean result;
192
193 File physicalFile = new File( fileCheck.getLocation() );
194 if ( fileCheck.isExists() )
195 {
196 getLog().debug( "Verifying existence of " + physicalFile );
197 result = physicalFile.exists();
198 if ( !result )
199 {
200 results.addExistenceFailure( fileCheck );
201 }
202 }
203 else
204 {
205 getLog().debug( "Verifying absence of " + physicalFile );
206 result = !physicalFile.exists();
207 if ( !result )
208 {
209 results.addNonExistenceFailure( fileCheck );
210 }
211 }
212
213 return result;
214 }
215
216
217
218
219 public void setBaseDir( File theBasedir )
220 {
221 this.basedir = theBasedir;
222 }
223
224
225
226
227 public void setVerificationFile( File file )
228 {
229 this.verificationFile = file;
230 }
231
232
233
234
235
236 public void setVerificationResultPrinter( VerificationResultPrinter printer )
237 {
238 this.resultPrinter = printer;
239 }
240
241
242
243
244 public void setFailOnError( boolean failOnError )
245 {
246 this.failOnError = failOnError;
247 }
248 }