PR: MNG-348

Submitted by:	Vincent Massol
Reviewed by:	Brett Porter (applied with some changes)
Add a verifier plugin, for doing integration tests


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@165612 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-05-02 09:17:30 +00:00
parent 6bb39db726
commit 6dd94566ab
20 changed files with 804 additions and 3 deletions

View File

@ -19,6 +19,8 @@ it0004: The simplest of pom installation. We have a pom and we install it in
it0005: The simplest of pom installation. We have a snapshot pom and we install
it in local repository.
it0006: Integration test for the verifier plugin.
it0007: We specify a parent in the POM and make sure that it is downloaded as
part of the process.

View File

@ -4,6 +4,7 @@ it0002
it0003
it0004
it0005
it0006
it0007
it0008
it0009
@ -15,4 +16,4 @@ it0013
it0016
it0017
it0018
#it0019
it0019

View File

@ -0,0 +1 @@
integration-test

View File

@ -0,0 +1,29 @@
<model>
<parent>
<artifactId>maven-verifier-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>1.0-alpha-2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-verifier-plugin-test</artifactId>
<name>Maven Verifier Plugin Test</name>
<version>1.0-alpha-2-SNAPSHOT</version>
<dependencies/>
<build>
<plugins>
<plugin>
<artifactId>maven-verifier-plugin</artifactId>
<!-- Problem here: if I specify a version, it won't build if the verifier plugin is not
in the remote repo. If I don't specify a version, it fails...
Now, even if I have built the verifier plugin locally m2 insist for downloading it
from the remote repo. Why is that? -->
<version>1.0-alpha-2-SNAPSHOT</version>
<goals>
<goal>
<id>verify</id>
</goal>
</goals>
</plugin>
</plugins>
</build>
</model>

View File

@ -0,0 +1,15 @@
<verifications>
<files>
<file>
<location>src/test/verifier/verifications.xml</location>
</file>
<file>
<location>src/test/verifier/invalidfile</location>
<exists>false</exists>
</file>
<file>
<location>pom.xml</location>
<contains>Maven.*Plugin Test</contains>
</file>
</files>
</verifications>

View File

@ -0,0 +1,60 @@
<model>
<parent>
<artifactId>maven-plugin-parent</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-verifier-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>Maven Verifier Plugin</name>
<version>1.0-alpha-2-SNAPSHOT</version>
<developers>
<developer>
<id>vmassol</id>
<name>Vincent Massol</name>
<email>vmassol@apache.org</email>
<organization>Apache Software Foundation</organization>
<roles>
<role>Creator</role>
<role>Java Developer</role>
</roles>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.0-alpha-2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-modello-plugin</artifactId>
<version>1.0-alpha-2-SNAPSHOT</version>
<configuration>
<model>src/main/mdo/verifications.mdo</model>
<version>1.0.0</version>
</configuration>
<goals>
<goal>
<id>xpp3-reader</id>
</goal>
<goal>
<id>xpp3-writer</id>
</goal>
<goal>
<id>java</id>
</goal>
</goals>
</plugin>
</plugins>
</build>
</model>

View File

@ -0,0 +1,74 @@
package org.apache.maven.plugin.verifier;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.monitor.logging.Log;
import java.util.Iterator;
public class ConsoleVerificationResultPrinter
implements VerificationResultPrinter
{
private Log log;
public ConsoleVerificationResultPrinter( Log log )
{
this.log = log;
}
public void print( VerificationResult results )
{
printExistenceFailures( results );
printNonExistenceFailures( results );
printContentFailures( results );
}
private void printExistenceFailures( VerificationResult results )
{
for ( Iterator i = results.getExistenceFailures().iterator(); i.hasNext(); )
{
org.apache.maven.plugin.verifier.model.File file = (org.apache.maven.plugin.verifier.model.File) i.next();
printMessage( "File not found [" + file.getLocation() + "]" );
}
}
private void printNonExistenceFailures( VerificationResult results )
{
for ( Iterator i = results.getNonExistenceFailures().iterator(); i.hasNext(); )
{
org.apache.maven.plugin.verifier.model.File file = (org.apache.maven.plugin.verifier.model.File) i.next();
printMessage( "File should not exist [" + file.getLocation() + "]" );
}
}
private void printContentFailures( VerificationResult results )
{
for ( Iterator i = results.getContentFailures().iterator(); i.hasNext(); )
{
org.apache.maven.plugin.verifier.model.File file = (org.apache.maven.plugin.verifier.model.File) i.next();
printMessage( "File [" + file.getLocation() + "] does not match regexp [" + file.getContains() + "]" );
}
}
private void printMessage( String message )
{
this.log.error( "[Verifier] " + message );
}
}

