You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ke...@apache.org on 2007/02/27 22:51:51 UTC

svn commit: r512437 - in /maven: core-integration-testing/trunk/core-integration-tests/ core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/ shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/

Author: kenney
Date: Tue Feb 27 13:51:50 2007
New Revision: 512437

URL: http://svn.apache.org/viewvc?view=rev&rev=512437
Log:
* Added getMavenVersion call to the Verifier

* Added a flag to run maven internally instead of forked (using reflection), nice for in IDE's.

* Added possibility to skip tests for certain maven versions, specified by a versionrange

* made it26 and it31 only run for 2.0.x

* added dependency to maven-artifact 2.0.5 on core-integration-tests for the VerionRange and DefaultArtifactVersion.

Modified:
    maven/core-integration-testing/trunk/core-integration-tests/pom.xml
    maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/AbstractMavenIntegrationTestCase.java
    maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/IntegrationTestSuite.java
    maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0026Test.java
    maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0031Test.java
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java

Modified: maven/core-integration-testing/trunk/core-integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/pom.xml?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/pom.xml (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/pom.xml Tue Feb 27 13:51:50 2007
@@ -34,10 +34,16 @@
     </plugins>
   </build>
   <dependencies>
+    <!-- needed for VersionRange and Version -->
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact</artifactId>
+      <version>2.0.5</version>
+    </dependency>
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
       <artifactId>maven-verifier</artifactId>
-      <version>1.0</version>
+      <version>1.1-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>junit</groupId>

Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/AbstractMavenIntegrationTestCase.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/AbstractMavenIntegrationTestCase.java?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/AbstractMavenIntegrationTestCase.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/AbstractMavenIntegrationTestCase.java Tue Feb 27 13:51:50 2007
@@ -1,5 +1,8 @@
 package org.apache.maven.integrationtests;
 
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.it.util.FileUtils;
 
 import java.io.File;
@@ -15,18 +18,57 @@
 public abstract class AbstractMavenIntegrationTestCase
     extends TestCase
 {
+    /**
+     * Save System.out for progress reports etc.
+     */
+    private static PrintStream out = System.out;
+
+    private boolean skip;
+
+    private DefaultArtifactVersion version;
+
+    private VersionRange versionRange;
+
+    protected AbstractMavenIntegrationTestCase()
+    {
+    }
+
+    protected AbstractMavenIntegrationTestCase( String versionRangeStr )
+        throws InvalidVersionSpecificationException
+    {
+        this.versionRange = VersionRange.createFromVersionSpec( versionRangeStr );
+
+        String v = System.getProperty( "maven.version" );
+        if ( v != null )
+        {
+            this.version = new DefaultArtifactVersion( v );
+            if ( !versionRange.containsVersion( this.version ) )
+            {
+                skip = true;
+            }
+        }
+        else
+        {
+            out.print( "WARNING: " + getITName() + ": version range '" + versionRange
+                + "' supplied but no maven version - not skipping test." );
+        }
+    }
+
     protected void runTest()
         throws Throwable
     {
+        out.print( getITName() + "(" + getName() + ").." );
+
+        if ( skip )
+        {
+            out.println( " Skipping (version " + version + " not in range " + versionRange + ")" );
+            return;
+        }
+
         if ( "true".equals( System.getProperty( "useEmptyLocalRepository", "false" ) ) )
         {
             setupLocalRepo();
         }
-
-        // save System.out since running the test will replace it
-        PrintStream out = System.out;
-
-        out.print( getITName() + "(" + getName() + ").." );
 
         try
         {

Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/IntegrationTestSuite.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/IntegrationTestSuite.java?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/IntegrationTestSuite.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/IntegrationTestSuite.java Tue Feb 27 13:51:50 2007
@@ -1,13 +1,26 @@
 package org.apache.maven.integrationtests;
 
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+
+import java.io.PrintStream;
+
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 public class IntegrationTestSuite
     extends AbstractMavenIntegrationTestCase
 {
-    public static Test suite()
+    private static PrintStream out = System.out;
+
+    public static Test suite() throws VerificationException
     {
+        String mavenVersion = new Verifier( "" ).getMavenVersion();
+
+        out.println( "Running integration tests for Maven " + mavenVersion );
+
+        System.setProperty( "maven.version", mavenVersion );
+
         TestSuite suite = new TestSuite();
         suite.addTestSuite( MavenIT0000Test.class );
         suite.addTestSuite( MavenIT0001Test.class );

Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0026Test.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0026Test.java?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0026Test.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0026Test.java Tue Feb 27 13:51:50 2007
@@ -1,5 +1,6 @@
 package org.apache.maven.integrationtests;
 
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.it.Verifier;
 import org.apache.maven.it.util.ResourceExtractor;
 
@@ -9,6 +10,11 @@
 public class MavenIT0026Test
     extends AbstractMavenIntegrationTestCase
 {
+    public MavenIT0026Test()
+        throws InvalidVersionSpecificationException
+    {
+        super( "[,2.1-SNAPSHOT)" );
+    }
 
     /**
      * Test merging of global- and user-level settings.xml files.
@@ -29,4 +35,3 @@
 
     }
 }
-

Modified: maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0031Test.java
URL: http://svn.apache.org/viewvc/maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0031Test.java?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0031Test.java (original)
+++ maven/core-integration-testing/trunk/core-integration-tests/src/test/java/org/apache/maven/integrationtests/MavenIT0031Test.java Tue Feb 27 13:51:50 2007
@@ -1,5 +1,6 @@
 package org.apache.maven.integrationtests;
 
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.it.Verifier;
 import org.apache.maven.it.util.ResourceExtractor;
 
@@ -9,11 +10,15 @@
 public class MavenIT0031Test
     extends AbstractMavenIntegrationTestCase
 {
+    public MavenIT0031Test()
+        throws InvalidVersionSpecificationException
+    {
+        super( "[,2.1-SNAPSHOT)" );
+    }
 
     /**
-     * Test usage of plugins.xml mapping file on the repository to resolve
-     * plugin artifactId from it's prefix using the pluginGroups in
-     * the provided settings.xml.
+     * Test usage of plugins.xml mapping file on the repository to resolve plugin artifactId from it's prefix using the
+     * pluginGroups in the provided settings.xml.
      */
     public void testit0031()
         throws Exception
@@ -30,9 +35,8 @@
         verifier.setVerifierProperties( verifierProperties );
         verifier.executeGoal( "modello:java" );
         verifier.assertFilePresent( "target/generated-sources/modello/org/apache/maven/it/it0031/Root.java" );
-// don't verify error free log
+        // don't verify error free log
         verifier.resetStreams();
 
     }
-}
-
+}
\ No newline at end of file

Modified: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java?view=diff&rev=512437&r1=512436&r2=512437
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java (original)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java Tue Feb 27 13:51:50 2007
@@ -1,6 +1,5 @@
 package org.apache.maven.it;
 
-import junit.framework.Assert;
 import org.apache.maven.it.util.FileUtils;
 import org.apache.maven.it.util.StringUtils;
 import org.apache.maven.it.util.cli.CommandLineException;
@@ -16,11 +15,13 @@
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
+
 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;
@@ -28,8 +29,13 @@
 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;
@@ -41,6 +47,8 @@
 import java.util.Properties;
 import java.util.StringTokenizer;
 
+import junit.framework.Assert;
+
 /**
  * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
@@ -74,6 +82,8 @@
 
     private boolean debug;
 
+    private boolean forkJvm = true;
+
     public Verifier( String basedir, String settingsFile )
         throws VerificationException
     {
@@ -83,10 +93,18 @@
     public Verifier( String basedir, String settingsFile, boolean debug )
         throws VerificationException
     {
+        this( basedir, settingsFile, debug, true );
+    }
+
+    public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm )
+        throws VerificationException
+    {
         this.basedir = basedir;
 
         this.debug = debug;
 
+        this.forkJvm = forkJvm;
+
         if ( !debug )
         {
             originalOut = System.out;
@@ -786,11 +804,35 @@
         executeGoals( goals, Collections.EMPTY_MAP );
     }
 
-    public void executeGoals( List goals, Map envVars )
-        throws VerificationException
+    private 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
+        {
+            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" );
@@ -827,32 +869,14 @@
                 System.out.println();
             }
 
-            cli.setWorkingDirectory( getBasedir() );
-
-            String executable;
-
-            // 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.
-
-            if ( mavenHome != null )
+            if ( System.getenv( "JAVA_HOME" ) == null && envVars.get( "JAVA_HOME" ) == null )
             {
-                executable = mavenHome + "/bin/mvn";
+                cli.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ));
             }
-            else
-            {
-                File f = new File( System.getProperty( "user.home" ), "m2/bin/mvn" );
 
-                if ( f.exists() )
-                {
-                    executable = f.getAbsolutePath();
-                }
-                else
-                {
-                    executable = "mvn";
-                }
-            }
+            cli.setWorkingDirectory( getBasedir() );
 
-            cli.setExecutable( executable );
+            cli.setExecutable( getExecutable() );
 
             for ( Iterator it = cliOptions.iterator(); it.hasNext(); )
             {
@@ -890,17 +914,9 @@
                 cli.createArgument().setValue( (String) i.next() );
             }
 
-            Writer logWriter = new FileWriter( logFile );
-
-            StreamConsumer out = new WriterStreamConsumer( logWriter );
-
-            StreamConsumer err = new WriterStreamConsumer( logWriter );
-
             System.out.println( "Command: " + Commandline.toString( cli.getCommandline() ) );
 
-            ret = CommandLineUtils.executeCommandLine( cli, out, err );
-
-            logWriter.close();
+            ret = runCommandLine( System.getProperty( "maven.home" ), cli, logFile );
         }
         catch ( CommandLineException e )
         {
@@ -917,6 +933,223 @@
 
             throw new VerificationException(
                 "Exit code was non-zero: " + ret + "; log = \n" + getLogContents( logFile ) );
+        }
+    }
+
+    public String getMavenVersion()
+        throws VerificationException
+    {
+        Commandline cmd = new Commandline();
+        cmd.setExecutable( getExecutable() );
+        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 );
+        }
+
+        List l = loadFile( log, false );
+
+        String first = (String) l.get( 0 );
+        if ( !first.startsWith( "Maven version: " ) )
+        {
+            throw new VerificationException( "Illegal maven output: expecting 'Maven version: ' but got " + first );
+        }
+
+        return first.substring( "Maven version: ".length() ).trim();
+    }
+
+
+    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 = System.getenv( "M2_HOME" );
+        }
+
+        if ( mavenHome == null )
+        {
+            mavenHome = System.getProperty( "user.home" ) + "/local/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 );
         }
     }