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.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   * Verifies the existence or non-existence of files/directories and optionally checks file content against a regular
41   * expression.
42   *
43   * @author <a href="vmassol@apache.org">Vincent Massol</a>
44   * @version $Id: VerifierMojo.java 1672699 2015-04-10 16:45:27Z khmarbaise $
45   */
46  @Mojo( name = "verify", defaultPhase = LifecyclePhase.INTEGRATION_TEST )
47  public class VerifierMojo
48      extends AbstractMojo
49  {
50      /**
51       * Project base directory (prepended to relative file paths).
52       */
53      @Parameter( property = "basedir", required = true )
54      private File basedir;
55  
56      /**
57       * The file containing the verifications to perform.
58       */
59      // CHECKSTYLE_OFF: LineLength
60      @Parameter( property = "verifier.verificationFile", defaultValue = "${basedir}/src/test/verifier/verifications.xml", required = true )
61      private File verificationFile;
62      // CHECKSTYLE_ON: LineLength
63  
64      /**
65       * Whether the build will fail on verification errors.
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       * {@inheritDoc}
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 ( org.apache.maven.plugin.verifier.model.File file : verifications.getFiles() )
117             {
118                 // Transform the file to check into an absolute path prefixing the basedir if
119                 // the location is relative
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     // CHECKSTYLE_OFF: LineLength
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         // Note: Very inefficient way as we load the whole file in memory. If you have a better
172         // idea, please submit it!
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     // CHECKSTYLE_ON: LineLength
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      * @param theBasedir Set the base directory.
218      */
219     public void setBaseDir( File theBasedir )
220     {
221         this.basedir = theBasedir;
222     }
223 
224     /**
225      * @param file Set the file for verification.
226      */
227     public void setVerificationFile( File file )
228     {
229         this.verificationFile = file;
230     }
231 
232     /**
233      * @param printer The verification result printer.
234      * @see {@link VerificationResultPrinter}
235      */
236     public void setVerificationResultPrinter( VerificationResultPrinter printer )
237     {
238         this.resultPrinter = printer;
239     }
240 
241     /**
242      * @param failOnError true to fail on error false otherwise.
243      */
244     public void setFailOnError( boolean failOnError )
245     {
246         this.failOnError = failOnError;
247     }
248 }