View File

@ -0,0 +1,67 @@
package org.apache.maven.plugin.verifier;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.plugin.verifier.model.File;
import java.util.ArrayList;
import java.util.List;
public class VerificationResult
{
private List existenceFailures = new ArrayList();
private List nonExistenceFailures = new ArrayList();
private List contentFailures = new ArrayList();
public void addExistenceFailure( File file )
{
existenceFailures.add( file );
}
public void addNonExistenceFailure( File file )
{
nonExistenceFailures.add( file );
}
public void addContentFailure( File file )
{
contentFailures.add( file );
}
public List getExistenceFailures()
{
return existenceFailures;
}
public List getNonExistenceFailures()
{
return nonExistenceFailures;
}
public List getContentFailures()
{
return contentFailures;
}
public boolean hasFailures()
{
return !getExistenceFailures().isEmpty() || !getNonExistenceFailures().isEmpty() ||
!getContentFailures().isEmpty();
}
}

View File

@ -0,0 +1,22 @@
package org.apache.maven.plugin.verifier;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public interface VerificationResultPrinter
{
void print( VerificationResult result );
}

View File

@ -0,0 +1,222 @@
package org.apache.maven.plugin.verifier;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.verifier.model.Verifications;
import org.apache.maven.plugin.verifier.model.io.xpp3.VerificationsXpp3Reader;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Verifies existence or non-existence of files/directories an optionally checks file content against a regexp.
*
* @goal verify
* @phase integration-test
*
* @author <a href="vmassol@apache.org">Vincent Massol</a>
* @version $Id$
*/
public class VerifierMojo
extends AbstractMojo
{
/**
* @parameter expression="${basedir}"
* @required
*/
private String basedir;
/**
* @parameter expression="${basedir}/src/test/verifier/verifications.xml"
* @required
*/
private File verificationFile;
/**
* @required
*/
private boolean failOnError = true;
private VerificationResultPrinter resultPrinter = new ConsoleVerificationResultPrinter( getLog() );
public void execute()
throws MojoExecutionException
{
VerificationResult results = verify();
this.resultPrinter.print( results );
// Fail the build if there are errors
if ( this.failOnError && results.hasFailures() )
{
throw new MojoExecutionException( "There are test failures" );
}
}
/**
* @param file the file path of the file to check (can be relative or absolute). If relative
* the project's basedir will be prefixed.
* @return the absolute file path of the file to check
*/
protected File getAbsoluteFileToCheck( File file )
{
File result = file;
if ( !file.isAbsolute() )
{
result = new File( new File( this.basedir ), file.getPath() );
}
return result;
}
private VerificationResult verify()
throws MojoExecutionException
{
VerificationResult results = new VerificationResult();
Reader reader = null;
try
{
reader = new FileReader( this.verificationFile );
VerificationsXpp3Reader xppReader = new VerificationsXpp3Reader();
Verifications verifications = xppReader.read( reader );
for ( Iterator i = verifications.getFiles().iterator(); i.hasNext(); )
{
org.apache.maven.plugin.verifier.model.File file = (org.apache.maven.plugin.verifier.model.File) i.next();
// Transform the file to check into an absolute path prefixing the basedir if
// the location is relative
if ( file.getLocation() != null )
{
file.setLocation( getAbsoluteFileToCheck( new File( file.getLocation() ) ).getPath() );
verifyFile( file, results );
}
else
{
throw new MojoExecutionException( "Missing <location> element" );
}
}
}
catch ( org.codehaus.plexus.util.xml.pull.XmlPullParserException e )
{
throw new MojoExecutionException( "Error while verifying files", e );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error while verifying files", e );
}
finally
{
IOUtil.close( reader );
}
return results;
}
private boolean verifyFile( org.apache.maven.plugin.verifier.model.File fileCheck, VerificationResult results )
throws IOException
{
boolean result;
result = verifyFileExistence( fileCheck, results );
if ( result && fileCheck.getContains() != null )
{
result = result && verifyFileContent( fileCheck, results );
}
return result;
}
private boolean verifyFileContent( org.apache.maven.plugin.verifier.model.File fileCheck,
VerificationResult results )
throws IOException
{
boolean result = false;
Pattern pattern = Pattern.compile( fileCheck.getContains() );
// Note: Very inefficient way as we load the whole file in memory. If you have a better
// idea, please submit it!
Matcher matcher = pattern.matcher( FileUtils.fileRead( new File( fileCheck.getLocation() ) ) );
if ( matcher.find() )
{
result = true;
}
else
{
results.addContentFailure( fileCheck );
}
return result;
}
private boolean verifyFileExistence( org.apache.maven.plugin.verifier.model.File fileCheck,
VerificationResult results )
{
boolean result = false;
File physicalFile = new File( fileCheck.getLocation() );
if ( fileCheck.isExists() )
{
result = physicalFile.exists();
if ( !result )
{
results.addExistenceFailure( fileCheck );
}
}
else
{
result = !physicalFile.exists();
if ( !result )
{
results.addNonExistenceFailure( fileCheck );
}
}
return result;
}
public void setBaseDir( String basedir )
{
this.basedir = basedir;
}
public void setVerificationFile( File file )
{
this.verificationFile = file;
}
public void setVerificationResultPrinter( VerificationResultPrinter printer )
{
this.resultPrinter = printer;
}
public void setFailOnError( boolean failOnError )
{
this.failOnError = failOnError;
}
}

