Skip to content

Commit

Permalink
Migrate tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Jun 30, 2023
1 parent ae3f9a8 commit c8f84db
Show file tree
Hide file tree
Showing 15 changed files with 465 additions and 557 deletions.
32 changes: 24 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,36 @@ ext.isReleaseVersion = provider {
!version.endsWith("SNAPSHOT")
}

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
dependencies {
testImplementation platform(testLibs.junit.bom)
}

java {
withJavadocJar()
withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
testImplementation project.testLibs.junit
testing {
suites {
configureEach {
useJUnitJupiter()
dependencies {
implementation.bundle(testLibs.bundles.junit)
}
targets {
configureEach {
testTask.configure {
testLogging {
events "passed", "skipped", "failed"
}
systemProperty 'file.encoding', 'UTF-8'
}
}
}
}
}
}

sourceSets {
Expand All @@ -55,10 +75,6 @@ tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}

tasks.withType(Test).configureEach {
systemProperty 'file.encoding', 'UTF-8'
}

tasks.withType(Jar).configureEach {
// General manifest info
manifest {
Expand Down
7 changes: 5 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ dependencyResolutionManagement {

versionCatalogs {
testLibs {
version('junit', '4.13.1')
version('junit', '5.9.3')

library('junit', 'junit', 'junit').versionRef('junit')
library('junit-bom', 'org.junit', 'junit-bom').versionRef('junit')
library('junit-jupiter', 'org.junit.jupiter', 'junit-jupiter').withoutVersion()

bundle('junit', ['junit-jupiter'])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,44 @@
*/
package org.firebirdsql.decimal;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

import static org.firebirdsql.decimal.util.ByteArrayHelper.hexToBytes;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
* @author <a href="mailto:[email protected]">Mark Rotteveel</a>
*/
@RunWith(Parameterized.class)
public class Decimal128ByteConversionTest {

@Parameterized.Parameter
public String description;
@Parameterized.Parameter(1)
public byte[] sourceBytes;
@Parameterized.Parameter(2)
public Decimal128 decimalValue;
@Parameterized.Parameter(3)
public byte[] targetBytes;
class Decimal128ByteConversionTest {

@Test
public void testConversionFromBytesToDecimal128() {
assumeTrue("No source bytes for " + description, sourceBytes != null);
@SuppressWarnings("unused")
@ParameterizedTest(name = "{index}: value {0} ({2})")
@MethodSource("data")
void testConversionFromBytesToDecimal128(String description, byte[] sourceBytes, Decimal128 decimalValue,
byte[] targetBytes) {
assumeTrue(sourceBytes != null, "No source bytes for " + description);
Decimal128 result = Decimal128.parseBytes(sourceBytes);

assertEquals("Expected " + description, decimalValue, result);
assertEquals(decimalValue, result, "Expected " + description);
}

@Test
public void testConversionFromDecimal128ToBytes() {
assumeTrue("No target bytes for " + description, targetBytes != null);
@SuppressWarnings("unused")
@ParameterizedTest(name = "{index}: value {0} ({2})")
@MethodSource("data")
void testConversionFromDecimal128ToBytes(String description, byte[] sourceBytes, Decimal128 decimalValue,
byte[] targetBytes) {
assumeTrue(targetBytes != null, "No target bytes for " + description);
byte[] result = decimalValue.toBytes();

assertArrayEquals(targetBytes, result);
}

@Parameterized.Parameters(name = "{index}: value {0} ({2})")
public static Collection<Object[]> data() {
return Arrays.asList(
static Stream<Arguments> data() {
return Stream.of(
testCase("POSITIVE_INFINITY",
new byte[] { 0b0_11110_00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
Decimal128.POSITIVE_INFINITY),
Expand Down Expand Up @@ -446,7 +437,7 @@ public static Collection<Object[]> data() {
* String encoding of the decimal value (compatible with parsing by {@link BigDecimal}
* @return Test case data
*/
private static Object[] testCase(String sourceEncodedString, String decimalString) {
private static Arguments testCase(String sourceEncodedString, String decimalString) {
return testCase(decimalString, hexToBytes(sourceEncodedString), dec(decimalString));
}

Expand All @@ -464,7 +455,7 @@ private static Object[] testCase(String sourceEncodedString, String decimalStrin
* Hex string of binary encoding of decimal (target)
* @return Test case data
*/
private static Object[] testCase(String sourceEncodedString, String decimalString, String targetEncodedString) {
private static Arguments testCase(String sourceEncodedString, String decimalString, String targetEncodedString) {
return testCase(decimalString, hexToBytes(sourceEncodedString), dec(decimalString),
hexToBytes(targetEncodedString));
}
Expand All @@ -481,7 +472,7 @@ private static Object[] testCase(String sourceEncodedString, String decimalStrin
* Decimal128 value
* @return Test case data
*/
private static Object[] testCase(String description, byte[] sourceBytes, Decimal128 decimal128Value) {
private static Arguments testCase(String description, byte[] sourceBytes, Decimal128 decimal128Value) {
return testCase(description, sourceBytes, decimal128Value, sourceBytes);
}

Expand All @@ -499,9 +490,9 @@ private static Object[] testCase(String description, byte[] sourceBytes, Decimal
* Binary encoding of decimal (target)
* @return Test case data
*/
private static Object[] testCase(String description, byte[] sourceBytes, Decimal128 decimal128Value,
private static Arguments testCase(String description, byte[] sourceBytes, Decimal128 decimal128Value,
byte[] targetBytes) {
return new Object[] { description, sourceBytes, decimal128Value, targetBytes };
return Arguments.of(description, sourceBytes, decimal128Value, targetBytes);
}

private static Decimal128 dec(String decimalString) {
Expand Down
Loading

0 comments on commit c8f84db

Please sign in to comment.