Skip to content

Commit

Permalink
Reverting BaseMojoTestCase to the version using ProjectBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Sep 13, 2022
1 parent a022659 commit 2b8f10d
Showing 1 changed file with 43 additions and 75 deletions.
118 changes: 43 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,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
Expand Down Expand Up @@ -53,11 +54,25 @@
*/
public abstract class BaseMojoTestCase extends AbstractMojoTestCase
{
private final DefaultMaven maven;
private final ProjectBuilder projectBuilder;

{
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.
*
* @param <T> target Mojo subclass
* @param goal to execute on the plugin
* @param pomFilePath path to the pom project to inject
* @return a Mojo instance
Expand All @@ -66,72 +81,25 @@ public abstract class BaseMojoTestCase extends AbstractMojoTestCase
@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 );
MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
mavenExecutionRequest.setPom( pomFile );
mavenExecutionRequest.setLocalRepository( new StubArtifactRepository( getBasedir() ) );
mavenExecutionRequest.setGoals( Collections.singletonList( goal ) );
mavenExecutionRequest.setInteractiveMode( false );

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() );
DefaultRepositorySystemSession repositorySystemSession =
(DefaultRepositorySystemSession) maven.newRepositorySession( mavenExecutionRequest );

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" );
}} );
MavenProject mavenProject = projectBuilder.build( pomFile,
mavenExecutionRequest.getProjectBuildingRequest().setRepositorySession( repositorySystemSession )
.setResolveDependencies( true ) ).getProject();

setCompileSourceRoots( Collections.singletonList( getBasedir() + "/src/main/java" ) );
setTestCompileSourceRoots( Collections.singletonList( getBasedir() + "/src/test/java" ) );
}

@Override
public File getBasedir()
{
return pomFile.getParentFile();
}

@Override
public Properties getProperties()
{
return getModel().getProperties();
}

@Override
public List<Dependency> getDependencies()
{
return getModel().getDependencies();
}

@Override
public DependencyManagement getDependencyManagement()
{
return getModel().getDependencyManagement();
}
return (T) lookupConfiguredMojo(
new MavenSession( getContainer(), mavenExecutionRequest, new DefaultMavenExecutionResult(),
mavenProject ), newMojoExecution( goal ) );
}
}

0 comments on commit 2b8f10d

Please sign in to comment.