View File

@ -0,0 +1,65 @@
<model>
<id>verifications</id>
<name>Verifications</name>
<description><![CDATA[
Project-specific configuration file for the Verifier plugin. It contains
definition of expected files and directories to be checked for after an
integration test has run. It can also check for file content against
regexps.]]></description>
<defaults>
<default>
<key>package</key>
<value>org.apache.maven.plugin.verifier.model</value>
</default>
</defaults>
<classes>
<class rootElement="true" xml.tagName="verifications">
<name>Verifications</name>
<version>1.0.0</version>
<description>Root element of the project verification file.</description>
<fields>
<field>
<name>files</name>
<version>1.0.0</version>
<description><![CDATA[
List of files and directories to check for.
]]></description>
<association>
<type>File</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
<name>File</name>
<version>1.0.0</version>
<fields>
<field>
<name>location</name>
<version>1.0.0</version>
<required>true</required>
<description><![CDATA[Location of the file or directory to check.]]></description>
<type>String</type>
</field>
<field>
<name>contains</name>
<version>1.0.0</version>
<description><![CDATA[Check the content against this regexp.]]></description>
<required>false</required>
<type>String</type>
</field>
<field>
<name>exists</name>
<version>1.0.0</version>
<description><![CDATA[
If true checks for file or directory existence, otherwise checks that the
file or directory doesn't exist.
]]></description>
<defaultValue>true</defaultValue>
<type>boolean</type>
</field>
</fields>
</class>
</classes>
</model>

View File

