Skip to content

Commit

Permalink
PR #45 - update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Jun 28, 2021
1 parent 2d2c093 commit ccf0f27
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,19 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo {
* The entry point to Aether, i.e. the component doing all the work.
*/
@Component
private RepositorySystem repoSystem;
protected RepositorySystem repoSystem;

/**
* The current repository/network configuration of Maven.
*/
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
private RepositorySystemSession repoSession;
protected RepositorySystemSession repoSession;

/**
* The project's remote repositories to use for the resolution of plugins and their dependencies.
*/
@Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
private List<RemoteRepository> remoteRepos;
protected List<RemoteRepository> remoteRepos;

/**
* List of artifacts on which perform sources scanning
Expand Down Expand Up @@ -1021,7 +1021,7 @@ private Optional<List<String>> resolveProcessorPathEntries() throws MojoExecutio
new ArrayList<>(requiredDependencies), this.remoteRepos);
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);

DependencyResult resolutionResult = this.repoSystem.resolveDependencies(this.repoSession,
DependencyResult resolutionResult = this.repoSystem.resolveDependencies( this.repoSession,
dependencyRequest);

List<String> artifactPaths = new ArrayList<>(resolutionResult.getArtifactResults().size());
Expand All @@ -1036,7 +1036,12 @@ private Optional<List<String>> resolveProcessorPathEntries() throws MojoExecutio
}
}

private Optional<String> buildProcessorPath() throws MojoExecutionException {
/**
*
* @return
* @throws MojoExecutionException
*/
Optional<String> buildProcessorPath() throws MojoExecutionException {
Optional<List<String>> processorPathEntries = this.resolveProcessorPathEntries();
return processorPathEntries.map(value -> StringUtils.join(value.iterator(), File.pathSeparator));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package org.bsc.maven.plugin.processor;

import org.apache.maven.plugin.AbstractMojo;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.resources.TestResources;
import org.junit.Assert;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.*;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static java.lang.String.format;
import static org.junit.Assert.*;

public class AnnotationProcessorMojoTest {

//final Path localRepoDir = Paths.get( System.getProperty("user.home"), ".m2", "repository");
final Path localRepoDir = Paths.get( "src", "test", "resources", "localRepo");

@Rule
public MojoRule mojoRule = new MojoRule();

Expand All @@ -24,22 +35,174 @@ public class AnnotationProcessorMojoTest {

final Path outputPath = Paths.get("target", "classes");

/**
*
* @param baseDir
* @param goal
* @return
*
* @ref https://stackoverflow.com/a/42216471/521197
*/
<T extends Mojo> T lookupConfiguredMojo( Path baseDir, String goal ) throws Exception {

final Path localRepoDir = Paths.get( System.getProperty("user.home"), ".m2", "repository");

final MavenProject project = mojoRule.readMavenProject(baseDir.toFile());

// Generate session
final MavenSession session = mojoRule.newMavenSession(project);

// add localRepo - framework doesn't do this on its own
final org.apache.maven.artifact.repository.ArtifactRepository localRepo =
new org.apache.maven.artifact.repository.MavenArtifactRepository("local",
localRepoDir.toString(),
new org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout(),
new org.apache.maven.artifact.repository.ArtifactRepositoryPolicy( true,
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ),
new org.apache.maven.artifact.repository.ArtifactRepositoryPolicy( true,
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE )

);

session.getRequest().setLocalRepository(localRepo);

// Generate Execution and Mojo for testing
final MojoExecution execution = mojoRule.newMojoExecution("process");

return (T)mojoRule.lookupConfiguredMojo(session, execution);

}

Path artifactPath( String artifact ) {
return Paths.get( localRepoDir.toString(), artifact);
}

/**
*
*/
class TestLocalRepositoryManager implements org.eclipse.aether.repository.LocalRepositoryManager {

final org.eclipse.aether.repository.LocalRepository localRepo;


public TestLocalRepositoryManager() {
localRepo = new org.eclipse.aether.repository.LocalRepository(localRepoDir.toString());
}

@Override
public LocalRepository getRepository() {
return localRepo;
}

@Override
public String getPathForLocalArtifact(Artifact artifact) {
return localRepoDir.toString();
}

@Override
public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository remoteRepository, String s) {
throw new UnsupportedOperationException( format("getPathForRemoteArtifact(%s)", artifact));
}

@Override
public String getPathForLocalMetadata(Metadata metadata) {
throw new UnsupportedOperationException( format("getPathForLocalMetadata(%s)", metadata));
}

@Override
public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository remoteRepository, String s) {
throw new UnsupportedOperationException( format("getPathForRemoteMetadata(%s)", metadata));
}

@Override
public LocalArtifactResult find(RepositorySystemSession repositorySystemSession, LocalArtifactRequest localArtifactRequest) {

final Artifact artifact = localArtifactRequest.getArtifact();

final LocalArtifactResult result = new LocalArtifactResult(localArtifactRequest);
result.setAvailable(true);

if( "jar".equals(artifact.getExtension()) ) {

final File file = artifactPath(
format("%s-%s.%s", artifact.getArtifactId(), artifact.getVersion(), artifact.getExtension()))
.toFile();

assertTrue(file.exists());

result.setFile(file);
}

return result;
}

@Override
public LocalMetadataResult find(RepositorySystemSession repositorySystemSession, LocalMetadataRequest localMetadataRequest) {
throw new UnsupportedOperationException(format("find(RepositorySystemSession,LocalMetadataRequest:%s)", localMetadataRequest));
}

@Override
public void add(RepositorySystemSession repositorySystemSession, LocalArtifactRegistration localArtifactRegistration) {
throw new UnsupportedOperationException(format("add(RepositorySystemSession,LocalArtifactRegistration%s)",localArtifactRegistration));
}

@Override
public void add(RepositorySystemSession repositorySystemSession, LocalMetadataRegistration localMetadataRegistration) {
throw new UnsupportedOperationException(format("add(RepositorySystemSession,LocalMetadataRegistration:%s)", localMetadataRegistration));
}
}

/**
*
* @param baseDir
* @param goal
* @return
*
* @ref https://stackoverflow.com/a/42216471/521197
*/
MainAnnotationProcessorMojo lookupConfiguredMojoUsingAether( Path baseDir, String goal ) throws Exception {

final MavenProject project = mojoRule.readMavenProject(baseDir.toFile());

// Generate session
final MavenSession session = mojoRule.newMavenSession(project);

// Generate Execution and Mojo for testing
final MojoExecution execution = mojoRule.newMojoExecution(goal);

final MainAnnotationProcessorMojo mojo =
(MainAnnotationProcessorMojo) mojoRule.lookupConfiguredMojo(session, execution);

org.eclipse.aether.DefaultRepositorySystemSession repoSession =
(org.eclipse.aether.DefaultRepositorySystemSession)mojo.repoSession;

org.eclipse.aether.repository.LocalRepositoryManager localRepoManager =
repoSession.getLocalRepositoryManager();

if( localRepoManager == null ) {

repoSession.setLocalRepositoryManager( new TestLocalRepositoryManager() );
}

return mojo;

}

@Test
public void testPR45() throws Exception {
final File pom = Paths.get(outputPath.toString(), "pr45", "pom.xml").toFile();

assertNotNull(pom);

final File baseDir = Paths.get(outputPath.toString(), "pr45").toFile();
assertNotNull(baseDir);
assertTrue( baseDir.exists() );
assertTrue( baseDir.isDirectory() );

final Mojo mojo = mojoRule.lookupConfiguredMojo(baseDir, "process");
final Path baseDir = Paths.get(outputPath.toString(), "pr45");

assertTrue(mojo instanceof MainAnnotationProcessorMojo);
assertNotNull(baseDir);
assertTrue( baseDir.toFile().exists() );
assertTrue( baseDir.toFile().isDirectory() );

final MainAnnotationProcessorMojo myMojo = (MainAnnotationProcessorMojo) mojo;
final MainAnnotationProcessorMojo myMojo = lookupConfiguredMojoUsingAether(baseDir, "process");

myMojo.execute();

Expand All @@ -51,8 +214,17 @@ public void testPR45() throws Exception {
assertNotNull( coord );
assertEquals( "org.mapstruct", coord.getGroupId() );
assertEquals( "mapstruct-processor", coord.getArtifactId() );
assertEquals( "1.0", coord.getVersion() );
assertEquals( "1.4.2.Final", coord.getVersion() );

assertNotNull( "repoSystem not initialized", myMojo.repoSystem );
assertNotNull( "repoSession not initialized", myMojo.repoSession );
assertNotNull( "remoteRepos not initialized", myMojo.remoteRepos );


final Optional<String> processorPath = myMojo.buildProcessorPath();

assertTrue( processorPath.isPresent() );
assertEquals( artifactPath( "mapstruct-processor-1.4.2.Final.jar").toAbsolutePath().toString(), processorPath.get() );

}
}
Binary file not shown.
2 changes: 1 addition & 1 deletion processor/src/test/resources/pr45/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<annotationProcessorPath>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.0</version>
<version>1.4.2.Final</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
Expand Down

0 comments on commit ccf0f27

Please sign in to comment.