Skip to content

Commit

Permalink
[MSHARED-1089] Upgrade maven-verifier to JDK 8 / Junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 23, 2022
1 parent 526d53c commit f1b6d34
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 54 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
</parent>

<artifactId>maven-verifier</artifactId>
<version>1.8.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>Apache Maven Verifier Component</name>
<description>Provides a test harness for Maven integration tests.</description>

<properties>
<javaVersion>7</javaVersion>
<javaVersion>8</javaVersion>
<project.build.outputTimestamp>2022-03-19T12:57:53Z</project.build.outputTimestamp>
</properties>

Expand Down Expand Up @@ -73,9 +73,9 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/apache/maven/it/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1060,7 +1060,7 @@ public void assertFilePresent( String file )
}
catch ( VerificationException e )
{
Assert.fail( e.getMessage() );
Assertions.fail( e.getMessage() );
}
}

Expand Down Expand Up @@ -1108,7 +1108,7 @@ public void assertFileMatches( String file, String regex )
}
catch ( VerificationException e )
{
Assert.fail( e.getMessage() );
Assertions.fail( e.getMessage() );
}
}

Expand Down Expand Up @@ -1137,7 +1137,7 @@ public void assertFileNotPresent( String file )
}
catch ( VerificationException e )
{
Assert.fail( e.getMessage() );
Assertions.fail( e.getMessage() );
}
}

Expand Down Expand Up @@ -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() );
}
}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/org/apache/maven/it/Embedded3xLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 ) );
}
}
33 changes: 17 additions & 16 deletions src/test/java/org/apache/maven/it/ForkedLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,9 +53,9 @@ public class ForkedLauncherTest
public void mvnw() throws Exception
{
launcher = new ForkedLauncher( ".", Collections.<String, String>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" );
Expand All @@ -65,20 +67,19 @@ public void mvnw() throws Exception
public void mvnwDebug() throws Exception
{
launcher = new ForkedLauncher( ".", Collections.<String, String>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" );

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<String> text = new ArrayList<>();
String line;
Expand All @@ -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 ) );
}
}

Expand Down
33 changes: 15 additions & 18 deletions src/test/java/org/apache/maven/it/VerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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" );
}

Expand Down

0 comments on commit f1b6d34

Please sign in to comment.