From c8f84dbd0bd05c9f08898f994e6a1fc0016a7d00 Mon Sep 17 00:00:00 2001 From: Mark Rotteveel Date: Fri, 30 Jun 2023 11:11:55 +0200 Subject: [PATCH] Migrate tests to JUnit 5 --- build.gradle | 32 +++- settings.gradle | 7 +- .../decimal/Decimal128ByteConversionTest.java | 65 ++++---- .../firebirdsql/decimal/Decimal128Test.java | 148 +++++++++-------- .../decimal/Decimal128ValueOfTest.java | 42 ++--- .../decimal/Decimal32ByteConversionTest.java | 65 ++++---- .../firebirdsql/decimal/Decimal32Test.java | 151 +++++++++--------- .../decimal/Decimal32ValueOfTest.java | 42 ++--- .../decimal/Decimal64ByteConversionTest.java | 65 ++++---- .../firebirdsql/decimal/Decimal64Test.java | 144 ++++++++--------- .../decimal/Decimal64ValueOfTest.java | 42 ++--- .../firebirdsql/decimal/DecimalCodecTest.java | 11 +- .../decimal/DecimalFormatTest.java | 104 ++++++------ .../decimal/DecimalTypeFromFirstByteTest.java | 83 ++++------ .../org/firebirdsql/decimal/ExamplesTest.java | 21 ++- 15 files changed, 465 insertions(+), 557 deletions(-) diff --git a/build.gradle b/build.gradle index 2803e7e..3e5fbbe 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { @@ -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 { diff --git a/settings.gradle b/settings.gradle index 95d564f..0ab4fe9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -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']) } } } diff --git a/src/test/java/org/firebirdsql/decimal/Decimal128ByteConversionTest.java b/src/test/java/org/firebirdsql/decimal/Decimal128ByteConversionTest.java index 8d13850..4b926fe 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal128ByteConversionTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal128ByteConversionTest.java @@ -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 Mark Rotteveel - */ -@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 data() { - return Arrays.asList( + static Stream 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), @@ -446,7 +437,7 @@ public static Collection 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)); } @@ -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)); } @@ -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); } @@ -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) { diff --git a/src/test/java/org/firebirdsql/decimal/Decimal128Test.java b/src/test/java/org/firebirdsql/decimal/Decimal128Test.java index e787ede..4b5f70c 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal128Test.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal128Test.java @@ -21,33 +21,32 @@ */ package org.firebirdsql.decimal; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -public class Decimal128Test { - - @Rule - public final ExpectedException expectedException = ExpectedException.none(); +class Decimal128Test { private static final Decimal128 POSITIVE_ZERO = Decimal128.valueOf(BigDecimal.ZERO); private static final Decimal128 NEGATIVE_ZERO = POSITIVE_ZERO.negate(); @Test - public void valueOf_Decimal128_isIdentity() { + void valueOf_Decimal128_isIdentity() { Decimal128 decimal128Value = Decimal128.valueOf("123"); assertSame(decimal128Value, Decimal128.valueOf(decimal128Value)); } @Test - public void valueOf_Decimal32_conversion() { + void valueOf_Decimal32_conversion() { Decimal32 decimal32Value = Decimal32.valueOf("123"); Decimal128 decimal128Value = Decimal128.valueOf("123"); @@ -55,7 +54,7 @@ public void valueOf_Decimal32_conversion() { } @Test - public void valueOf_Decimal64_conversion() { + void valueOf_Decimal64_conversion() { Decimal64 decimal64Value = Decimal64.valueOf("123"); Decimal128 decimal128Value = Decimal128.valueOf("123"); @@ -63,7 +62,7 @@ public void valueOf_Decimal64_conversion() { } @Test - public void valueOf_Decimal32_full_precision() { + void valueOf_Decimal32_full_precision() { Decimal32 decimal32Value = Decimal32.valueOf("1.234567"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567"); @@ -71,7 +70,7 @@ public void valueOf_Decimal32_full_precision() { } @Test - public void valueOf_Decimal64_full_precision() { + void valueOf_Decimal64_full_precision() { Decimal64 decimal64Value = Decimal64.valueOf("1.234567890123456"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567890123456"); @@ -79,7 +78,7 @@ public void valueOf_Decimal64_full_precision() { } @Test - public void valueOf_Decimal32_small() { + void valueOf_Decimal32_small() { Decimal32 decimal32Value = Decimal32.valueOf("1.234567E-95"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567E-95"); @@ -87,7 +86,7 @@ public void valueOf_Decimal32_small() { } @Test - public void valueOf_Decimal64_small() { + void valueOf_Decimal64_small() { Decimal64 decimal64Value = Decimal64.valueOf("1.234567890123456E-383"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567890123456E-383"); @@ -95,7 +94,7 @@ public void valueOf_Decimal64_small() { } @Test - public void valueOf_Decimal32_large() { + void valueOf_Decimal32_large() { Decimal64 decimal32Value = Decimal64.valueOf("1.234567E96"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567E96"); @@ -103,7 +102,7 @@ public void valueOf_Decimal32_large() { } @Test - public void valueOf_Decimal64_large() { + void valueOf_Decimal64_large() { Decimal64 decimal64Value = Decimal64.valueOf("1.234567890123456E384"); Decimal128 decimal128Value = Decimal128.valueOf("1.234567890123456E384"); @@ -111,7 +110,7 @@ public void valueOf_Decimal64_large() { } @Test - public void valueOf_specials() { + void valueOf_specials() { for (Decimal decimal : Arrays.asList(Decimal32.POSITIVE_INFINITY, Decimal64.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY)) { assertSame(Decimal128.POSITIVE_INFINITY, Decimal128.valueOf(decimal)); @@ -139,7 +138,7 @@ public void valueOf_specials() { } @Test - public void toBigDecimal_finiteValue() { + void toBigDecimal_finiteValue() { String decimalString = "1.23456E10"; Decimal128 decimal128Value = Decimal128.valueOf(decimalString); BigDecimal bigDecimalValue = new BigDecimal(decimalString); @@ -148,7 +147,7 @@ public void toBigDecimal_finiteValue() { } @Test - public void toBigDecimal_specials() { + void toBigDecimal_specials() { for (Decimal128 special : Arrays.asList(Decimal128.POSITIVE_INFINITY, Decimal128.NEGATIVE_INFINITY, Decimal128.POSITIVE_NAN, Decimal128.NEGATIVE_NAN, Decimal128.POSITIVE_SIGNALING_NAN, Decimal128.NEGATIVE_SIGNALING_NAN)) { @@ -156,14 +155,14 @@ public void toBigDecimal_specials() { special.toBigDecimal(); fail("toBigDecimal should have thrown DecimalInconvertibleException for " + special); } catch (DecimalInconvertibleException e) { - assertEquals("DecimalInconvertibleException.getDecimalType", special.getType(), e.getDecimalType()); - assertEquals("DecimalInconvertibleException.getSignum", special.signum(), e.getSignum()); + assertEquals(special.getType(), e.getDecimalType(), "DecimalInconvertibleException.getDecimalType"); + assertEquals(special.signum(), e.getSignum(), "DecimalInconvertibleException.getSignum"); } } } @Test - public void doubleValue_finiteValue() { + void doubleValue_finiteValue() { String decimalString = "1.23456E10"; Decimal128 decimal128Value = Decimal128.valueOf(decimalString); @@ -171,134 +170,133 @@ public void doubleValue_finiteValue() { } @Test - public void doubleValue_positiveInfinity() { + void doubleValue_positiveInfinity() { assertEquals(Double.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_negativeInfinity() { + void doubleValue_negativeInfinity() { assertEquals(Double.NEGATIVE_INFINITY, Decimal128.NEGATIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_NaNs() { + void doubleValue_NaNs() { for (Decimal128 special : Arrays.asList(Decimal128.POSITIVE_NAN, Decimal128.NEGATIVE_NAN, Decimal128.POSITIVE_SIGNALING_NAN, Decimal128.NEGATIVE_SIGNALING_NAN)) { - assertTrue("NaN for " + special, Double.isNaN(special.doubleValue())); + assertTrue(Double.isNaN(special.doubleValue()), "NaN for " + special); } } @Test - public void valueOf_finiteDouble() { + void valueOf_finiteDouble() { assertEquals(Decimal128.valueOf("1.23456"), Decimal128.valueOf(1.23456)); } @Test - public void valueOf_doubleMax_noOverflow() { + void valueOf_doubleMax_noOverflow() { assertEquals(Decimal128.valueOf("1.7976931348623157E+308"), Decimal128.valueOf(Double.MAX_VALUE, OverflowHandling.THROW_EXCEPTION)); } @Test - public void valueOf_doublePositiveInfinity() { + void valueOf_doublePositiveInfinity() { assertSame(Decimal128.POSITIVE_INFINITY, Decimal128.valueOf(Double.POSITIVE_INFINITY)); } @Test - public void valueOf_doubleNegativeInfinity() { + void valueOf_doubleNegativeInfinity() { assertSame(Decimal128.NEGATIVE_INFINITY, Decimal128.valueOf(Double.NEGATIVE_INFINITY)); } @Test - public void valueOf_doubleNaN() { + void valueOf_doubleNaN() { assertSame(Decimal128.POSITIVE_NAN, Decimal128.valueOf(Double.NaN)); } @Test - public void valueOf_stringOutOfRange_toPositiveInfinity() { + void valueOf_stringOutOfRange_toPositiveInfinity() { assertSame(Decimal128.POSITIVE_INFINITY, Decimal128.valueOf("9.9E10000")); } @Test - public void valueOf_stringOutOfRange_toNegativeInfinity() { + void valueOf_stringOutOfRange_toNegativeInfinity() { assertSame(Decimal128.NEGATIVE_INFINITY, Decimal128.valueOf("-9.9E10000")); } @Test - public void valueOf_stringOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -9999 is out of range for this type"); - - Decimal128.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION); + void valueOf_stringOutOfRange_throwException() { + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal128.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -9999 is out of range for this type", exception.getMessage()); } @Test - public void toDecimal_Decimal128_Decimal128() { + void toDecimal_Decimal128_Decimal128() { Decimal128 value = Decimal128.valueOf("1.23456"); assertSame(value, value.toDecimal(Decimal128.class)); } @Test - public void toDecimal_Decimal128_Decimal32() { + void toDecimal_Decimal128_Decimal32() { Decimal128 value = Decimal128.valueOf("1.23456"); assertEquals(Decimal32.valueOf("1.23456"), value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal128_Decimal32_valueOutOfRange_toInfinity() { + void toDecimal_Decimal128_Decimal32_valueOutOfRange_toInfinity() { Decimal128 value = Decimal128.valueOf("1.23456E97"); assertSame(Decimal32.POSITIVE_INFINITY, value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal128_Decimal32_valueOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -92 is out of range for this type"); + void toDecimal_Decimal128_Decimal32_valueOutOfRange_throwException() { Decimal128 value = Decimal128.valueOf("1.23456E97"); - value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -92 is out of range for this type", exception.getMessage()); } @Test - public void toDecimal_Decimal128_Decimal64() { + void toDecimal_Decimal128_Decimal64() { Decimal128 value = Decimal128.valueOf("1.23456"); assertEquals(Decimal64.valueOf("1.23456"), value.toDecimal(Decimal64.class)); } @Test - public void toDecimal_Decimal128_Decimal64_valueOutOfRange_toInfinity() { + void toDecimal_Decimal128_Decimal64_valueOutOfRange_toInfinity() { Decimal128 value = Decimal128.valueOf("1.23456E385"); assertSame(Decimal32.POSITIVE_INFINITY, value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal128_Decimal64_valueOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -380 is out of range for this type"); + void toDecimal_Decimal128_Decimal64_valueOutOfRange_throwException() { Decimal128 value = Decimal128.valueOf("1.23456E385"); - value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -380 is out of range for this type", exception.getMessage()); } @Test - public void validateConstant_POSITIVE_ZERO() { + void validateConstant_POSITIVE_ZERO() { assertEquals(BigDecimal.ZERO, POSITIVE_ZERO.toBigDecimal()); assertEquals(Signum.POSITIVE, POSITIVE_ZERO.signum()); } @Test - public void validateConstant_NEGATIVE_ZERO() { + void validateConstant_NEGATIVE_ZERO() { assertEquals(BigDecimal.ZERO, NEGATIVE_ZERO.toBigDecimal()); assertEquals(Signum.NEGATIVE, NEGATIVE_ZERO.signum()); } @Test - public void negate_One() { + void negate_One() { Decimal128 negativeOne = Decimal128.valueOf(BigDecimal.ONE).negate(); assertEquals(Decimal128.valueOf(BigDecimal.ONE.negate()), @@ -308,7 +306,7 @@ public void negate_One() { } @Test - public void negate_positiveZero() { + void negate_positiveZero() { Decimal128 negativeZero = POSITIVE_ZERO.negate(); assertEquals(NEGATIVE_ZERO, negativeZero); @@ -317,7 +315,7 @@ public void negate_positiveZero() { } @Test - public void negate_negativeZero() { + void negate_negativeZero() { Decimal128 positiveZero = NEGATIVE_ZERO.negate(); assertEquals(POSITIVE_ZERO, positiveZero); @@ -326,91 +324,89 @@ public void negate_negativeZero() { } @Test - public void negate_positiveInfinity() { + void negate_positiveInfinity() { assertEquals(Decimal128.NEGATIVE_INFINITY, Decimal128.POSITIVE_INFINITY.negate()); } @Test - public void negate_negativeInfinity() { + void negate_negativeInfinity() { assertEquals(Decimal128.POSITIVE_INFINITY, Decimal128.NEGATIVE_INFINITY.negate()); } @Test - public void negate_positiveNaN() { + void negate_positiveNaN() { assertEquals(Decimal128.NEGATIVE_NAN, Decimal128.POSITIVE_NAN.negate()); } @Test - public void negate_negativeNaN() { + void negate_negativeNaN() { assertEquals(Decimal128.POSITIVE_NAN, Decimal128.NEGATIVE_NAN.negate()); } @Test - public void negate_positiveSignallingNaN() { + void negate_positiveSignallingNaN() { assertEquals(Decimal128.NEGATIVE_SIGNALING_NAN, Decimal128.POSITIVE_SIGNALING_NAN.negate()); } @Test - public void negate_negativeSignallingNaN() { + void negate_negativeSignallingNaN() { assertEquals(Decimal128.POSITIVE_SIGNALING_NAN, Decimal128.NEGATIVE_SIGNALING_NAN.negate()); } @Test - public void valueOfExact_BigInteger_min() { + void valueOfExact_BigInteger_min() { final BigInteger value = new BigInteger("-9999999999999999999999999999999999"); assertEquals(new BigDecimal("-9999999999999999999999999999999999"), Decimal128.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_min_minusOne() { + void valueOfExact_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999999999999999999999999999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal128.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal128.valueOfExact(value)); } @Test - public void valueOfExact_BigInteger_max() { + void valueOfExact_BigInteger_max() { final BigInteger value = new BigInteger("9999999999999999999999999999999999"); assertEquals(new BigDecimal("9999999999999999999999999999999999"), Decimal128.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_max_plusOne() { + void valueOfExact_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999999999999999999999999999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal128.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal128.valueOfExact(value)); } @Test - public void valueOf_BigInteger_min() { + void valueOf_BigInteger_min() { final BigInteger value = new BigInteger("-9999999999999999999999999999999999"); assertEquals(new BigDecimal("-9999999999999999999999999999999999"), Decimal128.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_min_minusOne() { + void valueOf_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999999999999999999999999999999").subtract(BigInteger.ONE); assertEquals(new BigDecimal("-1000000000000000000000000000000000E+1"), Decimal128.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max() { + void valueOf_BigInteger_max() { final BigInteger value = new BigInteger("9999999999999999999999999999999999"); assertEquals(new BigDecimal("9999999999999999999999999999999999"), Decimal128.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max_plusOne() { + void valueOf_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999999999999999999999999999999").add(BigInteger.ONE); assertEquals(new BigDecimal("1000000000000000000000000000000000E+1"), Decimal128.valueOf(value).toBigDecimal()); } - + } diff --git a/src/test/java/org/firebirdsql/decimal/Decimal128ValueOfTest.java b/src/test/java/org/firebirdsql/decimal/Decimal128ValueOfTest.java index 170b513..56f3973 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal128ValueOfTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal128ValueOfTest.java @@ -21,16 +21,14 @@ */ 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.ArrayList; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test for the {@link Decimal128#valueOf(String)} method, the rounding applied, and handling of specials. @@ -40,30 +38,18 @@ * * @author Mark Rotteveel */ -@RunWith(Parameterized.class) -public class Decimal128ValueOfTest { +class Decimal128ValueOfTest { - /** - * Value to supply to the valueOf method - */ - @Parameterized.Parameter - public String sourceValue; - /** - * Value of the toString() of the resulting value - */ - @Parameterized.Parameter(1) - public String expectedValue; - - @Test - public void valueOfTest() { + @ParameterizedTest(name = "{index}: value {0} (expect {1}))") + @MethodSource("data") + void valueOfTest(String sourceValue, String expectedValue) { Decimal128 value = Decimal128.valueOf(sourceValue); assertEquals(expectedValue, value.toString()); } - @Parameterized.Parameters(name = "{index}: value {0} (expect {1}))") - public static Collection data() { - return new ArrayList<>(Arrays.asList( + static Stream data() { + return Stream.of( // Zero testCase("0", "0"), testCase("+0", "0"), @@ -126,10 +112,10 @@ public static Collection data() { testCase("-sNaN", "-sNaN"), testCase("-snan", "-sNaN"), testCase("-SNAN", "-sNaN") - )); + ); } - private static Object[] testCase(String source, String result) { - return new Object[] { source, result }; + private static Arguments testCase(String source, String result) { + return Arguments.of(source, result); } } diff --git a/src/test/java/org/firebirdsql/decimal/Decimal32ByteConversionTest.java b/src/test/java/org/firebirdsql/decimal/Decimal32ByteConversionTest.java index 2a6232f..ec9c6e6 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal32ByteConversionTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal32ByteConversionTest.java @@ -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 Mark Rotteveel - */ -@RunWith(Parameterized.class) -public class Decimal32ByteConversionTest { - - @Parameterized.Parameter - public String description; - @Parameterized.Parameter(1) - public byte[] sourceBytes; - @Parameterized.Parameter(2) - public Decimal32 decimalValue; - @Parameterized.Parameter(3) - public byte[] targetBytes; +class Decimal32ByteConversionTest { - @Test - public void testConversionFromBytesToDecimal32() { - assumeTrue("No source bytes for " + description, sourceBytes != null); + @SuppressWarnings("unused") + @ParameterizedTest(name = "{index}: value {0} ({2})") + @MethodSource("data") + void testConversionFromBytesToDecimal32(String description, byte[] sourceBytes, Decimal32 decimalValue, + byte[] targetBytes) { + assumeTrue(sourceBytes != null, "No source bytes for " + description); Decimal32 result = Decimal32.parseBytes(sourceBytes); - assertEquals("Expected " + description, decimalValue, result); + assertEquals(decimalValue, result, "Expected " + description); } - @Test - public void testConversionFromDecimal32ToBytes() { - assumeTrue("No target bytes for " + description, targetBytes != null); + @SuppressWarnings("unused") + @ParameterizedTest(name = "{index}: value {0} ({2})") + @MethodSource("data") + void testConversionFromDecimal32ToBytes(String description, byte[] sourceBytes, Decimal32 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 data() { - return Arrays.asList( + static Stream data() { + return Stream.of( testCase("POSITIVE_INFINITY", new byte[] { 0b0_11110_00, 0, 0, 0 }, Decimal32.POSITIVE_INFINITY), testCase("POSITIVE_INFINITY", @@ -328,7 +319,7 @@ public static Collection 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)); } @@ -346,7 +337,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)); } @@ -363,7 +354,7 @@ private static Object[] testCase(String sourceEncodedString, String decimalStrin * Decimal32 value * @return Test case data */ - private static Object[] testCase(String description, byte[] sourceBytes, Decimal32 decimal32Value) { + private static Arguments testCase(String description, byte[] sourceBytes, Decimal32 decimal32Value) { return testCase(description, sourceBytes, decimal32Value, sourceBytes); } @@ -381,9 +372,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, Decimal32 decimal32Value, + private static Arguments testCase(String description, byte[] sourceBytes, Decimal32 decimal32Value, byte[] targetBytes) { - return new Object[] { description, sourceBytes, decimal32Value, targetBytes }; + return Arguments.of(description, sourceBytes, decimal32Value, targetBytes); } private static Decimal32 dec(String decimalString) { diff --git a/src/test/java/org/firebirdsql/decimal/Decimal32Test.java b/src/test/java/org/firebirdsql/decimal/Decimal32Test.java index 537b4cb..bbf027f 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal32Test.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal32Test.java @@ -21,33 +21,32 @@ */ package org.firebirdsql.decimal; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -public class Decimal32Test { - - @Rule - public final ExpectedException expectedException = ExpectedException.none(); +class Decimal32Test { private static final Decimal32 POSITIVE_ZERO = Decimal32.valueOf(BigDecimal.ZERO); private static final Decimal32 NEGATIVE_ZERO = POSITIVE_ZERO.negate(); @Test - public void valueOf_Decimal32_isIdentity() { + void valueOf_Decimal32_isIdentity() { Decimal32 decimal32Value = Decimal32.valueOf("123"); assertSame(decimal32Value, Decimal32.valueOf(decimal32Value)); } @Test - public void valueOf_Decimal64_conversion() { + void valueOf_Decimal64_conversion() { Decimal64 decimal64Value = Decimal64.valueOf("123"); Decimal32 decimal32Value = Decimal32.valueOf("123"); @@ -55,7 +54,7 @@ public void valueOf_Decimal64_conversion() { } @Test - public void valueOf_Decimal128_conversion() { + void valueOf_Decimal128_conversion() { Decimal128 decimal128Value = Decimal128.valueOf("123"); Decimal32 decimal32Value = Decimal32.valueOf("123"); @@ -63,7 +62,7 @@ public void valueOf_Decimal128_conversion() { } @Test - public void valueOf_Decimal64_rounding() { + void valueOf_Decimal64_rounding() { Decimal64 decimal64Value = Decimal64.valueOf("1.234567890123456"); Decimal32 decimal32Value = Decimal32.valueOf("1.234568"); @@ -71,7 +70,7 @@ public void valueOf_Decimal64_rounding() { } @Test - public void valueOf_Decimal128_rounding() { + void valueOf_Decimal128_rounding() { Decimal128 decimal128Value = Decimal128.valueOf("1.23456789012345678901234568901234"); Decimal32 decimal32Value = Decimal32.valueOf("1.234568"); @@ -79,7 +78,7 @@ public void valueOf_Decimal128_rounding() { } @Test - public void valueOf_Decimal64_roundingToZero() { + void valueOf_Decimal64_roundingToZero() { Decimal64 decimal64Value = Decimal64.valueOf("1E-300"); Decimal32 decimal32Value = Decimal32.valueOf("0E-101"); @@ -87,7 +86,7 @@ public void valueOf_Decimal64_roundingToZero() { } @Test - public void valueOf_Decimal128_roundingToZero() { + void valueOf_Decimal128_roundingToZero() { Decimal128 decimal128Value = Decimal128.valueOf("1E-6000"); Decimal32 decimal32Value = Decimal32.valueOf("0E-101"); @@ -95,39 +94,39 @@ public void valueOf_Decimal128_roundingToZero() { } @Test - public void valueOf_Decimal64_overflowToInfinity() { + void valueOf_Decimal64_overflowToInfinity() { Decimal64 decimal64Value = Decimal64.valueOf("1E300"); assertEquals(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf(decimal64Value)); } @Test - public void valueOf_Decimal64_overflow_ThrowException() { + void valueOf_Decimal64_overflow_ThrowException() { Decimal64 decimal64Value = Decimal64.valueOf("1E300"); - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -300 is out of range for this type"); - Decimal32.valueOf(decimal64Value, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal32.valueOf(decimal64Value, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -300 is out of range for this type", exception.getMessage()); } @Test - public void valueOf_Decimal128_overflowToInfinity() { + void valueOf_Decimal128_overflowToInfinity() { Decimal128 decimal128Value = Decimal128.valueOf("1E6000"); assertEquals(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf(decimal128Value)); } @Test - public void valueOf_Decimal128_overflow_ThrowException() { + void valueOf_Decimal128_overflow_ThrowException() { Decimal128 decimal128Value = Decimal128.valueOf("1E6000"); - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -6000 is out of range for this type"); - Decimal32.valueOf(decimal128Value, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal32.valueOf(decimal128Value, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -6000 is out of range for this type", exception.getMessage()); } @Test - public void valueOf_specials() { + void valueOf_specials() { for (Decimal decimal : Arrays.asList(Decimal32.POSITIVE_INFINITY, Decimal64.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY)) { assertSame(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf(decimal)); @@ -155,7 +154,7 @@ public void valueOf_specials() { } @Test - public void toBigDecimal_finiteValue() { + void toBigDecimal_finiteValue() { String decimalString = "1.23456E10"; Decimal32 decimal32Value = Decimal32.valueOf(decimalString); BigDecimal bigDecimalValue = new BigDecimal(decimalString); @@ -164,7 +163,7 @@ public void toBigDecimal_finiteValue() { } @Test - public void toBigDecimal_specials() { + void toBigDecimal_specials() { for (Decimal32 special : Arrays.asList(Decimal32.POSITIVE_INFINITY, Decimal32.NEGATIVE_INFINITY, Decimal32.POSITIVE_NAN, Decimal32.NEGATIVE_NAN, Decimal32.POSITIVE_SIGNALING_NAN, Decimal32.NEGATIVE_SIGNALING_NAN)) { @@ -172,14 +171,14 @@ public void toBigDecimal_specials() { special.toBigDecimal(); fail("toBigDecimal should have thrown DecimalInconvertibleException for " + special); } catch (DecimalInconvertibleException e) { - assertEquals("DecimalInconvertibleException.getDecimalType", special.getType(), e.getDecimalType()); - assertEquals("DecimalInconvertibleException.getSignum", special.signum(), e.getSignum()); + assertEquals(special.getType(), e.getDecimalType(), "DecimalInconvertibleException.getDecimalType"); + assertEquals(special.signum(), e.getSignum(), "DecimalInconvertibleException.getSignum"); } } } @Test - public void doubleValue_finiteValue() { + void doubleValue_finiteValue() { String decimalString = "1.23456E10"; Decimal32 decimal32Value = Decimal32.valueOf(decimalString); @@ -187,109 +186,107 @@ public void doubleValue_finiteValue() { } @Test - public void doubleValue_positiveInfinity() { + void doubleValue_positiveInfinity() { assertEquals(Double.POSITIVE_INFINITY, Decimal32.POSITIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_negativeInfinity() { + void doubleValue_negativeInfinity() { assertEquals(Double.NEGATIVE_INFINITY, Decimal32.NEGATIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_NaNs() { + void doubleValue_NaNs() { for (Decimal32 special : Arrays.asList(Decimal32.POSITIVE_NAN, Decimal32.NEGATIVE_NAN, Decimal32.POSITIVE_SIGNALING_NAN, Decimal32.NEGATIVE_SIGNALING_NAN)) { - assertTrue("NaN for " + special, Double.isNaN(special.doubleValue())); + assertTrue(Double.isNaN(special.doubleValue()), "NaN for " + special); } } @Test - public void valueOf_finiteDouble() { + void valueOf_finiteDouble() { assertEquals(Decimal32.valueOf("1.23456"), Decimal32.valueOf(1.23456)); } @Test - public void valueOf_doublePositiveInfinity() { + void valueOf_doublePositiveInfinity() { assertSame(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf(Double.POSITIVE_INFINITY)); } @Test - public void valueOf_doubleNegativeInfinity() { + void valueOf_doubleNegativeInfinity() { assertSame(Decimal32.NEGATIVE_INFINITY, Decimal32.valueOf(Double.NEGATIVE_INFINITY)); } @Test - public void valueOf_doubleNaN() { + void valueOf_doubleNaN() { assertSame(Decimal32.POSITIVE_NAN, Decimal32.valueOf(Double.NaN)); } @Test - public void valueOf_doubleMax_OverflowToInfinity() { + void valueOf_doubleMax_OverflowToInfinity() { assertEquals(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf(Double.MAX_VALUE)); } @Test - public void valueOf_doubleMax_Overflow_ThrowException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -302 is out of range for this type"); - - Decimal32.valueOf(Double.MAX_VALUE, OverflowHandling.THROW_EXCEPTION); + void valueOf_doubleMax_Overflow_ThrowException() { + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal32.valueOf(Double.MAX_VALUE, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -302 is out of range for this type", exception.getMessage()); } @Test - public void valueOf_stringOutOfRange_toPositiveInfinity() { + void valueOf_stringOutOfRange_toPositiveInfinity() { assertSame(Decimal32.POSITIVE_INFINITY, Decimal32.valueOf("9.9E10000")); } @Test - public void valueOf_stringOutOfRange_toNegativeInfinity() { + void valueOf_stringOutOfRange_toNegativeInfinity() { assertSame(Decimal32.NEGATIVE_INFINITY, Decimal32.valueOf("-9.9E10000")); } @Test - public void valueOf_stringOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -9999 is out of range for this type"); - - Decimal32.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION); + void valueOf_stringOutOfRange_throwException() { + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal32.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -9999 is out of range for this type", exception.getMessage()); } @Test - public void toDecimal_Decimal32_Decimal32() { + void toDecimal_Decimal32_Decimal32() { Decimal32 value = Decimal32.valueOf("1.23456"); assertSame(value, value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal32_Decimal64() { + void toDecimal_Decimal32_Decimal64() { Decimal32 value = Decimal32.valueOf("1.23456"); assertEquals(Decimal64.valueOf("1.23456"), value.toDecimal(Decimal64.class)); } @Test - public void toDecimal_Decimal32_Decimal128() { + void toDecimal_Decimal32_Decimal128() { Decimal32 value = Decimal32.valueOf("1.23456"); assertEquals(Decimal128.valueOf("1.23456"), value.toDecimal(Decimal128.class)); } @Test - public void validateConstant_POSITIVE_ZERO() { + void validateConstant_POSITIVE_ZERO() { assertEquals(BigDecimal.ZERO, POSITIVE_ZERO.toBigDecimal()); assertEquals(Signum.POSITIVE, POSITIVE_ZERO.signum()); } @Test - public void validateConstant_NEGATIVE_ZERO() { + void validateConstant_NEGATIVE_ZERO() { assertEquals(BigDecimal.ZERO, NEGATIVE_ZERO.toBigDecimal()); assertEquals(Signum.NEGATIVE, NEGATIVE_ZERO.signum()); } @Test - public void negate_One() { + void negate_One() { Decimal32 negativeOne = Decimal32.valueOf(BigDecimal.ONE).negate(); assertEquals(Decimal32.valueOf(BigDecimal.ONE.negate()), @@ -299,7 +296,7 @@ public void negate_One() { } @Test - public void negate_positiveZero() { + void negate_positiveZero() { Decimal32 negativeZero = POSITIVE_ZERO.negate(); assertEquals(NEGATIVE_ZERO, negativeZero); @@ -308,7 +305,7 @@ public void negate_positiveZero() { } @Test - public void negate_negativeZero() { + void negate_negativeZero() { Decimal32 positiveZero = NEGATIVE_ZERO.negate(); assertEquals(POSITIVE_ZERO, positiveZero); @@ -317,88 +314,86 @@ public void negate_negativeZero() { } @Test - public void negate_positiveInfinity() { + void negate_positiveInfinity() { assertEquals(Decimal32.NEGATIVE_INFINITY, Decimal32.POSITIVE_INFINITY.negate()); } @Test - public void negate_negativeInfinity() { + void negate_negativeInfinity() { assertEquals(Decimal32.POSITIVE_INFINITY, Decimal32.NEGATIVE_INFINITY.negate()); } @Test - public void negate_positiveNaN() { + void negate_positiveNaN() { assertEquals(Decimal32.NEGATIVE_NAN, Decimal32.POSITIVE_NAN.negate()); } @Test - public void negate_negativeNaN() { + void negate_negativeNaN() { assertEquals(Decimal32.POSITIVE_NAN, Decimal32.NEGATIVE_NAN.negate()); } @Test - public void negate_positiveSignallingNaN() { + void negate_positiveSignallingNaN() { assertEquals(Decimal32.NEGATIVE_SIGNALING_NAN, Decimal32.POSITIVE_SIGNALING_NAN.negate()); } @Test - public void negate_negativeSignallingNaN() { + void negate_negativeSignallingNaN() { assertEquals(Decimal32.POSITIVE_SIGNALING_NAN, Decimal32.NEGATIVE_SIGNALING_NAN.negate()); } @Test - public void valueOfExact_BigInteger_min() { + void valueOfExact_BigInteger_min() { final BigInteger value = new BigInteger("-9999999"); assertEquals(new BigDecimal("-9999999"), Decimal32.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_min_minusOne() { + void valueOfExact_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal32.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal32.valueOfExact(value)); } @Test - public void valueOfExact_BigInteger_max() { + void valueOfExact_BigInteger_max() { final BigInteger value = new BigInteger("9999999"); assertEquals(new BigDecimal("9999999"), Decimal32.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_max_plusOne() { + void valueOfExact_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal32.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal32.valueOfExact(value)); } @Test - public void valueOf_BigInteger_min() { + void valueOf_BigInteger_min() { final BigInteger value = new BigInteger("-9999999"); assertEquals(new BigDecimal("-9999999"), Decimal32.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_min_minusOne() { + void valueOf_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999").subtract(BigInteger.ONE); assertEquals(new BigDecimal("-1000000E+1"), Decimal32.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max() { + void valueOf_BigInteger_max() { final BigInteger value = new BigInteger("9999999"); assertEquals(new BigDecimal("9999999"), Decimal32.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max_plusOne() { + void valueOf_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999").add(BigInteger.ONE); assertEquals(new BigDecimal("1000000E+1"), Decimal32.valueOf(value).toBigDecimal()); diff --git a/src/test/java/org/firebirdsql/decimal/Decimal32ValueOfTest.java b/src/test/java/org/firebirdsql/decimal/Decimal32ValueOfTest.java index d8439da..9aacaa3 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal32ValueOfTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal32ValueOfTest.java @@ -21,16 +21,14 @@ */ 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.ArrayList; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test for the {@link Decimal32#valueOf(String)} method, the rounding applied, and handling of specials. @@ -40,30 +38,18 @@ * * @author Mark Rotteveel */ -@RunWith(Parameterized.class) -public class Decimal32ValueOfTest { +class Decimal32ValueOfTest { - /** - * Value to supply to the valueOf method - */ - @Parameterized.Parameter - public String sourceValue; - /** - * Value of the toString() of the resulting value - */ - @Parameterized.Parameter(1) - public String expectedValue; - - @Test - public void valueOfTest() { + @ParameterizedTest(name = "{index}: value {0} (expect {1}))") + @MethodSource("data") + void valueOfTest(String sourceValue, String expectedValue) { Decimal32 value = Decimal32.valueOf(sourceValue); assertEquals(expectedValue, value.toString()); } - @Parameterized.Parameters(name = "{index}: value {0} (expect {1}))") - public static Collection data() { - return new ArrayList<>(Arrays.asList( + static Stream data() { + return Stream.of( // Zero testCase("0", "0"), testCase("+0", "0"), @@ -126,10 +112,10 @@ public static Collection data() { testCase("-sNaN", "-sNaN"), testCase("-snan", "-sNaN"), testCase("-SNAN", "-sNaN") - )); + ); } - private static Object[] testCase(String source, String result) { - return new Object[] { source, result }; + private static Arguments testCase(String source, String result) { + return Arguments.of(source, result); } } diff --git a/src/test/java/org/firebirdsql/decimal/Decimal64ByteConversionTest.java b/src/test/java/org/firebirdsql/decimal/Decimal64ByteConversionTest.java index 877e948..1feb1c2 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal64ByteConversionTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal64ByteConversionTest.java @@ -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 Mark Rotteveel - */ -@RunWith(Parameterized.class) -public class Decimal64ByteConversionTest { - - @Parameterized.Parameter - public String description; - @Parameterized.Parameter(1) - public byte[] sourceBytes; - @Parameterized.Parameter(2) - public Decimal64 decimalValue; - @Parameterized.Parameter(3) - public byte[] targetBytes; +class Decimal64ByteConversionTest { - @Test - public void testConversionFromBytesToDecimal64() { - assumeTrue("No source bytes for " + description, sourceBytes != null); + @SuppressWarnings("unused") + @ParameterizedTest(name = "{index}: value {0} ({2})") + @MethodSource("data") + void testConversionFromBytesToDecimal64(String description, byte[] sourceBytes, Decimal64 decimalValue, + byte[] targetBytes) { + assumeTrue(sourceBytes != null, "No source bytes for " + description); Decimal64 result = Decimal64.parseBytes(sourceBytes); - assertEquals("Expected " + description, decimalValue, result); + assertEquals(decimalValue, result, "Expected " + description); } - @Test - public void testConversionFromDecimal64ToBytes() { - assumeTrue("No target bytes for " + description, targetBytes != null); + @SuppressWarnings("unused") + @ParameterizedTest(name = "{index}: value {0} ({2})") + @MethodSource("data") + void testConversionFromDecimal64ToBytes(String description, byte[] sourceBytes, Decimal64 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 data() { - return Arrays.asList( + static Stream data() { + return Stream.of( testCase("POSITIVE_INFINITY", new byte[] { 0b0_11110_00, 0, 0, 0, 0, 0, 0, 0 }, Decimal64.POSITIVE_INFINITY), testCase("POSITIVE_INFINITY", @@ -434,7 +425,7 @@ public static Collection 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)); } @@ -452,7 +443,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)); } @@ -469,7 +460,7 @@ private static Object[] testCase(String sourceEncodedString, String decimalStrin * Decimal64 value * @return Test case data */ - private static Object[] testCase(String description, byte[] sourceBytes, Decimal64 decimal64Value) { + private static Arguments testCase(String description, byte[] sourceBytes, Decimal64 decimal64Value) { return testCase(description, sourceBytes, decimal64Value, sourceBytes); } @@ -487,9 +478,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, Decimal64 decimal64Value, + private static Arguments testCase(String description, byte[] sourceBytes, Decimal64 decimal64Value, byte[] targetBytes) { - return new Object[] { description, sourceBytes, decimal64Value, targetBytes }; + return Arguments.of(description, sourceBytes, decimal64Value, targetBytes); } private static Decimal64 dec(String decimalString) { diff --git a/src/test/java/org/firebirdsql/decimal/Decimal64Test.java b/src/test/java/org/firebirdsql/decimal/Decimal64Test.java index 6b872c2..2c95ddd 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal64Test.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal64Test.java @@ -21,33 +21,32 @@ */ package org.firebirdsql.decimal; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; -public class Decimal64Test { +class Decimal64Test { - @Rule - public final ExpectedException expectedException = ExpectedException.none(); - private static final Decimal64 POSITIVE_ZERO = Decimal64.valueOf(BigDecimal.ZERO); private static final Decimal64 NEGATIVE_ZERO = POSITIVE_ZERO.negate(); @Test - public void valueOf_Decimal64_isIdentity() { + void valueOf_Decimal64_isIdentity() { Decimal64 decimal64Value = Decimal64.valueOf("123"); assertSame(decimal64Value, Decimal64.valueOf(decimal64Value)); } @Test - public void valueOf_Decimal32_conversion() { + void valueOf_Decimal32_conversion() { Decimal32 decimal32Value = Decimal32.valueOf("123"); Decimal64 decimal64Value = Decimal64.valueOf("123"); @@ -55,7 +54,7 @@ public void valueOf_Decimal32_conversion() { } @Test - public void valueOf_Decimal128_conversion() { + void valueOf_Decimal128_conversion() { Decimal128 decimal128Value = Decimal128.valueOf("123"); Decimal64 decimal64Value = Decimal64.valueOf("123"); @@ -63,7 +62,7 @@ public void valueOf_Decimal128_conversion() { } @Test - public void valueOf_Decimal32_full_precision() { + void valueOf_Decimal32_full_precision() { Decimal32 decimal32Value = Decimal32.valueOf("1.234567"); Decimal64 decimal64Value = Decimal64.valueOf("1.234567"); @@ -71,7 +70,7 @@ public void valueOf_Decimal32_full_precision() { } @Test - public void valueOf_Decimal128_rounding() { + void valueOf_Decimal128_rounding() { Decimal128 decimal128Value = Decimal128.valueOf("1.23456789012345678901234568901234"); Decimal64 decimal64Value = Decimal64.valueOf("1.234567890123457"); @@ -79,7 +78,7 @@ public void valueOf_Decimal128_rounding() { } @Test - public void valueOf_Decimal32_small() { + void valueOf_Decimal32_small() { Decimal32 decimal32Value = Decimal32.valueOf("1.234567E-95"); Decimal64 decimal64Value = Decimal64.valueOf("1.234567E-95"); @@ -87,7 +86,7 @@ public void valueOf_Decimal32_small() { } @Test - public void valueOf_Decimal128_roundingToZero() { + void valueOf_Decimal128_roundingToZero() { Decimal128 decimal128Value = Decimal128.valueOf("1E-6000"); Decimal64 decimal64Value = Decimal64.valueOf("0E-398"); @@ -95,7 +94,7 @@ public void valueOf_Decimal128_roundingToZero() { } @Test - public void valueOf_Decimal32_large() { + void valueOf_Decimal32_large() { Decimal64 decimal32Value = Decimal64.valueOf("1.234567E96"); Decimal64 decimal64Value = Decimal64.valueOf("1.234567E96"); @@ -103,23 +102,23 @@ public void valueOf_Decimal32_large() { } @Test - public void valueOf_Decimal128_overflowToInfinity() { + void valueOf_Decimal128_overflowToInfinity() { Decimal128 decimal128Value = Decimal128.valueOf("1E6000"); assertEquals(Decimal64.POSITIVE_INFINITY, Decimal64.valueOf(decimal128Value)); } @Test - public void valueOf_Decimal128_overflow_ThrowException() { + void valueOf_Decimal128_overflow_ThrowException() { Decimal128 decimal128Value = Decimal128.valueOf("1E6000"); - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -6000 is out of range for this type"); - Decimal64.valueOf(decimal128Value, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal64.valueOf(decimal128Value, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -6000 is out of range for this type", exception.getMessage()); } @Test - public void valueOf_specials() { + void valueOf_specials() { for (Decimal decimal : Arrays.asList(Decimal32.POSITIVE_INFINITY, Decimal64.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY)) { assertSame(Decimal64.POSITIVE_INFINITY, Decimal64.valueOf(decimal)); @@ -147,7 +146,7 @@ public void valueOf_specials() { } @Test - public void toBigDecimal_finiteValue() { + void toBigDecimal_finiteValue() { String decimalString = "1.23456E10"; Decimal64 decimal64Value = Decimal64.valueOf(decimalString); BigDecimal bigDecimalValue = new BigDecimal(decimalString); @@ -156,7 +155,7 @@ public void toBigDecimal_finiteValue() { } @Test - public void toBigDecimal_specials() { + void toBigDecimal_specials() { for (Decimal64 special : Arrays.asList(Decimal64.POSITIVE_INFINITY, Decimal64.NEGATIVE_INFINITY, Decimal64.POSITIVE_NAN, Decimal64.NEGATIVE_NAN, Decimal64.POSITIVE_SIGNALING_NAN, Decimal64.NEGATIVE_SIGNALING_NAN)) { @@ -164,14 +163,14 @@ public void toBigDecimal_specials() { special.toBigDecimal(); fail("toBigDecimal should have thrown DecimalInconvertibleException for " + special); } catch (DecimalInconvertibleException e) { - assertEquals("DecimalInconvertibleException.getDecimalType", special.getType(), e.getDecimalType()); - assertEquals("DecimalInconvertibleException.getSignum", special.signum(), e.getSignum()); + assertEquals(special.getType(), e.getDecimalType(), "DecimalInconvertibleException.getDecimalType"); + assertEquals(special.signum(), e.getSignum(), "DecimalInconvertibleException.getSignum"); } } } @Test - public void doubleValue_finiteValue() { + void doubleValue_finiteValue() { String decimalString = "1.23456E10"; Decimal64 decimal64Value = Decimal64.valueOf(decimalString); @@ -179,118 +178,117 @@ public void doubleValue_finiteValue() { } @Test - public void doubleValue_positiveInfinity() { + void doubleValue_positiveInfinity() { assertEquals(Double.POSITIVE_INFINITY, Decimal64.POSITIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_negativeInfinity() { + void doubleValue_negativeInfinity() { assertEquals(Double.NEGATIVE_INFINITY, Decimal64.NEGATIVE_INFINITY.doubleValue(), 0); } @Test - public void doubleValue_NaNs() { + void doubleValue_NaNs() { for (Decimal64 special : Arrays.asList(Decimal64.POSITIVE_NAN, Decimal64.NEGATIVE_NAN, Decimal64.POSITIVE_SIGNALING_NAN, Decimal64.NEGATIVE_SIGNALING_NAN)) { - assertTrue("NaN for " + special, Double.isNaN(special.doubleValue())); + assertTrue(Double.isNaN(special.doubleValue()), "NaN for " + special); } } @Test - public void valueOf_finiteDouble() { + void valueOf_finiteDouble() { assertEquals(Decimal64.valueOf("1.23456"), Decimal64.valueOf(1.23456)); } @Test - public void valueOf_doubleMax_noOverflow() { + void valueOf_doubleMax_noOverflow() { assertEquals(Decimal64.valueOf("1.797693134862316E+308"), Decimal64.valueOf(Double.MAX_VALUE, OverflowHandling.THROW_EXCEPTION)); } @Test - public void valueOf_doublePositiveInfinity() { + void valueOf_doublePositiveInfinity() { assertSame(Decimal64.POSITIVE_INFINITY, Decimal64.valueOf(Double.POSITIVE_INFINITY)); } @Test - public void valueOf_doubleNegativeInfinity() { + void valueOf_doubleNegativeInfinity() { assertSame(Decimal64.NEGATIVE_INFINITY, Decimal64.valueOf(Double.NEGATIVE_INFINITY)); } @Test - public void valueOf_doubleNaN() { + void valueOf_doubleNaN() { assertSame(Decimal64.POSITIVE_NAN, Decimal64.valueOf(Double.NaN)); } @Test - public void valueOf_stringOutOfRange_toPositiveInfinity() { + void valueOf_stringOutOfRange_toPositiveInfinity() { assertSame(Decimal64.POSITIVE_INFINITY, Decimal64.valueOf("9.9E10000")); } @Test - public void valueOf_stringOutOfRange_toNegativeInfinity() { + void valueOf_stringOutOfRange_toNegativeInfinity() { assertSame(Decimal64.NEGATIVE_INFINITY, Decimal64.valueOf("-9.9E10000")); } @Test - public void valueOf_stringOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -9999 is out of range for this type"); - - Decimal64.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION); + void valueOf_stringOutOfRange_throwException() { + var exception = assertThrows(DecimalOverflowException.class, () -> + Decimal64.valueOf("9.9E10000", OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -9999 is out of range for this type", exception.getMessage()); } @Test - public void toDecimal_Decimal64_Decimal64() { + void toDecimal_Decimal64_Decimal64() { Decimal64 value = Decimal64.valueOf("1.23456"); assertSame(value, value.toDecimal(Decimal64.class)); } @Test - public void toDecimal_Decimal64_Decimal32() { + void toDecimal_Decimal64_Decimal32() { Decimal64 value = Decimal64.valueOf("1.23456"); assertEquals(Decimal32.valueOf("1.23456"), value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal64_Decimal32_valueOutOfRange_toInfinity() { + void toDecimal_Decimal64_Decimal32_valueOutOfRange_toInfinity() { Decimal64 value = Decimal64.valueOf("1.23456E97"); assertSame(Decimal32.POSITIVE_INFINITY, value.toDecimal(Decimal32.class)); } @Test - public void toDecimal_Decimal64_Decimal32_valueOutOfRange_throwException() { - expectedException.expect(DecimalOverflowException.class); - expectedException.expectMessage("The scale -92 is out of range for this type"); + void toDecimal_Decimal64_Decimal32_valueOutOfRange_throwException() { Decimal64 value = Decimal64.valueOf("1.23456E97"); - value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION); + var exception = assertThrows(DecimalOverflowException.class, () -> + value.toDecimal(Decimal32.class, OverflowHandling.THROW_EXCEPTION)); + assertEquals("The scale -92 is out of range for this type", exception.getMessage()); } @Test - public void toDecimal_Decimal64_Decimal128() { + void toDecimal_Decimal64_Decimal128() { Decimal64 value = Decimal64.valueOf("1.23456"); assertEquals(Decimal128.valueOf("1.23456"), value.toDecimal(Decimal128.class)); } @Test - public void validateConstant_POSITIVE_ZERO() { + void validateConstant_POSITIVE_ZERO() { assertEquals(BigDecimal.ZERO, POSITIVE_ZERO.toBigDecimal()); assertEquals(Signum.POSITIVE, POSITIVE_ZERO.signum()); } @Test - public void validateConstant_NEGATIVE_ZERO() { + void validateConstant_NEGATIVE_ZERO() { assertEquals(BigDecimal.ZERO, NEGATIVE_ZERO.toBigDecimal()); assertEquals(Signum.NEGATIVE, NEGATIVE_ZERO.signum()); } @Test - public void negate_One() { + void negate_One() { Decimal64 negativeOne = Decimal64.valueOf(BigDecimal.ONE).negate(); assertEquals(Decimal64.valueOf(BigDecimal.ONE.negate()), @@ -300,7 +298,7 @@ public void negate_One() { } @Test - public void negate_positiveZero() { + void negate_positiveZero() { Decimal64 negativeZero = POSITIVE_ZERO.negate(); assertEquals(NEGATIVE_ZERO, negativeZero); @@ -309,7 +307,7 @@ public void negate_positiveZero() { } @Test - public void negate_negativeZero() { + void negate_negativeZero() { Decimal64 positiveZero = NEGATIVE_ZERO.negate(); assertEquals(POSITIVE_ZERO, positiveZero); @@ -318,88 +316,86 @@ public void negate_negativeZero() { } @Test - public void negate_positiveInfinity() { + void negate_positiveInfinity() { assertEquals(Decimal64.NEGATIVE_INFINITY, Decimal64.POSITIVE_INFINITY.negate()); } @Test - public void negate_negativeInfinity() { + void negate_negativeInfinity() { assertEquals(Decimal64.POSITIVE_INFINITY, Decimal64.NEGATIVE_INFINITY.negate()); } @Test - public void negate_positiveNaN() { + void negate_positiveNaN() { assertEquals(Decimal64.NEGATIVE_NAN, Decimal64.POSITIVE_NAN.negate()); } @Test - public void negate_negativeNaN() { + void negate_negativeNaN() { assertEquals(Decimal64.POSITIVE_NAN, Decimal64.NEGATIVE_NAN.negate()); } @Test - public void negate_positiveSignallingNaN() { + void negate_positiveSignallingNaN() { assertEquals(Decimal64.NEGATIVE_SIGNALING_NAN, Decimal64.POSITIVE_SIGNALING_NAN.negate()); } @Test - public void negate_negativeSignallingNaN() { + void negate_negativeSignallingNaN() { assertEquals(Decimal64.POSITIVE_SIGNALING_NAN, Decimal64.NEGATIVE_SIGNALING_NAN.negate()); } @Test - public void valueOfExact_BigInteger_min() { + void valueOfExact_BigInteger_min() { final BigInteger value = new BigInteger("-9999999999999999"); assertEquals(new BigDecimal("-9999999999999999"), Decimal64.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_min_minusOne() { + void valueOfExact_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999999999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal64.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal64.valueOfExact(value)); } @Test - public void valueOfExact_BigInteger_max() { + void valueOfExact_BigInteger_max() { final BigInteger value = new BigInteger("9999999999999999"); assertEquals(new BigDecimal("9999999999999999"), Decimal64.valueOfExact(value).toBigDecimal()); } @Test - public void valueOfExact_BigInteger_max_plusOne() { + void valueOfExact_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999999999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - Decimal64.valueOfExact(value); + assertThrows(DecimalOverflowException.class, () -> Decimal64.valueOfExact(value)); } @Test - public void valueOf_BigInteger_min() { + void valueOf_BigInteger_min() { final BigInteger value = new BigInteger("-9999999999999999"); assertEquals(new BigDecimal("-9999999999999999"), Decimal64.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_min_minusOne() { + void valueOf_BigInteger_min_minusOne() { final BigInteger value = new BigInteger("-9999999999999999").subtract(BigInteger.ONE); assertEquals(new BigDecimal("-1000000000000000E+1"), Decimal64.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max() { + void valueOf_BigInteger_max() { final BigInteger value = new BigInteger("9999999999999999"); assertEquals(new BigDecimal("9999999999999999"), Decimal64.valueOf(value).toBigDecimal()); } @Test - public void valueOf_BigInteger_max_plusOne() { + void valueOf_BigInteger_max_plusOne() { final BigInteger value = new BigInteger("9999999999999999").add(BigInteger.ONE); assertEquals(new BigDecimal("1000000000000000E+1"), Decimal64.valueOf(value).toBigDecimal()); diff --git a/src/test/java/org/firebirdsql/decimal/Decimal64ValueOfTest.java b/src/test/java/org/firebirdsql/decimal/Decimal64ValueOfTest.java index a84d9d0..5b354c0 100644 --- a/src/test/java/org/firebirdsql/decimal/Decimal64ValueOfTest.java +++ b/src/test/java/org/firebirdsql/decimal/Decimal64ValueOfTest.java @@ -21,16 +21,14 @@ */ 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.ArrayList; -import java.util.Arrays; -import java.util.Collection; +import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Test for the {@link Decimal64#valueOf(String)} method, the rounding applied, and handling of specials. @@ -40,30 +38,18 @@ * * @author Mark Rotteveel */ -@RunWith(Parameterized.class) -public class Decimal64ValueOfTest { +class Decimal64ValueOfTest { - /** - * Value to supply to the valueOf method - */ - @Parameterized.Parameter - public String sourceValue; - /** - * Value of the toString() of the resulting value - */ - @Parameterized.Parameter(1) - public String expectedValue; - - @Test - public void valueOfTest() { + @ParameterizedTest(name = "{index}: value {0} (expect {1}))") + @MethodSource("data") + void valueOfTest(String sourceValue, String expectedValue) { Decimal64 value = Decimal64.valueOf(sourceValue); assertEquals(expectedValue, value.toString()); } - @Parameterized.Parameters(name = "{index}: value {0} (expect {1}))") - public static Collection data() { - return new ArrayList<>(Arrays.asList( + static Stream data() { + return Stream.of( // Zero testCase("0", "0"), testCase("+0", "0"), @@ -126,10 +112,10 @@ public static Collection data() { testCase("-sNaN", "-sNaN"), testCase("-snan", "-sNaN"), testCase("-SNAN", "-sNaN") - )); + ); } - private static Object[] testCase(String source, String result) { - return new Object[] { source, result }; + private static Arguments testCase(String source, String result) { + return Arguments.of(source, result); } } diff --git a/src/test/java/org/firebirdsql/decimal/DecimalCodecTest.java b/src/test/java/org/firebirdsql/decimal/DecimalCodecTest.java index c5c2132..86e5b1c 100644 --- a/src/test/java/org/firebirdsql/decimal/DecimalCodecTest.java +++ b/src/test/java/org/firebirdsql/decimal/DecimalCodecTest.java @@ -21,17 +21,14 @@ */ package org.firebirdsql.decimal; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -/** - * @author Mark Rotteveel - */ -public class DecimalCodecTest { +class DecimalCodecTest { @Test - public void testCalculateExponent() { + void testCalculateExponent() { assertEquals(0b01000001, DecimalCodec.decodeExponent(new byte[] { -1, 0b0100 }, 1, 6)); assertEquals(0b01000011, DecimalCodec.decodeExponent(new byte[] { -1, 0b1111 }, 1, 6)); assertEquals(0b01000010, DecimalCodec.decodeExponent(new byte[] { -1, 0b01000 }, 1, 6)); diff --git a/src/test/java/org/firebirdsql/decimal/DecimalFormatTest.java b/src/test/java/org/firebirdsql/decimal/DecimalFormatTest.java index e1381b3..4358dfa 100644 --- a/src/test/java/org/firebirdsql/decimal/DecimalFormatTest.java +++ b/src/test/java/org/firebirdsql/decimal/DecimalFormatTest.java @@ -22,39 +22,35 @@ package org.firebirdsql.decimal; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; import java.math.BigInteger; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Test to check if the decimal constants match the specification. * * @author Mark Rotteveel */ -public class DecimalFormatTest { - - @Rule - public final ExpectedException expectedException = ExpectedException.none(); +class DecimalFormatTest { @Test - public void decimal32() { + void decimal32() { final DecimalFormat decimalFormat = DecimalFormat.Decimal32; - assertEquals("formatBitLength", 32, decimalFormat.formatBitLength); - assertEquals("formatByteLength", 4, decimalFormat.formatByteLength); - assertEquals("coefficientDigits", 7, decimalFormat.coefficientDigits); - assertEquals("exponentContinuationBits", 6, decimalFormat.exponentContinuationBits); - assertEquals("coefficientContinuationBits", 20, decimalFormat.coefficientContinuationBits); - assertEquals("eLimit", 191, decimalFormat.eLimit); - assertEquals("exponentBias", 101, decimalFormat.biasedExponent(0)); + assertEquals(32, decimalFormat.formatBitLength, "formatBitLength"); + assertEquals(4, decimalFormat.formatByteLength, "formatByteLength"); + assertEquals(7, decimalFormat.coefficientDigits, "coefficientDigits"); + assertEquals(6, decimalFormat.exponentContinuationBits, "exponentContinuationBits"); + assertEquals(20, decimalFormat.coefficientContinuationBits, "coefficientContinuationBits"); + assertEquals(191, decimalFormat.eLimit, "eLimit"); + assertEquals(101, decimalFormat.biasedExponent(0), "exponentBias"); } @Test - public void decimal32_validateCoefficient_min() { + void decimal32_validateCoefficient_min() { final DecimalFormat decimalFormat = DecimalFormat.Decimal32; final BigInteger value = new BigInteger("-9999999"); @@ -62,16 +58,15 @@ public void decimal32_validateCoefficient_min() { } @Test - public void decimal32_validateCoefficient_min_minusOne() { + void decimal32_validateCoefficient_min_minusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal32; final BigInteger value = new BigInteger("-9999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } @Test - public void decimal32_validateCoefficient_max() { + void decimal32_validateCoefficient_max() { final DecimalFormat decimalFormat = DecimalFormat.Decimal32; final BigInteger value = new BigInteger("9999999"); @@ -79,28 +74,27 @@ public void decimal32_validateCoefficient_max() { } @Test - public void decimal32_validateCoefficient_max_plusOne() { + void decimal32_validateCoefficient_max_plusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal32; final BigInteger value = new BigInteger("9999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } @Test - public void decimal64() { + void decimal64() { final DecimalFormat decimalFormat = DecimalFormat.Decimal64; - assertEquals("formatBitLength", 64, decimalFormat.formatBitLength); - assertEquals("formatByteLength", 8, decimalFormat.formatByteLength); - assertEquals("coefficientDigits", 16, decimalFormat.coefficientDigits); - assertEquals("exponentContinuationBits", 8, decimalFormat.exponentContinuationBits); - assertEquals("coefficientContinuationBits", 50, decimalFormat.coefficientContinuationBits); - assertEquals("eLimit", 767, decimalFormat.eLimit); - assertEquals("exponentBias", 398, decimalFormat.biasedExponent(0)); + assertEquals(64, decimalFormat.formatBitLength, "formatBitLength"); + assertEquals(8, decimalFormat.formatByteLength, "formatByteLength"); + assertEquals(16, decimalFormat.coefficientDigits, "coefficientDigits"); + assertEquals(8, decimalFormat.exponentContinuationBits, "exponentContinuationBits"); + assertEquals(50, decimalFormat.coefficientContinuationBits, "coefficientContinuationBits"); + assertEquals(767, decimalFormat.eLimit, "eLimit"); + assertEquals(398, decimalFormat.biasedExponent(0), "exponentBias"); } @Test - public void decimal64_validateCoefficient_min() { + void decimal64_validateCoefficient_min() { final DecimalFormat decimalFormat = DecimalFormat.Decimal64; final BigInteger value = new BigInteger("-9999999999999999"); @@ -108,16 +102,15 @@ public void decimal64_validateCoefficient_min() { } @Test - public void decimal64_validateCoefficient_min_minusOne() { + void decimal64_validateCoefficient_min_minusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal64; final BigInteger value = new BigInteger("-9999999999999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } @Test - public void decimal64_validateCoefficient_max() { + void decimal64_validateCoefficient_max() { final DecimalFormat decimalFormat = DecimalFormat.Decimal64; final BigInteger value = new BigInteger("9999999999999999"); @@ -125,28 +118,27 @@ public void decimal64_validateCoefficient_max() { } @Test - public void decimal64_validateCoefficient_max_plusOne() { + void decimal64_validateCoefficient_max_plusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal64; final BigInteger value = new BigInteger("9999999999999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } @Test - public void decimal128() { + void decimal128() { final DecimalFormat decimalFormat = DecimalFormat.Decimal128; - assertEquals("formatBitLength", 128, decimalFormat.formatBitLength); - assertEquals("formatByteLength", 16, decimalFormat.formatByteLength); - assertEquals("coefficientDigits", 34, decimalFormat.coefficientDigits); - assertEquals("exponentContinuationBits", 12, decimalFormat.exponentContinuationBits); - assertEquals("coefficientContinuationBits", 110, decimalFormat.coefficientContinuationBits); - assertEquals("eLimit", 12287, decimalFormat.eLimit); - assertEquals("exponentBias", 6176, decimalFormat.biasedExponent(0)); + assertEquals(128, decimalFormat.formatBitLength, "formatBitLength"); + assertEquals(16, decimalFormat.formatByteLength, "formatByteLength"); + assertEquals(34, decimalFormat.coefficientDigits, "coefficientDigits"); + assertEquals(12, decimalFormat.exponentContinuationBits, "exponentContinuationBits"); + assertEquals(110, decimalFormat.coefficientContinuationBits, "coefficientContinuationBits"); + assertEquals(12287, decimalFormat.eLimit, "eLimit"); + assertEquals(6176, decimalFormat.biasedExponent(0), "exponentBias"); } @Test - public void decimal128_validateCoefficient_min() { + void decimal128_validateCoefficient_min() { final DecimalFormat decimalFormat = DecimalFormat.Decimal128; final BigInteger value = new BigInteger("-9999999999999999999999999999999999"); @@ -154,16 +146,15 @@ public void decimal128_validateCoefficient_min() { } @Test - public void decimal128_validateCoefficient_min_minusOne() { + void decimal128_validateCoefficient_min_minusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal128; final BigInteger value = new BigInteger("-9999999999999999999999999999999999").subtract(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } @Test - public void decimal128_validateCoefficient_max() { + void decimal128_validateCoefficient_max() { final DecimalFormat decimalFormat = DecimalFormat.Decimal128; final BigInteger value = new BigInteger("9999999999999999999999999999999999"); @@ -171,12 +162,11 @@ public void decimal128_validateCoefficient_max() { } @Test - public void decimal128_validateCoefficient_max_plusOne() { + void decimal128_validateCoefficient_max_plusOne() { final DecimalFormat decimalFormat = DecimalFormat.Decimal128; final BigInteger value = new BigInteger("9999999999999999999999999999999999").add(BigInteger.ONE); - expectedException.expect(DecimalOverflowException.class); - decimalFormat.validateCoefficient(value); + assertThrows(DecimalOverflowException.class, () -> decimalFormat.validateCoefficient(value)); } } \ No newline at end of file diff --git a/src/test/java/org/firebirdsql/decimal/DecimalTypeFromFirstByteTest.java b/src/test/java/org/firebirdsql/decimal/DecimalTypeFromFirstByteTest.java index 03df3ba..e9d94e8 100644 --- a/src/test/java/org/firebirdsql/decimal/DecimalTypeFromFirstByteTest.java +++ b/src/test/java/org/firebirdsql/decimal/DecimalTypeFromFirstByteTest.java @@ -21,32 +21,22 @@ */ 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.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.stream.IntStream; +import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -/** - * @author Mark Rotteveel - */ -@RunWith(Parameterized.class) -public class DecimalTypeFromFirstByteTest { +class DecimalTypeFromFirstByteTest { private static final int SPECIALS_MASK = 0b0_11110_00; - @Parameterized.Parameter - public int firstByte; - @Parameterized.Parameter(1) - public DecimalType decimalType; - - @Test - public void testFromFirstByte_byte() { + @ParameterizedTest(name = "{index}: {0} => {1}") + @MethodSource("data") + void testFromFirstByte_byte(int firstByte, DecimalType decimalType) { assertEquals(decimalType, DecimalType.fromFirstByte((byte) firstByte)); } @@ -56,37 +46,32 @@ public void testFromFirstByte_byte() { // assertEquals(decimalType, DecimalType.fromFirstByte(firstByte)); // } - @Parameterized.Parameters(name = "{index}: {0} => {1}") - public static Collection data() { - List values = new ArrayList<>(Arrays.asList( - testCase(0b0_11110_00, DecimalType.INFINITY), - testCase(0b1_11110_00, DecimalType.INFINITY), - testCase(0b0_11110_10, DecimalType.INFINITY), - testCase(0b1_11110_10, DecimalType.INFINITY), - testCase(0b0_11111_00, DecimalType.NAN), - testCase(0b1_11111_00, DecimalType.NAN), - testCase(0b0_11111_10, DecimalType.SIGNALING_NAN), - testCase(0b1_11111_10, DecimalType.SIGNALING_NAN), - testCase(0b0_11110_01, DecimalType.INFINITY), - testCase(0b1_11110_01, DecimalType.INFINITY), - testCase(0b0_11110_11, DecimalType.INFINITY), - testCase(0b1_11110_11, DecimalType.INFINITY), - testCase(0b0_11111_01, DecimalType.NAN), - testCase(0b1_11111_01, DecimalType.NAN), - testCase(0b0_11111_11, DecimalType.SIGNALING_NAN), - testCase(0b1_11111_11, DecimalType.SIGNALING_NAN) - )); - for (int value = 0; value <= 0xFF; value++) { - if ((value & SPECIALS_MASK) == SPECIALS_MASK) { - continue; - } - values.add(testCase(value, DecimalType.FINITE)); - } - return values; + static Stream data() { + return Stream.concat( + Stream.of( + testCase(0b0_11110_00, DecimalType.INFINITY), + testCase(0b1_11110_00, DecimalType.INFINITY), + testCase(0b0_11110_10, DecimalType.INFINITY), + testCase(0b1_11110_10, DecimalType.INFINITY), + testCase(0b0_11111_00, DecimalType.NAN), + testCase(0b1_11111_00, DecimalType.NAN), + testCase(0b0_11111_10, DecimalType.SIGNALING_NAN), + testCase(0b1_11111_10, DecimalType.SIGNALING_NAN), + testCase(0b0_11110_01, DecimalType.INFINITY), + testCase(0b1_11110_01, DecimalType.INFINITY), + testCase(0b0_11110_11, DecimalType.INFINITY), + testCase(0b1_11110_11, DecimalType.INFINITY), + testCase(0b0_11111_01, DecimalType.NAN), + testCase(0b1_11111_01, DecimalType.NAN), + testCase(0b0_11111_11, DecimalType.SIGNALING_NAN), + testCase(0b1_11111_11, DecimalType.SIGNALING_NAN)), + IntStream.rangeClosed(0, 0XFF) + .filter(value -> (value & SPECIALS_MASK) != SPECIALS_MASK) + .mapToObj(value -> testCase(value, DecimalType.FINITE))); } - private static Object[] testCase(int firstByte, DecimalType decimalType) { - return new Object[] { firstByte, decimalType }; + private static Arguments testCase(int firstByte, DecimalType decimalType) { + return Arguments.of(firstByte, decimalType); } } \ No newline at end of file diff --git a/src/test/java/org/firebirdsql/decimal/ExamplesTest.java b/src/test/java/org/firebirdsql/decimal/ExamplesTest.java index 0fb47ad..281a6e5 100644 --- a/src/test/java/org/firebirdsql/decimal/ExamplesTest.java +++ b/src/test/java/org/firebirdsql/decimal/ExamplesTest.java @@ -19,44 +19,43 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - package org.firebirdsql.decimal; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for the examples in README.md. * * @author Mark Rotteveel */ -public class ExamplesTest { +class ExamplesTest { @Test - public void decimal32ParseBytes() { - byte[] bytes = {(byte) 0xc7, (byte) 0xf4, (byte) 0xd2, (byte) 0xe7}; + void decimal32ParseBytes() { + byte[] bytes = { (byte) 0xc7, (byte) 0xf4, (byte) 0xd2, (byte) 0xe7 }; Decimal32 decimal32 = Decimal32.parseBytes(bytes); BigDecimal bigDecimal = decimal32.toBigDecimal(); assertEquals(new BigDecimal("-1.234567E+96"), bigDecimal); } @Test - public void decimal32ToBytes() { + void decimal32ToBytes() { BigDecimal bigDecimal = new BigDecimal("-7.50E-7"); Decimal32 decimal32 = Decimal32.valueOf(bigDecimal); byte[] bytes = decimal32.toBytes(); - assertArrayEquals(new byte[] {(byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0}, bytes); + assertArrayEquals(new byte[] { (byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0 }, bytes); } @Test - public void decimal32ToBytes_overflowThrowException() { + void decimal32ToBytes_overflowThrowException() { BigDecimal bigDecimal = new BigDecimal("-7.50E-7"); Decimal32 decimal32 = Decimal32.valueOf(bigDecimal, OverflowHandling.THROW_EXCEPTION); byte[] bytes = decimal32.toBytes(); - assertArrayEquals(new byte[] {(byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0}, bytes); + assertArrayEquals(new byte[] { (byte) 0xa1, (byte) 0xc0, 0x03, (byte) 0xd0 }, bytes); } }