From 3d9dea68d943ff46fb89309cd1c4425d51b4421e Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Tue, 21 Jul 2009 23:30:26 +0000 Subject: [PATCH] o using the same technique for the embedder as we are for the core tests o trying on the grid and then i'll share a test jar git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@796579 13f79535-47bb-0310-9956-ffa450edef68 --- .../AbstractCoreMavenComponentTestCase.java | 1 + .../AbstractCoreMavenComponentTestCase.java | 306 ++++++++++++++++++ .../MavenEmbedderAligningBasedirTest.java | 17 +- .../maven/embedder/MavenEmbedderTest.java | 57 ++-- .../AbstractEmbedderExecutionTestCase.java | 133 -------- ...bedderCrappySettingsConfigurationTest.java | 17 +- 6 files changed, 368 insertions(+), 163 deletions(-) create mode 100644 maven-embedder/src/test/java/org/apache/maven/embedder/AbstractCoreMavenComponentTestCase.java delete mode 100644 maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java diff --git a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index 545296a287..7aa9a8331b 100644 --- a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -87,6 +87,7 @@ public abstract class AbstractCoreMavenComponentTestCase MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setPom( pom ) .setProjectPresent( true ) + .setShowErrors( true ) .setPluginGroups( Arrays.asList( new String[] { "org.apache.maven.plugins" } ) ) .setLocalRepository( getLocalRepository() ) .setRemoteRepositories( getRemoteRepositories() ) diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractCoreMavenComponentTestCase.java b/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractCoreMavenComponentTestCase.java new file mode 100644 index 0000000000..628ada721b --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/AbstractCoreMavenComponentTestCase.java @@ -0,0 +1,306 @@ +package org.apache.maven.embedder; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.InvalidRepositoryException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Build; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Exclusion; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.Repository; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.plexus.ContainerConfiguration; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.util.FileUtils; + +public abstract class AbstractCoreMavenComponentTestCase + extends PlexusTestCase +{ + @Requirement + protected RepositorySystem repositorySystem; + + @Requirement + protected org.apache.maven.project.ProjectBuilder projectBuilder; + + protected void setUp() + throws Exception + { + repositorySystem = lookup( RepositorySystem.class ); + projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class ); + } + + @Override + protected void tearDown() + throws Exception + { + repositorySystem = null; + projectBuilder = null; + super.tearDown(); + } + + abstract protected String getProjectsDirectory(); + + protected File getProject( String name ) + throws Exception + { + File source = new File( new File( getBasedir(), getProjectsDirectory() ), name ); + File target = new File( new File( getBasedir(), "target" ), name ); + if ( !target.exists() ) + { + FileUtils.copyDirectoryStructure( source, target ); + } + return new File( target, "pom.xml" ); + } + + /** + * We need to customize the standard Plexus container with the plugin discovery listener which + * is what looks for the META-INF/maven/plugin.xml resources that enter the system when a Maven + * plugin is loaded. + * + * We also need to customize the Plexus container with a standard plugin discovery listener + * which is the MavenPluginCollector. When a Maven plugin is discovered the MavenPluginCollector + * collects the plugin descriptors which are found. + */ + protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) + { +// containerConfiguration.addComponentDiscoverer( PluginManager.class ); +// containerConfiguration.addComponentDiscoveryListener( PluginManager.class ); + } + + protected MavenExecutionRequest createMavenExecutionRequest( File pom ) + throws Exception + { + MavenExecutionRequest request = new DefaultMavenExecutionRequest() + .setPom( pom ) + .setProjectPresent( true ) + .setShowErrors( true ) + .setPluginGroups( Arrays.asList( new String[] { "org.apache.maven.plugins" } ) ) + .setLocalRepository( getLocalRepository() ) + .setRemoteRepositories( getRemoteRepositories() ) + .setPluginArtifactRepositories( getPluginArtifactRepositories() ) + .setGoals( Arrays.asList( new String[] { "package" } ) ) + .setProperties( new Properties() ); + + return request; + } + + // layer the creation of a project builder configuration with a request, but this will need to be + // a Maven subclass because we don't want to couple maven to the project builder which we need to + // separate. + protected MavenSession createMavenSession( File pom ) + throws Exception + { + return createMavenSession( pom, new Properties() ); + } + + protected MavenSession createMavenSession( File pom, Properties executionProperties ) + throws Exception + { + MavenExecutionRequest request = createMavenExecutionRequest( pom ); + + ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest() + .setLocalRepository( request.getLocalRepository() ) + .setRemoteRepositories( request.getRemoteRepositories() ) + .setPluginArtifactRepositories( request.getPluginArtifactRepositories() ) + .setExecutionProperties( executionProperties ); + + MavenProject project = null; + + if ( pom != null ) + { + project = projectBuilder.build( pom, configuration ); + } + else + { + project = createStubMavenProject(); + } + + MavenSession session = new MavenSession( getContainer(), request, new DefaultMavenExecutionResult(), project ); + + return session; + } + + protected MavenProject createStubMavenProject() + { + Model model = new Model(); + model.setGroupId( "org.apache.maven.test" ); + model.setArtifactId( "maven-test" ); + model.setVersion( "1.0" ); + return new MavenProject( model ); + } + + protected List getRemoteRepositories() + throws InvalidRepositoryException + { + return Arrays.asList( repositorySystem.createDefaultRemoteRepository() ); + } + + protected List getPluginArtifactRepositories() + throws InvalidRepositoryException + { + Repository itRepo = new Repository(); + itRepo.setId( "maven.it" ); + itRepo.setUrl( "http://repository.sonatype.org/content/repositories/maven.snapshots" ); + return Arrays.asList( repositorySystem.createDefaultRemoteRepository(), repositorySystem.buildArtifactRepository( itRepo ) ); + } + + protected ArtifactRepository getLocalRepository() + throws InvalidRepositoryException + { + return repositorySystem.createDefaultLocalRepository(); + } + + protected class ProjectBuilder + { + private MavenProject project; + + public ProjectBuilder( String groupId, String artifactId, String version ) + { + Model model = new Model(); + model.setModelVersion( "4.0.0" ); + model.setGroupId( groupId ); + model.setArtifactId( artifactId ); + model.setVersion( version ); + model.setBuild( new Build() ); + project = new MavenProject( model ); + } + + public ProjectBuilder setGroupId( String groupId ) + { + project.setGroupId( groupId ); + return this; + } + + public ProjectBuilder setArtifactId( String artifactId ) + { + project.setArtifactId( artifactId ); + return this; + } + + public ProjectBuilder setVersion( String version ) + { + project.setVersion( version ); + return this; + } + + // Dependencies + // + public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope ) + { + return addDependency( groupId, artifactId, version, scope, (Exclusion)null ); + } + + public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion ) + { + return addDependency( groupId, artifactId, version, scope, null, exclusion ); + } + + public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath ) + { + return addDependency( groupId, artifactId, version, scope, systemPath, null ); + } + + public ProjectBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion ) + { + Dependency d = new Dependency(); + d.setGroupId( groupId ); + d.setArtifactId( artifactId ); + d.setVersion( version ); + d.setScope( scope ); + + if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) ) + { + d.setSystemPath( systemPath ); + } + + if ( exclusion != null ) + { + d.addExclusion( exclusion ); + } + + project.getDependencies().add( d ); + + return this; + } + + // Plugins + // + public ProjectBuilder addPlugin( Plugin plugin ) + { + project.getBuildPlugins().add( plugin ); + return this; + } + + public MavenProject get() + { + return project; + } + } + + protected class PluginBuilder + { + private Plugin plugin; + + public PluginBuilder( String groupId, String artifactId, String version ) + { + plugin = new Plugin(); + plugin.setGroupId( groupId ); + plugin.setArtifactId( artifactId ); + plugin.setVersion( version ); + } + + // Dependencies + // + public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, Exclusion exclusion ) + { + return addDependency( groupId, artifactId, version, scope, exclusion ); + } + + public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath ) + { + return addDependency( groupId, artifactId, version, scope, systemPath, null ); + } + + public PluginBuilder addDependency( String groupId, String artifactId, String version, String scope, String systemPath, Exclusion exclusion ) + { + Dependency d = new Dependency(); + d.setGroupId( groupId ); + d.setArtifactId( artifactId ); + d.setVersion( version ); + d.setScope( scope ); + + if ( systemPath != null && scope.equals( Artifact.SCOPE_SYSTEM ) ) + { + d.setSystemPath( systemPath ); + } + + if ( exclusion != null ) + { + d.addExclusion( exclusion ); + } + + plugin.getDependencies().add( d ); + + return this; + } + + public Plugin get() + { + return plugin; + } + } +} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java index 0a57461565..2914039656 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderAligningBasedirTest.java @@ -20,19 +20,15 @@ package org.apache.maven.embedder; */ import java.io.File; -import java.util.Arrays; import java.util.List; -import junit.framework.TestCase; - -import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.FileUtils; public class MavenEmbedderAligningBasedirTest - extends TestCase + extends AbstractCoreMavenComponentTestCase { protected String basedir; @@ -93,10 +89,14 @@ public class MavenEmbedderAligningBasedirTest FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); + MavenExecutionRequest request = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setBaseDirectory( targetDirectory ) .setShowErrors( true ) .setGoals( Arrays.asList( new String[]{"package"} ) ); + */ MavenExecutionResult result = mavenEmbedder.execute( request ); @@ -110,4 +110,11 @@ public class MavenEmbedderAligningBasedirTest assertTrue( jar.exists() ); } + + @Override + protected String getProjectsDirectory() + { + // TODO Auto-generated method stub + return null; + } } diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java index 7f2fde8962..7d89fa8fe8 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -26,8 +26,6 @@ import java.io.Writer; import java.util.Arrays; import java.util.List; -import junit.framework.TestCase; - import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; @@ -49,7 +47,7 @@ import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; public class MavenEmbedderTest - extends TestCase + extends AbstractCoreMavenComponentTestCase { protected String basedir; @@ -110,10 +108,14 @@ public class MavenEmbedderTest FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); + MavenExecutionRequest request = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setBaseDirectory( targetDirectory ) .setShowErrors( true ) .setGoals( Arrays.asList( new String[]{"package"} ) ); + */ MavenExecutionResult result = mavenEmbedder.execute( request ); @@ -137,10 +139,14 @@ public class MavenEmbedderTest FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); + MavenExecutionRequest request = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setBaseDirectory( targetDirectory ) .setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "install" } ) ); + */ MavenExecutionResult result = mavenEmbedder.execute( request ); @@ -186,9 +192,13 @@ public class MavenEmbedderTest FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); + MavenExecutionRequest request = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setPom( new File( targetDirectory, "pom.xml" ) ).setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "package" } ) ); + */ MavenExecutionResult result = mavenEmbedder.execute( request ); @@ -214,10 +224,14 @@ public class MavenEmbedderTest // Check with profile not active + MavenExecutionRequest requestWithoutProfile = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest requestWithoutProfile = new DefaultMavenExecutionRequest() .setPom( new File( targetDirectory, "pom.xml" ) ) .setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "validate" } ) ); + */ MavenExecutionResult r0 = mavenEmbedder.execute( requestWithoutProfile ); @@ -233,11 +247,16 @@ public class MavenEmbedderTest // Check with profile activated + MavenExecutionRequest request = createMavenExecutionRequest( new File( targetDirectory, "pom.xml" ) ); + request.addActiveProfile( "embedderProfile" ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setPom( new File( targetDirectory, "pom.xml" ) ) .setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "validate" } ) ) .addActiveProfile( "embedderProfile" ); + */ MavenExecutionResult r1 = mavenEmbedder.execute( request ); @@ -282,9 +301,12 @@ public class MavenEmbedderTest mavenEmbedder.writeModel( writer, model ); writer.close(); - /* execute maven */ + MavenExecutionRequest request = createMavenExecutionRequest( pom ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest().setPom( pom ).setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "package" } ) ); + */ MavenExecutionResult result = mavenEmbedder.execute( request ); @@ -301,9 +323,13 @@ public class MavenEmbedderTest mavenEmbedder.writeModel( writer, model ); writer.close(); - /* execute Maven */ + request = createMavenExecutionRequest( pom ); + + /* request = new DefaultMavenExecutionRequest().setPom( pom ).setShowErrors( true ) .setGoals( Arrays.asList( new String[] { "package" } ) ); + */ + result = mavenEmbedder.execute( request ); assertNoExceptions( result ); @@ -314,22 +340,6 @@ public class MavenEmbedderTest assertEquals( "2.4.3", p.getVersion() ); } - // ---------------------------------------------------------------------- - // Repository - // ---------------------------------------------------------------------- - - /* - public void testLocalRepositoryRetrieval() - throws Exception - { - assertNotNull( mavenEmbedder.getLocalRepository().getBasedir() ); - } - */ - - // ---------------------------------------------------------------------- - // Model Reading - // ---------------------------------------------------------------------- - public void testModelReading() throws Exception { @@ -520,4 +530,9 @@ public class MavenEmbedderTest return new File( basedir, "src/test/resources/" + name ); } + @Override + protected String getProjectsDirectory() + { + return null; + } } diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java b/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java deleted file mode 100644 index 7dbe845adb..0000000000 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/execution/AbstractEmbedderExecutionTestCase.java +++ /dev/null @@ -1,133 +0,0 @@ -package org.apache.maven.embedder.execution; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.File; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; - -import org.apache.maven.embedder.AbstractEmbedderTestCase; -import org.apache.maven.execution.DefaultMavenExecutionRequest; -import org.apache.maven.execution.MavenExecutionRequest; -import org.apache.maven.execution.MavenExecutionResult; -import org.codehaus.plexus.util.FileUtils; - -public abstract class AbstractEmbedderExecutionTestCase - extends AbstractEmbedderTestCase -{ - protected File runWithProject( String goal ) - throws Exception - { - return runWithProject( Collections.singletonList( goal ), null ); - } - - protected File runWithProject( String goal, - Properties properties ) - throws Exception - { - return runWithProject( Collections.singletonList( goal ), properties ); - } - - protected File runWithProject( String[] goals ) - throws Exception - { - return runWithProject( Arrays.asList( goals ), null ); - } - - protected File runWithProject( String[] goals, - Properties properties ) - throws Exception - { - return runWithProject( Arrays.asList( goals ), properties ); - } - - protected File runWithProject( List goals ) - throws Exception - { - return runWithProject( goals, null ); - } - - protected File runWithProject( List goals, - Properties properties ) - throws Exception - { - /* - if ( request.getBaseDirectory() == null || !new File( request.getBaseDirectory() ).exists() ) - { - throw new IllegalStateException( "You must specify a valid base directory in your execution request for this test." ); - } - */ - - File testDirectory = new File( getBasedir(), "src/test/embedder-test-project" ); - - File targetDirectory = new File( getBasedir(), "target/" + getId() ); - - FileUtils.copyDirectoryStructure( testDirectory, targetDirectory ); - - MavenExecutionRequest request = new DefaultMavenExecutionRequest() - .setShowErrors( true ) - //.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_DEBUG ) - .setBaseDirectory( targetDirectory ) - .setGoals( goals ); - - System.out.println( "properties = " + properties ); - - if ( properties != null ) - { - request.setProperties( properties ); - } - - MavenExecutionResult result = maven.execute( request ); - - assertNoExceptions( result ); - - return targetDirectory; - } - - protected abstract String getId(); - - protected void assertNoExceptions( MavenExecutionResult result ) - { - if ( !result.hasExceptions() ) - { - return; - } - - for ( Iterator i = result.getExceptions().iterator(); i.hasNext(); ) - { - Exception exception = (Exception) i.next(); - - exception.printStackTrace( System.err ); - } - - fail( "Encountered Exceptions in MavenExecutionResult during " + getName() ); - } - - protected void assertFileExists( File file ) - { - if ( !file.exists() ) - { - fail( "The specified file '" + file + "' does not exist." ); - } - } -} diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java index 527e98683f..b572078c7a 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/validation/MavenEmbedderCrappySettingsConfigurationTest.java @@ -20,20 +20,18 @@ package org.apache.maven.embedder.validation; */ import java.io.File; -import java.util.Arrays; +import org.apache.maven.embedder.AbstractCoreMavenComponentTestCase; import org.apache.maven.embedder.Configuration; import org.apache.maven.embedder.ConfigurationValidationResult; import org.apache.maven.embedder.MavenEmbedder; import org.apache.maven.embedder.SimpleConfiguration; -import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.PlexusTestCase; public class MavenEmbedderCrappySettingsConfigurationTest - extends PlexusTestCase + extends AbstractCoreMavenComponentTestCase { public void testEmbedderWillStillStartupWhenTheSettingsConfigurationIsCrap() throws Exception @@ -55,9 +53,13 @@ public class MavenEmbedderCrappySettingsConfigurationTest //assertNotNull( embedder.getLocalRepository().getBasedir() ); + MavenExecutionRequest request = createMavenExecutionRequest( new File( projectDirectory, "pom.xml" ) ); + + /* MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setBaseDirectory( projectDirectory ) .setGoals( Arrays.asList( new String[]{"validate"} ) ); + */ MavenExecutionResult result = embedder.execute( request ); @@ -78,4 +80,11 @@ public class MavenEmbedderCrappySettingsConfigurationTest // END SNIPPET: simple-embedder-example } + + @Override + protected String getProjectsDirectory() + { + // TODO Auto-generated method stub + return null; + } }