Skip to content

Commit

Permalink
Switch to junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 19, 2023
1 parent 21bf165 commit 75398b1
Show file tree
Hide file tree
Showing 34 changed files with 428 additions and 522 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ limitations under the License.
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/codehaus/plexus/util/CollectionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* <p>CollectionUtilsTest class.</p>
Expand Down Expand Up @@ -135,7 +135,7 @@ public void testMergeMapArray()
/**
* <p>testMavenPropertiesLoading.</p>
*/
@Test
@org.junit.jupiter.api.Test
public void testMavenPropertiesLoading()
{
// Mimic MavenSession properties loading. Properties listed
Expand Down Expand Up @@ -195,7 +195,7 @@ public void testMavenPropertiesLoading()
/**
* <p>testIteratorToListWithAPopulatedList.</p>
*/
@Test
@org.junit.jupiter.api.Test
public void testIteratorToListWithAPopulatedList()
{
List<String> original = new ArrayList<String>();
Expand All @@ -218,7 +218,7 @@ public void testIteratorToListWithAPopulatedList()
/**
* <p>testIteratorToListWithAEmptyList.</p>
*/
@Test
@org.junit.jupiter.api.Test
public void testIteratorToListWithAEmptyList()
{
List<String> original = new ArrayList<String>();
Expand Down
48 changes: 21 additions & 27 deletions src/test/java/org/codehaus/plexus/util/DirectoryScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.io.IOException;
Expand All @@ -35,10 +35,8 @@
import java.util.List;
import java.util.Set;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Base class for testcases doing tests with files.
Expand All @@ -50,15 +48,12 @@
public class DirectoryScannerTest
extends FileBasedTestCase
{
@Rule
public TestName name = new TestName();

private static String testDir = getTestDirectory().getPath();

/**
* <p>setUp.</p>
*/
@Before
@BeforeEach
public void setUp()
{
try
Expand Down Expand Up @@ -190,7 +185,7 @@ private boolean checkTestFilesSymlinks()
catch ( IOException e )
{
System.err.println( String.format( "The unit test '%s.%s' will be skipped, reason: %s",
this.getClass().getSimpleName(), name.getMethodName(),
this.getClass().getSimpleName(), getTestMethodName(),
e.getMessage() ) );
System.out.println( String.format( "This test requires symlinks files in '%s' directory.",
symlinksDirectory.getPath() ) );
Expand All @@ -217,11 +212,10 @@ public void testGeneral()

List<File> fileNames = FileUtils.getFiles( new File( testDir ), includes, excludes, false );

assertEquals( "Wrong number of results.", 3, fileNames.size() );
assertTrue( "3 not found.", fileNames.contains( new File( "scanner3.dat" ) ) );
assertTrue( "4 not found.", fileNames.contains( new File( "scanner4.dat" ) ) );
assertTrue( "5 not found.", fileNames.contains( new File( "scanner5.dat" ) ) );

assertEquals( 3, fileNames.size(), "Wrong number of results." );
assertTrue( fileNames.contains( new File( "scanner3.dat" ) ), "3 not found." );
assertTrue( fileNames.contains( new File( "scanner4.dat" ) ), "4 not found." );
assertTrue( fileNames.contains( new File( "scanner5.dat" ) ), "5 not found." );
}

/**
Expand All @@ -241,10 +235,10 @@ public void testIncludesExcludesWithWhiteSpaces()

List<File> fileNames = FileUtils.getFiles( new File( testDir ), includes, excludes, false );

assertEquals( "Wrong number of results.", 3, fileNames.size() );
assertTrue( "3 not found.", fileNames.contains( new File( "scanner3.dat" ) ) );
assertTrue( "4 not found.", fileNames.contains( new File( "scanner4.dat" ) ) );
assertTrue( "5 not found.", fileNames.contains( new File( "scanner5.dat" ) ) );
assertEquals( 3, fileNames.size(), "Wrong number of results." );
assertTrue( fileNames.contains( new File( "scanner3.dat" ) ), "3 not found." );
assertTrue( fileNames.contains( new File( "scanner4.dat" ) ), "4 not found." );
assertTrue( fileNames.contains( new File( "scanner5.dat" ) ), "5 not found." );
}

/**
Expand Down Expand Up @@ -347,7 +341,7 @@ public void testDirectoriesWithHyphens()
ds.scan();

String[] files = ds.getIncludedFiles();
assertEquals( "Wrong number of results.", 3, files.length );
assertEquals( 3, files.length, "Wrong number of results." );
}

/**
Expand Down Expand Up @@ -395,7 +389,7 @@ public void testAntExcludesOverrideIncludes()
*
* @throws java.io.IOException if any.
*/
@Test
@org.junit.jupiter.api.Test
public void testAntExcludesOverrideIncludesWithExplicitAntPrefix()
throws IOException
{
Expand Down Expand Up @@ -436,7 +430,7 @@ public void testAntExcludesOverrideIncludesWithExplicitAntPrefix()
*
* @throws java.io.IOException if any.
*/
@Test
@org.junit.jupiter.api.Test
public void testRegexIncludeWithExcludedPrefixDirs()
throws IOException
{
Expand Down Expand Up @@ -472,7 +466,7 @@ public void testRegexIncludeWithExcludedPrefixDirs()
*
* @throws java.io.IOException if any.
*/
@Test
@org.junit.jupiter.api.Test
public void testRegexExcludeWithNegativeLookahead()
throws IOException
{
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/org/codehaus/plexus/util/DirectoryWalkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;

import java.io.File;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Test;
import java.io.File;

/**
* <p>DirectoryWalkerTest class.</p>
Expand Down Expand Up @@ -50,11 +50,11 @@ public void testDirectoryWalk()

walker.scan();

assertEquals( "Walk Collector / Starting Count", 1, collector.startCount );
assertNotNull( "Walk Collector / Starting Dir", collector.startingDir );
assertEquals( "Walk Collector / Finish Count", 1, collector.finishCount );
assertEquals( "Walk Collector / Steps Count", 4, collector.steps.size() );
assertTrue( "Walk Collector / percentage low >= 0", collector.percentageLow >= 0 );
assertTrue( "Walk Collector / percentage high <= 100", collector.percentageHigh <= 100 );
assertEquals( 1, collector.startCount, "Walk Collector / Starting Count" );
assertNotNull( collector.startingDir, "Walk Collector / Starting Dir" );
assertEquals( 1, collector.finishCount, "Walk Collector / Finish Count" );
assertEquals( 4, collector.steps.size(), "Walk Collector / Steps Count" );
assertTrue( collector.percentageLow >= 0, "Walk Collector / percentage low >= 0" );
assertTrue( collector.percentageHigh <= 100, "Walk Collector / percentage high <= 100" );
}
}
90 changes: 34 additions & 56 deletions src/test/java/org/codehaus/plexus/util/FileBasedTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
* limitations under the License.
*/

import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -27,11 +33,10 @@
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.Arrays;

import junit.framework.AssertionFailedError;

/**
* Base class for testcases doing tests with files.
*
Expand All @@ -43,6 +48,8 @@ public abstract class FileBasedTestCase
{
private static File testDir;

private TestInfo testInfo;

/**
* <p>getTestDirectory.</p>
*
Expand Down Expand Up @@ -186,7 +193,7 @@ protected File newFile( String filename )
protected void checkFile( final File file, final File referenceFile )
throws Exception
{
assertTrue( "Check existence of output file", file.exists() );
assertTrue( file.exists(), "Check existence of output file" );
assertEqualContent( referenceFile, file );
}

Expand All @@ -205,7 +212,7 @@ protected void checkWrite( final OutputStream output )
}
catch ( final Throwable t )
{
throw new AssertionFailedError( "The copy() method closed the stream " + "when it shouldn't have. "
fail( "The copy() method closed the stream " + "when it shouldn't have. "
+ t.getMessage() );
}
}
Expand All @@ -225,7 +232,7 @@ protected void checkWrite( final Writer output )
}
catch ( final Throwable t )
{
throw new AssertionFailedError( "The copy() method closed the stream " + "when it shouldn't have. "
fail( "The copy() method closed the stream " + "when it shouldn't have. "
+ t.getMessage() );
}
}
Expand All @@ -241,7 +248,7 @@ protected void deleteFile( final File file )
{
if ( file.exists() )
{
assertTrue( "Couldn't delete file: " + file, file.delete() );
assertTrue( file.delete(), "Couldn't delete file: " + file );
}
}

Expand All @@ -258,37 +265,10 @@ private void assertEqualContent( final File f0, final File f1 )
* + " and " + f1 + " have differing file sizes (" + f0.length() + " vs " + f1.length() + ")", ( f0.length() ==
* f1.length() ) );
*/
final InputStream is0 = Files.newInputStream( f0.toPath() );
try
{
final InputStream is1 = Files.newInputStream( f1.toPath() );
try
{
final byte[] buf0 = new byte[1024];
final byte[] buf1 = new byte[1024];
int n0 = 0;
int n1 = 0;

while ( -1 != n0 )
{
n0 = is0.read( buf0 );
n1 = is1.read( buf1 );
assertTrue( "The files " + f0 + " and " + f1 + " have differing number of bytes available (" + n0
+ " vs " + n1 + ")", ( n0 == n1 ) );

assertTrue( "The files " + f0 + " and " + f1 + " have different content",
Arrays.equals( buf0, buf1 ) );
}
}
finally
{
is1.close();
}
}
finally
{
is0.close();
}
byte[] buf0 = Files.readAllBytes( f0.toPath() );
byte[] buf1 = Files.readAllBytes( f0.toPath() );
assertArrayEquals( buf0, buf1,
"The files " + f0 + " and " + f1 + " have different content" );
}

/**
Expand All @@ -301,20 +281,8 @@ private void assertEqualContent( final File f0, final File f1 )
protected void assertEqualContent( final byte[] b0, final File file )
throws IOException
{
final InputStream is = Files.newInputStream( file.toPath() );
try
{
byte[] b1 = new byte[b0.length];
int numRead = is.read( b1 );
assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 );
for ( int i = 0; i < numRead; assertTrue( "Byte " + i + " differs (" + b0[i] + " != " + b1[i] + ")",
b0[i] == b1[i] ), i++ )
;
}
finally
{
is.close();
}
byte[] b1 = Files.readAllBytes( file.toPath() );
assertArrayEquals(b0, b1, "Content differs");
}

/**
Expand All @@ -324,9 +292,9 @@ protected void assertEqualContent( final byte[] b0, final File file )
*/
protected void assertIsDirectory( File file )
{
assertTrue( "The File doesn't exists: " + file.getAbsolutePath(), file.exists() );
assertTrue( file.exists(), "The File doesn't exists: " + file.getAbsolutePath() );

assertTrue( "The File isn't a directory: " + file.getAbsolutePath(), file.isDirectory() );
assertTrue( file.isDirectory(), "The File isn't a directory: " + file.getAbsolutePath() );
}

/**
Expand All @@ -336,8 +304,18 @@ protected void assertIsDirectory( File file )
*/
protected void assertIsFile( File file )
{
assertTrue( "The File doesn't exists: " + file.getAbsolutePath(), file.exists() );
assertTrue( file.exists(), "The File doesn't exists: " + file.getAbsolutePath() );

assertTrue( file.isFile(), "The File isn't a file: " + file.getAbsolutePath() );
}

assertTrue( "The File isn't a file: " + file.getAbsolutePath(), file.isFile() );
@BeforeEach
void init(TestInfo testInfo) {
this.testInfo = testInfo;
}

protected String getTestMethodName() {
return testInfo.getTestMethod().map(Method::getName).orElse(null);
}

}

0 comments on commit 75398b1

Please sign in to comment.