From 9d437abd805ba2861fa6d74182396617984f9f34 Mon Sep 17 00:00:00 2001 From: Lukas Lieb Date: Wed, 18 Dec 2024 14:52:53 +0100 Subject: [PATCH] Add tests --- .../ivy/maven/validate/ValidateMojo.java | 4 +- .../ivy/maven/validate/TestValidateMojo.java | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/test/java/ch/ivyteam/ivy/maven/validate/TestValidateMojo.java diff --git a/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java b/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java index 5ffd0989..5bb000c5 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/validate/ValidateMojo.java @@ -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; diff --git a/src/test/java/ch/ivyteam/ivy/maven/validate/TestValidateMojo.java b/src/test/java/ch/ivyteam/ivy/maven/validate/TestValidateMojo.java new file mode 100644 index 00000000..cc51e823 --- /dev/null +++ b/src/test/java/ch/ivyteam/ivy/maven/validate/TestValidateMojo.java @@ -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 rule = new ProjectMojoRule( + 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; + } +}