make integration tests run under java

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163065 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2004-09-09 11:13:12 +00:00
parent abb54cdba5
commit 62584adb1a
15 changed files with 263 additions and 133 deletions

View File

@ -5,6 +5,10 @@
ARGS="$@"
if [ ! -z "$M2_HOME" ]; then
ARGS="$ARGS -Dmaven.home=$M2_HOME"
fi
# Build and install mboot
(
echo "-----------------------------------------------------------------------"

View File

@ -1,15 +1,21 @@
package org.apache.maven.it;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Properties;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -21,20 +27,53 @@ import org.w3c.dom.Document;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class Verifier
{
private String basedir;
private static String localRepo;
private File homeDir;
private final String basedir;
private String localRepo;
private final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
private final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
public Verifier( String basedir, String homeDir )
private final PrintStream originalOut;
private final PrintStream originalErr;
public Verifier( String basedir )
{
this.basedir = basedir;
this.homeDir = new File( homeDir );
originalOut = System.out;
System.setOut( new PrintStream( outStream ) );
originalErr = System.err;
System.setErr( new PrintStream( errStream ) );
}
public void resetStreams()
{
System.setOut( originalOut );
System.setErr( originalErr );
}
public void displayStreamBuffers()
{
String out = outStream.toString();
if ( out != null && out.trim().length() > 0 )
{
System.out.println( "----- Standard Out -----" );
System.out.println( out );
}
String err = errStream.toString();
if ( err != null && err.trim().length() > 0 )
{
System.err.println( "----- Standard Error -----" );
System.err.println( err );
}
}
// ----------------------------------------------------------------------
@ -43,50 +82,139 @@ public class Verifier
public void verify() throws VerificationException
{
List lines = loadFile( basedir, "expected-results.txt" );
for ( Iterator i = lines.iterator(); i.hasNext(); )
{
String line = ( String ) i.next();
verifyExpectedResult( line );
}
}
private static List loadFile( String basedir, String filename ) throws VerificationException
{
return loadFile( new File( basedir, filename ) );
}
private static List loadFile( File file ) throws VerificationException
{
List lines = new ArrayList();
try
{
retrieveLocalRepo();
BufferedReader reader = new BufferedReader( new FileReader( new File( basedir, "expected-results.txt" ) ) );
BufferedReader reader = new BufferedReader( new FileReader( file ) );
String line = "";
while ( (line = reader.readLine()) != null )
{
verifyExpectedResult( line );
line = line.trim();
if ( line.startsWith( "#" ) || line.length() == 0 )
{
continue;
}
line = replace( line, "${localRepository}", localRepo );
lines.add( line );
}
}
catch ( Exception e )
{
throw new VerificationException( e );
}
System.out.println( "-----------------------------------------------------------------------------------> OK" );
return lines;
}
private void retrieveLocalRepo() throws Exception
public void executeHook( String filename ) throws VerificationException
{
localRepo = System.getProperty( "maven.repo.local" );
if ( localRepo == null )
try
{
// parse ~/.m2/pom.xml for it...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File( basedir, filename );
if ( !f.exists() )
{
return;
}
File pom = new File( homeDir, ".m2/pom.xml" );
Document dom = builder.parse( pom );
localRepo = XPathAPI.selectSingleNode( dom, "/project/local/repository/text()" ).getNodeValue();
List lines = loadFile( f );
for ( Iterator i = lines.iterator(); i.hasNext(); )
{
String line = ( String ) i.next();
executeCommand( line );
}
}
catch ( VerificationException e )
{
throw e;
}
catch ( Exception e )
{
throw new VerificationException( e );
}
}
private static void executeCommand( String line ) throws VerificationException
{
int index = line.indexOf( " " );
String cmd;
String args = null;
if ( index >= 0 )
{
cmd = line.substring( 0, index );
args = line.substring( index + 1 );
}
else
{
cmd = line;
}
if ( cmd.equals( "rm" ) )
{
System.out.println( "Removing file: " + args );
File f = new File( args );
if ( f.exists() && !f.delete() )
{
throw new VerificationException( "Error removing file - delete failed" );
}
}
else
{
throw new VerificationException( "unknown command: " + cmd );
}
}
private static String retrieveLocalRepo()
{
String repo = System.getProperty( "maven.repo.local" );
if ( repo == null )
{
try
{
// parse ~/.m2/pom.xml for it...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File pom = new File( System.getProperty( "user.home" ), ".m2/pom.xml" );
Document dom = builder.parse( pom );
repo = XPathAPI.selectSingleNode( dom, "/project/local/repository/text()" ).getNodeValue();
}
catch ( Exception e )
{
System.err.println( "WARNING: failed to parse user pom (ignoring): " + e.getMessage() );
}
}
if ( repo == null )
{
repo = System.getProperty( "user.home" ) + "/.m2/repository";
}
return repo;
}
private void verifyExpectedResult( String line ) throws VerificationException
{
line = replace( line, "${localRepository}", localRepo );
if ( line.indexOf( "!/" ) > 0 )
{
String urlString = "jar:file:" + line;
String urlString = "jar:file:" + basedir + "/" + line;
try
{
@ -106,23 +234,11 @@ public class Verifier
}
else
{
File expectedFile;
if ( line.startsWith( "/" ) )
File expectedFile = new File( line );
if ( !expectedFile.isAbsolute() && !line.startsWith( "/" ) )
{
expectedFile = new File( line );
}
else
{
if ( line.indexOf( ":" ) > 0 ) //windows
{
expectedFile = new File( line );
}
else
{
expectedFile = new File( basedir, line );
}
}
expectedFile = new File( basedir, line );
}
if ( !expectedFile.exists() )
{
@ -168,25 +284,102 @@ public class Verifier
return buf.toString();
}
public void executeGoals( String filename ) throws VerificationException
{
String mavenHome = System.getProperty( "maven.home" );
if ( mavenHome == null )
{
throw new VerificationException( "maven.home has not been specified" );
}
List goals = loadFile( basedir, filename );
if ( goals.size() == 0 )
{
throw new VerificationException( "No goals specified" );
}
List allGoals = new ArrayList();
allGoals.add( "clean:clean" );
allGoals.addAll( goals );
try
{
String prevUserDir = System.getProperty( "user.dir" );
System.setProperty( "user.dir", basedir );
System.setProperty( "classworlds.conf", mavenHome + "/bin/classworlds.conf" );
URL classWorldsUrl = new URL( "file:" + mavenHome + "/core/classworlds-1.1-SNAPSHOT.jar" );
ClassLoader cl = URLClassLoader.newInstance( new URL[] { classWorldsUrl } );
Class c = Class.forName( "org.codehaus.classworlds.Launcher", true, cl );
Method m = c.getMethod( "main", new Class[] { String[].class } );
m.invoke( null, new Object[] { allGoals.toArray( new String[0] ) } );
System.setProperty( "user.dir", prevUserDir );
}
catch ( Exception e )
{
throw new VerificationException( e );
}
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
public static void main( String args[] )
{
Verifier verifier = new Verifier( args[0], args[1] );
String basedir = System.getProperty( "user.dir" );
localRepo = retrieveLocalRepo();
try
List tests = null;
try
{
verifier.verify();
tests = loadFile( basedir, "integration-tests.txt" );
}
catch ( VerificationException e )
{
System.out.println( e.getMessage() );
System.exit( 1 );
System.err.println( "Unable to load integration tests file" );
System.err.println( e.getMessage() );
System.exit( 2 );
}
System.exit( 0 );
if ( tests.size() == 0 )
{
System.out.println( "No tests to run" );
}
int exitCode = 0;
for ( Iterator i = tests.iterator(); i.hasNext(); )
{
String test = ( String ) i.next();
System.out.print( test + "... " );
Verifier verifier = new Verifier( basedir + "/" + test );
try
{
verifier.executeHook( "prebuild-hook.txt" );
verifier.executeGoals( "goals.txt" );
verifier.executeHook( "postbuild-hook.txt" );
verifier.verify();
verifier.resetStreams();
System.out.println( "OK" );
}
catch ( VerificationException e )
{
verifier.resetStreams();
System.out.println( "FAILED" );
verifier.displayStreamBuffers();
e.printStackTrace();
exitCode = 1;
}
}
System.exit( exitCode );
}
}
}

View File

@ -1 +0,0 @@
it*-log.txt

View File

@ -0,0 +1 @@
rm ${localRepository}/maven/jars/maven-it-support-1.0.jar

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Removing maven-it-support-1.0.jar from the local repository so we can verify its downloading ..."
rm -f $1/maven/jars/maven-it-support-1.0.jar > /dev/null 2>&1

View File

@ -0,0 +1 @@
rm ${localRepository}/maven/jars/maven-core-it0003-1.0.jar

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Removing maven-core-it0003-1.0.jar from the local repository so we can verify jar installation ..."
rm -f $1/maven/jars/maven-core-it0003-1.0.jar > /dev/null 2>&1

View File

@ -0,0 +1 @@
rm ${localRepository}/maven/poms/maven-core-it0004-1.0.pom

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Removing maven-core-it0004-1.0.pom from the local repository so we can verify pom installation ..."
rm -f $1/maven/poms/maven-core-it0004-1.0.pom > /dev/null 2>&1

View File

@ -0,0 +1 @@
rm ${localRepository}/maven/poms/maven-core-it0005-1.0-SNAPSHOT.pom

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Removing maven-core-it0005-1.0.pom from the local repository so we can verify pom installation ..."
rm -f $1/maven/poms/maven-core-it0005-1.0-SNAPSHOT.pom > /dev/null 2>&1

View File

@ -0,0 +1 @@
rm ${localRepository}/maven/poms/maven-it-support-parent-1.0.pom

View File

@ -1,5 +0,0 @@
#!/bin/sh
echo "Removing maven-it-support-parent-1.0.pom from the local repository so we can verify its downloading ..."
rm -f $1/maven/poms/maven-it-support-parent-1.0.pom > /dev/null 2>&1

View File

@ -2,67 +2,21 @@
# This process assumes that maven-core-it-verifier has been built.
home=`pwd`
cp=../../maven-core-it-verifier/target/maven-core-it-verifier-1.0.jar
cp=../maven-core-it-verifier/target/maven-core-it-verifier-1.0.jar
verifier=org.apache.maven.it.Verifier
integration_tests=`cat integration-tests.txt | egrep -v '^#'`
# TODO: need a consistent way to discover M2_HOME across this, bootstrap and m2 itself, as well as have a sensible
# default, and a way to override. There must be only one way.
# I like the idea of using the one in the path, or using -Dmaven.home to override
# The m2 shell script should not care what installation it is in - it should use the installation defined on the
# command line
#If this doesn't have a value, we'll parse $HOME/.m2/pom.xml in the Verifier.
local_repo=
jvm_args="$@"
for i in "$@"
do
j=`echo $i | sed 's/^-Dmaven.repo.local=//'`
if [ "$i" != "$j" ]; then
local_repo=$j
fi
done
if [ ! -z "$M2_HOME" ]; then
jvm_args="$jvm_args -Dmaven.home=$M2_HOME"
fi
for integration_test in $integration_tests
do
(
cd $integration_test
if [ -f prebuild.hook ]
then
echo
sh prebuild.hook "$local_repo"
echo
fi
jvm_opts=
if [ "$local_repo" != "" ]
then
jvm_opts="-Dmaven.repo.local=$local_repo"
fi
m2 $jvm_opts clean:clean `cat goals.txt`
if [ -f postbuild.hook ]
then
echo
sh postbuild.hook
echo
fi
basedir=.
java $jvm_opts -cp "$cp" $verifier "$basedir" "$HOME"
) > ${integration_test}-log.txt
if [ "$?" = "0" ]
then
echo "Integration test $integration_test OK"
else
echo "Integration test $integration_test FAILED!"
echo "Details:"
cat ${integration_test}-log.txt
echo
fi
done
java $jvm_args -cp "$cp" $verifier

BIN
mboot.jar

Binary file not shown.