Skip to content

Commit

Permalink
[MINVOKER-284] Switch to JUnit5, drop maven-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Apr 30, 2024
1 parent 7c329e1 commit 17d8b69
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 218 deletions.
25 changes: 6 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,20 @@ under the License.
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.10.0</version>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
11 changes: 11 additions & 0 deletions src/it/exec-timeout-invoker-level/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ under the License.
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<goalPrefix>test</goalPrefix>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
11 changes: 11 additions & 0 deletions src/it/exec-timeout-mojo-level/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ under the License.
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<goalPrefix>test</goalPrefix>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -91,13 +92,13 @@
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.InterpolationFilterReader;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.StreamConsumer;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.XmlStreamWriter;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomWriter;

Expand Down Expand Up @@ -751,8 +752,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (encoding == null || encoding.isEmpty()) {
getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
+ ", i.e. build is platform dependent!");
getLog().warn("File encoding has not been set, using platform encoding "
+ Charset.defaultCharset().displayName() + ", i.e. build is platform dependent!");
}

// done it here to prevent issues with concurrent access in case of parallel run
Expand Down Expand Up @@ -1522,7 +1523,7 @@ private void runBuild(
File interpolatedPomFile = interpolatePomFile(pomFile, basedir);
// FIXME: Think about the following code part -- ^^^^^^^ END

getLog().info(buffer().a("Building: ").strong(buildJob.getProject()).toString());
getLog().info(buffer().a("Building: ").strong(buildJob.getProject()).build());

InvokerProperties invokerProperties = getInvokerProperties(basedir, globalInvokerProperties);

Expand Down Expand Up @@ -2274,13 +2275,12 @@ void buildInterpolatedFile(File originalFile, File interpolatedFile) throws Mojo

// interpolation with token @...@
try (Reader reader =
new InterpolationFilterReader(ReaderFactory.newXmlReader(originalFile), composite, "@", "@")) {
new InterpolationFilterReader(new XmlStreamReader(originalFile), composite, "@", "@")) {
xml = IOUtil.toString(reader);
}

try (Writer writer = WriterFactory.newXmlWriter(interpolatedFile)) {
try (Writer writer = new XmlStreamWriter(interpolatedFile)) {
interpolatedFile.getParentFile().mkdirs();

writer.write(xml);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

Expand Down Expand Up @@ -64,7 +63,7 @@ protected void executeReport(Locale locale) throws MavenReportException {
BuildJobXpp3Reader buildJobReader = new BuildJobXpp3Reader();
List<BuildJob> buildJobs = new ArrayList<>(reportFiles.length);
for (File reportFile : reportFiles) {
try (XmlStreamReader xmlReader = ReaderFactory.newXmlReader(reportFile)) {
try (XmlStreamReader xmlReader = new XmlStreamReader(reportFile)) {
buildJobs.add(buildJobReader.read(xmlReader));
} catch (XmlPullParserException e) {
throw new MavenReportException("Failed to parse report file: " + reportFile, e);
Expand Down Expand Up @@ -103,6 +102,7 @@ private File[] getReportFiles() {
return ReportUtils.getReportFiles(reportsDirectory);
}

@Override
public boolean canGenerateReport() {
return getReportFiles().length > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.invoker.model.BuildJob;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.plexus.util.IOUtil;

import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;

Expand All @@ -38,7 +38,7 @@
*/
class InvokerSession {
private static final String SEPARATOR =
buffer().strong("-------------------------------------------------").toString();
buffer().strong("-------------------------------------------------").build();

private List<BuildJob> buildJobs;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/maven/plugins/invoker/PomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
Expand All @@ -43,7 +43,7 @@ class PomUtils {
* @throws MojoExecutionException If the POM file could not be loaded.
*/
public static Model loadPom(File pomFile) throws MojoExecutionException {
try (Reader reader = ReaderFactory.newXmlReader(pomFile)) {
try (Reader reader = new XmlStreamReader(pomFile)) {
return new MavenXpp3Reader().read(reader, false);
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Failed to parse POM: " + pomFile, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
InvokerSession invokerSession = new InvokerSession();

for (File reportFile : reportFiles) {
try (Reader xmlReader = ReaderFactory.newXmlReader(reportFile)) {
try (Reader xmlReader = new XmlStreamReader(reportFile)) {
invokerSession.addJob(reader.read(xmlReader));
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Failed to parse report file: " + reportFile, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,18 @@
*/
package org.apache.maven.plugins.invoker;

import java.util.Properties;
import java.io.File;

import org.apache.maven.model.Scm;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.codehaus.plexus.util.ReflectionUtils;

/**
* @author Olivier Lamy
* @since 22 nov. 07
*/
public class ExtendedMavenProjectStub extends MavenProjectStub {
private Scm scm;

private Properties properties;

public Scm getScm() {
return scm;
}

public void setScm(Scm scm) {
this.scm = scm;
}
abstract class AbstractTestUtil {

public Properties getProperties() {
return properties;
protected String getBasedir() {
String path = System.getProperty("basedir");
return path != null ? path : new File("").getAbsolutePath();
}

public void setProperties(Properties properties) {
this.properties = properties;
protected void setVariableValueToObject(Object object, String filed, Object value) throws IllegalAccessException {
ReflectionUtils.setVariableValueInObject(object, filed, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,80 +20,80 @@

import java.io.File;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.maven.model.Scm;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.junit.jupiter.api.Test;

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

/**
* @author Olivier Lamy
* @since 22 nov. 07
*/
public class InterpolationTest extends AbstractMojoTestCase {
class InterpolationTest extends AbstractTestUtil {

protected MavenProjectStub buildMavenProjectStub() {
ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
private MavenProject buildMavenProjectStub() {
MavenProject project = new MavenProject();
project.setVersion("1.0-SNAPSHOT");
project.setArtifactId("foo");
project.setGroupId("bar");
project.setFile(new File(getBasedir(), "pom.xml"));
Properties properties = new Properties();
properties.put("fooOnProject", "barOnProject");
project.setProperties(properties);
project.getModel().setProperties(properties);
Scm scm = new Scm();
scm.setConnection("http://blabla");
project.setScm(scm);
return project;
}

public void testCompositeMap() {
@Test
void testCompositeMap() {

Properties properties = new Properties();
Map<String, Object> properties = new HashMap<>();
properties.put("foo", "bar");
properties.put("version", "2.0-SNAPSHOT");
CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), (Map) properties, false);
assertEquals("1.0-SNAPSHOT", compositeMap.get("pom.version"));
assertEquals("bar", compositeMap.get("foo"));
assertEquals("bar", compositeMap.get("pom.groupId"));
assertEquals("http://blabla", compositeMap.get("pom.scm.connection"));
assertEquals("barOnProject", compositeMap.get("fooOnProject"));
CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), properties, false);
assertThat(compositeMap).containsEntry("pom.version", "1.0-SNAPSHOT");
assertThat(compositeMap).containsEntry("foo", "bar");
assertThat(compositeMap).containsEntry("pom.groupId", "bar");
assertThat(compositeMap).containsEntry("pom.scm.connection", "http://blabla");
assertThat(compositeMap).containsEntry("fooOnProject", "barOnProject");
}

public void testPomInterpolation() throws Exception {
Reader reader = null;
@Test
void testPomInterpolation() throws Exception {
File interpolatedPomFile;
try {
InvokerMojo invokerMojo = new InvokerMojo();
setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
setVariableValueToObject(invokerMojo, "settings", new Settings());
Properties properties = new Properties();
properties.put("foo", "bar");
properties.put("version", "2.0-SNAPSHOT");
setVariableValueToObject(invokerMojo, "filterProperties", properties);
String dirPath =
getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
+ "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
InvokerMojo invokerMojo = new InvokerMojo();
setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
setVariableValueToObject(invokerMojo, "settings", new Settings());
Properties properties = new Properties();
properties.put("foo", "bar");
properties.put("version", "2.0-SNAPSHOT");
setVariableValueToObject(invokerMojo, "filterProperties", properties);
String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
+ "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";

interpolatedPomFile = new File(getBasedir(), "target/interpolated-pom.xml");
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
reader = ReaderFactory.newXmlReader(interpolatedPomFile);
interpolatedPomFile = new File(getBasedir(), "target/interpolated-pom.xml");
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
try (Reader reader = new XmlStreamReader(interpolatedPomFile)) {
String content = IOUtil.toString(reader);
assertThat(content.indexOf("<interpolateValue>bar</interpolateValue>"))
.isPositive();
}
// recreate it to test delete if exists before creation
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
try (Reader reader = new XmlStreamReader(interpolatedPomFile)) {
String content = IOUtil.toString(reader);
assertTrue(content.indexOf("<interpolateValue>bar</interpolateValue>") > 0);
reader.close();
reader = null;
// recreate it to test delete if exists before creation
invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
reader = ReaderFactory.newXmlReader(interpolatedPomFile);
content = IOUtil.toString(reader);
assertTrue(content.indexOf("<interpolateValue>bar</interpolateValue>") > 0);
reader.close();
reader = null;
} finally {
IOUtil.close(reader);
assertThat(content.indexOf("<interpolateValue>bar</interpolateValue>"))
.isPositive();
}
}
}
Loading

0 comments on commit 17d8b69

Please sign in to comment.