use Junit5 instead of TestNG

We want to be able to use @SpringBootTest tests that fully initialize
the Spring application. This is much easier done with Junit than TestNG.
Gradle does not support (at least not easily) to run Junit and TestNG
tests. Therefore we switch to Junit with all tests.
The original reason for using TestNG was that Junit didn't support
data providers. But that finally changed in Junit5 with
ParameterizedTest.
This commit is contained in:
2019-12-13 14:33:20 +01:00
parent 394e16ad27
commit 07ad62ddd9
26 changed files with 660 additions and 837 deletions

View File

@@ -1,39 +1,37 @@
package org.lucares.utils.byteencoder;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.lucares.collections.LongList;
import org.lucares.utils.byteencoder.VariableByteEncoder;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@Test
public class VariableByteEncoderTest {
@DataProvider
public Object[][] providerEncodeDecode() {
return new Object[][] { //
public static Stream<Arguments> providerEncodeDecode() {
return Stream.of( //
// encoded into 1 byte
{ 10, -5, 5 }, //
{ 10, 0, 5 }, //
{ 10, -63, 63 }, //
Arguments.of(10, -5, 5), //
Arguments.of(10, 0, 5), //
Arguments.of(10, -63, 63), //
// encoded into 2 bytes
{ 10, 130, 131 }, //
Arguments.of(10, 130, 131), //
// encoded into 3 bytes
{ 10, -8191, 8191 }, //
Arguments.of(10, -8191, 8191), //
// encoded into n bytes
{ 1, Long.MAX_VALUE / 2 - 4, Long.MAX_VALUE / 2 }, //
{ 1, Long.MIN_VALUE / 2, Long.MAX_VALUE / 2 }, //
{ 11, Long.MIN_VALUE / 2 + 1, Long.MIN_VALUE / 2 + 3 }, //
{ 12, Long.MAX_VALUE / 2 - 3, Long.MAX_VALUE / 2 },//
};
Arguments.of(1, Long.MAX_VALUE / 2 - 4, Long.MAX_VALUE / 2), //
Arguments.of(1, Long.MIN_VALUE / 2, Long.MAX_VALUE / 2), //
Arguments.of(11, Long.MIN_VALUE / 2 + 1, Long.MIN_VALUE / 2 + 3), //
Arguments.of(12, Long.MAX_VALUE / 2 - 3, Long.MAX_VALUE / 2)//
);
}
@Test(dataProvider = "providerEncodeDecode")
@ParameterizedTest
@MethodSource("providerEncodeDecode")
public void testEncodeDecode(final long numValues, final long minValue, final long maxValue) {
final LongList originalValues = new LongList();
@@ -48,63 +46,63 @@ public class VariableByteEncoderTest {
final LongList actualValues = VariableByteEncoder.decode(buffer);
assertEquals(actualValues.toString(), originalValues.toString());
Assertions.assertEquals(originalValues.toString(), actualValues.toString());
}
@DataProvider
public Object[][] providerEncodeDecodeOfTwoValues() {
return new Object[][] { //
{ 12345, 67890, false, 1 }, // first value needs three bytes, it does not fit
{ 12345, 67890, false, 2 }, // first value needs three bytes, it does not fit
{ 12345, 67890, false, 3 }, // first value needs three bytes, second value does not fit
{ 12345, 67890, false, 4 }, // first value needs three bytes, second value does not fit
{ 12345, 67890, false, 5 }, // first value needs three bytes, second value does not fit
{ 12345, 67890, true, 6 }, // both values need three bytes
{ 12345, 67890, true, 10 }, //
};
public static Stream<Arguments> providerEncodeDecodeOfTwoValues() {
return Stream.of( //
Arguments.of(12345, 67890, false, 1), // first value needs three bytes, it does not fit
Arguments.of(12345, 67890, false, 2), // first value needs three bytes, it does not fit
Arguments.of(12345, 67890, false, 3), // first value needs three bytes, second value does not fit
Arguments.of(12345, 67890, false, 4), // first value needs three bytes, second value does not fit
Arguments.of(12345, 67890, false, 5), // first value needs three bytes, second value does not fit
Arguments.of(12345, 67890, true, 6), // both values need three bytes
Arguments.of(12345, 67890, true, 10) //
);
}
@Test(dataProvider = "providerEncodeDecodeOfTwoValues")
@ParameterizedTest
@MethodSource("providerEncodeDecodeOfTwoValues")
public void testEncodeDecodeOfTwoValues(final long value1, final long value2, final boolean fits,
final int bufferSize) {
final LongList originalValues = new LongList();
final byte[] buffer = new byte[bufferSize];
final int bytesAdded = VariableByteEncoder.encodeInto(value1, value2, buffer, 0);
Assert.assertEquals(bytesAdded > 0, fits);
Assertions.assertEquals(fits, bytesAdded > 0);
if (fits) {
originalValues.addAll(value1, value2);
} else {
Assert.assertEquals(buffer, new byte[bufferSize],
Assertions.assertArrayEquals(new byte[bufferSize], buffer,
"checks that buffer is resetted after it discovers the values do not fit");
}
final LongList decodedValues = VariableByteEncoder.decode(buffer);
Assert.assertEquals(decodedValues, originalValues);
Assertions.assertEquals(originalValues, decodedValues);
}
@DataProvider
public Object[][] providerNededBytes() {
return new Object[][] { //
{ 0, 1 }, //
{ -10, 1 }, //
{ 10, 1 }, //
{ -63, 1 }, //
{ 63, 1 }, //
{ -64, 2 }, //
{ 64, 2 }, //
{ -8191, 2 }, //
{ 8191, 2 }, //
{ -8192, 3 }, //
{ 8192, 3 }, //
};
public static Stream<Arguments> providerNededBytes() {
return Stream.of( //
Arguments.of(0, 1), //
Arguments.of(-10, 1), //
Arguments.of(10, 1), //
Arguments.of(-63, 1), //
Arguments.of(63, 1), //
Arguments.of(-64, 2), //
Arguments.of(64, 2), //
Arguments.of(-8191, 2), //
Arguments.of(8191, 2), //
Arguments.of(-8192, 3), //
Arguments.of(8192, 3) //
);
}
@Test(dataProvider = "providerNededBytes")
@ParameterizedTest
@MethodSource("providerNededBytes")
public void testNeededBytes(final long value, final int expectedNeededBytes) {
final int neededBytes = VariableByteEncoder.neededBytes(value);
final byte[] encoded = VariableByteEncoder.encode(value);
Assert.assertEquals(encoded.length, neededBytes);
Assertions.assertEquals(neededBytes, encoded.length);
}
}