You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2008/07/25 03:13:23 UTC

svn commit: r679641 - /maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java

Author: jvanzyl
Date: Thu Jul 24 18:13:23 2008
New Revision: 679641

URL: http://svn.apache.org/viewvc?rev=679641&view=rev
Log:
o chop the invocation bits out of the verifier

Modified:
    maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java

Modified: maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java
URL: http://svn.apache.org/viewvc/maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java?rev=679641&r1=679640&r2=679641&view=diff
==============================================================================
--- maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java (original)
+++ maven/shared/branches/embedder-verifier/src/main/java/org/apache/maven/it/Verifier.java Thu Jul 24 18:13:23 2008
@@ -19,24 +19,11 @@
  * under the License.
  */
 
-import org.apache.maven.it.util.FileUtils;
-import org.apache.maven.it.util.StringUtils;
-import org.apache.maven.it.util.cli.CommandLineException;
-import org.apache.maven.it.util.cli.CommandLineUtils;
-import org.apache.maven.it.util.cli.Commandline;
-import org.apache.maven.it.util.cli.StreamConsumer;
-import org.apache.maven.it.util.cli.WriterStreamConsumer;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.helpers.DefaultHandler;
-
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.FilenameFilter;
@@ -44,21 +31,12 @@
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.io.Writer;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLClassLoader;
-import java.security.AccessControlException;
-import java.security.Permission;
-import java.text.DecimalFormat;
-import java.text.NumberFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.regex.Pattern;
@@ -69,6 +47,17 @@
 
 import junit.framework.Assert;
 