@ -0,0 +1,198 @@
package org.apache.maven.plugin.verifier;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import junit.framework.TestCase;
import java.io.File;
import org.apache.maven.plugin.MojoExecutionException;
public class VerifierMojoTest
extends TestCase
{
public void testPrefixWithBaseDir()
{
VerifierMojo mojo = new VerifierMojo();
mojo.setBaseDir( "c:/some/path" );
File result = mojo.getAbsoluteFileToCheck( new File( "target/dummy.txt" ) );
File expectedResult = new File( "c:/some/path/target/dummy.txt" );
assertEquals( expectedResult.getPath(), result.getPath() );
}
public void testDoNotPrefixWhenAbsolutePath()
{
VerifierMojo mojo = new VerifierMojo();
mojo.setBaseDir( "c:/some/path" );
File result = mojo.getAbsoluteFileToCheck( new File( "c:/project/target/dummy.txt" ) );
File expectedResult = new File( "c:/project/target/dummy.txt" );
assertEquals( expectedResult.getPath(), result.getPath() );
}
public void testCheckFileThatDoesNotExist()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/FileDoesNotExist.xml" ).getFile() );
mojo.setBaseDir( "c:/some/path" );
mojo.setVerificationFile( file );
mojo.setFailOnError( true );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 1, result.getExistenceFailures().size() );
assertEquals( 0, result.getNonExistenceFailures().size() );
assertEquals( 0, result.getContentFailures().size() );
}
} );
try
{
mojo.execute();
fail( "Should have thrown an exception" );
}
catch ( MojoExecutionException expected )
{
assertTrue( true );
}
}
public void testCheckFileThatExists()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/FileExists.xml" ).getFile() );
mojo.setBaseDir( file.getParent() );
mojo.setVerificationFile( file );
mojo.setFailOnError( true );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 0, result.getExistenceFailures().size() );
assertEquals( 0, result.getNonExistenceFailures().size() );
assertEquals( 0, result.getContentFailures().size() );
}
} );
mojo.execute();
}
public void testCheckForInexistentFile()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/InexistentFile.xml" ).getFile() );
mojo.setBaseDir( "c:/some/path" );
mojo.setVerificationFile( file );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 0, result.getExistenceFailures().size() );
assertEquals( 0, result.getNonExistenceFailures().size() );
assertEquals( 0, result.getContentFailures().size() );
}
} );
mojo.execute();
}
public void testCheckForInexistentFileThatExists()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/InexistentFileThatExists.xml" ).getFile() );
mojo.setBaseDir( file.getParent() );
mojo.setVerificationFile( file );
mojo.setFailOnError( true );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 0, result.getExistenceFailures().size() );
assertEquals( 1, result.getNonExistenceFailures().size() );
assertEquals( 0, result.getContentFailures().size() );
}
} );
try
{
mojo.execute();
fail( "Should have thrown an exception" );
}
catch ( MojoExecutionException expected )
{
assertTrue( true );
}
}
public void testCheckFileForContent()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/FileExistsValidContent.xml" ).getFile() );
mojo.setBaseDir( file.getParent() );
mojo.setVerificationFile( file );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 0, result.getExistenceFailures().size() );
assertEquals( 0, result.getNonExistenceFailures().size() );
assertEquals( 0, result.getContentFailures().size() );
}
} );
mojo.execute();
}
public void testCheckFileForInvalidContent()
throws Exception
{
VerifierMojo mojo = new VerifierMojo();
File file = new File( getClass().getResource( "/FileExistsInvalidContent.xml" ).getFile() );
mojo.setBaseDir( file.getParent() );
mojo.setVerificationFile( file );
mojo.setFailOnError( true );
mojo.setVerificationResultPrinter( new VerificationResultPrinter()
{
public void print( VerificationResult result )
{
assertEquals( 0, result.getExistenceFailures().size() );
assertEquals( 0, result.getNonExistenceFailures().size() );
assertEquals( 1, result.getContentFailures().size() );
}
} );
try
{
mojo.execute();
fail( "Should have thrown an exception" );
}
catch ( MojoExecutionException expected )
{
assertTrue( true );
}
}
}

View File

@ -0,0 +1,7 @@
<verifications>
<files>
<file>
<location>src/test/resources/Dummy.xml</location>
</file>
</files>
</verifications>

View File

@ -0,0 +1,7 @@
<verifications>
<files>
<file>
<location>FileExists.xml</location>
</file>
</files>
</verifications>

View File

@ -0,0 +1,8 @@
<verifications>
<files>
<file>
<location>FileExistsValidContent.xml</location>
<contains><![CDATA[something]]></contains>
</file>
</files>
</verifications>

View File

@ -0,0 +1,8 @@
<verifications>
<files>
<file>
<location>FileExistsValidContent.xml</location>
<contains><![CDATA[location>FileExists]]></contains>
</file>
</files>
</verifications>

View File

@ -0,0 +1,8 @@
<verifications>
<files>
<file>
<location>src/test/resources/Dummy.xml</location>
<exists>false</exists>
</file>
</files>
</verifications>

View File

@ -0,0 +1,8 @@
<verifications>
<files>
<file>
<location>InexistentFileThatExists.xml</location>
<exists>false</exists>
</file>
</files>
</verifications>

View File

@ -67,9 +67,7 @@
</repository>
</distributionManagement>
<modules>
<!--
<module>maven-assembly-plugin</module>
-->
<module>maven-clean-plugin</module>
<module>maven-compiler-plugin</module>
<module>maven-deploy-plugin</module>
@ -82,6 +80,7 @@
<module>maven-site-plugin</module>
<module>maven-surefire-plugin</module>
<module>maven-war-plugin</module>
<module>maven-verifier-plugin</module>
</modules>
<scm>
<connection>scm:svn:http://svn.apache.org/repos/asf/maven/components/trunk/maven-plugins</connection>