View Javadoc

1   package org.apache.maven.plugin.verifier;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Verifies the existence or non-existence of files/directories and optionally checks file content against a regular expression.
39   *
40   * @goal verify
41   * @phase integration-test
42   *
43   * @author <a href="vmassol@apache.org">Vincent Massol</a>
44   * @version $Id: VerifierMojo.java 900472 2010-01-18 17:36:37Z dennisl $
45   */
46  public class VerifierMojo
47      extends AbstractMojo
48  {
49      /**
50       * Project base directory (prepended to relative file paths).
51       *
52       * @parameter expression="${basedir}"
53       * @required
54       */
55      private File basedir;
56  
57      /**
58       * The file containing the verifications to perform.
59       *
60       * @parameter default-value="${basedir}/src/test/verifier/verifications.xml" expression="${verifier.verificationFile}"
61       * @required
62       */
63      private File verificationFile;
64  
65      /**
66       * Whether the build will fail on verification errors.
67       *
68       * @parameter default-value="true" expression="${verifier.failOnError}"
69       * @required
70       */
71      private boolean failOnError;
72  
73      private VerificationResultPrinter resultPrinter = new ConsoleVerificationResultPrinter( getLog() );
74  
75      public void execute()
76          throws MojoExecutionException
77      {
78          VerificationResult results = verify();
79          this.resultPrinter.print( results );
80  
81          // Fail the build if there are errors
82          if ( this.failOnError && results.hasFailures() )
83          {
84              throw new MojoExecutionException( "There are test failures" );
85          }
86      }
87  
88      /**
89       * @param file the file path of the file to check (can be relative or absolute). If relative
90       *             the project's basedir will be prefixed.
91       * @return the absolute file path of the file to check
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 ( Iterator i = verifications.getFiles().iterator(); i.hasNext(); )
117             {
118                 org.apache.maven.plugin.verifier.model.File file =
119                     (org.apache.maven.plugin.verifier.model.File) i.next();
120 
121                 // Transform the file to check into an absolute path prefixing the basedir if
122                 // the location is relative
123                 if ( file.getLocation() != null )
124                 {
125                     file.setLocation( getAbsoluteFileToCheck( new File( file.getLocation() ) ).getPath() );
126                     verifyFile( file, results );
127                 }
128                 else
129                 {
130                     throw new MojoExecutionException( "Missing <location> element" );
131                 }
132             }
133         }
134         catch ( org.codehaus.plexus.util.xml.pull.XmlPullParserException e )
135         {
136             throw new MojoExecutionException( "Error while verifying files", e );
137         }
138         catch ( IOException e )
139         {
140             throw new MojoExecutionException( "Error while verifying files", e );
141         }
142         finally
143         {
144             IOUtil.close( reader );
145         }
146 
147         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         result = verifyFileExistence( fileCheck, results );
156         if ( result && fileCheck.getContains() != null )
157         {
158             result = result && verifyFileContent( fileCheck, results );
159         }
160 
161         return result;
162     }
163 
164     private boolean verifyFileContent( org.apache.maven.plugin.verifier.model.File fileCheck,
165                                        VerificationResult results )
166         throws IOException
167     {
168         boolean result = false;
169 
170         getLog().debug( "Verifying contents of " + fileCheck.getLocation() );
171 
172         Pattern pattern = Pattern.compile( fileCheck.getContains() );
173 
174         // Note: Very inefficient way as we load the whole file in memory. If you have a better
175         // idea, please submit it!
176         Matcher matcher = pattern.matcher( FileUtils.fileRead( new File( fileCheck.getLocation() ) ) );
177 
178         if ( matcher.find() )
179         {
180             result = true;
181         }
182         else
183         {
184             results.addContentFailure( fileCheck );
185         }
186 
187         return result;
188     }
189 
190     private boolean verifyFileExistence( org.apache.maven.plugin.verifier.model.File fileCheck,
191                                          VerificationResult results )
192     {
193         boolean result = false;
194 
195         File physicalFile = new File( fileCheck.getLocation() );
196         if ( fileCheck.isExists() )
197         {
198             getLog().debug( "Verifying existence of " + physicalFile );
199             result = physicalFile.exists();
200             if ( !result )
201             {
202                 results.addExistenceFailure( fileCheck );
203             }
204         }
205         else
206         {
207             getLog().debug( "Verifying absence of " + physicalFile );
208             result = !physicalFile.exists();
209             if ( !result )
210             {
211                 results.addNonExistenceFailure( fileCheck );
212             }
213         }
214 
215         return result;
216     }
217 
218     public void setBaseDir( File basedir )
219     {
220         this.basedir = basedir;
221     }
222 
223     public void setVerificationFile( File file )
224     {
225         this.verificationFile = file;
226     }
227 
228     public void setVerificationResultPrinter( VerificationResultPrinter printer )
229     {
230         this.resultPrinter = printer;
231     }
232 
233     public void setFailOnError( boolean failOnError )
234     {
235         this.failOnError = failOnError;
236     }
237 }