Skip to content

Commit

Permalink
Fixed mojohaus#676: implementing default parameter resolution. Switch…
Browse files Browse the repository at this point in the history
…ing to using the class loader to resolve test files.
  • Loading branch information
jarmoniuk committed Sep 10, 2022
1 parent ff7f209 commit be181f9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/test/java/org/codehaus/mojo/versions/SetMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
147 changes: 72 additions & 75 deletions src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,8 +59,39 @@
* </pre>
* @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.
Expand All @@ -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 extends Mojo> 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<Dependency> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<property>dummy-api-version</property>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<newVersion>2.0.0</newVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<updateBuildOutputTimestampPolicy>onchange</updateBuildOutputTimestampPolicy>
<artifactId>dummy-api</artifactId>
<newVersion>2.0</newVersion>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<depVersion>DEVELOP-SNAPSHOT</depVersion>
Expand Down

0 comments on commit be181f9

Please sign in to comment.