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

@@ -11,32 +11,32 @@ import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
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.api.DateTimeRange;
import org.lucares.pdb.api.Entries;
import org.lucares.pdb.api.Query;
import org.lucares.performance.db.PerformanceDb;
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 CsvToEntryTransformerTest {
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 testIngest() throws IOException, InterruptedException {
final OffsetDateTime dateA = OffsetDateTime.now();
final OffsetDateTime dateB = OffsetDateTime.now();
@@ -56,13 +56,13 @@ public class CsvToEntryTransformerTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("tag=tagValue", DateTimeRange.max())).singleGroup().flatMap();
Assert.assertEquals(4, result.size());
Assertions.assertEquals(result.size(), 4);
Assert.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0));
Assert.assertEquals(1, result.get(1));
Assertions.assertEquals(result.get(0), dateA.toInstant().toEpochMilli());
Assertions.assertEquals(result.get(1), 1);
Assert.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2));
Assert.assertEquals(2, result.get(3));
Assertions.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli());
Assertions.assertEquals(result.get(3), 2);
}
}
@@ -76,6 +76,7 @@ public class CsvToEntryTransformerTest {
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testIgnoreColumns() throws IOException, InterruptedException {
try (final PerformanceDb db = new PerformanceDb(dataDirectory)) {
@@ -93,7 +94,7 @@ public class CsvToEntryTransformerTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final List<String> availableFields = db.getFields(DateTimeRange.max());
Assert.assertEquals(availableFields.toString(), List.of("tag").toString(),
Assertions.assertEquals(List.of("tag").toString(), availableFields.toString(),
"the ignored field is not returned");
}
}

View File

@@ -4,15 +4,18 @@ import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lucares.collections.LongList;
import org.lucares.pdb.api.DateTimeRange;
import org.lucares.pdb.api.GroupResult;
import org.lucares.pdb.api.Query;
import org.lucares.performance.db.PerformanceDb;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -24,15 +27,19 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = TestOverrides.class)
@SpringBootTest(classes = MySpringTestConfiguration.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class PdbControllerTest {
static {
Configurator.setRootLevel(Level.INFO);
}
@Autowired
private PerformanceDb performanceDb;
@@ -40,7 +47,7 @@ public class PdbControllerTest {
private TestRestTemplate rest;
@Test
public void testUploadCsv() {
public void testUploadCsv() throws InterruptedException {
final String ignoredColumn = "ignoredColumn";
final String timeColumn = "time";
@@ -53,22 +60,23 @@ public class PdbControllerTest {
final CsvReaderSettings settings = CsvReaderSettings.create(timeColumn, ',', ignoredColumn);
uploadCsv(settings, csv);
TimeUnit.SECONDS.sleep(1);
{
final GroupResult groupResult = performanceDb.get(new Query("tag=tagValue", DateTimeRange.ofDay(dateA)))
.singleGroup();
final LongList result = groupResult.flatMap();
System.out.println(PdbTestUtil.timeValueLongListToString(result));
Assert.assertEquals(result.size(), 4);
Assertions.assertEquals(4, result.size());
Assert.assertEquals(result.get(0), dateA.toInstant().toEpochMilli());
Assert.assertEquals(result.get(1), 1);
Assertions.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0));
Assertions.assertEquals(1, result.get(1));
Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli());
Assert.assertEquals(result.get(3), 2);
Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2));
Assertions.assertEquals(2, result.get(3));
}
{
final List<String> fields = performanceDb.getFields(DateTimeRange.max());
Assert.assertTrue("ignoredColumn not in fields. fields: " + fields, !fields.contains(ignoredColumn));
Assertions.assertTrue(!fields.contains(ignoredColumn), "ignoredColumn not in fields. fields: " + fields);
}
}
@@ -90,7 +98,7 @@ public class PdbControllerTest {
final ResponseEntity<String> response = rest.exchange("/data", HttpMethod.POST, entity, String.class);
Assert.assertEquals("response status", HttpStatus.CREATED, response.getStatusCode());
Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED, "response status");
}
}

View File