+import org.apache.maven.it.util.FileUtils;
+import org.apache.maven.it.util.cli.CommandLineException;
+import org.apache.maven.it.util.cli.CommandLineUtils;
+import org.apache.maven.it.util.cli.Commandline;
+import org.apache.maven.it.util.cli.StreamConsumer;
+import org.apache.maven.it.util.cli.WriterStreamConsumer;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
+
 /**
  * @author Jason van Zyl
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
@@ -877,432 +866,7 @@
             }
         }
     }
-
-    // ----------------------------------------------------------------------
-    //
-    // ----------------------------------------------------------------------
-
-    public void executeGoal( String goal )
-        throws VerificationException
-    {
-        executeGoal( goal, Collections.EMPTY_MAP );
-    }
-
-    public void executeGoal( String goal, Map envVars )
-        throws VerificationException
-    {
-        executeGoals( Arrays.asList( new String[] { goal } ), envVars );
-    }
-
-    public void executeGoals( List goals )
-        throws VerificationException
-    {
-        executeGoals( goals, Collections.EMPTY_MAP );
-    }
-
-    public String getExecutable()
-    {
-        // Use a strategy for finding the maven executable, John has a simple method like this
-        // but a little strategy + chain of command would be nicer.
-
-        String mavenHome = System.getProperty( "maven.home" );
-
-        if ( mavenHome != null )
-        {
-            return mavenHome + "/bin/mvn";
-        }
-        else
-        {
-            mavenHome = defaultMavenHome;
-
-            if ( mavenHome != null )
-            {
-                return mavenHome + "/bin/mvn";
-            }
-
-            File f = new File( System.getProperty( "user.home" ), "m2/bin/mvn" );
-
-            if ( f.exists() )
-            {
-                return f.getAbsolutePath();
-            }
-            else
-            {
-                return "mvn";
-            }
-        }
-    }
-
-    public void executeGoals( List goals, Map envVars )
-        throws VerificationException
-    {
-        if ( goals.size() == 0 )
-        {
-            throw new VerificationException( "No goals specified" );
-        }
-
-        List allGoals = new ArrayList();
-
-        if ( autoclean )
-        {
-            allGoals.add( "clean:clean" );
-        }
-
-        allGoals.addAll( goals );
-
-        int ret;
-
-        File logFile = new File( getBasedir(), LOG_FILENAME );
-        try
-        {
-            Commandline cli = createCommandLine();
-
-            for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); )
-            {
-                String key = (String) i.next();
-
-                cli.addEnvironment( key, (String) envVars.get( key ) );
-
-       /* What was the point of this? It doesn't work on windows.
-        *   try
-                {
-                    FileUtils.fileWrite( "/tmp/foo.txt", "setting envar[ " + key + " = " + envVars.get( key ) );
-                }
-                catch ( IOException e )
-                {
-                    e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates.
-                }*/
-
-               // System.out.println();
-            }
-
-            if ( envVars.get( "JAVA_HOME" ) == null )
-            {
-                cli.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
-            }
-
-            cli.setWorkingDirectory( getBasedir() );
-
-            for ( Iterator it = cliOptions.iterator(); it.hasNext(); )
-            {
-                String key = String.valueOf( it.next() );
-
-                String resolvedArg = resolveCommandLineArg( key );
-
-                cli.createArgument().setLine( resolvedArg );
-            }
-
-            cli.createArgument().setValue( "-e" );
-
-            cli.createArgument().setValue( "--no-plugin-registry" );
-
-            cli.createArgument().setValue( "--batch-mode" );
-
-            for ( Iterator i = systemProperties.keySet().iterator(); i.hasNext(); )
-            {
-                String key = (String) i.next();
-                cli.createArgument().setLine( "-D" + key + "=" + systemProperties.getProperty( key ) );
-            }
-
-            boolean useMavenRepoLocal =
-                Boolean.valueOf( verifierProperties.getProperty( "use.mavenRepoLocal", "true" ) ).booleanValue();
-
-            if ( useMavenRepoLocal )
-            {
-                // Note: Make sure that the repo is surrounded by quotes as it can possibly have
-                // spaces in its path.
-                cli.createArgument().setLine( "-Dmaven.repo.local=" + "\"" + localRepo + "\"" );
-            }
-
-            for ( Iterator i = allGoals.iterator(); i.hasNext(); )
-            {
-                cli.createArgument().setValue( (String) i.next() );
-            }
-
-           // System.out.println( "Command: " + Commandline.toString( cli.getCommandline() ) );
-
-            ret = runCommandLine( System.getProperty( "maven.home" ), cli, logFile );
-        }
-        catch ( CommandLineException e )
-        {
-            throw new VerificationException( e );
-        }
-        catch ( IOException e )
-        {
-            throw new VerificationException( e );
-        }
-
-        if ( ret > 0 )
-        {
-            System.err.println( "Exit code: " + ret );
-
-            throw new VerificationException(
-                "Exit code was non-zero: " + ret + "; log = \n" + getLogContents( logFile ) );
-        }
-    }
-
-    public String getMavenVersion()
-        throws VerificationException
-    {
-        Commandline cmd = createCommandLine();
-        cmd.addArguments( new String[] { "--version" } );
-
-        File log;
-        try
-        {
-            log = File.createTempFile( "maven", "log" );
-        }
-        catch ( IOException e )
-        {
-            throw new VerificationException( "Error creating temp file", e );
-        }
-
-        try
-        {
-            runCommandLine( System.getProperty( "maven.home" ), cmd, log );
-        }
-        catch ( CommandLineException e )
-        {
-            throw new VerificationException( "Error running commandline " + cmd.toString(), e );
-        }
-        catch ( IOException e )
-        {
-            throw new VerificationException( "IO Error communicating with commandline " + cmd.toString(), e );
-        }
-
-        String version = null;
-
-        List logLines = loadFile( log, false );
-
-        for ( Iterator it = logLines.iterator(); version == null && it.hasNext(); )
-        {
-            String line = (String) it.next();
-
-            if ( line.startsWith( "Maven version: " ) )
-            {
-                version = line.substring( "Maven version: ".length() );
-            }
-        }
-
-        if ( version == null )
-        {
-            throw new VerificationException( "Illegal maven output: String 'Maven version: ' not found in the following output:\n"
-                + StringUtils.join( logLines.iterator(), "\n" ) );
-        }
-        else
-        {
-            return version;
-        }
-    }
-
-    private Commandline createCommandLine()
-    {
-        Commandline cmd = new Commandline();
-        String executable = getExecutable();
-        if ( executable.endsWith( "/bin/mvn" ) )
-        {
-            cmd.addEnvironment( "M2_HOME", executable.substring( 0, executable.length() - 8 ) );
-        }
-        cmd.setExecutable( executable );
-        return cmd;
-    }
-
-    private int runCommandLine( String mavenHome, Commandline cli, File logFile )
-        throws CommandLineException, IOException
-    {
-        if ( forkJvm )
-        {
-            Writer logWriter = new FileWriter( logFile );
-
-            StreamConsumer out = new WriterStreamConsumer( logWriter );
-
-            StreamConsumer err = new WriterStreamConsumer( logWriter );
-
-            try
-            {
-                return CommandLineUtils.executeCommandLine( cli, out, err );
-            }
-            finally
-            {
-                logWriter.close();
-            }
-        }
-
-        if ( mavenHome == null )
-        {
-            mavenHome = defaultMavenHome;
-        }
-
-        if ( mavenHome == null )
-        {
-            mavenHome = System.getProperty( "user.home" ) + "/local/apache-maven-2.1-SNAPSHOT";
-        }
-
-        File coreDir = new File( mavenHome, "core/boot" );
-        File[] files = coreDir.listFiles();
-        File classWorldFile = null;
-        for ( int i = 0; files != null && i < files.length; i++ )
-        {
-            if ( files[i].getName().indexOf( "plexus-classworlds" ) >= 0 )
-            {
-                classWorldFile = files[i];
-                break;
-            }
-        }
-
-        if ( classWorldFile == null )
-        {
-            throw new CommandLineException( "Cannot find plexus-classworlds in " + coreDir );
-        }
-
-        URLClassLoader cl;
-
-        try
-        {
-            cl = new URLClassLoader( new URL[] { classWorldFile.toURI().toURL() }, null );
-        }
-        catch ( MalformedURLException e )
-        {
-            throw new CommandLineException( "Cannot conver to url: " + classWorldFile, e );
-        }
-
-        class ExitSecurityException
-            extends SecurityException
-        {
-
-            private int status;
-
-            public ExitSecurityException( int status )
-            {
-                this.status = status;
-            }
-
-            public int getStatus()
-            {
-                return status;
-            }
-        }
-        ;
-
-        try
-        {
-            Class c = cl.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
-
-            Method m = c.getMethod( "mainWithExitCode", new Class[] { String[].class } );
-
-            SecurityManager oldSm = System.getSecurityManager();
-
-            try
-            {
-                System.setSecurityManager( new SecurityManager()
-                {
-                    public void checkPermission( Permission perm )
-                    {
-                        // ok
-                    }
-
-                    public void checkExit( int status )
-                    {
-                        throw new ExitSecurityException( status );
-                    }
-                } );
-            }
-            catch ( AccessControlException e )
-            {
-                throw new CommandLineException( "Error isntalling securitymanager", e );
-            }
-
-            cli.createArgument().setValue( "-f" );
-            cli.createArgument().setValue( cli.getWorkingDirectory().getAbsolutePath() + "/pom.xml" );
-
-            PrintStream oldOut = System.out;
-            PrintStream oldErr = System.err;
-
-            String oldCwConf = System.getProperty( "classworlds.conf" );
-            String oldMavenHome = System.getProperty( "maven.home" );
-
-            ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
-
-            try
-            {
-                Thread.currentThread().setContextClassLoader( cl );// ClassLoader.getSystemClassLoader() );
-                FileOutputStream logWriter = new FileOutputStream( logFile );
-                System.setOut( new PrintStream( logWriter ) );
-                System.setErr( new PrintStream( logWriter ) );
-
-                System.setProperty( "classworlds.conf", new File( mavenHome, "bin/m2.conf" ).getAbsolutePath() );
-                System.setProperty( "maven.home", mavenHome );
-
-                return ( (Integer) m.invoke( null, new Object[] { cli.getArguments() } ) ).intValue();
-            }
-            catch ( ExitSecurityException e )
-            {
-                oldOut.println( "exit security exception caught: status=" + e.getStatus() );
-                return e.getStatus();
-            }
-            finally
-            {
-                System.setOut( oldOut );
-                System.setErr( oldErr );
-                if ( oldCwConf == null )
-                {
-                    System.getProperties().remove( "classworlds.conf" );
-                }
-                else
-                {
-                    System.setProperty( "classworlds.conf", oldCwConf );
-                }
-                if ( oldMavenHome == null )
-                {
-                    System.getProperties().remove( "maven.home" );
-                }
-                else
-                {
-                    System.setProperty( "maven.home", oldMavenHome );
-                }
-                Thread.currentThread().setContextClassLoader( oldCl );
-                System.setSecurityManager( oldSm );
-            }
-        }
-        catch ( ClassNotFoundException e )
-        {
-            throw new CommandLineException( "Cannot load classworlds launcher", e );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            throw new CommandLineException( "Cannot find classworlds launcher's main method", e );
-        }
-        catch ( IllegalArgumentException e )
-        {
-            throw new CommandLineException( "Error executing classworlds launcher's main method", e );
-        }
-        catch ( InvocationTargetException e )
-        {
-            if ( e.getCause() instanceof ExitSecurityException )
-            {
-                return ( (ExitSecurityException) e.getCause() ).getStatus();
-            }
-            throw new CommandLineException( "Error executing classworlds launcher's main method", e );
-        }
-        catch ( IllegalAccessException e )
-        {
-            throw new CommandLineException( "Error executing classworlds launcher's main method", e );
-        }
-    }
-
-    private static String getLogContents( File logFile )
-    {
-        try
-        {
-            return FileUtils.fileRead( logFile );
-        }
-        catch ( IOException e )
-        {
-            // ignore
-            return "(Error reading log contents: " + e.getMessage() + ")";
-        }
-    }
-
+ 
     private String resolveCommandLineArg( String key )
     {
         String result = key.replaceAll( "\\$\\{basedir\\}", getBasedir() );
@@ -1315,226 +879,6 @@
         return result;
     }
 
