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

@@ -14,29 +14,29 @@ import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.lucares.collections.LongList;
import org.lucares.pdb.diskstorage.DiskStorage;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class BSFileTest {
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@Test
public void testBlockStorage() throws Exception {
final Path file = dataDirectory.resolve("data.int.db");
final int numLongs = 1000;
@@ -68,11 +68,12 @@ public class BSFileTest {
final BSFile bsFile = BSFile.existingFile(blockOffset, ds, NullCustomizer.INSTANCE);
final LongList actualLongs = bsFile.asLongList();
final LongList expectedLongs = LongList.rangeClosed(0, numLongs - 1);
Assert.assertEquals(actualLongs, expectedLongs);
Assertions.assertEquals(expectedLongs, actualLongs);
}
System.out.println("duration read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
}
@Test
public void testBlockStorageMultithreading() throws Exception {
final ExecutorService pool = Executors.newCachedThreadPool();
@@ -125,7 +126,7 @@ public class BSFileTest {
try (BSFile bsFile = BSFile.existingFile(rootBlockNumber, ds, NullCustomizer.INSTANCE)) {
final LongList actualLongs = bsFile.asLongList();
final LongList expectedLongs = expectedValues;
Assert.assertEquals(actualLongs, expectedLongs, "for rootBlockNumber=" + rootBlockNumber);
Assertions.assertEquals(expectedLongs, actualLongs, "for rootBlockNumber=" + rootBlockNumber);
}
}
}

View File

@@ -6,27 +6,29 @@ import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.lucares.collections.LongList;
import org.lucares.pdb.diskstorage.DiskStorage;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class TimeSeriesFileTest {
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@Test
public void testBlockStorageTimeValue() throws Exception {
final Path file = dataDirectory.resolve("data.int.db");
final Random random = ThreadLocalRandom.current();
@@ -76,7 +78,7 @@ public class TimeSeriesFileTest {
final TimeSeriesFile bsFile = TimeSeriesFile.existingFile(blockNumber, ds);
final LongList actualLongs = bsFile.asTimeValueLongList();
Assert.assertEquals(actualLongs, expectedLongs);
Assertions.assertEquals(expectedLongs, actualLongs);
}
System.out.println("duration read: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
}

View File

@@ -10,24 +10,24 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class DiskStorageTest {
private static final int BLOCK_SIZE = 512;
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@@ -38,7 +38,8 @@ public class DiskStorageTest {
*
* @throws Exception
*/
@Test(enabled = false)
@Test
@Disabled
public void testFlushingASectorOrABlock() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
Files.deleteIfExists(databaseFile);
@@ -92,7 +93,7 @@ public class DiskStorageTest {
}
}
@Test(enabled = true)
@Test
public void testDiskStorage() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
@@ -136,18 +137,20 @@ public class DiskStorageTest {
}
}
@Test(enabled = true, expectedExceptions = IllegalArgumentException.class)
@Test
public void testAllocationSmallerThanMinimalBlockSize() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
final Path databaseFile = dataDirectory.resolve("db.ds");
try (DiskStorage ds = new DiskStorage(databaseFile, dataDirectory)) {
try (DiskStorage ds = new DiskStorage(databaseFile, dataDirectory)) {
final int blockSize = 31; // minimal block size is 32
ds.allocateBlock(blockSize);
}
final int blockSize = 31; // minimal block size is 32
ds.allocateBlock(blockSize);
}
});
}
@Test(enabled = true)
@Test
public void testAllocateAndFreeSingleBlockInFreeList() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
@@ -158,20 +161,20 @@ public class DiskStorageTest {
final long block_40_71 = ds.allocateBlock(blockSize);
final long block_72_103 = ds.allocateBlock(blockSize);
Assert.assertEquals(block_8_39, 8);
Assert.assertEquals(block_40_71, 40);
Assert.assertEquals(block_72_103, 72);
Assertions.assertEquals(8, block_8_39);
Assertions.assertEquals(40, block_40_71);
Assertions.assertEquals(72, block_72_103);
ds.free(block_40_71, blockSize);
// should reuse the block we just freed
final long actual_block_40_71 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_40_71, 40);
Assertions.assertEquals(40, actual_block_40_71);
}
}
@Test(enabled = true)
@Test
public void testAllocateAndFreeMultipleBlocksInFreeList() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
@@ -192,23 +195,23 @@ public class DiskStorageTest {
// should reuse the first block we just freed
// this removes the root node of the free list
final long actual_block_40_71 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_40_71, 40);
Assertions.assertEquals(40, actual_block_40_71);
// should reuse the second block we just freed
final long actual_block_72_103 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_72_103, 72);
Assertions.assertEquals(72, actual_block_72_103);
// should reuse the third block we just freed
// this removes the last node of the free list
final long actual_block_104_135 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_104_135, 104);
Assertions.assertEquals(104, actual_block_104_135);
final long block_168_199 = ds.allocateBlock(blockSize);
Assert.assertEquals(block_168_199, 168);
Assertions.assertEquals(168, block_168_199);
}
}
@Test(enabled = true)
@Test
public void testAllocateAndFreeInsertFreeNodeInTheMiddleOfTheFreeList() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
@@ -228,17 +231,17 @@ public class DiskStorageTest {
// the first free block is re-used
final long actual_block_72_103 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_72_103, block_72_103);
Assertions.assertEquals(block_72_103, actual_block_72_103);
final long actual_block_104_135 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_104_135, block_104_135);
Assertions.assertEquals(block_104_135, actual_block_104_135);
final long actual_block_136_167 = ds.allocateBlock(blockSize);
Assert.assertEquals(actual_block_136_167, block_136_167);
Assertions.assertEquals(block_136_167, actual_block_136_167);
}
}
@Test(enabled = true)
@Test
public void testAllocateAndFreeMultipleBlocksWithDifferentSizes() throws Exception {
final Path databaseFile = dataDirectory.resolve("db.ds");
@@ -256,7 +259,7 @@ public class DiskStorageTest {
ds.free(small_block_136_167, blockSizeSmall);
final long actual_small_block_136_167 = ds.allocateBlock(blockSizeSmall);
Assert.assertEquals(actual_small_block_136_167, small_block_136_167);
Assertions.assertEquals(small_block_136_167, actual_small_block_136_167);
}
}

