You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2006/11/10 19:25:57 UTC

svn commit: r473412 - in /maven/sandbox/maven-plugin-testing-tools: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/maven/ src/main/java/org/apache/maven/shared/ src/main/java/org/apache/maven/shar...

Author: jdcasey
Date: Fri Nov 10 10:25:56 2006
New Revision: 473412

URL: http://svn.apache.org/viewvc?view=rev&rev=473412
Log:
Creating new project to stage plugin artifacts to a testing localRepo, execute maven builds via the maven-invoker, and other similar things used to test plugin projects.

Added:
    maven/sandbox/maven-plugin-testing-tools/
    maven/sandbox/maven-plugin-testing-tools/pom.xml   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/
    maven/sandbox/maven-plugin-testing-tools/src/main/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/test/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java   (with props)
    maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java   (with props)

Added: maven/sandbox/maven-plugin-testing-tools/pom.xml
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/pom.xml?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/pom.xml (added)
+++ maven/sandbox/maven-plugin-testing-tools/pom.xml Fri Nov 10 10:25:56 2006
@@ -0,0 +1,76 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  
+  <parent>
+    <groupId>org.apache.maven.shared</groupId>
+    <artifactId>maven-shared-components</artifactId>
+    <version>4-SNAPSHOT</version>
+  </parent>
+  
+  <artifactId>maven-plugin-testing-tools</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Maven Plugin Testing Tools</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact</artifactId>
+      <version>2.0.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact-manager</artifactId>
+      <version>2.0.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <version>2.0.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-model</artifactId>
+      <version>2.0.4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-repository-builder</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-invoker</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-test-tools</artifactId>
+      <version>1.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-maven-plugin</artifactId>
+        <version>1.3</version>
+        <executions>
+          <execution>
+            <id>create-component-descriptor</id>
+            <goals>
+              <goal>descriptor</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/sandbox/maven-plugin-testing-tools/pom.xml
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,184 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.shared.invoker.DefaultInvocationRequest;
+import org.apache.maven.shared.invoker.DefaultInvoker;
+import org.apache.maven.shared.invoker.InvocationOutputHandler;
+import org.apache.maven.shared.invoker.InvocationRequest;
+import org.apache.maven.shared.invoker.InvocationResult;
+import org.apache.maven.shared.invoker.Invoker;
+import org.apache.maven.shared.invoker.MavenInvocationException;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * @plexus.component role="org.apache.maven.shared.test.plugin.BuildTool" role-hint="default"
+ * @author jdcasey
+ *
+ */
+public class BuildTool
+    implements Initializable, Disposable
+{
+    public static final String ROLE = BuildTool.class.getName();
+    
+    private Invoker mavenInvoker;
+
+    protected InvocationResult executeMaven( File pom, Properties properties, List goals, File buildLogFile )
+        throws TestToolsException
+    {
+        InvocationRequest request = createBasicInvocationRequest( pom, properties, goals, buildLogFile );
+        
+        return executeMaven( request );
+    }
+    
+    public InvocationResult executeMaven( InvocationRequest request )
+    throws TestToolsException
+    {
+        try
+        {
+            return mavenInvoker.execute( request );
+        }
+        catch ( MavenInvocationException e )
+        {
+            throw new TestToolsException( "Error executing maven.", e );
+        }
+        finally
+        {
+            closeHandlers( request );
+        }
+    }
+
+    private void startInvoker()
+        throws IOException
+    {
+        if ( mavenInvoker == null )
+        {
+            mavenInvoker = new DefaultInvoker();
+            
+            if ( System.getProperty( "maven.home" ) == null )
+            {
+                Properties envars = CommandLineUtils.getSystemEnvVars();
+                
+                String mavenHome = envars.getProperty( "M2_HOME" );
+                
+                if ( mavenHome != null )
+                {
+                    mavenInvoker.setMavenHome( new File( mavenHome ) );
+                }
+            }
+        }
+    }
+
+    private void closeHandlers( InvocationRequest request )
+    {
+        InvocationOutputHandler outHandler = request.getOutputHandler( null );
+
+        if ( outHandler != null && ( outHandler instanceof LoggerHandler ) )
+        {
+            ( (LoggerHandler) outHandler ).close();
+        }
+
+        InvocationOutputHandler errHandler = request.getErrorHandler( null );
+
+        if ( errHandler != null && ( outHandler == null || errHandler != outHandler )
+            && ( errHandler instanceof LoggerHandler ) )
+        {
+            ( (LoggerHandler) errHandler ).close();
+        }
+    }
+
+    public InvocationRequest createBasicInvocationRequest( File pom, Properties properties, List goals,
+                                                            File buildLogFile )
+    {
+        InvocationRequest request = new DefaultInvocationRequest();
+
+        request.setPomFile( pom );
+
+        request.setGoals( goals );
+
+        request.setProperties( properties );
+
+        LoggerHandler handler = new LoggerHandler( buildLogFile );
+
+        request.setOutputHandler( handler );
+        request.setErrorHandler( handler );
+
+        return request;
+    }
+
+    private static final class LoggerHandler
+        implements InvocationOutputHandler
+    {
+        private static final String LS = System.getProperty( "line.separator" );
+
+        private final File output;
+
+        private FileWriter writer;
+
+        LoggerHandler( File logFile )
+        {
+            output = logFile;
+        }
+
+        public void consumeLine( String line )
+        {
+            if ( writer == null )
+            {
+                try
+                {
+                    writer = new FileWriter( output );
+                }
+                catch ( IOException e )
+                {
+                    throw new IllegalStateException( "Failed to open build log: " + output + "\n\nError: "
+                        + e.getMessage() );
+                }
+            }
+
+            try
+            {
+                writer.write( line + LS );
+                writer.flush();
+            }
+            catch ( IOException e )
+            {
+                throw new IllegalStateException( "Failed to write to build log: " + output + " output:\n\n\'" + line
+                    + "\'\n\nError: " + e.getMessage() );
+            }
+        }
+
+        void close()
+        {
+            IOUtil.close( writer );
+        }
+
+    }
+
+    public void initialize()
+        throws InitializationException
+    {
+        try
+        {
+            startInvoker();
+        }
+        catch ( IOException e )
+        {
+            throw new InitializationException( "Error detecting maven home.", e );
+        }
+        
+    }
+
+    public void dispose()
+    {
+        // TODO: When we switch to the embedder, use this to deallocate the MavenEmbedder, along 
+        // with the PlexusContainer and ClassRealm that it wraps.
+    }
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/BuildTool.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,68 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.project.MavenProject;
+
+import java.io.File;
+
+/**
+ * @plexus.component role="org.apache.maven.shared.test.plugin.PluginTestTool" role-hint="default"
+ * @author jdcasey
+ *
+ */
+public class PluginTestTool
+{
+    public static final String ROLE = PluginTestTool.class.getName();
+
+    /**
+     * @plexus.requirement role-hint="default"
+     */
+    private ProjectTool projectTool;
+
+    /**
+     * @plexus.requirement role-hint="default"
+     */
+    private RepositoryTool repositoryTool;
+
+    public File preparePluginForIntegrationTesting( String testVersion )
+        throws TestToolsException
+    {
+        return prepareForTesting( testVersion, false, null );
+    }
+
+    public File preparePluginForUnitTestingWithMavenBuilds( String testVersion )
+        throws TestToolsException
+    {
+        return prepareForTesting( testVersion, true, null );
+    }
+
+    public File preparePluginForIntegrationTesting( String testVersion, File localRepositoryDir )
+        throws TestToolsException
+    {
+        return prepareForTesting( testVersion, false, localRepositoryDir );
+    }
+
+    public File preparePluginForUnitTestingWithMavenBuilds( String testVersion, File localRepositoryDir )
+        throws TestToolsException
+    {
+        return prepareForTesting( testVersion, true, localRepositoryDir );
+    }
+
+    private File prepareForTesting( String testVersion, boolean skipUnitTests, File localRepositoryDir )
+        throws TestToolsException
+    {
+        File pomFile = new File( "pom.xml" );
+        File buildLog = new File( "target/test-build-logs/setup.build.log" );
+        File localRepoDir = localRepositoryDir;
+        
+        if ( localRepoDir == null )
+        {
+            localRepoDir = new File( "target/test-local-repository" );
+        }
+
+        MavenProject project = projectTool.packageProjectArtifact( pomFile, testVersion, skipUnitTests, buildLog );
+        repositoryTool.createLocalRepositoryFromPlugin( project, localRepoDir );
+
+        return localRepoDir;
+    }
+
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/PluginTestTool.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,282 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.model.Build;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.project.artifact.ProjectArtifactMetadata;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * @plexus.component role="org.apache.maven.shared.test.plugin.ProjectTool" role-hint="default"
+ * @author jdcasey
+ *
+ */
+public class ProjectTool
+{
+    public static final String ROLE = ProjectTool.class.getName();
+
+    /**
+     * @plexus.requirement role-hint="default"
+     */
+    private BuildTool buildTool;
+
+    /**
+     * @plexus.requirement role-hint="default"
+     */
+    private RepositoryTool repositoryTool;
+
+    /**
+     * @plexus.requirement
+     */
+    private MavenProjectBuilder projectBuilder;
+    
+    /**
+     * @plexus.requirement
+     */
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * @plexus.requirement
+     */
+    private ArtifactFactory artifactFactory;
+
+    public MavenProject readProject( File pomFile )
+        throws TestToolsException
+    {
+        return readProject( pomFile, repositoryTool.findLocalRepositoryDirectory() );
+    }
+
+    public MavenProject readProject( File pomFile, File localRepositoryBasedir )
+        throws TestToolsException
+    {
+        try
+        {
+            ArtifactRepository localRepository = repositoryTool
+                .createLocalArtifactRepositoryInstance( localRepositoryBasedir );
+
+            return projectBuilder.build( pomFile, localRepository, null );
+        }
+        catch ( ProjectBuildingException e )
+        {
+            throw new TestToolsException( "Error building MavenProject instance from test pom: " + pomFile, e );
+        }
+    }
+
+    public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests )
+        throws TestToolsException
+    {
+        return packageProjectArtifact( pomFile, testVersion, skipUnitTests, null );
+    }
+
+    public MavenProject packageProjectArtifact( File pomFile, String testVersion, boolean skipUnitTests, File logFile )
+        throws TestToolsException
+    {
+        PomInfo pomInfo = manglePomForTesting( pomFile, testVersion, skipUnitTests );
+
+        Properties properties = new Properties();
+
+        List goals = new ArrayList();
+        goals.add( "package" );
+
+        File buildLog = logFile == null ? pomInfo.getBuildLogFile() : logFile;
+
+        buildTool.executeMaven( pomInfo.getPomFile(), properties, goals, buildLog );
+
+        File artifactFile = new File( pomInfo.getBuildOutputDirectory() + "/" + pomInfo.getFinalName() );
+
+        try
+        {
+            MavenProject project = projectBuilder.build( pomInfo.getPomFile(), repositoryTool
+                .createLocalArtifactRepositoryInstance(), null );
+
+            Artifact artifact = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging() );
+            
+            artifact.setFile( artifactFile );
+            artifact.addMetadata( new ProjectArtifactMetadata( artifact, project.getFile() ) );
+            
+            project.setArtifact( artifact );
+
+            return project;
+        }
+        catch ( ProjectBuildingException e )
+        {
+            throw new TestToolsException(
+                                          "Error building MavenProject instance from test pom: " + pomInfo.getPomFile(),
+                                          e );
+        }
+    }
+
+    protected PomInfo manglePomForTesting( File pomFile, String testVersion, boolean skipUnitTests )
+        throws TestToolsException
+    {
+        File input = new File( "pom.xml" );
+
+        File output = new File( "pom-test.xml" );
+        output.deleteOnExit();
+
+        FileReader reader = null;
+        FileWriter writer = null;
+
+        Model model = null;
+        String finalName = null;
+        String buildOutputDirectory = null;
+
+        try
+        {
+            reader = new FileReader( input );
+            writer = new FileWriter( output );
+
+            model = new MavenXpp3Reader().read( reader );
+
+            model.setVersion( testVersion );
+
+            Build build = model.getBuild();
+            if ( build == null )
+            {
+                build = new Build();
+                model.setBuild( build );
+            }
+
+            finalName = build.getFinalName();
+            
+            if ( finalName == null )
+            {
+                ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( model.getPackaging() );
+                
+                String ext = handler.getExtension();
+                
+                finalName = model.getArtifactId() + "-" + model.getVersion() + "." + ext;
+            }
+            
+            buildOutputDirectory = build.getOutputDirectory();
+            
+            if ( buildOutputDirectory == null )
+            {
+                buildOutputDirectory = "target";
+            }
+
+            if ( skipUnitTests )
+            {
+                List plugins = build.getPlugins();
+                Plugin plugin = null;
+                for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
+                {
+                    Plugin plug = (Plugin) iter.next();
+
+                    if ( "maven-surefire-plugin".equals( plug.getArtifactId() ) )
+                    {
+                        plugin = plug;
+                        break;
+                    }
+                }
+
+                if ( plugin == null )
+                {
+                    plugin = new Plugin();
+                    plugin.setArtifactId( "maven-surefire-plugin" );
+                    build.addPlugin( plugin );
+                }
+
+                Xpp3Dom configDom = (Xpp3Dom) plugin.getConfiguration();
+                if ( configDom == null )
+                {
+                    configDom = new Xpp3Dom( "configuration" );
+                    plugin.setConfiguration( configDom );
+                }
+
+                Xpp3Dom skipDom = new Xpp3Dom( "skip" );
+                skipDom.setValue( "true" );
+
+                configDom.addChild( skipDom );
+            }
+
+            new MavenXpp3Writer().write( writer, model );
+        }
+        catch ( IOException e )
+        {
+            throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new TestToolsException( "Error creating test-time version of POM for: " + input, e );
+        }
+        finally
+        {
+            IOUtil.close( reader );
+            IOUtil.close( writer );
+        }
+
+        return new PomInfo( output, model.getGroupId(), model.getArtifactId(), model.getVersion(),
+                            buildOutputDirectory, finalName );
+    }
+
+    static final class PomInfo
+    {
+        private final File pomFile;
+
+        private final String groupId;
+
+        private final String artifactId;
+
+        private final String version;
+
+        private final String finalName;
+
+        private final String buildOutputDirectory;
+
+        PomInfo( File pomFile, String groupId, String artifactId, String version, String buildOutputDirectory,
+                 String finalName )
+        {
+            this.pomFile = pomFile;
+            this.groupId = groupId;
+            this.artifactId = artifactId;
+            this.version = version;
+            this.buildOutputDirectory = buildOutputDirectory;
+            this.finalName = finalName;
+        }
+
+        public File getPomFile()
+        {
+            return pomFile;
+        }
+
+        public String getBuildOutputDirectory()
+        {
+            return buildOutputDirectory;
+        }
+
+        public String getFinalName()
+        {
+            return finalName;
+        }
+
+        public File getBuildLogFile()
+        {
+            return new File( buildOutputDirectory + "/test-build-logs/" + groupId + "_" + artifactId + "_" + version
+                + ".build.log" );
+        }
+
+    }
+
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/ProjectTool.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,245 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.installer.ArtifactInstallationException;
+import org.apache.maven.artifact.installer.ArtifactInstaller;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Parent;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.artifact.ProjectArtifactMetadata;
+import org.apache.maven.settings.MavenSettingsBuilder;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.shared.repository.RepositoryAssembler;
+import org.apache.maven.shared.repository.RepositoryAssemblyException;
+import org.apache.maven.shared.repository.RepositoryBuilderConfigSource;
+import org.apache.maven.shared.repository.model.RepositoryInfo;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.codehaus.plexus.context.Context;
+import org.codehaus.plexus.context.ContextException;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+/**
+ * @plexus.component role="org.apache.maven.shared.test.plugin.RepositoryTool" role-hint="default"
+ * @author jdcasey
+ *
+ */
+public class RepositoryTool
+    implements Contextualizable
+{
+    public static final String ROLE = RepositoryTool.class.getName();
+
+    /**
+     * @plexus.requirement
+     */
+    private ArtifactRepositoryFactory repositoryFactory;
+
+    /**
+     * @plexus.requirement
+     */
+    private RepositoryAssembler repositoryAssembler;
+
+    /**
+     * @plexus.requirement
+     */
+    private MavenSettingsBuilder settingsBuilder;
+
+    /**
+     * @plexus.requirement
+     */
+    private ArtifactFactory artifactFactory;
+
+    /**
+     * @plexus.requirement
+     */
+    private ArtifactInstaller artifactInstaller;
+
+    // contextualized.
+    private PlexusContainer container;
+
+    public File findLocalRepositoryDirectory() throws TestToolsException
+    {
+        Settings settings;
+        try
+        {
+            settings = settingsBuilder.buildSettings();
+        }
+        catch ( IOException e )
+        {
+            throw new TestToolsException( "Error building Maven settings.", e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new TestToolsException( "Error building Maven settings.", e );
+        }
+
+        return new File( settings.getLocalRepository() );
+    }
+
+    public ArtifactRepository createLocalArtifactRepositoryInstance()
+        throws TestToolsException
+    {
+        File localRepoDir = findLocalRepositoryDirectory();
+        
+        return createLocalArtifactRepositoryInstance( localRepoDir );
+    }
+
+    public ArtifactRepository createLocalArtifactRepositoryInstance( File localRepositoryDirectory )
+        throws TestToolsException
+    {
+        ArtifactRepositoryLayout defaultLayout;
+        try
+        {
+            defaultLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, "default" );
+        }
+        catch ( ComponentLookupException e )
+        {
+            throw new TestToolsException( "Error retrieving default repository layout.", e );
+        }
+
+        try
+        {
+            return repositoryFactory.createArtifactRepository( "local", localRepositoryDirectory.toURL()
+                .toExternalForm(), defaultLayout, null, null );
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new TestToolsException( "Error converting local repo directory to a URL.", e );
+        }
+
+    }
+
+    public void buildRepository( File targetDirectory, RepositoryInfo repositoryInfo,
+                                 RepositoryBuilderConfigSource configSource )
+        throws RepositoryAssemblyException
+    {
+        repositoryAssembler.buildRemoteRepository( targetDirectory, repositoryInfo, configSource );
+    }
+
+    public void createLocalRepositoryFromPlugin( MavenProject pluginProject, File targetLocalRepoBasedir )
+        throws TestToolsException
+    {
+        Artifact artifact = pluginProject.getArtifact();
+        ArtifactRepository localRepository = createLocalArtifactRepositoryInstance( targetLocalRepoBasedir );
+
+        String localPath = localRepository.pathOf( artifact );
+
+        File destination = new File( localRepository.getBasedir(), localPath );
+        if ( !destination.getParentFile().exists() )
+        {
+            destination.getParentFile().mkdirs();
+        }
+
+        try
+        {
+            artifactInstaller.install( artifact.getFile(), artifact, localRepository );
+        }
+        catch ( ArtifactInstallationException e )
+        {
+            throw new TestToolsException( "Error installing plugin artifact to target local repository: "
+                + targetLocalRepoBasedir, e );
+        }
+
+        installLocallyReachableAncestorPoms( pluginProject.getFile(), localRepository );
+    }
+
+    private void installLocallyReachableAncestorPoms( File pomFile, ArtifactRepository localRepo )
+        throws TestToolsException
+    {
+        MavenXpp3Reader pomReader = new MavenXpp3Reader();
+
+        File pom = pomFile;
+
+        boolean firstPass = true;
+
+        while ( pom != null )
+        {
+
+            if ( !pom.exists() )
+            {
+                pom = null;
+                break;
+            }
+
+            String pomGroupId = null;
+            String pomArtifactId = null;
+            String pomVersion = null;
+
+            FileReader reader = null;
+
+            File currentPom = pom;
+
+            try
+            {
+                reader = new FileReader( pom );
+
+                Model model = pomReader.read( reader );
+
+                pomGroupId = model.getGroupId();
+                pomArtifactId = model.getArtifactId();
+                pomVersion = model.getVersion();
+
+                Parent parent = model.getParent();
+                if ( parent != null )
+                {
+                    pom = new File( pom.getParentFile(), parent.getRelativePath() );
+                }
+                else
+                {
+                    pom = null;
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new TestToolsException( "Error reading ancestor POM: " + currentPom, e );
+            }
+            catch ( XmlPullParserException e )
+            {
+                throw new TestToolsException( "Error reading ancestor POM: " + currentPom, e );
+            }
+            finally
+            {
+                IOUtil.close( reader );
+            }
+
+            if ( !firstPass )
+            {
+                Artifact pomArtifact = artifactFactory.createProjectArtifact( pomGroupId, pomArtifactId, pomVersion );
+                pomArtifact.addMetadata( new ProjectArtifactMetadata( pomArtifact, currentPom ) );
+
+                try
+                {
+                    artifactInstaller.install( currentPom, pomArtifact, localRepo );
+                }
+                catch ( ArtifactInstallationException e )
+                {
+                    throw new TestToolsException( "Error installing ancestor POM: " + currentPom + " to target local repository: " + localRepo.getBasedir(), e );
+                }
+            }
+            else
+            {
+                firstPass = false;
+            }
+        }
+    }
+
+    public void contextualize( Context context )
+        throws ContextException
+    {
+        this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
+    }
+
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/RepositoryTool.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,19 @@
+package org.apache.maven.shared.test.plugin;
+
+public class TestToolsException
+    extends Exception
+{
+
+    private static final long serialVersionUID = 1L;
+
+    public TestToolsException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    public TestToolsException( String message )
+    {
+        super( message );
+    }
+
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/main/java/org/apache/maven/shared/test/plugin/TestToolsException.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,72 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.metadata.ArtifactMetadata;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.artifact.ProjectArtifactMetadata;
+import org.apache.maven.shared.test.plugin.ProjectTool.PomInfo;
+import org.codehaus.plexus.PlexusTestCase;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.Iterator;
+
+public class ProjectToolTest
+    extends PlexusTestCase
+{
+    
+    public void testManglePomForTesting_ShouldPopulateOutDirAndFinalName() throws Exception
+    {
+        ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
+        
+        File pomFile = new File( "pom.xml" );
+        
+        PomInfo info = tool.manglePomForTesting( pomFile, "test", true );
+        
+        assertEquals( "target", info.getBuildOutputDirectory() );
+        assertEquals( "maven-plugin-testing-tools-test.jar", info.getFinalName() );
+    }
+
+    public void testPackageProjectArtifact_ShouldPopulateArtifactFileWithJarLocation() throws Exception
+    {
+        ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
+        
+        File pomFile = new File( "pom.xml" );
+        
+        MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
+        
+        String path = "target/maven-plugin-testing-tools-test.jar";
+
+        assertEquals( path, project.getArtifact().getFile().getPath() );
+    }
+    
+    public void testPackageProjectArtifact_ShouldPopulateWithCorrectArtifactAndMetadata() throws Exception
+    {
+        ProjectTool tool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
+        
+        File pomFile = new File( "pom.xml" );
+        
+        MavenProject project = tool.packageProjectArtifact( pomFile, "test", true );
+        
+        Artifact artifact = project.getArtifact();
+        
+        assertEquals( "jar", artifact.getType() );
+        assertTrue( artifact.getFile().exists() );
+        
+        Collection metadata = artifact.getMetadataList();
+        
+        boolean foundPomMetadata = false;
+        
+        for ( Iterator it = metadata.iterator(); it.hasNext(); )
+        {
+            ArtifactMetadata metadataItem = (ArtifactMetadata) it.next();
+            
+            if ( metadataItem instanceof ProjectArtifactMetadata )
+            {
+                foundPomMetadata = true;
+            }
+        }
+        
+        assertTrue( foundPomMetadata );
+    }
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/ProjectToolTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java?view=auto&rev=473412
==============================================================================
--- maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java (added)
+++ maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java Fri Nov 10 10:25:56 2006
@@ -0,0 +1,67 @@
+package org.apache.maven.shared.test.plugin;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.artifact.ProjectArtifactMetadata;
+import org.apache.maven.shared.tools.easymock.TestFileManager;
+import org.codehaus.plexus.PlexusTestCase;
+
+import java.io.File;
+
+public class RepositoryToolTest
+    extends PlexusTestCase
+{
+
+    private TestFileManager fileManager;
+
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        
+        fileManager = new TestFileManager( "RepositoryToolTest.", "" );
+    }
+
+    public void tearDown()
+        throws Exception
+    {
+        super.tearDown();
+        
+        fileManager.cleanUp();
+    }
+
+    public void testCreateLocalRepositoryFromPlugin_ShouldWriteJarAndPom()
+        throws Exception
+    {
+        RepositoryTool repoTool = (RepositoryTool) lookup( RepositoryTool.ROLE, "default" );
+        
+        File tempDir = fileManager.createTempDir();
+        
+        String pomContent = "<project><modelVersion>4.0.0</modelVersion></project>";
+        String jarContent = "This is a test";
+        
+        File pom = fileManager.createFile( tempDir, "pom.xml", pomContent );
+        File jar = fileManager.createFile( tempDir, "artifact-test.jar", jarContent );
+
+        MavenProject pluginProject = new MavenProject();
+        
+        ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+        
+        Artifact pluginArtifact = artifactFactory.createArtifact( "group", "artifact", "test", null, "jar" );
+        pluginArtifact.setFile( jar );
+        pluginArtifact.addMetadata( new ProjectArtifactMetadata( pluginArtifact, pom ) );
+        
+        pluginProject.setArtifact( pluginArtifact );
+        pluginProject.setFile( pom );
+        
+        File targetLocalRepoBasedir = fileManager.createTempDir();
+
+        repoTool.createLocalRepositoryFromPlugin( pluginProject, targetLocalRepoBasedir );
+        
+        fileManager.assertFileExistence( targetLocalRepoBasedir, "group/artifact/test/artifact-test.pom", true );
+        fileManager.assertFileContents( targetLocalRepoBasedir, "group/artifact/test/artifact-test.jar", jarContent );
+        
+    }
+
+}

Propchange: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/maven-plugin-testing-tools/src/test/java/org/apache/maven/shared/test/plugin/RepositoryToolTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



Re: code re-use (svn commit: r473412)

Posted by John Casey <ca...@gmail.com>.
I have every intention of documenting and releasing this for use in plugin
integration-testing. I'm not sure why we'd put this in the testing harness,
as it's a very different type of testing that this enables. As for mangling
the pom, I'm only doing at most two things:

1. changing the version to a known value, so you can make absolute reference
to a plugin-and-mojo when executing a build, or when referencing the plugin
in a test POM.

2. turning off surefire, for cases where the tests are actually run in the
unit-testing phase of the build (otherwise, it'd wind up in a recursive loop
when it tries to package and stage the plugin jar to the test
local-repository).

I only just got this code fully working today, so I haven't had the time to
write up documentation yet...but I will be documenting this, as I think it's
going to be a key part of my own plugin-testing strategy going forward. It
simplifies things that I'm already doing in the assembly plugin, and keeps
the local repository clean in the process.

I've got several new libraries in the sandbox, I know. Everything that I've
put out there - with the exception of this last library - is well-tested and
just needs some overview documentation. And, all of it is in use somewhere
in the assembly plugin, the Buckminster integration I'm working on, or the
eclipse plugin...so it's not like this stuff is of theoretical use.

-j

On 11/10/06, Brett Porter <br...@apache.org> wrote:
>
> John,
>
> There's a lot of stuff there. Can you write a doc for how to use it?
> I can't really grok what all the local repository shuffling etc is
> meant for. And mangling POMs for testing sounds like a bad thing :)
>
> Also, should it be part of the existing testing harness?
>
> A lot of this code also appears like it could land elsewhere (the
> invocation stuff in there seems fairly generic).
>
> We very seriously need to start consolidating and making our code
> accessible to other developers rather than churning out module after
> module into the sandbox, I think.
>
> - Brett
>
> On 11/11/2006, at 5:25 AM, jdcasey@apache.org wrote:
>
> > Author: jdcasey
> > Date: Fri Nov 10 10:25:56 2006
> > New Revision: 473412
> >
> > URL: http://svn.apache.org/viewvc?view=rev&rev=473412
> > Log:
> > Creating new project to stage plugin artifacts to a testing
> > localRepo, execute maven builds via the maven-invoker, and other
> > similar things used to test plugin projects.
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

Re: code re-use (svn commit: r473412)

Posted by Brett Porter <br...@apache.org>.
John,

There's a lot of stuff there. Can you write a doc for how to use it?  
I can't really grok what all the local repository shuffling etc is  
meant for. And mangling POMs for testing sounds like a bad thing :)

Also, should it be part of the existing testing harness?

A lot of this code also appears like it could land elsewhere (the  
invocation stuff in there seems fairly generic).

We very seriously need to start consolidating and making our code  
accessible to other developers rather than churning out module after  
module into the sandbox, I think.

- Brett

On 11/11/2006, at 5:25 AM, jdcasey@apache.org wrote:

> Author: jdcasey
> Date: Fri Nov 10 10:25:56 2006
> New Revision: 473412
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=473412
> Log:
> Creating new project to stage plugin artifacts to a testing  
> localRepo, execute maven builds via the maven-invoker, and other  
> similar things used to test plugin projects.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org