-    private static List discoverIntegrationTests( String directory )
-        throws VerificationException
-    {
-        try
-        {
-            ArrayList tests = new ArrayList();
-
-            List subTests = FileUtils.getFiles( new File( directory ), "**/goals.txt", null );
-
-            for ( Iterator i = subTests.iterator(); i.hasNext(); )
-            {
-                File testCase = (File) i.next();
-                tests.add( testCase.getParent() );
-            }
-
-            return tests;
-        }
-        catch ( IOException e )
-        {
-            throw new VerificationException( directory + " is not a valid test case container", e );
-        }
-    }
-
-    private void displayLogFile()
-    {
-        System.out.println( "Log file contents:" );
-        try
-        {
-            BufferedReader reader = new BufferedReader( new FileReader( new File( getBasedir(), LOG_FILENAME ) ) );
-            String line = reader.readLine();
-            while ( line != null )
-            {
-                System.out.println( line );
-                line = reader.readLine();
-            }
-            reader.close();
-        }
-        catch ( FileNotFoundException e )
-        {
-            System.err.println( "Error: " + e );
-        }
-        catch ( IOException e )
-        {
-            System.err.println( "Error: " + e );
-        }
-    }
-
-    // ----------------------------------------------------------------------
-    //
-    // ----------------------------------------------------------------------
-
-    public static void main( String args[] )
-        throws VerificationException
-    {
-        String basedir = System.getProperty( "user.dir" );
-
-        List tests = null;
-
-        List argsList = new ArrayList();
-
-        String settingsFile = null;
-
-        // skip options
-        for ( int i = 0; i < args.length; i++ )
-        {
-            if ( args[i].startsWith( "-D" ) )
-            {
-                int index = args[i].indexOf( "=" );
-                if ( index >= 0 )
-                {
-                    System.setProperty( args[i].substring( 2, index ), args[i].substring( index + 1 ) );
-                }
-                else
-                {
-                    System.setProperty( args[i].substring( 2 ), "true" );
-                }
-            }
-            else if ( "-s".equals( args[i] ) || "--settings".equals( args[i] ) )
-            {
-                if ( i == args.length - 1 )
-                {
-                    // should have been detected before
-                    throw new IllegalStateException( "missing argument to -s" );
-                }
-                i += 1;
-
-                settingsFile = args[i];
-            }
-            else if ( args[i].startsWith( "-" ) )
-            {
-                System.out.println( "skipping unrecognised argument: " + args[i] );
-            }
-            else
-            {
-                argsList.add( args[i] );
-            }
-        }
-
-        if ( argsList.size() == 0 )
-        {
-            if ( FileUtils.fileExists( basedir + File.separator + "integration-tests.txt" ) )
-            {
-                try
-                {
-                    tests = FileUtils.loadFile( new File( basedir, "integration-tests.txt" ) );
-                }
-                catch ( IOException e )
-                {
-                    System.err.println( "Unable to load integration tests file" );
-
-                    System.err.println( e.getMessage() );
-
-                    System.exit( 2 );
-                }
-            }
-            else
-            {
-                tests = discoverIntegrationTests( "." );
-            }
-        }
-        else
-        {
-            tests = new ArrayList( argsList.size() );
-            NumberFormat fmt = new DecimalFormat( "0000" );
-            for ( int i = 0; i < argsList.size(); i++ )
-            {
-                String test = (String) argsList.get( i );
-                if ( test.endsWith( "," ) )
-                {
-                    test = test.substring( 0, test.length() - 1 );
-                }
-
-                if ( StringUtils.isNumeric( test ) )
-                {
-
-                    test = "it" + fmt.format( Integer.valueOf( test ) );
-                    test.trim();
-                    tests.add( test );
-                }
-                else if ( "it".startsWith( test ) )
-                {
-                    test = test.trim();
-                    if ( test.length() > 0 )
-                    {
-                        tests.add( test );
-                    }
-                }
-                else if ( FileUtils.fileExists( test ) && new File( test ).isDirectory() )
-                {
-                    tests.addAll( discoverIntegrationTests( test ) );
-                }
-                else
-                {
-                    System.err.println(
-                        "[WARNING] rejecting " + test + " as an invalid test or test source directory" );
-                }
-            }
-        }
-
-        if ( tests.size() == 0 )
-        {
-            System.out.println( "No tests to run" );
-        }
-
-        int exitCode = 0;
-
-        List failed = new ArrayList();
-        for ( Iterator i = tests.iterator(); i.hasNext(); )
-        {
-            String test = (String) i.next();
-
-            System.out.print( test + "... " );
-
-            String dir = basedir + "/" + test;
-
-            if ( !new File( dir, "goals.txt" ).exists() )
-            {
-                System.err.println( "Test " + test + " in " + dir + " does not exist" );
-
-                System.exit( 2 );
-            }
-
-            Verifier verifier = new Verifier( dir );
-            verifier.findLocalRepo( settingsFile );
-
-            System.out.println( "Using default local repository: " + verifier.localRepo );
-
-            try
-            {
-                runIntegrationTest( verifier );
-            }
-            catch ( Throwable e )
-            {
-                verifier.resetStreams();
-
-                System.out.println( "FAILED" );
-
-                verifier.displayStreamBuffers();
-
-                System.out.println( ">>>>>> Error Stacktrace:" );
-                e.printStackTrace( System.out );
-                System.out.println( "<<<<<< Error Stacktrace" );
-
-                verifier.displayLogFile();
-
-                exitCode = 1;
-
-                failed.add( test );
-            }
-        }
-
-        System.out.println( tests.size() - failed.size() + "/" + tests.size() + " passed" );
-        if ( !failed.isEmpty() )
-        {
-            System.out.println( "Failed tests: " + failed );
-        }
-
-        System.exit( exitCode );
-    }
-
     private void findLocalRepo( String settingsFile )
         throws VerificationException
     {
@@ -1561,41 +905,6 @@
         }
     }
 
