Skip to content

Commit

Permalink
Move tests to Junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Apr 7, 2023
1 parent ae277e5 commit e9d7ed2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 89 deletions.
22 changes: 5 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,6 @@
<version>2.0b6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>file-management</artifactId>
Expand All @@ -132,15 +121,14 @@
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/codehaus/mojo/buildhelper/FixtureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Properties;

/**
* A utility class for managing test resources.
* A utility class for managing test resources - used by integration tests - groovy scripting.
*
* @author Adrian Price <a href="mailto:demonfiddler@virginmedia.com">demonfiddler@virginmedia.com</a>
* @since 1.12
Expand Down Expand Up @@ -118,7 +118,7 @@ public static void createResources( File baseDir, Properties properties, long ba
if ( !propertyValue.isEmpty() )
{
int offset = Integer.parseInt( propertyValue );
long ts = baseTimestamp + ( offset * 1000 );
long ts = baseTimestamp + ( offset * 1000L);
file.setLastModified( ts );
assert file.lastModified() == ts : "failed to set last modified timestamp for "
+ file.getCanonicalPath();
Expand Down
53 changes: 26 additions & 27 deletions src/test/java/org/codehaus/mojo/buildhelper/ParseVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
* SOFTWARE.
*/

import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Properties;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParseVersionTest
{
private static class TestParseVersionMojo
public static class TestParseVersionMojo
extends ParseVersionMojo
{
private Properties properties;
private final Properties properties;

public TestParseVersionMojo( Properties properties )
{
Expand All @@ -49,17 +50,18 @@ protected void defineProperty( String name, String value )
}
}

@Nested
public class TestParseVersion
{
private Properties props;

private ParseVersionMojo mojo;

@BeforeMethod
@BeforeEach
public void beforeClass()
{
props = new Properties();
mojo = new TestParseVersionMojo( props );
mojo = new TestParseVersionMojo(props);
mojo.setPropertyPrefix( "parsed" );

mojo.setFormattedPropertyPrefix( "formatted" );
Expand All @@ -72,9 +74,7 @@ public void beforeClass()
}

@Test
public void testJunkVersion()
throws Exception
{
public void checkJunkVersion() {
// Test a junk version string
mojo.parseVersion( "junk" );

Expand All @@ -87,9 +87,7 @@ public void testJunkVersion()
}

@Test
public void testBasicMavenVersionString()
throws Exception
{
public void checkBasicMavenVersionString() {
// Test a basic maven version string
mojo.parseVersion( "1.0.0" );

Expand All @@ -102,7 +100,7 @@ public void testBasicMavenVersionString()
}

@Test
public void testVersionStringWithQualifier()
public void checkVersionStringWithQualifier()
{
// Test a version string with qualifier
mojo.parseVersion( "2.3.4-beta-5" );
Expand All @@ -116,7 +114,7 @@ public void testVersionStringWithQualifier()
}

@Test
public void testOSGiVersionStringWithQualifier()
public void checkOSGiVersionStringWithQualifier()
{
// Test an osgi version string
mojo.parseVersion( "2.3.4.beta_5" );
Expand All @@ -130,7 +128,7 @@ public void testOSGiVersionStringWithQualifier()
}

@Test
public void testSnapshotVersion()
public void checkSnapshotVersion()
{
// Test a snapshot version string
mojo.parseVersion( "1.2.3-SNAPSHOT" );
Expand All @@ -144,7 +142,7 @@ public void testSnapshotVersion()
}

@Test
public void testSnapshotVersion2()
public void checkSnapshotVersion2()
{
// Test a snapshot version string
mojo.parseVersion( "2.0.17-SNAPSHOT" );
Expand All @@ -158,7 +156,7 @@ public void testSnapshotVersion2()
}

@Test
public void testVersionStringWithBuildNumber()
public void checkVersionStringWithBuildNumber()
{
// Test a version string with a build number
mojo.parseVersion( "1.2.3-4" );
Expand All @@ -173,7 +171,7 @@ public void testVersionStringWithBuildNumber()
}

@Test
public void testSnapshotVersionStringWithBuildNumber()
public void checkSnapshotVersionStringWithBuildNumber()
{
// Test a version string with a build number
mojo.parseVersion( "1.2.3-4-SNAPSHOT" );
Expand All @@ -188,17 +186,18 @@ public void testSnapshotVersionStringWithBuildNumber()

}

public class TestParseNextVersion
@Nested
class TestParseNextVersion
{
private Properties props;

private ParseVersionMojo mojo;

@BeforeMethod
@BeforeEach
public void beforeClass()
{
props = new Properties();
mojo = new TestParseVersionMojo( props );
mojo = new TestParseVersionMojo(props);
mojo.setPropertyPrefix( "parsed" );
mojo.setFormattedPropertyPrefix( "formatted" );
// The settings should in line with the given defaultValues of the parameters
Expand All @@ -210,7 +209,7 @@ public void beforeClass()
}

@Test
public void testJunkVersion()
public void checkJunkVersion()
{
mojo.parseVersion( "junk" );

Expand Down Expand Up @@ -290,18 +289,18 @@ public void testVersionStringWithBuildNumber()

}


public class TestFormattedVersion
@Nested
class TestFormattedVersion
{
private Properties props;

private ParseVersionMojo mojo;

@BeforeMethod
@BeforeEach
public void beforeClass()
{
props = new Properties();
mojo = new TestParseVersionMojo( props );
mojo = new TestParseVersionMojo(props);
mojo.setPropertyPrefix( "parsed" );
mojo.setFormattedPropertyPrefix( "formatted" );
// The settings should in line with the given defaultValues of the parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,83 +19,72 @@
* under the License.
*/

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
*/
public class VersionInformationTest
{

private VersionInformation createVersion( String version )
{
return new VersionInformation( version );
}

private void assertVersion( VersionInformation v, int major, int minor, int patch, long buildNumber,
String qualifier )
{
assertThat( v.getMajor() ).isEqualTo( major );
assertThat( v.getMinor() ).isEqualTo( minor );
assertThat( v.getPatch() ).isEqualTo( patch );
assertThat( v.getBuildNumber() ).isEqualTo( buildNumber );
assertThat( v.getQualifier() ).isEqualTo( qualifier );
}

// @formatter:off
@DataProvider
public Object[][] createVersions() {
return new Object[][] {
{ "1", 1, 0, 0, 0, null },
return new Object[][] {
{ "1", 1, 0, 0, 0, null },
{ "1-23", 1, 0, 0, 23, null },
{ "1-23.heger", 1, 0, 0, 23, ".heger" },
{ "1-23.heger", 1, 0, 0, 23, ".heger" },
{ "1.2-45", 1, 2, 0, 45, null },
{ "1.2-45.qual", 1, 2, 0, 45, ".qual" },
{ "1.2-45.qual", 1, 2, 0, 45, ".qual" },
{ "1.2-45-qual", 1, 2, 0, 45, "-qual" },
{ "3.2", 3, 2, 0, 0, null },
{ "5.7.1", 5, 7, 1, 0, null },
{ "3.2", 3, 2, 0, 0, null },
{ "5.7.1", 5, 7, 1, 0, null },
{ "1.9.04-0012", 1, 9, 4, 12, null },
{ "01.9.04-0012", 1, 9, 4, 12, null },
{ "01.9.04-0012", 1, 9, 4, 12, null },
{ "20.03.5-22", 20, 3, 5, 22, null },
{ "20.03.5-056", 20, 3, 5, 56, null },
{ "20.03.5-056", 20, 3, 5, 56, null },
{ "20.4.06.0-SNAPSHOT", 20, 4, 6, 0, "0-SNAPSHOT" },
{ "1.2.3-SNAPSHOT", 1, 2, 3, 0, "SNAPSHOT" },
{ "1.2.3-01-SNAPSHOT", 1, 2, 3, 1, "-SNAPSHOT" },
{ "1.2.3-SNAPSHOT", 1, 2, 3, 0, "SNAPSHOT" },
{ "1.2.3-01-SNAPSHOT", 1, 2, 3, 1, "-SNAPSHOT" },
{ "1.2.3-1-SNAPSHOT", 1, 2, 3, 1, "-SNAPSHOT" },
{ "20.03.5.2016060708", 20, 3, 5, 0, "2016060708" },
{ "20.03.5-23-2016060708", 20, 3, 5, 23, "-2016060708" },
{ "20.03.5-23.2016060708", 20, 3, 5, 23, ".2016060708" },
{ "20.03.5-111.anton", 20, 3, 5, 111, ".anton" },
{ "20.03.5-111.anton", 20, 3, 5, 111, ".anton" },
{ "20.03.5.22-SNAPSHOT", 20, 3, 5, 0, "22-SNAPSHOT" },
{ "20.03.5.XYZ.345", 20, 3, 5, 0, "XYZ.345" },
{ "20.03.5.XYZ.345", 20, 3, 5, 0, "XYZ.345" },
{ "20-88.03.5.XYZ.345", 20, 0, 0, 88, ".03.5.XYZ.345" },
{ "20.4-88.03.5.XYZ.345", 20, 4, 0, 88, ".03.5.XYZ.345" },
{ "020.04-88.03.5.XYZ.345", 20, 4, 0, 88, ".03.5.XYZ.345" },
{ "20.7.12-88.03.5.XYZ.345", 20, 7, 12, 88, ".03.5.XYZ.345" },
{ "20.03.5-111-34-anton", 20, 3, 5, 111, "-34-anton" },
{ "20.03.5-111-34.anton", 20, 3, 5, 111, "-34.anton" },
{ "junk", 0, 0, 0, 0, "junk" },
{ "2.3.4-beta_5", 2, 3, 4, 0, "beta_5" },
{ "20.03.5-111-34.anton", 20, 3, 5, 111, "-34.anton" },
{ "junk", 0, 0, 0, 0, "junk" },
{ "2.3.4-beta_5", 2, 3, 4, 0, "beta_5" },
{ "2.3.4.beta_5", 2, 3, 4, 0, "beta_5" },
{ "1.2.3-20171002135756", 1, 2, 3, 20171002135756l, null }
{ "1.2.3-20171002135756", 1, 2, 3, 20171002135756L, null }
};
}
// @formatter:on

@Test( dataProvider = "createVersions" )
@ParameterizedTest
@MethodSource("createVersions")
public void checkVersions( String version, int major, int minor, int patch, long buildNumber, String qualifier )
{
VersionInformation vi = createVersion( version );
assertVersion( vi, major, minor, patch, buildNumber, qualifier );
}
VersionInformation vi = new VersionInformation(version);
assertEquals( vi.getMajor(), major);
assertEquals( vi.getMinor(), minor);
assertEquals( vi.getPatch(), patch);
assertEquals( vi.getBuildNumber(), buildNumber);
assertEquals( vi.getQualifier(), qualifier);
}

@Test( expectedExceptions = NumberFormatException.class )
@Test
public void shouldFaileWithNumberFormatException()
{
String version = "999999999999.12345678.12.beta_5";
createVersion( version );
assertThrows(NumberFormatException.class, () -> new VersionInformation("999999999999.12345678.12.beta_5"));
}
}

0 comments on commit e9d7ed2

Please sign in to comment.