From f1b6d34728242569a4a3afb034f775515a5317e2 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 23 Jun 2022 16:20:47 +0200 Subject: [PATCH 1/3] [MSHARED-1089] Upgrade maven-verifier to JDK 8 / Junit 5 --- pom.xml | 10 +++--- .../java/org/apache/maven/it/Verifier.java | 12 +++---- .../maven/it/Embedded3xLauncherTest.java | 18 +++++----- .../apache/maven/it/ForkedLauncherTest.java | 33 ++++++++++--------- .../org/apache/maven/it/VerifierTest.java | 33 +++++++++---------- 5 files changed, 52 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index a68fe0e..4b9a500 100644 --- a/pom.xml +++ b/pom.xml @@ -28,13 +28,13 @@ maven-verifier - 1.8.1-SNAPSHOT + 2.0.0-SNAPSHOT Apache Maven Verifier Component Provides a test harness for Maven integration tests. - 7 + 8 2022-03-19T12:57:53Z @@ -73,9 +73,9 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter + 5.8.0 org.hamcrest diff --git a/src/main/java/org/apache/maven/it/Verifier.java b/src/main/java/org/apache/maven/it/Verifier.java index 5313698..d5ba884 100644 --- a/src/main/java/org/apache/maven/it/Verifier.java +++ b/src/main/java/org/apache/maven/it/Verifier.java @@ -59,7 +59,7 @@ import org.apache.maven.shared.utils.cli.StreamConsumer; import org.apache.maven.shared.utils.cli.WriterStreamConsumer; import org.apache.maven.shared.utils.io.FileUtils; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -1060,7 +1060,7 @@ public void assertFilePresent( String file ) } catch ( VerificationException e ) { - Assert.fail( e.getMessage() ); + Assertions.fail( e.getMessage() ); } } @@ -1108,7 +1108,7 @@ public void assertFileMatches( String file, String regex ) } catch ( VerificationException e ) { - Assert.fail( e.getMessage() ); + Assertions.fail( e.getMessage() ); } } @@ -1137,7 +1137,7 @@ public void assertFileNotPresent( String file ) } catch ( VerificationException e ) { - Assert.fail( e.getMessage() ); + Assertions.fail( e.getMessage() ); } } @@ -1189,7 +1189,7 @@ private void assertArtifactPresence( boolean wanted, String org, String name, St } catch ( VerificationException e ) { - Assert.fail( e.getMessage() ); + Assertions.fail( e.getMessage() ); } } @@ -1924,7 +1924,7 @@ public void assertArtifactContents( String org, String artifact, String version, throws IOException { String fileName = getArtifactPath( org, artifact, version, type ); - Assert.assertEquals( contents, FileUtils.fileRead( fileName ) ); + Assertions.assertEquals( contents, FileUtils.fileRead( fileName ) ); } static class UserModelReader diff --git a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java index 9548a1b..d3d697a 100644 --- a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java +++ b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java @@ -19,22 +19,22 @@ * under the License. */ -import java.io.File; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; + public class Embedded3xLauncherTest { - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + public Path temporaryFolder; private final String workingDir = Paths.get( "src/test/resources" ).toAbsolutePath().toString(); @@ -56,11 +56,11 @@ public void testWithMavenHome() throws Exception private void runLauncher( MavenLauncher launcher ) throws Exception { - File logFile = temporaryFolder.newFile( "build.log" ); + Path logFile = temporaryFolder.resolve( "build.log" ); - int exitCode = launcher.run( new String[] {"clean"}, new Properties(), workingDir, logFile ); + int exitCode = launcher.run( new String[] {"clean"}, new Properties(), workingDir, logFile.toFile() ); assertThat( "exit code unexpected, build log: " + System.lineSeparator() + - new String( Files.readAllBytes( logFile.toPath() ) ), exitCode, is( 0 ) ); + new String( Files.readAllBytes( logFile ) ), exitCode, is( 0 ) ); } } diff --git a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java index 1173593..9087035 100644 --- a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java +++ b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java @@ -22,26 +22,28 @@ */ import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; +import java.nio.file.Files; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Properties; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + public class ForkedLauncherTest { - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + public Path temporaryFolder; private ForkedLauncher launcher; @@ -51,9 +53,9 @@ public class ForkedLauncherTest public void mvnw() throws Exception { launcher = new ForkedLauncher( ".", Collections.emptyMap(), false, true ); - File logFile = temporaryFolder.newFile( "build.log" ); + Path logFile = temporaryFolder.resolve( "build.log" ); - int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile ); + int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() ); // most likely this contains the exception in case exitCode != 0 expectFileLine( logFile, "Hello World" ); @@ -65,9 +67,9 @@ public void mvnw() throws Exception public void mvnwDebug() throws Exception { launcher = new ForkedLauncher( ".", Collections.emptyMap(), true, true ); - File logFile = temporaryFolder.newFile( "build.log" ); + Path logFile = temporaryFolder.resolve( "build.log" ); - int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile ); + int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() ); // most likely this contains the exception in case exitCode != 0 expectFileLine( logFile, "Hello World" ); @@ -75,10 +77,9 @@ public void mvnwDebug() throws Exception assertThat( "exit code", exitCode , is ( 0 ) ); } - static void expectFileLine( File file, String expectedline ) throws IOException + static void expectFileLine( Path file, String expectedline ) throws IOException { - try ( FileReader fr = new FileReader( file ); - BufferedReader br = new BufferedReader( fr ) ) + try ( BufferedReader br = Files.newBufferedReader( file, StandardCharsets.UTF_8 ) ) { Collection text = new ArrayList<>(); String line; @@ -92,7 +93,7 @@ static void expectFileLine( File file, String expectedline ) throws IOException } String message = "%s doesn't contain '%s', was:\n%s"; - fail( String.format( message, file.getName(), expectedline, text ) ); + fail( String.format( message, file.getFileName().toString(), expectedline, text ) ); } } diff --git a/src/test/java/org/apache/maven/it/VerifierTest.java b/src/test/java/org/apache/maven/it/VerifierTest.java index 69cd80a..2fcc164 100644 --- a/src/test/java/org/apache/maven/it/VerifierTest.java +++ b/src/test/java/org/apache/maven/it/VerifierTest.java @@ -19,28 +19,24 @@ * under the License. */ -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Properties; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; -import static org.hamcrest.CoreMatchers.isA; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; public class VerifierTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @TempDir + public Path temporaryFolder; private void check( String expected, String... lines ) { @@ -105,19 +101,20 @@ public void testStripAnsi() @Test public void testLoadPropertiesFNFE() throws VerificationException { - exception.expectCause( isA( FileNotFoundException.class ) ); - - Verifier verifier = new Verifier( "src/test/resources" ); - verifier.loadProperties( "unknown.properties" ); + VerificationException exception = assertThrows( VerificationException.class, () -> { + Verifier verifier = new Verifier( "src/test/resources" ); + verifier.loadProperties( "unknown.properties" ); + } ); + assertInstanceOf( FileNotFoundException.class, exception.getCause() ); } @Test public void testDedicatedMavenHome() throws VerificationException, IOException { String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString(); - Verifier verifier = new Verifier( temporaryFolder.getRoot().toString(), null, false, mavenHome ); + Verifier verifier = new Verifier( temporaryFolder.toString(), null, false, mavenHome ); verifier.executeGoal( "some-goal" ); - File logFile = new File( verifier.getBasedir(), verifier.getLogFileName() ); + Path logFile = Paths.get( verifier.getBasedir(), verifier.getLogFileName() ); ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" ); } From dcc3b894512eb1c0ebe8ba1accdeae1ce83db4e4 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 31 Aug 2022 09:16:16 +0200 Subject: [PATCH 2/3] Use dir instead of folder, remove some unnecessary toString() calls --- .../apache/maven/it/Embedded3xLauncherTest.java | 4 ++-- .../org/apache/maven/it/ForkedLauncherTest.java | 15 +++++++-------- .../java/org/apache/maven/it/VerifierTest.java | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java index d3d697a..d58c495 100644 --- a/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java +++ b/src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java @@ -34,7 +34,7 @@ public class Embedded3xLauncherTest { @TempDir - public Path temporaryFolder; + public Path temporaryDir; private final String workingDir = Paths.get( "src/test/resources" ).toAbsolutePath().toString(); @@ -56,7 +56,7 @@ public void testWithMavenHome() throws Exception private void runLauncher( MavenLauncher launcher ) throws Exception { - Path logFile = temporaryFolder.resolve( "build.log" ); + Path logFile = temporaryDir.resolve( "build.log" ); int exitCode = launcher.run( new String[] {"clean"}, new Properties(), workingDir, logFile.toFile() ); diff --git a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java index 9087035..7808cae 100644 --- a/src/test/java/org/apache/maven/it/ForkedLauncherTest.java +++ b/src/test/java/org/apache/maven/it/ForkedLauncherTest.java @@ -28,7 +28,6 @@ import java.nio.file.Files; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -43,7 +42,7 @@ public class ForkedLauncherTest { @TempDir - public Path temporaryFolder; + public Path temporaryDir; private ForkedLauncher launcher; @@ -52,8 +51,8 @@ public class ForkedLauncherTest @Test public void mvnw() throws Exception { - launcher = new ForkedLauncher( ".", Collections.emptyMap(), false, true ); - Path logFile = temporaryFolder.resolve( "build.log" ); + launcher = new ForkedLauncher( ".", Collections.emptyMap(), false, true ); + Path logFile = temporaryDir.resolve( "build.log" ); int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() ); @@ -66,8 +65,8 @@ public void mvnw() throws Exception @Test public void mvnwDebug() throws Exception { - launcher = new ForkedLauncher( ".", Collections.emptyMap(), true, true ); - Path logFile = temporaryFolder.resolve( "build.log" ); + launcher = new ForkedLauncher( ".", Collections.emptyMap(), true, true ); + Path logFile = temporaryDir.resolve( "build.log" ); int exitCode = launcher.run( new String[0], new Properties(), workingDir, logFile.toFile() ); @@ -92,8 +91,8 @@ static void expectFileLine( Path file, String expectedline ) throws IOException text.add( line ); } - String message = "%s doesn't contain '%s', was:\n%s"; - fail( String.format( message, file.getFileName().toString(), expectedline, text ) ); + String message = "%s doesn't contain '%s', was:%n%s"; + fail( String.format( message, file.getFileName(), expectedline, text ) ); } } diff --git a/src/test/java/org/apache/maven/it/VerifierTest.java b/src/test/java/org/apache/maven/it/VerifierTest.java index 2fcc164..ebbe466 100644 --- a/src/test/java/org/apache/maven/it/VerifierTest.java +++ b/src/test/java/org/apache/maven/it/VerifierTest.java @@ -36,7 +36,7 @@ public class VerifierTest { @TempDir - public Path temporaryFolder; + public Path temporaryDir; private void check( String expected, String... lines ) { @@ -112,7 +112,7 @@ public void testLoadPropertiesFNFE() throws VerificationException public void testDedicatedMavenHome() throws VerificationException, IOException { String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString(); - Verifier verifier = new Verifier( temporaryFolder.toString(), null, false, mavenHome ); + Verifier verifier = new Verifier( temporaryDir.toString(), null, false, mavenHome ); verifier.executeGoal( "some-goal" ); Path logFile = Paths.get( verifier.getBasedir(), verifier.getLogFileName() ); ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" ); From da7df0942660266152705d1a665e555afb23b1a2 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 31 Aug 2022 09:27:20 +0200 Subject: [PATCH 3/3] Remove junit from compile scope --- pom.xml | 2 + .../java/org/apache/maven/it/Verifier.java | 100 ------------------ .../org/apache/maven/it/VerifierTest.java | 4 +- 3 files changed, 4 insertions(+), 102 deletions(-) diff --git a/pom.xml b/pom.xml index 4b9a500..77b756c 100644 --- a/pom.xml +++ b/pom.xml @@ -76,11 +76,13 @@ org.junit.jupiter junit-jupiter 5.8.0 + test org.hamcrest hamcrest-core 2.2 + test diff --git a/src/main/java/org/apache/maven/it/Verifier.java b/src/main/java/org/apache/maven/it/Verifier.java index d5ba884..ebf54cb 100644 --- a/src/main/java/org/apache/maven/it/Verifier.java +++ b/src/main/java/org/apache/maven/it/Verifier.java @@ -59,7 +59,6 @@ import org.apache.maven.shared.utils.cli.StreamConsumer; import org.apache.maven.shared.utils.cli.WriterStreamConsumer; import org.apache.maven.shared.utils.io.FileUtils; -import org.junit.jupiter.api.Assertions; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -1046,24 +1045,6 @@ public void verifyFilePresent( String file ) throws VerificationException verifyFilePresence( file, true ); } - /** - * - * @param file the path of the file to check - * @deprecated Use {@link #verifyFilePresent(String)} instead. - */ - @Deprecated - public void assertFilePresent( String file ) - { - try - { - verifyFilePresent( file ); - } - catch ( VerificationException e ) - { - Assertions.fail( e.getMessage() ); - } - } - /** * Verifies the given file's content matches an regular expression. * Note this method also checks that the file exists and is readable. @@ -1090,28 +1071,6 @@ public void verifyFileContentMatches( String file, String regex ) throws Verific } } - /** - * Check the given file's content matches a regular expression. Note this method also checks that the file exists - * and is readable. - * - * @param file the path of the file to check - * @param regex a regular expression that the file's contents should match - * @see Pattern - * @deprecated Use {@link #verifyFileContentMatches(String, String)} instead. - */ - @Deprecated - public void assertFileMatches( String file, String regex ) - { - try - { - verifyFileContentMatches( file, regex ); - } - catch ( VerificationException e ) - { - Assertions.fail( e.getMessage() ); - } - } - /** * Verifies that the given file does not exist. * @@ -1123,24 +1082,6 @@ public void verifyFileNotPresent( String file ) throws VerificationException verifyFilePresence( file, false ); } - /** - * - * @param file the path of the file to check - * @deprecated Use {@link #verifyFileNotPresent(String)} instead. - */ - @Deprecated - public void assertFileNotPresent( String file ) - { - try - { - verifyFileNotPresent( file ); - } - catch ( VerificationException e ) - { - Assertions.fail( e.getMessage() ); - } - } - private void verifyArtifactPresence( boolean wanted, String groupId, String artifactId, String version, String ext ) throws VerificationException { @@ -1181,36 +1122,6 @@ public void verifyArtifactNotPresent( String groupId, String artifactId, String verifyArtifactPresence( false, groupId, artifactId, version, ext ); } - private void assertArtifactPresence( boolean wanted, String org, String name, String version, String ext ) - { - try - { - verifyArtifactPresence( wanted, org, name, version, ext ); - } - catch ( VerificationException e ) - { - Assertions.fail( e.getMessage() ); - } - } - - /** - * @deprecated Use {@link #verifyArtifactPresent(String, String, String, String)} instead. - */ - @Deprecated - public void assertArtifactPresent( String org, String name, String version, String ext ) - { - assertArtifactPresence( true, org, name, version, ext ); - } - - /** - * @deprecated Use {@link #verifyArtifactNotPresent(String, String, String, String)} instead. - */ - @Deprecated - public void assertArtifactNotPresent( String org, String name, String version, String ext ) - { - assertArtifactPresence( false, org, name, version, ext ); - } - private void verifyExpectedResult( String line ) throws VerificationException { @@ -1916,17 +1827,6 @@ public void verifyArtifactContent( String groupId, String artifactId, String ver } } - /** - * @deprecated Use {@link #verifyArtifactContent(String, String, String, String, String)} instead. - */ - @Deprecated - public void assertArtifactContents( String org, String artifact, String version, String type, String contents ) - throws IOException - { - String fileName = getArtifactPath( org, artifact, version, type ); - Assertions.assertEquals( contents, FileUtils.fileRead( fileName ) ); - } - static class UserModelReader extends DefaultHandler { diff --git a/src/test/java/org/apache/maven/it/VerifierTest.java b/src/test/java/org/apache/maven/it/VerifierTest.java index ebbe466..f80cb1d 100644 --- a/src/test/java/org/apache/maven/it/VerifierTest.java +++ b/src/test/java/org/apache/maven/it/VerifierTest.java @@ -86,8 +86,8 @@ public void testFileInJarPresent() { //File file = new File( "src/test/resources/mshared104.jar!fud.xml" ); Verifier verifier = new Verifier( "src/test/resources" ); - verifier.assertFilePresent( "mshared104.jar!/pom.xml" ); - verifier.assertFileNotPresent( "mshared104.jar!/fud.xml" ); + verifier.verifyFilePresent( "mshared104.jar!/pom.xml" ); + verifier.verifyFileNotPresent( "mshared104.jar!/fud.xml" ); } @Test