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