You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by br...@apache.org on 2004/09/09 13:13:12 UTC

cvs commit: maven-components/maven-core-it-verifier/src/main/java/org/apache/maven/it Verifier.java

brett       2004/09/09 04:13:12

  Modified:    .        m2-bootstrap-all.sh mboot.jar
               maven-core-it maven-core-it.sh
               maven-core-it-verifier/src/main/java/org/apache/maven/it
                        Verifier.java
  Added:       maven-core-it/it0002 prebuild-hook.txt
               maven-core-it/it0003 prebuild-hook.txt
               maven-core-it/it0004 prebuild-hook.txt
               maven-core-it/it0005 prebuild-hook.txt
               maven-core-it/it0007 prebuild-hook.txt
  Removed:     maven-core-it .cvsignore
               maven-core-it/it0002 prebuild.hook
               maven-core-it/it0003 prebuild.hook
               maven-core-it/it0004 prebuild.hook
               maven-core-it/it0005 prebuild.hook
               maven-core-it/it0007 prebuild.hook
  Log:
  make integration tests run under java
  
  Revision  Changes    Path
  1.4       +4 -0      maven-components/m2-bootstrap-all.sh
  
  Index: m2-bootstrap-all.sh
  ===================================================================
  RCS file: /home/cvs/maven-components/m2-bootstrap-all.sh,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- m2-bootstrap-all.sh	2 Sep 2004 12:34:16 -0000	1.3
  +++ m2-bootstrap-all.sh	9 Sep 2004 11:13:10 -0000	1.4
  @@ -5,6 +5,10 @@
   
   ARGS="$@"
   
  +if [ ! -z "$M2_HOME" ]; then
  +  ARGS="$ARGS -Dmaven.home=$M2_HOME"
  +fi
  +
   # Build and install mboot
   (
     echo "-----------------------------------------------------------------------"
  
  
  
  1.22      +28 -28    maven-components/mboot.jar
  
  	<<Binary file>>
  
  
  1.16      +13 -59    maven-components/maven-core-it/maven-core-it.sh
  
  Index: maven-core-it.sh
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core-it/maven-core-it.sh,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- maven-core-it.sh	3 Sep 2004 21:34:13 -0000	1.15
  +++ maven-core-it.sh	9 Sep 2004 11:13:11 -0000	1.16
  @@ -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 '^#'`
  -
  -#If this doesn't have a value, we'll parse $HOME/.m2/pom.xml in the Verifier.
  -local_repo=
  -
  -for i in "$@"
  -do
  - j=`echo $i | sed 's/^-Dmaven.repo.local=//'`
  - if [ "$i" != "$j" ]; then
  -  local_repo=$j
  - fi
  -done
  -
  -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
  +# 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
  +
  +jvm_args="$@"
  +
  +if [ ! -z "$M2_HOME" ]; then
  +  jvm_args="$jvm_args -Dmaven.home=$M2_HOME"
  +fi
   
  -done
  +java $jvm_args -cp "$cp" $verifier
   
  
  
  
  1.1                  maven-components/maven-core-it/it0002/prebuild-hook.txt
  
  Index: prebuild-hook.txt
  ===================================================================
  rm ${localRepository}/maven/jars/maven-it-support-1.0.jar
  
  
  
  1.1                  maven-components/maven-core-it/it0003/prebuild-hook.txt
  
  Index: prebuild-hook.txt
  ===================================================================
  rm ${localRepository}/maven/jars/maven-core-it0003-1.0.jar
  
  
  
  1.1                  maven-components/maven-core-it/it0004/prebuild-hook.txt
  
  Index: prebuild-hook.txt
  ===================================================================
  rm ${localRepository}/maven/poms/maven-core-it0004-1.0.pom
  
  
  
  1.1                  maven-components/maven-core-it/it0005/prebuild-hook.txt
  
  Index: prebuild-hook.txt
  ===================================================================
  rm ${localRepository}/maven/poms/maven-core-it0005-1.0-SNAPSHOT.pom
  
  
  
  1.1                  maven-components/maven-core-it/it0007/prebuild-hook.txt
  
  Index: prebuild-hook.txt
  ===================================================================
  rm ${localRepository}/maven/poms/maven-it-support-parent-1.0.pom
  
  
  
  1.13      +241 -48   maven-components/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java
  
  Index: Verifier.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core-it-verifier/src/main/java/org/apache/maven/it/Verifier.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Verifier.java	3 Sep 2004 21:34:13 -0000	1.12
  +++ Verifier.java	9 Sep 2004 11:13:12 -0000	1.13
  @@ -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 @@
   
   /**
    * @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 void verify() throws VerificationException
       {
  -        try
  +        List lines = loadFile( basedir, "expected-results.txt" );
  +        for ( Iterator i = lines.iterator(); i.hasNext(); )
           {
  -            retrieveLocalRepo();
  +            String line = ( String ) i.next();
  +            verifyExpectedResult( line );
  +        }
  +    }
  +
  +    private static List loadFile( String basedir, String filename ) throws VerificationException
  +    {
  +        return loadFile( new File( basedir, filename ) );
  +    }
   
  -            BufferedReader reader = new BufferedReader( new FileReader( new File( basedir, "expected-results.txt" ) ) );
  +    private static List loadFile( File file ) throws VerificationException
  +    {
  +        List lines = new ArrayList();
  +
  +        try
  +        {
  +            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 );
           }
  +        return lines;
  +    }
  +
  +    public void executeHook( String filename ) throws VerificationException
  +    {
  +        try
  +        {
  +            File f = new File( basedir, filename );
  +            if ( !f.exists() )
  +            {
  +                return;
  +            }
   
  -        System.out.println( "-----------------------------------------------------------------------------------> OK" );
  +            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 void retrieveLocalRepo() throws Exception
  +    private static void executeCommand( String line ) throws VerificationException
       {
  -        localRepo = System.getProperty( "maven.repo.local" );
  -        if ( localRepo == null )
  +        int index = line.indexOf( " " );
  +        String cmd;
  +        String args = null;
  +        if ( index >= 0 )
  +        {
  +            cmd = line.substring( 0, index );
  +            args = line.substring( index + 1 );
  +        }
  +        else
           {
  -            // parse ~/.m2/pom.xml for it...
  -            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  -            DocumentBuilder builder = factory.newDocumentBuilder();
  +            cmd = line;
  +        }
   
  -            File pom = new File( homeDir, ".m2/pom.xml" );
  -            Document dom = builder.parse( pom );
  +        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 );
  +        }
  +    }
   
  -            localRepo = XPathAPI.selectSingleNode( dom, "/project/local/repository/text()" ).getNodeValue();
  +    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 @@
           }
           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 @@
           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.err.println( "Unable to load integration tests file" );
  +            System.err.println( e.getMessage() );
  +            System.exit( 2 );
  +        }
   
  -            System.exit( 1 );
  +        if ( tests.size() == 0 )
  +        {
  +            System.out.println( "No tests to run" );
           }
   
  -        System.exit( 0 );
  +        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 );
       }
  -}
  \ No newline at end of file
  +}
  +