diff --git a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java index fb674768a8..aac28240d5 100644 --- a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java @@ -95,7 +95,7 @@ public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureExcepti @Test public void testVersionlessDependency() throws Exception { - SetMojo myMojo = createMojo( "set", "src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml" ); + SetMojo myMojo = createMojo( "set", "org/codehaus/mojo/set/versionless-01/pom.xml" ); myMojo.execute(); } } diff --git a/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java b/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java index b3aecf2e2b..ada201ae0e 100644 --- a/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java @@ -39,7 +39,7 @@ public void testNullNewVersion() throws Exception { SetPropertyMojo mojo = createMojo( "set-property", - "src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml" ); + "org/codehaus/mojo/set-property/null-new-version-pom.xml" ); assertThat( mojo.getProject().getProperties(), is( mojo.getProject().getModel().getProperties() ) ); try { @@ -58,7 +58,7 @@ public void testNullProperty() throws Exception { SetPropertyMojo mojo = createMojo( "set-property", - "src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml" ); + "org/codehaus/mojo/set-property/null-property-pom.xml" ); try { mojo.execute(); diff --git a/src/test/java/org/codehaus/mojo/versions/UseDepVersionMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseDepVersionMojoTest.java index be965fd487..353d8be3d6 100644 --- a/src/test/java/org/codehaus/mojo/versions/UseDepVersionMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UseDepVersionMojoTest.java @@ -35,7 +35,7 @@ public class UseDepVersionMojoTest extends BaseMojoTestCase public void testIssue673() throws Exception { UseDepVersionMojo mojo = createMojo( "use-dep-version", - "src/test/resources/org/codehaus/mojo/use-dep-version/issue-637-pom.xml" ); + "org/codehaus/mojo/use-dep-version/issue-637-pom.xml" ); setVariableValueToObject( mojo, "processDependencies", true ); setVariableValueToObject( mojo, "processDependencyManagement", true ); setVariableValueToObject( mojo, "excludeReactor", true ); diff --git a/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java b/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java index 4d499afd14..8fb43e62a7 100644 --- a/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java +++ b/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java @@ -1,21 +1,29 @@ package org.codehaus.mojo.versions.utils; import java.io.File; -import java.io.IOException; +import java.net.URL; import java.util.Collections; -import java.util.List; -import java.util.Properties; -import org.apache.maven.model.Build; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.DependencyManagement; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.DefaultMaven; +import org.apache.maven.Maven; +import org.apache.maven.artifact.repository.DefaultArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequestPopulator; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.Mojo; import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; -import org.codehaus.plexus.util.ReaderFactory; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.repository.RepositorySystem; +import org.apache.maven.settings.MavenSettingsBuilder; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -51,8 +59,39 @@ * * @author Andrzej Jarmoniuk */ +@SuppressWarnings( "deprecation" ) public abstract class BaseMojoTestCase extends AbstractMojoTestCase { + private final DefaultMaven maven; + private final MavenSettingsBuilder mavenSettingsBuilder; + private final MavenExecutionRequestPopulator mavenExecutionRequestPopulator; + private final ProjectBuilder projectBuilder; + private static final DefaultArtifactRepository ARTIFACT_REPOSITORY; + private static final LocalRepository LOCAL_REPOSITORY; + + static + { + ARTIFACT_REPOSITORY = new DefaultArtifactRepository( "id", + RepositorySystem.defaultUserLocalRepository.getAbsolutePath(), + new DefaultRepositoryLayout() ); + LOCAL_REPOSITORY = + new LocalRepository( RepositorySystem.defaultUserLocalRepository.getAbsolutePath() ); + } + + { + try + { + maven = (DefaultMaven) getContainer().lookup( Maven.class ); + mavenSettingsBuilder = (MavenSettingsBuilder) getContainer().lookup( MavenSettingsBuilder.ROLE ); + mavenExecutionRequestPopulator = getContainer().lookup( MavenExecutionRequestPopulator.class ); + projectBuilder = lookup( ProjectBuilder.class ); + } + catch ( ComponentLookupException e ) + { + throw new RuntimeException( e ); + } + } + /** * Lookup the mojo leveraging the actual subprojects pom * and injects the project using the given pom file path. @@ -62,75 +101,33 @@ public abstract class BaseMojoTestCase extends AbstractMojoTestCase * @return a Mojo instance * @throws Exception thrown if mojo lookup fails */ - @SuppressWarnings( "unchecked" ) protected T createMojo( String goal, String pomFilePath ) throws Exception { - File pomFile = new File( pomFilePath ); - T mojo = (T) lookupMojo( goal, pomFile ); - setVariableValueToObject( mojo, "project", new TestProjectStub( pomFile ) ); - return mojo; - } - - private static class TestProjectStub extends MavenProjectStub - { - private final File pomFile; + URL pomResource = getClass().getClassLoader().getResource( pomFilePath ); + assert pomResource != null; + File pomFile = new File( pomResource.getFile() ); - /** - * Default constructor - */ - private TestProjectStub( File pomFile ) throws IOException, XmlPullParserException - { - this.pomFile = pomFile; - MavenXpp3Reader pomReader = new MavenXpp3Reader(); - Model model = pomReader.read( ReaderFactory.newXmlReader( pomFile ) ); - model.setPomFile( pomFile ); - - setModel( model ); - setOriginalModel( model.clone() ); - setGroupId( model.getGroupId() ); - setArtifactId( model.getArtifactId() ); - setVersion( model.getVersion() ); - setName( model.getName() ); - setUrl( model.getUrl() ); - setPackaging( model.getPackaging() ); - setFile( model.getPomFile() ); + MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); + mavenExecutionRequest.setPom( pomFile ); + mavenExecutionRequest.setLocalRepository( ARTIFACT_REPOSITORY ); + mavenExecutionRequest.setGoals( Collections.singletonList( goal ) ); - setBuild( new Build() - {{ - setFinalName( model.getArtifactId() ); - setDirectory( getBasedir() + "/target" ); - setSourceDirectory( getBasedir() + "/src/main/java" ); - setOutputDirectory( getBasedir() + "/target/classes" ); - setTestSourceDirectory( getBasedir() + "/src/test/java" ); - setTestOutputDirectory( getBasedir() + "/target/test-classes" ); - }} ); - - setCompileSourceRoots( Collections.singletonList( getBasedir() + "/src/main/java" ) ); - setTestCompileSourceRoots( Collections.singletonList( getBasedir() + "/src/test/java" ) ); - } + Settings settings = mavenSettingsBuilder.buildSettings( mavenExecutionRequest ); + settings.setInteractiveMode( false ); + mavenExecutionRequestPopulator.populateFromSettings( mavenExecutionRequest, settings ); + DefaultRepositorySystemSession repositorySystemSession = + (DefaultRepositorySystemSession) maven.newRepositorySession( mavenExecutionRequest ); + repositorySystemSession.setLocalRepositoryManager( + new SimpleLocalRepositoryManagerFactory().newInstance( repositorySystemSession, LOCAL_REPOSITORY ) ); - @Override - public File getBasedir() - { - return pomFile.getParentFile(); - } + MavenProject mavenProject = projectBuilder.build( pomFile, + mavenExecutionRequest.getProjectBuildingRequest().setRepositorySession( repositorySystemSession ) + .setResolveDependencies( true ) ).getProject(); - @Override - public Properties getProperties() - { - return getModel().getProperties(); - } - - @Override - public List getDependencies() - { - return getModel().getDependencies(); - } - - @Override - public DependencyManagement getDependencyManagement() - { - return getModel().getDependencyManagement(); - } + @SuppressWarnings( "unchecked" ) + T mojo = (T) lookupConfiguredMojo( + new MavenSession( getContainer(), mavenExecutionRequest, new DefaultMavenExecutionResult(), + mavenProject ), newMojoExecution( goal ) ); + return mojo; } } diff --git a/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml b/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml index 94e1f8d097..0d61aeb68b 100644 --- a/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml +++ b/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml @@ -21,6 +21,7 @@ + org.codehaus.mojo versions-maven-plugin dummy-api-version diff --git a/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml b/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml index b429aa3b7b..f29325c65b 100644 --- a/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml +++ b/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml @@ -21,6 +21,7 @@ + org.codehaus.mojo versions-maven-plugin 2.0.0 diff --git a/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml b/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml index 22956c5178..ff646d28bd 100644 --- a/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml +++ b/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml @@ -26,9 +26,12 @@ + org.codehaus.mojo versions-maven-plugin + + set + - onchange dummy-api 2.0 diff --git a/src/test/resources/org/codehaus/mojo/use-dep-version/issue-637-pom.xml b/src/test/resources/org/codehaus/mojo/use-dep-version/issue-637-pom.xml index 5f2edfe99f..86a1af95e4 100644 --- a/src/test/resources/org/codehaus/mojo/use-dep-version/issue-637-pom.xml +++ b/src/test/resources/org/codehaus/mojo/use-dep-version/issue-637-pom.xml @@ -17,6 +17,7 @@ + org.codehaus.mojo versions-maven-plugin DEVELOP-SNAPSHOT