-    private static void runIntegrationTest( Verifier verifier )
-        throws VerificationException
-    {
-        verifier.executeHook( "prebuild-hook.txt" );
-
-        Properties properties = verifier.loadProperties( "system.properties" );
-
-        Properties controlProperties = verifier.loadProperties( "verifier.properties" );
-
-        boolean chokeOnErrorOutput =
-            Boolean.valueOf( controlProperties.getProperty( "failOnErrorOutput", "true" ) ).booleanValue();
-
-        List goals = verifier.loadFile( verifier.getBasedir(), "goals.txt", false );
-
-        List cliOptions = verifier.loadFile( verifier.getBasedir(), "cli-options.txt", false );
-
-        verifier.setCliOptions( cliOptions );
-
-        verifier.setSystemProperties( properties );
-
-        verifier.setVerifierProperties( controlProperties );
-
-        verifier.executeGoals( goals );
-
-        verifier.executeHook( "postbuild-hook.txt" );
-
-        System.out.println( "*** Verifying: fail when [ERROR] detected? " + chokeOnErrorOutput + " ***" );
-
-        verifier.verify( chokeOnErrorOutput );
-
-        verifier.resetStreams();
-
-        System.out.println( "OK" );
-    }
-
     public void assertArtifactContents( String org, String artifact, String version, String type, String contents )
         throws IOException
     {