@@ -8,7 +8,6 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -17,40 +16,41 @@ import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
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.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.Query;
import org.lucares.pdb.datastore.internal.DataStore;
import org.lucares.performance.db.PdbExport;
import org.lucares.performance.db.PerformanceDb;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
@Test
public class TcpIngestorTest {
private static final Logger LOGGER = LoggerFactory.getLogger(TcpIngestorTest.class);
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 testIngestDataViaTcpStream() throws Exception {
final OffsetDateTime dateA = OffsetDateTime.now();
@@ -82,16 +82,17 @@ public class TcpIngestorTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("host=" + host, DateTimeRange.ofDay(dateA))).singleGroup()
.flatMap();
Assert.assertEquals(result.size(), 4);
Assertions.assertEquals(4, result.size());
Assert.assertEquals(result.get(0), dateA.toInstant().toEpochMilli());
Assert.assertEquals(result.get(1), 1);
Assertions.assertEquals(dateA.toInstant().toEpochMilli(), result.get(0));
Assertions.assertEquals(1, result.get(1));
Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli());
Assert.assertEquals(result.get(3), 2);
Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2));
Assertions.assertEquals(2, result.get(3));
}
}
@Test
public void testIngestDataViaTcpStream_CustomFormat() throws Exception {
final long dateA = Instant.now().toEpochMilli();
@@ -137,16 +138,16 @@ public class TcpIngestorTest {
// 5. check that the data is correctly inserted
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap();
Assert.assertEquals(result.size(), 6);
Assertions.assertEquals(6, result.size());
Assert.assertEquals(result.get(0), dateA);
Assert.assertEquals(result.get(1), 1);
Assertions.assertEquals(dateA, result.get(0));
Assertions.assertEquals(1, result.get(1));
Assert.assertEquals(result.get(2), dateC);
Assert.assertEquals(result.get(3), 3);
Assertions.assertEquals(dateC, result.get(2));
Assertions.assertEquals(3, result.get(3));
Assert.assertEquals(result.get(4), dateB);
Assert.assertEquals(result.get(5), 2);
Assertions.assertEquals(dateB, result.get(4));
Assertions.assertEquals(2, result.get(5));
}
}
@@ -190,27 +191,18 @@ public class TcpIngestorTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap();
Assert.assertEquals(result.size(), 4);
Assertions.assertEquals(4, result.size());
Assert.assertEquals(result.get(0), dateA.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli());
Assert.assertEquals(result.get(1), -1);
Assertions.assertEquals(dateA.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(0));
Assertions.assertEquals(-1, result.get(1));
Assert.assertEquals(result.get(2), dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli());
Assert.assertEquals(result.get(3), 2);
Assertions.assertEquals(dateB.toInstant().truncatedTo(ChronoUnit.MILLIS).toEpochMilli(), result.get(2));
Assertions.assertEquals(2, result.get(3));
}
}
@DataProvider
public Object[][] providerSendingFormats() {
final List<Object[]> data = new ArrayList<>();
data.add(new Object[] { "csv" });
data.add(new Object[] { "json" });
return data.toArray(Object[][]::new);
}
@Test(dataProvider = "providerSendingFormats")
@ParameterizedTest
@ValueSource(strings = { "csv", "json" })
public void testRandomOrder(final String format) throws Exception {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
@@ -252,10 +244,11 @@ public class TcpIngestorTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("host=" + host, dateRange)).singleGroup().flatMap();
Assert.assertEquals(LongPair.fromLongList(result), LongPair.fromLongList(expected));
Assertions.assertEquals(LongPair.fromLongList(expected), LongPair.fromLongList(result));
}
}
@Test
public void testCsvIngestorIgnoresColumns() throws Exception {
try (TcpIngestor ingestor = new TcpIngestor(dataDirectory)) {
@@ -277,11 +270,12 @@ public class TcpIngestorTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final List<String> availableFields = db.getFields(DateTimeRange.max());
Assert.assertEquals(availableFields.toString(), List.of("host").toString(),
Assertions.assertEquals(List.of("host").toString(), availableFields.toString(),
"the ignored field is not returned");
}
}
@Test
public void testCsvIngestorHandlesDurationAtEnd() throws Exception {
final String host = "someHost";
@@ -311,10 +305,10 @@ public class TcpIngestorTest {
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
final LongList result = db.get(new Query("host=" + host, DateTimeRange.max())).singleGroup().flatMap();
Assert.assertEquals(result.size(), 4);
Assertions.assertEquals(4, result.size());
Assert.assertEquals(result.get(1), value1);
Assert.assertEquals(result.get(3), value2);
Assertions.assertEquals(value1, result.get(1));
Assertions.assertEquals(value2, result.get(3));
}
}
}