View File

@@ -1,37 +1,34 @@
package org.lucares.pdb.map;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.lucares.pdb.map.NodeEntry.ValueType;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Assertions;
@Test
public class NodeEntryTest {
@DataProvider
public Object[][] providerPrefixCompare() {
final List<Object[]> result = new ArrayList<>();
result.add(new Object[] { "ab", "abc", -1 });
result.add(new Object[] { "abb", "abc", -1 });
result.add(new Object[] { "abc", "abc", 0 });
result.add(new Object[] { "abcd", "abc", 0 });
result.add(new Object[] { "abd", "abc", 1 });
result.add(new Object[] { "abz", "abc", 23 });
return result.toArray(Object[][]::new);
public static Stream<Arguments> providerPrefixCompare() {
return Stream.of(//
Arguments.of("ab", "abc", -1), //
Arguments.of("abb", "abc", -1), //
Arguments.of("abc", "abc", 0), //
Arguments.of("abcd", "abc", 0), //
Arguments.of("abd", "abc", 1), //
Arguments.of("abz", "abc", 23) //
);
}
@Test(dataProvider = "providerPrefixCompare")
@ParameterizedTest
@MethodSource("providerPrefixCompare")
public void testPrefixCompare(final String key, final String prefix, final int expected) {
final NodeEntry nodeEntry = new NodeEntry(ValueType.NODE_POINTER, key.getBytes(StandardCharsets.UTF_8),
new byte[0]);
final int actual = nodeEntry.compareKeyPrefix(prefix.getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(actual, expected, key + " ? " + prefix);
Assertions.assertEquals(expected, actual, key + " ? " + prefix);
}
}

View File

@@ -6,14 +6,13 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.lucares.pdb.diskstorage.DiskBlock;
import org.lucares.pdb.map.NodeEntry.ValueType;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.junit.jupiter.api.Assertions;
@Test
public class PersistentMapDiskNodeTest {
@Test
public void serializeDeserialize() throws Exception {
final List<NodeEntry> entries = new ArrayList<>();
@@ -31,7 +30,7 @@ public class PersistentMapDiskNodeTest {
final PersistentMapDiskNode actualNode = PersistentMapDiskNode.parse(nodeOffset,
new DiskBlock(nodeOffset, byteBuffer));
Assert.assertEquals(actualNode.getEntries(), entries);
Assertions.assertEquals(entries, actualNode.getEntries());
}
private static NodeEntry newNode(final ValueType type, final String key, final String value) {

View File

@@ -1,62 +0,0 @@
//package org.lucares.pdb.map;
//
//import java.io.IOException;
//import java.nio.file.Files;
//import java.nio.file.Path;
//
//import org.lucares.collections.LongList;
//import org.lucares.utils.file.FileUtils;
//import org.testng.Assert;
//import org.testng.annotations.AfterMethod;
//import org.testng.annotations.BeforeMethod;
//import org.testng.annotations.Test;
//
//@Test
//public class PersistentMapOfListsOfLongsTest {
//
// private Path dataDirectory;
//
// @BeforeMethod
// public void beforeMethod() throws IOException {
// dataDirectory = Files.createTempDirectory("pdb");
// }
//
// @AfterMethod
// public void afterMethod() throws IOException {
// FileUtils.delete(dataDirectory);
// }
//
// public void test() throws IOException {
//
// final String mapFilePrefix = "test";
// final String keyA = "a";
// final String keyB = "b";
//
// final int size = 10;
// final LongList a = LongList.range(0, size);
// a.shuffle();
// final LongList b = LongList.range(0, size);
// b.shuffle();
//
// try (PersistentMapOfListsOfLongs<String> map = new PersistentMapOfListsOfLongs<>(dataDirectory, mapFilePrefix,
// PersistentMap.STRING_CODER)) {
//
// for (int i = 0; i < size; i++) {
// map.appendLong(keyA, a.get(i));
// map.appendLong(keyB, b.get(i));
// }
// }
//
// try (PersistentMapOfListsOfLongs<String> map = new PersistentMapOfListsOfLongs<>(dataDirectory, mapFilePrefix,
// PersistentMap.STRING_CODER)) {
//
// final LongList actualA = new LongList();
// map.getLongs(keyA).forEachOrdered(actualA::addAll);
// Assert.assertEquals(actualA, a);
//
// final LongList actualB = new LongList();
// map.getLongs(keyB).forEachOrdered(actualB::addAll);
// Assert.assertEquals(actualB, b);
// }
// }
//}

View File

@@ -15,27 +15,27 @@ import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test
public class PersistentMapTest {
private Path dataDirectory;
@BeforeMethod
@BeforeEach
public void beforeMethod() throws IOException {
dataDirectory = Files.createTempDirectory("pdb");
}
@AfterMethod
@AfterEach
public void afterMethod() throws IOException {
FileUtils.delete(dataDirectory);
}
@Test
public void testSingleValue() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final String value = "value1";
@@ -44,20 +44,20 @@ public class PersistentMapTest {
try (final PersistentMap<String, String> map = new PersistentMap<>(file, dataDirectory,
PersistentMap.STRING_CODER, PersistentMap.STRING_CODER)) {
Assert.assertNull(map.getValue(key));
Assertions.assertNull(map.getValue(key));
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
Assert.assertEquals(map.getValue(key), value);
Assertions.assertEquals(value, map.getValue(key));
}
try (final PersistentMap<String, String> map = new PersistentMap<>(file, dataDirectory,
PersistentMap.STRING_CODER, PersistentMap.STRING_CODER)) {
Assert.assertEquals(map.getValue(key), value);
Assertions.assertEquals(value, map.getValue(key));
}
}
@Test(invocationCount = 1)
@Test
public void testManyValues() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final var insertedValues = new HashMap<String, String>();
@@ -74,9 +74,9 @@ public class PersistentMapTest {
final String key = nextUUID.toString() + "__" + i;
final String value = "long value to waste some bytes " + i + "__"
+ UUID.randomUUID().toString().repeat(1);
Assert.assertNull(map.getValue(key));
Assertions.assertNull(map.getValue(key));
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
insertedValues.put(key, value);
@@ -91,7 +91,7 @@ public class PersistentMapTest {
map.print();
}
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " in the " + i + "th iteration");
}
}
@@ -105,21 +105,21 @@ public class PersistentMapTest {
map.visitNodeEntriesPreOrder(
(node, parentNode, nodeEntry, depth) -> maxDepth.set(Math.max(depth, maxDepth.get())));
Assert.assertTrue(maxDepth.get() >= 4,
Assertions.assertTrue(maxDepth.get() >= 4,
"The tree's depth. This test must have at least depth 4, "
+ "so that we can be sure that splitting parent nodes works recursively, but was "
+ maxDepth.get());
for (final var entry : insertedValues.entrySet()) {
final String actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " after all iterations");
}
}
}
@Test(invocationCount = 1)
@Test
public void testManySmallValues() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final var insertedValues = new HashMap<Long, Long>();
@@ -135,9 +135,9 @@ public class PersistentMapTest {
final Long key = (long) (rnd.nextGaussian() * Integer.MAX_VALUE);
final Long value = (long) (rnd.nextGaussian() * Integer.MAX_VALUE);
Assert.assertNull(map.getValue(key));
Assertions.assertNull(map.getValue(key));
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
insertedValues.put(key, value);
@@ -152,7 +152,7 @@ public class PersistentMapTest {
map.print();
}
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " in the " + i + "th iteration");
}
}
@@ -166,21 +166,21 @@ public class PersistentMapTest {
map.visitNodeEntriesPreOrder(
(node, parentNode, nodeEntry, depth) -> counter.addAndGet(nodeEntry.isInnerNode() ? 1 : 0));
Assert.assertEquals(counter.get(), 4,
Assertions.assertEquals(4, counter.get(),
"number of nodes should be small. Any number larger than 4 indicates, "
+ "that new inner nodes are created even though the existing inner "
+ "nodes could hold the values");
for (final var entry : insertedValues.entrySet()) {
final Long actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " after all iterations");
}
}
}
@Test(invocationCount = 1)
@Test
public void testManyEmptyValues() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final var insertedValues = new HashMap<Long, Empty>();
@@ -196,9 +196,9 @@ public class PersistentMapTest {
final Long key = (long) (rnd.nextGaussian() * Integer.MAX_VALUE);
final Empty value = Empty.INSTANCE;
Assert.assertNull(map.getValue(key));
Assertions.assertNull(map.getValue(key));
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
insertedValues.put(key, value);
@@ -213,7 +213,7 @@ public class PersistentMapTest {
map.print();
}
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " in the " + i + "th iteration");
}
}
@@ -227,21 +227,21 @@ public class PersistentMapTest {
map.visitNodeEntriesPreOrder(
(node, parentNode, nodeEntry, depth) -> counter.addAndGet(nodeEntry.isInnerNode() ? 1 : 0));
Assert.assertEquals(counter.get(), 4,
Assertions.assertEquals(4, counter.get(),
"number of nodes should be small. Any number larger than 4 indicates, "
+ "that new inner nodes are created even though the existing inner "
+ "nodes could hold the values");
for (final var entry : insertedValues.entrySet()) {
final Empty actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " after all iterations");
}
}
}
@Test(invocationCount = 1)
@Test
public void testEasyValues() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final var insertedValues = new HashMap<String, String>();
@@ -259,9 +259,9 @@ public class PersistentMapTest {
final String key = "" + keyNumber;
final String value = "value";
Assert.assertNull(map.getValue(key));
Assertions.assertNull(map.getValue(key));
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
insertedValues.put(key, value);
@@ -270,7 +270,7 @@ public class PersistentMapTest {
for (final var entry : insertedValues.entrySet()) {
final String actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " in the " + i + "th iteration");
}
}
@@ -286,7 +286,7 @@ public class PersistentMapTest {
for (final var entry : insertedValues.entrySet()) {
final String actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " after all iterations");
}
@@ -324,12 +324,12 @@ public class PersistentMapTest {
final Visitor<String, String> visitor = (key, value) -> actualBar.put(key, value);
map.visitValues("bar:", visitor);
Assert.assertEquals(actualBar, expectedBar);
Assertions.assertEquals(expectedBar, actualBar);
}
}
}
@Test(invocationCount = 1)
@Test
public void testLotsOfValues() throws Exception {
final Path file = dataDirectory.resolve("map.db");
final var insertedValues = new HashMap<Long, Long>();
@@ -349,7 +349,7 @@ public class PersistentMapTest {
continue;
}
Assert.assertNull(map.putValue(key, value));
Assertions.assertNull(map.putValue(key, value));
insertedValues.put(key, value);
@@ -362,7 +362,7 @@ public class PersistentMapTest {
map.print();
}
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " in the " + i + "th iteration");
}
}
@@ -381,7 +381,7 @@ public class PersistentMapTest {
final long start = System.nanoTime();
for (final var entry : insertedValues.entrySet()) {
final Long actualValue = map.getValue(entry.getKey());
Assert.assertEquals(actualValue, entry.getValue(),
Assertions.assertEquals(entry.getValue(), actualValue,
"value for key " + entry.getKey() + " after all iterations");
}
System.out.println("nodes=" + counter.get() + ", depth=" + maxDepth.get() + ": "