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..c417c773a0 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,22 @@ 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.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenExecutionRequest; +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.plugin.testing.stubs.StubArtifactRepository; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -53,6 +54,25 @@ */ public abstract class BaseMojoTestCase extends AbstractMojoTestCase { + private final DefaultMaven maven; + private final ProjectBuilder projectBuilder; + + static + { + } + + { + try + { + maven = (DefaultMaven) getContainer().lookup( Maven.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. @@ -65,72 +85,25 @@ public abstract class BaseMojoTestCase extends AbstractMojoTestCase @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() ); - - 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" ) ); - } - - @Override - public File getBasedir() - { - return pomFile.getParentFile(); - } + MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); + mavenExecutionRequest.setPom( pomFile ); + mavenExecutionRequest.setLocalRepository( new StubArtifactRepository( getBasedir() ) ); + mavenExecutionRequest.setGoals( Collections.singletonList( goal ) ); + mavenExecutionRequest.setInteractiveMode( false ); - @Override - public Properties getProperties() - { - return getModel().getProperties(); - } + DefaultRepositorySystemSession repositorySystemSession = + (DefaultRepositorySystemSession) maven.newRepositorySession( mavenExecutionRequest ); - @Override - public List getDependencies() - { - return getModel().getDependencies(); - } + MavenProject mavenProject = projectBuilder.build( pomFile, + mavenExecutionRequest.getProjectBuildingRequest().setRepositorySession( repositorySystemSession ) + .setResolveDependencies( true ) ).getProject(); - @Override - public DependencyManagement getDependencyManagement() - { - return getModel().getDependencyManagement(); - } + return (T) lookupConfiguredMojo( + new MavenSession( getContainer(), mavenExecutionRequest, new DefaultMavenExecutionResult(), + mavenProject ), newMojoExecution( goal ) ); } } 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