View File

@@ -5,197 +5,186 @@ import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.Assertions;
@Test
public class FastISODateParserTest {
@DataProvider(name = "providerValidDate")
public Object[][] providerValidDate() {
return new Object[][] { //
{ "2018-11-18T14:42:49.123456789Z" }, //
{ "2018-11-18T14:42:49.123456789+12:34" }, //
{ "2018-11-18T14:42:49.12345678Z" }, //
{ "2018-11-18T14:42:49.12345678+12:34" }, //
{ "2018-11-18T14:42:49.1234567Z" }, //
{ "2018-11-18T14:42:49.1234567+12:34" }, //
{ "2018-11-18T14:42:49.123456Z" }, //
{ "2018-11-18T14:42:49.123456+12:34" }, //
{ "2018-11-18T14:42:49.33256Z" }, //
{ "2018-11-18T14:42:49.33256+12:34" }, //
{ "2018-11-18T14:42:49.3325Z" }, //
{ "2018-11-18T14:42:49.3325+12:34" }, //
{ "2018-11-18T14:42:49.332Z" }, //
{ "2018-11-18T14:42:49.332+00:00" }, //
{ "2018-11-18T14:42:49.332+12:34" }, //
{ "2018-11-18T14:42:49.332-01:23" }, //
{ "2018-11-18T14:55:49.44Z" }, //
{ "2018-11-18T14:55:49.55-01:23" }, //
{ "2018-11-18T14:55:49.4Z" }, //
{ "2018-11-18T14:55:49.5-01:23" }, //
{ "2018-11-18T14:55:49.Z" }, //
{ "2018-11-18T14:55:49.-01:23" }, //
{ "2018-11-18T14:55:49Z" }, //
{ "2018-11-18T14:55:49-01:23" },//
};
public static Stream<Arguments> providerValidDate() {
return Stream.of( //
Arguments.of("2018-11-18T14:42:49.123456789Z"), //
Arguments.of("2018-11-18T14:42:49.123456789+12:34"), //
Arguments.of("2018-11-18T14:42:49.12345678Z"), //
Arguments.of("2018-11-18T14:42:49.12345678+12:34"), //
Arguments.of("2018-11-18T14:42:49.1234567Z"), //
Arguments.of("2018-11-18T14:42:49.1234567+12:34"), //
Arguments.of("2018-11-18T14:42:49.123456Z"), //
Arguments.of("2018-11-18T14:42:49.123456+12:34"), //
Arguments.of("2018-11-18T14:42:49.33256Z"), //
Arguments.of("2018-11-18T14:42:49.33256+12:34"), //
Arguments.of("2018-11-18T14:42:49.3325Z"), //
Arguments.of("2018-11-18T14:42:49.3325+12:34"), //
Arguments.of("2018-11-18T14:42:49.332Z"), //
Arguments.of("2018-11-18T14:42:49.332+00:00"), //
Arguments.of("2018-11-18T14:42:49.332+12:34"), //
Arguments.of("2018-11-18T14:42:49.332-01:23"), //
Arguments.of("2018-11-18T14:55:49.44Z"), //
Arguments.of("2018-11-18T14:55:49.55-01:23"), //
Arguments.of("2018-11-18T14:55:49.4Z"), //
Arguments.of("2018-11-18T14:55:49.5-01:23"), //
Arguments.of("2018-11-18T14:55:49.Z"), //
Arguments.of("2018-11-18T14:55:49.-01:23"), //
Arguments.of("2018-11-18T14:55:49Z"), //
Arguments.of("2018-11-18T14:55:49-01:23") //
);
}
@Test(dataProvider = "providerValidDate")
@ParameterizedTest
@MethodSource("providerValidDate")
public void testParseValidDate(final String date) {
final OffsetDateTime actualDate = new FastISODateParser().parse(date);
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
Assert.assertEquals(actualDate, expectedDate);
Assertions.assertEquals(expectedDate, actualDate, "date: " + date);
}
@Test(dataProvider = "providerValidDate")
@ParameterizedTest
@MethodSource("providerValidDate")
public void testParseValidDateAsEpochMilli(final String date) {
final long actualDate = new FastISODateParser().parseAsEpochMilli(date);
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
Assert.assertEquals(actualDate, expectedDate.toInstant().toEpochMilli());
Assertions.assertEquals(expectedDate.toInstant().toEpochMilli(), actualDate, "date: " + date);
}
@DataProvider(name = "providerPseudoISODate")
public Object[][] providerPseudoISODate() {
return new Object[][] { //
{ "2018-11-18T14:42:49,123Z", "2018-11-18T14:42:49.123Z" }, // with comman instead of dot
{ "2018-11-18T14:42:49,123+12:34", "2018-11-18T14:42:49.123+12:34" }, // with comman instead of dot
{ "2018-11-18T14:42:49.123", "2018-11-18T14:42:49.123Z" }, // without timezone
{ "2018-11-18T14:42:49,123", "2018-11-18T14:42:49.123Z" }, // with command, without timezone
};
public static Stream<Arguments> providerPseudoISODate() {
return Stream.of( //
Arguments.of("2018-11-18T14:42:49,123Z", "2018-11-18T14:42:49.123Z"), // with comma instead of dot
Arguments.of("2018-11-18T14:42:49,123+12:34", "2018-11-18T14:42:49.123+12:34"), // with comma instead of
// dot
Arguments.of("2018-11-18T14:42:49.123", "2018-11-18T14:42:49.123Z"), // without timezone
Arguments.of("2018-11-18T14:42:49,123", "2018-11-18T14:42:49.123Z") // with command, without timezone
);
}
@Test(dataProvider = "providerPseudoISODate")
@ParameterizedTest
@MethodSource("providerPseudoISODate")
public void testParsePseudoISODate(final String date, final String expectedDate) {
final OffsetDateTime actualDate = new FastISODateParser().parse(date);
final OffsetDateTime expected = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(expectedDate));
Assert.assertEquals(actualDate, expected);
Assertions.assertEquals(expected, actualDate);
}
@Test(dataProvider = "providerPseudoISODate")
@ParameterizedTest
@MethodSource("providerPseudoISODate")
public void testParsePseudoISODateAsEpochMilli(final String date, final String expectedDate) {
final long actualDate = new FastISODateParser().parseAsEpochMilli(date);
final OffsetDateTime expected = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(expectedDate));
Assert.assertEquals(actualDate, expected.toInstant().toEpochMilli());
Assertions.assertEquals(expected.toInstant().toEpochMilli(), actualDate);
}
@DataProvider(name = "providerParseInvalidDate")
public Object[][] providerParseInvalidDate() {
return new Object[][] { //
{ "a2018-11-18T14:42:49.332Z" }, //
{ "a018-11-18T14:42:49.332Z" }, //
{ "2a18-11-18T14:42:49.332Z" }, //
{ "20a8-11-18T14:42:49.332Z" }, //
{ "201a-11-18T14:42:49.332Z" }, //
{ "2018-a1-18T14:42:49.332Z" }, //
{ "2018-1a-18T14:42:49.332Z" }, //
{ "2018-11-a8T14:42:49.332Z" }, //
{ "2018-11-1aT14:42:49.332Z" }, //
{ "2018-11-18Ta4:42:49.332Z" }, //
{ "2018-11-18T1a:42:49.332Z" }, //
{ "2018-11-18T14:a2:49.332Z" }, //
{ "2018-11-18T14:4a:49.332Z" }, //
{ "2018-11-18T14:42:a9.332Z" }, //
{ "2018-11-18T14:42:4a.332Z" }, //
{ "2018-11-18T14:42:49.a32Z" }, //
{ "2018-11-18T14:42:49.3a2Z" }, //
{ "2018-11-18T14:42:49.33aZ" }, //
{ "2018-11-18T14:42:49.332a" }, //
{ "2018-11-18T14:42:49.332a00:00" }, //
{ "2018-11-18T14:42:49.332+a0:00" }, //
{ "2018-11-18T14:42:49.332+0a:00" }, //
{ "2018-11-18T14:42:49.332+00:a0" }, //
{ "2018-11-18T14:42:49.332+00:0a" }//
};
public static Stream<Arguments> providerParseInvalidDate() {
return Stream.of( //
Arguments.of("a2018-11-18T14:42:49.332Z"), //
Arguments.of("a018-11-18T14:42:49.332Z"), //
Arguments.of("2a18-11-18T14:42:49.332Z"), //
Arguments.of("20a8-11-18T14:42:49.332Z"), //
Arguments.of("201a-11-18T14:42:49.332Z"), //
Arguments.of("2018-a1-18T14:42:49.332Z"), //
Arguments.of("2018-1a-18T14:42:49.332Z"), //
Arguments.of("2018-11-a8T14:42:49.332Z"), //
Arguments.of("2018-11-1aT14:42:49.332Z"), //
Arguments.of("2018-11-18Ta4:42:49.332Z"), //
Arguments.of("2018-11-18T1a:42:49.332Z"), //
Arguments.of("2018-11-18T14:a2:49.332Z"), //
Arguments.of("2018-11-18T14:4a:49.332Z"), //
Arguments.of("2018-11-18T14:42:a9.332Z"), //
Arguments.of("2018-11-18T14:42:4a.332Z"), //
Arguments.of("2018-11-18T14:42:49.a32Z"), //
Arguments.of("2018-11-18T14:42:49.3a2Z"), //
Arguments.of("2018-11-18T14:42:49.33aZ"), //
Arguments.of("2018-11-18T14:42:49.332a"), //
Arguments.of("2018-11-18T14:42:49.332a00:00"), //
Arguments.of("2018-11-18T14:42:49.332+a0:00"), //
Arguments.of("2018-11-18T14:42:49.332+0a:00"), //
Arguments.of("2018-11-18T14:42:49.332+00:a0"), //
Arguments.of("2018-11-18T14:42:49.332+00:0a") //
);
}
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "providerParseInvalidDate")
@ParameterizedTest
@MethodSource("providerParseInvalidDate")
public void testParseInvalidDate(final String invalidDate) {
new FastISODateParser().parse(invalidDate);
try {
new FastISODateParser().parse(invalidDate);
Assertions.fail("expected illegalArgumentException for input: " + invalidDate);
} catch (final IllegalArgumentException e) {
// expected
}
}
@DataProvider(name = "providerDateToTimestamp")
public Object[][] providerDateToTimestamp() {
return new Object[][] { //
{ "2018-11-18T14:42:49.123Z" }, //
public static Stream<Arguments> providerDateToTimestamp() {
return Stream.of( //
Arguments.of("2018-11-18T14:42:49.123Z"), //
// There are no leap seconds in java-time:
// In reality, UTC has a leap second 2016-12-31T23:59:60Z, but java handles
// this differently. This makes it a little bit easier for us, because we do not
// have to handle this.
{ "2016-12-31T23:59:59.999Z" }, // before leap second
{ "2017-01-01T00:00:00.000Z" }, // after leap second
Arguments.of("2016-12-31T23:59:59.999Z"), // before leap second
Arguments.of("2017-01-01T00:00:00.000Z"), // after leap second
// normal leap days exist
{ "2016-02-28T23:59:59.999Z" }, // before leap day
{ "2016-02-29T00:00:00.000Z" }, // leap day
{ "2016-02-29T23:59:59.999Z" }, // leap day
{ "2016-03-01T00:00:00.000Z" }, // after leap day
Arguments.of("2016-02-28T23:59:59.999Z"), // before leap day
Arguments.of("2016-02-29T00:00:00.000Z"), // leap day
Arguments.of("2016-02-29T23:59:59.999Z"), // leap day
Arguments.of("2016-03-01T00:00:00.000Z"), // after leap day
// dates with non-UTC timezones
{ "2018-11-18T14:42:49.123+12:34" }, //
{ "2018-11-18T02:34:56.123+12:34" }, //
Arguments.of("2018-11-18T14:42:49.123+12:34"), //
Arguments.of("2018-11-18T02:34:56.123+12:34"), //
// dates with non-UTC timezones and leap days
{ "2016-02-29T00:59:59.999+01:00" }, // before leap day
{ "2016-02-29T01:00:00.000+01:00" }, // leap day
{ "2016-03-01T00:59:59.999+01:00" }, // leap day
{ "2016-03-01T01:00:00.000+01:00" }, // after leap day
};
Arguments.of("2016-02-29T00:59:59.999+01:00"), // before leap day
Arguments.of("2016-02-29T01:00:00.000+01:00"), // leap day
Arguments.of("2016-03-01T00:59:59.999+01:00"), // leap day
Arguments.of("2016-03-01T01:00:00.000+01:00") // after leap day
);
}
@Test(dataProvider = "providerDateToTimestamp")
@ParameterizedTest
@MethodSource("providerDateToTimestamp")
public void testDateToTimestamp(final String date) {
final long actualEpochMilli = new FastISODateParser().parseAsEpochMilli(date);
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
final long expectedEpochMilli = expectedDate.toInstant().toEpochMilli();
Assert.assertEquals(actualEpochMilli, expectedEpochMilli);
Assertions.assertEquals(expectedEpochMilli, actualEpochMilli, "date: " + date);
}
@Test(dataProvider = "providerDateToTimestamp")
@ParameterizedTest
@MethodSource("providerDateToTimestamp")
public void testDateToTimestampWithBytes(final String date) {
final byte[] dateAsBytes = date.getBytes(StandardCharsets.UTF_8);
final long actualEpochMilli = new FastISODateParser().parseAsEpochMilli(dateAsBytes, 0);
final OffsetDateTime expectedDate = OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(date));
final long expectedEpochMilli = expectedDate.toInstant().toEpochMilli();
Assert.assertEquals(actualEpochMilli, expectedEpochMilli);
}
@Test(enabled = false)
public void test() {
final OffsetDateTime expectedDate = OffsetDateTime
.from(DateTimeFormatter.ISO_DATE_TIME.parse("2016-12-31T23:00:00.000Z"));
final long epochMilli = expectedDate.toInstant().toEpochMilli();
for (int i = 0; i < 1000; i++) {
final long timestamp = epochMilli + i * 10000;
final OffsetDateTime date = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.UTC);
System.out.println(timestamp + " " + date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
Assertions.assertEquals(expectedEpochMilli, actualEpochMilli, "date: " + date);
}
public static void main(final String[] args) throws IOException, InterruptedException {

View File

@@ -4,30 +4,30 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
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;
@Test
public class DataSeriesStatsTest {
@DataProvider
public Object[][] providerAverage() {
final List<Object[]> result = new ArrayList<>();
public static Stream<Arguments> providerAverage() {
final List<Arguments> result = new ArrayList<>();
{
final List<DataSeriesStats> stats = Arrays.asList(//
new DataSeriesStats(10, 0, 0, 5.0)//
);
final double expected = 5.0;
result.add(new Object[] { stats, expected });
result.add(Arguments.of(stats, expected));
}
{
final List<DataSeriesStats> stats = Arrays.asList(//
new DataSeriesStats(0, 0, 0, 5.0)//
);
final double expected = 0.0; // no values
result.add(new Object[] { stats, expected });
result.add(Arguments.of(stats, expected));
}
{
@@ -36,7 +36,7 @@ public class DataSeriesStatsTest {
new DataSeriesStats(40, 0, 0, 1.0)//
);
final double expected = 1.8; // 90 / 50
result.add(new Object[] { stats, expected });
result.add(Arguments.of(stats, expected));
}
{
final List<DataSeriesStats> stats = Arrays.asList(//
@@ -45,16 +45,17 @@ public class DataSeriesStatsTest {
new DataSeriesStats(20, 0, 0, 2.0)//
);
final double expected = 3.0; // (35+40) / 25
result.add(new Object[] { stats, expected });
result.add(Arguments.of(stats, expected));
}
return result.toArray(new Object[0][]);
return result.stream();
}
@Test(dataProvider = "providerAverage")
@ParameterizedTest
@MethodSource("providerAverage")
public void testAverage(final Collection<DataSeriesStats> stats, final double expected) {
final double actual = DataSeriesStats.average(stats);
Assert.assertEquals(actual, expected, 0.01);
Assertions.assertEquals(actual, expected, 0.01);
}
}