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:
@@ -1,129 +0,0 @@
|
||||
//package org.lucares.performance.db;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.IOException;
|
||||
//import java.nio.file.Files;
|
||||
//import java.nio.file.Path;
|
||||
//import java.nio.file.StandardOpenOption;
|
||||
//import java.time.Instant;
|
||||
//import java.time.OffsetDateTime;
|
||||
//import java.time.ZoneId;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.Arrays;
|
||||
//import java.util.Iterator;
|
||||
//import java.util.List;
|
||||
//
|
||||
//import org.lucares.pdb.api.Entry;
|
||||
//import org.lucares.pdb.api.Tags;
|
||||
//import org.testng.Assert;
|
||||
//import org.testng.annotations.AfterMethod;
|
||||
//import org.testng.annotations.BeforeMethod;
|
||||
//import org.testng.annotations.DataProvider;
|
||||
//import org.testng.annotations.Test;
|
||||
//
|
||||
//@Test
|
||||
//public class PdbReaderWriterTest {
|
||||
//
|
||||
// private Path dataDirectory;
|
||||
//
|
||||
// private static final Tags TAGS = Tags.create();
|
||||
//
|
||||
// @BeforeMethod
|
||||
// public void beforeMethod() throws IOException {
|
||||
// dataDirectory = Files.createTempDirectory("pdb");
|
||||
// }
|
||||
//
|
||||
// @AfterMethod
|
||||
// public void afterMethod() throws IOException {
|
||||
// org.lucares.utils.file.FileUtils.delete(dataDirectory);
|
||||
// }
|
||||
//
|
||||
// @DataProvider(name = "providerWriteRead")
|
||||
// public Iterator<Object[]> providerWriteRead() {
|
||||
//
|
||||
// final OffsetDateTime two_sixteen = DateUtils.getDate(2016, 1, 1, 1, 1, 1);
|
||||
//
|
||||
// final List<Long> values = Arrays.asList(0L, 1L, 63L, 64L, 127L, 128L, 202L, 255L, 256L, 8191L, 8192L, 1048575L,
|
||||
// 1048576L, 134217728L, 17179869183L, 17179869184L, 2199023255551L, 2199023255552L, 281474976710655L,
|
||||
// 281474976710656L, 36028797018963967L, 36028797018963968L, 4611686018427387901L, 4611686018427387904L);
|
||||
//
|
||||
// final List<Object[]> result = new ArrayList<>();
|
||||
//
|
||||
// // single values
|
||||
// for (final Long value : values) {
|
||||
// result.add(new Object[] { Arrays.asList(new Entry(two_sixteen, value, TAGS)) });
|
||||
// }
|
||||
//
|
||||
// // multivalues
|
||||
// final List<Entry> entries = new ArrayList<>();
|
||||
// for (int i = 0; i < 100; i++) {
|
||||
//
|
||||
// final long epochMilli = 123456 * i;
|
||||
//
|
||||
// final OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.of("UTC"));
|
||||
//
|
||||
// entries.add(new Entry(date, i, TAGS));
|
||||
// }
|
||||
// result.add(new Object[] { entries });
|
||||
//
|
||||
// return result.iterator();
|
||||
// }
|
||||
//
|
||||
// @Test(dataProvider = "providerWriteRead")
|
||||
// public void testWriteRead(final List<Entry> entries) throws Exception {
|
||||
//
|
||||
// final File file = Files.createTempFile(dataDirectory, "pdb", ".db").toFile();
|
||||
// final Path relativePath = dataDirectory.relativize(file.toPath());
|
||||
// final PdbFile pdbFile = new PdbFile(relativePath, TAGS);
|
||||
//
|
||||
// try (PdbWriter writer = new PdbWriter(dataDirectory, pdbFile)) {
|
||||
// for (final Entry entry : entries) {
|
||||
// writer.write(entry);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// try (final PdbReader reader = new PdbReader(dataDirectory, pdbFile)) {
|
||||
//
|
||||
// for (final Entry entry : entries) {
|
||||
//
|
||||
// final Entry actual = reader.readEntry().orElseThrow(() -> new AssertionError());
|
||||
//
|
||||
// Assert.assertEquals(actual, entry);
|
||||
// }
|
||||
// reader.readEntry().ifPresent(e -> {
|
||||
// throw new AssertionError();
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test(expectedExceptions = FileCorruptException.class)
|
||||
// public void testReadExceptionOnCorruptEntries() throws Exception {
|
||||
//
|
||||
// final Entry entryA = new Entry(1, 1, TAGS);
|
||||
//
|
||||
// final File file = Files.createTempFile(dataDirectory, "pdb", ".db").toFile();
|
||||
// final Path relativePath = dataDirectory.relativize(file.toPath());
|
||||
// final PdbFile pdbFile = new PdbFile(relativePath, TAGS);
|
||||
//
|
||||
// try (PdbWriter writer = new PdbWriter(dataDirectory, pdbFile)) {
|
||||
// writer.write(entryA);
|
||||
// }
|
||||
//
|
||||
// // make the file corrupt
|
||||
// // two date consecutive increments will never happen in valid data
|
||||
// final byte[] corruptEntries = new byte[] { //
|
||||
// ByteType.DATE_INCREMENT.getBytePrefixAsByte(), //
|
||||
// ByteType.DATE_INCREMENT.getBytePrefixAsByte() //
|
||||
// };
|
||||
//
|
||||
// Files.write(file.toPath(), corruptEntries, StandardOpenOption.APPEND);
|
||||
//
|
||||
// try (final PdbReader reader = new PdbReader(dataDirectory, pdbFile)) {
|
||||
//
|
||||
// final Entry actualA = reader.readEntry().orElseThrow(() -> new AssertionError());
|
||||
// Assert.assertEquals(actualA, entryA);
|
||||
//
|
||||
// reader.readEntry(); // should throw FileCorruptException
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -12,6 +12,11 @@ import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.lucares.collections.LongList;
|
||||
import org.lucares.pdb.api.DateTimeRange;
|
||||
import org.lucares.pdb.api.Entry;
|
||||
@@ -19,28 +24,24 @@ import org.lucares.pdb.api.GroupResult;
|
||||
import org.lucares.pdb.api.Query;
|
||||
import org.lucares.pdb.api.Result;
|
||||
import org.lucares.pdb.api.Tags;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.lucares.utils.DateUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class PerformanceDbTest {
|
||||
|
||||
private Path dataDirectory;
|
||||
|
||||
@BeforeMethod
|
||||
@BeforeEach
|
||||
public void beforeMethod() throws IOException {
|
||||
dataDirectory = Files.createTempDirectory("pdb");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
@AfterEach
|
||||
public void afterMethod() throws IOException {
|
||||
org.lucares.utils.file.FileUtils.delete(dataDirectory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertRead() throws Exception {
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
@@ -53,13 +54,14 @@ public class PerformanceDbTest {
|
||||
final Result result = db.get(Query.createQuery(tags, DateTimeRange.ofDay(nowInUtc)));
|
||||
final LongList stream = result.singleGroup().flatMap();
|
||||
|
||||
Assert.assertEquals(stream.size(), 2);
|
||||
Assertions.assertEquals(2, stream.size());
|
||||
|
||||
Assert.assertEquals(stream.get(0), date);
|
||||
Assert.assertEquals(stream.get(1), value);
|
||||
Assertions.assertEquals(date, stream.get(0));
|
||||
Assertions.assertEquals(value, stream.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertIntoMultipleFilesRead() throws Exception {
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
@@ -76,12 +78,12 @@ public class PerformanceDbTest {
|
||||
|
||||
final LongList stream = db.get(Query.createQuery(tags, dateRange)).singleGroup().flatMap();
|
||||
|
||||
Assert.assertEquals(stream.size(), 4);
|
||||
Assertions.assertEquals(4, stream.size());
|
||||
|
||||
Assert.assertEquals(stream.get(0), dayOne);
|
||||
Assert.assertEquals(stream.get(1), valueOne);
|
||||
Assert.assertEquals(stream.get(2), dayTwo);
|
||||
Assert.assertEquals(stream.get(3), valueTwo);
|
||||
Assertions.assertEquals(dayOne, stream.get(0));
|
||||
Assertions.assertEquals(valueOne, stream.get(1));
|
||||
Assertions.assertEquals(dayTwo, stream.get(2));
|
||||
Assertions.assertEquals(valueTwo, stream.get(3));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,16 +104,8 @@ public class PerformanceDbTest {
|
||||
return result;
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] providerAppendToExistingFile() throws Exception {
|
||||
return new Object[][] { //
|
||||
{ 2 }, //
|
||||
{ 100 }, //
|
||||
{ 500 }, //
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerAppendToExistingFile")
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = { 2, 100, 500 })
|
||||
public void testAppendToExistingFile(final long numberOfEntries) throws Exception {
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
@@ -130,30 +124,22 @@ public class PerformanceDbTest {
|
||||
db.putEntries(entries);
|
||||
|
||||
final LongList actualEntries = db.get(Query.createQuery(tags, timeRange)).singleGroup().flatMap();
|
||||
Assert.assertEquals(actualEntries.size(), entries.size() * 2);
|
||||
Assertions.assertEquals(entries.size() * 2, actualEntries.size());
|
||||
|
||||
for (int i = 0; i < entries.size(); i++) {
|
||||
final Entry entry = entries.get(i);
|
||||
final long epochMilli = entry.getEpochMilli();
|
||||
final long value = entry.getValue();
|
||||
|
||||
Assert.assertEquals(actualEntries.get(i * 2), epochMilli);
|
||||
Assert.assertEquals(actualEntries.get(i * 2 + 1), value);
|
||||
Assertions.assertEquals(epochMilli, actualEntries.get(i * 2));
|
||||
Assertions.assertEquals(value, actualEntries.get(i * 2 + 1));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] providerAppendToExistingFileWithRestart() throws Exception {
|
||||
return new Object[][] { //
|
||||
{ 2 }, //
|
||||
{ 100 }, //
|
||||
{ 500 }, //
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerAppendToExistingFileWithRestart")
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = { 2, 100, 500 })
|
||||
public void testAppendToExistingFileWithRestart(final long numberOfEntries) throws Exception {
|
||||
final Tags tags;
|
||||
final List<Entry> expected = new ArrayList<>();
|
||||
@@ -182,12 +168,13 @@ public class PerformanceDbTest {
|
||||
expected.addAll(entries);
|
||||
|
||||
final LongList actualEntries = db.get(Query.createQuery(tags, timeRange)).singleGroup().flatMap();
|
||||
Assert.assertEquals(actualEntries.size(), expected.size() * 2);
|
||||
Assertions.assertEquals(expected.size() * 2, actualEntries.size());
|
||||
|
||||
Assert.assertEquals(actualEntries, toExpectedValues(expected));
|
||||
Assertions.assertEquals(toExpectedValues(expected), actualEntries);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertIntoMultipleFilesWithDifferentTags() throws Exception {
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
@@ -215,13 +202,13 @@ public class PerformanceDbTest {
|
||||
db.putEntries(entriesThree);
|
||||
|
||||
final LongList actualEntriesOne = db.get(Query.createQuery(tagsOne, dateRange)).singleGroup().flatMap();
|
||||
Assert.assertEquals(actualEntriesOne, toExpectedValues(entriesOne));
|
||||
Assertions.assertEquals(toExpectedValues(entriesOne), actualEntriesOne);
|
||||
|
||||
final LongList actualEntriesTwo = db.get(Query.createQuery(tagsTwo, dateRange)).singleGroup().flatMap();
|
||||
Assert.assertEquals(actualEntriesTwo, toExpectedValues(entriesTwo));
|
||||
Assertions.assertEquals(toExpectedValues(entriesTwo), actualEntriesTwo);
|
||||
|
||||
final LongList actualEntriesThree = db.get(Query.createQuery(tagsThree, dateRange)).singleGroup().flatMap();
|
||||
Assert.assertEquals(actualEntriesThree, toExpectedValues(entriesThree));
|
||||
Assertions.assertEquals(toExpectedValues(entriesThree), actualEntriesThree);
|
||||
|
||||
final LongList actualEntriesAll = db.get(Query.createQuery(tagsCommon, dateRange)).singleGroup().flatMap();
|
||||
final List<Entry> expectedAll = CollectionUtils.collate(entriesOne,
|
||||
@@ -232,10 +219,11 @@ public class PerformanceDbTest {
|
||||
actualEntriesAll.sort();
|
||||
expectedValues.sort();
|
||||
|
||||
Assert.assertEquals(actualEntriesAll, expectedValues);
|
||||
Assertions.assertEquals(expectedValues, actualEntriesAll);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupBySingleField() throws Exception {
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
final OffsetDateTime from = DateUtils.getDate(2016, 1, 1, 00, 00, 00);
|
||||
@@ -260,19 +248,20 @@ public class PerformanceDbTest {
|
||||
final Tags groupedBy = groupResult.getGroupedBy();
|
||||
|
||||
if (groupedBy.equals(Tags.createAndAddToDictionary(key, "one"))) {
|
||||
Assert.assertEquals(groupResult.flatMap(), entriesOne);
|
||||
Assertions.assertEquals(entriesOne, groupResult.flatMap());
|
||||
} else if (groupedBy.equals(Tags.createAndAddToDictionary(key, "two"))) {
|
||||
|
||||
Assert.assertEquals(groupResult.flatMap(), entriesTwo);
|
||||
Assertions.assertEquals(entriesTwo, groupResult.flatMap());
|
||||
} else if (groupedBy.isEmpty()) {
|
||||
Assert.assertEquals(groupResult.flatMap(), entriesThree);
|
||||
Assertions.assertEquals(entriesThree, groupResult.flatMap());
|
||||
} else {
|
||||
Assert.fail("unexpected group: " + groupResult.getGroupedBy());
|
||||
Assertions.fail("unexpected group: " + groupResult.getGroupedBy());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupByMultipleFields() throws Exception {
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
final OffsetDateTime from = DateUtils.getDate(2016, 1, 1, 00, 00, 00);
|
||||
@@ -302,7 +291,7 @@ public class PerformanceDbTest {
|
||||
final Tags groupedBy = groupResult.getGroupedBy();
|
||||
|
||||
if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "one", key2, "aaa"))) {
|
||||
Assert.assertEquals(groupResult.flatMap(), entriesOne);
|
||||
Assertions.assertEquals(entriesOne, groupResult.flatMap());
|
||||
} else if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "two", key2, "bbb"))) {
|
||||
// there is no defined order of the entries.
|
||||
// eventually we might return them in ascending order, but
|
||||
@@ -312,11 +301,11 @@ public class PerformanceDbTest {
|
||||
entriesTwo.sort();
|
||||
actualEntries.sort();
|
||||
|
||||
Assert.assertEquals(actualEntries, entriesTwo);
|
||||
Assertions.assertEquals(entriesTwo, actualEntries);
|
||||
} else if (groupedBy.equals(Tags.createAndAddToDictionary(key1, "three"))) {
|
||||
Assert.assertEquals(groupResult.flatMap(), entriesThree);
|
||||
Assertions.assertEquals(entriesThree, groupResult.flatMap());
|
||||
} else {
|
||||
Assert.fail("unexpected group: " + groupedBy);
|
||||
Assertions.fail("unexpected group: " + groupedBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user