Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-lli committed Dec 18, 2024
1 parent f7905cf commit 9d437ab
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
public class ValidateMojo extends AbstractMojo {

public static final String GOAL = "validate";
private static final String PLUGIN_GROUPID = "com.axonivy.ivy.ci";
private static final String PLUGIN_ARTIFACTID = "project-build-plugin";
protected static final String PLUGIN_GROUPID = "com.axonivy.ivy.ci";
protected static final String PLUGIN_ARTIFACTID = "project-build-plugin";

@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;
Expand Down
77 changes: 77 additions & 0 deletions src/test/java/ch/ivyteam/ivy/maven/validate/TestValidateMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ch.ivyteam.ivy.maven.validate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.File;
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.Rule;
import org.junit.Test;

import ch.ivyteam.ivy.maven.ProjectMojoRule;
import ch.ivyteam.ivy.maven.log.LogCollector;


public class TestValidateMojo {
private ValidateMojo mojo;

@Rule
public ProjectMojoRule<ValidateMojo> rule = new ProjectMojoRule<ValidateMojo>(
Path.of("src/test/resources/base"), ValidateMojo.GOAL) {
@Override
protected void before() throws Throwable {
super.before();
TestValidateMojo.this.mojo = getMojo();
}
};

@Test
public void samePluginVersions() throws Exception {
var log = new LogCollector();
rule.getMojo().setLog(log);
var p1 = createMavenProject("project1", "12.0.0");
var p2 = createMavenProject("project2", "12.0.0");
mojo.validateConsistentPluginVersion(List.of(p1, p2));
assertThat(log.getDebug()).hasSize(2);
assertThat(log.getDebug().get(0).toString())
.contains("com.axonivy.ivy.ci:project-build-plugin:12.0.0 configured in MavenProject: group:project1:12.0.0-SNAPSHOT");
assertThat(log.getDebug().get(1).toString())
.isEqualTo("com.axonivy.ivy.ci:project-build-plugin:12.0.0 configured in MavenProject: group:project2:12.0.0-SNAPSHOT");
assertThat(log.getErrors()).isEmpty();
}

@Test
public void differentPluginVersions() throws Exception {
var log = new LogCollector();
rule.getMojo().setLog(log);
var p1 = createMavenProject("project1", "12.0.0");
var p2 = createMavenProject("project2", "12.0.1");
assertThatThrownBy(() -> mojo.validateConsistentPluginVersion(List.of(p1, p2)))
.isInstanceOf(MojoExecutionException.class);
assertThat(log.getErrors()).hasSize(5);
assertThat(log.getErrors().get(0).toString()).isEqualTo("Several versions of project-build-plugins are configured [12.0.0, 12.0.1]:");
assertThat(log.getErrors().get(1).toString()).isEqualTo("12.0.0:");
assertThat(log.getErrors().get(2).toString()).contains("\tMavenProject: group:project1:12.0.0-SNAPSHOT");
assertThat(log.getErrors().get(3).toString()).isEqualTo("12.0.1:");
assertThat(log.getErrors().get(4).toString()).contains("\tMavenProject: group:project2:12.0.0-SNAPSHOT");
}

private MavenProject createMavenProject(String projectId, String version) {
var project = new MavenProject();
project.setGroupId("group");
project.setArtifactId(projectId);
project.setVersion("12.0.0-SNAPSHOT");
project.setFile(new File("src/test/resources/" + projectId));
var plugin = new Plugin();
plugin.setGroupId(ValidateMojo.PLUGIN_GROUPID);
plugin.setArtifactId(ValidateMojo.PLUGIN_ARTIFACTID);
plugin.setVersion(version);
project.getBuild().getPlugins().add(plugin);
return project;
}
}

0 comments on commit 9d437ab

Please sign in to comment.