Skip to content

Commit c55a378

Browse files
committed
dump timezone and offset for junit reporter xml timestamp
1 parent 4688072 commit c55a378

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Diff for: junit-platform-engine/src/main/java/org/junit/platform/engine/reporting/ReportEntry.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.apiguardian.api.API.Status.STABLE;
1414

1515
import java.time.LocalDateTime;
16+
import java.time.ZonedDateTime;
1617
import java.util.Collections;
1718
import java.util.LinkedHashMap;
1819
import java.util.Map;
@@ -32,7 +33,7 @@
3233
@API(status = STABLE, since = "1.0")
3334
public final class ReportEntry {
3435

35-
private final LocalDateTime timestamp = LocalDateTime.now();
36+
private final ZonedDateTime timestamp = ZonedDateTime.now();
3637
private final Map<String, String> keyValuePairs = new LinkedHashMap<>();
3738

3839
/**
@@ -86,6 +87,17 @@ public final Map<String, String> getKeyValuePairs() {
8687
* @return when this entry was created; never {@code null}
8788
*/
8889
public final LocalDateTime getTimestamp() {
90+
return this.timestamp.toLocalDateTime();
91+
}
92+
93+
/**
94+
* Get the zoned timestamp for when and where this {@code ReportEntry} was created.
95+
*
96+
* <p>Can be used, for example, to order entries.
97+
*
98+
* @return when this entry was created; never {@code null}
99+
*/
100+
public final ZonedDateTime getZonedTimestamp() {
89101
return this.timestamp;
90102
}
91103

Diff for: junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/XmlReportWriter.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
package org.junit.platform.reporting.legacy.xml;
1212

1313
import static java.text.MessageFormat.format;
14-
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
14+
import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
1515
import static java.util.stream.Collectors.toList;
1616
import static org.junit.platform.commons.util.ExceptionUtils.readStackTrace;
1717
import static org.junit.platform.commons.util.StringUtils.isNotBlank;
@@ -23,7 +23,7 @@
2323
import java.net.InetAddress;
2424
import java.net.UnknownHostException;
2525
import java.text.NumberFormat;
26-
import java.time.LocalDateTime;
26+
import java.time.ZonedDateTime;
2727
import java.util.ArrayList;
2828
import java.util.LinkedHashMap;
2929
import java.util.List;
@@ -116,7 +116,7 @@ private void writeSuiteAttributes(TestIdentifier testIdentifier, List<TestIdenti
116116
writeTestCounts(tests, writer);
117117
writeAttributeSafely(writer, "time", getTime(testIdentifier, numberFormat));
118118
writeAttributeSafely(writer, "hostname", getHostname().orElse("<unknown host>"));
119-
writeAttributeSafely(writer, "timestamp", ISO_LOCAL_DATE_TIME.format(getCurrentDateTime()));
119+
writeAttributeSafely(writer, "timestamp", ISO_ZONED_DATE_TIME.format(getCurrentDateTime()));
120120
}
121121

122122
private void writeTestCounts(List<TestIdentifier> tests, XMLStreamWriter writer) throws XMLStreamException {
@@ -236,7 +236,7 @@ private void collectReportEntries(TestIdentifier testIdentifier, List<String> sy
236236
systemOutElementsForCapturedOutput);
237237
removeIfPresentAndAddAsSeparateElement(keyValuePairs, STDERR_REPORT_ENTRY_KEY, systemErrElements);
238238
if (!keyValuePairs.isEmpty()) {
239-
buildReportEntryDescription(reportEntry.getTimestamp(), keyValuePairs, i + 1,
239+
buildReportEntryDescription(reportEntry.getZonedTimestamp(), keyValuePairs, i + 1,
240240
formattedReportEntries);
241241
}
242242
}
@@ -253,10 +253,10 @@ private void removeIfPresentAndAddAsSeparateElement(Map<String, String> keyValue
253253
}
254254
}
255255

256-
private void buildReportEntryDescription(LocalDateTime timestamp, Map<String, String> keyValuePairs,
256+
private void buildReportEntryDescription(ZonedDateTime timestamp, Map<String, String> keyValuePairs,
257257
int entryNumber, StringBuilder result) {
258258
result.append(
259-
format("Report Entry #{0} (timestamp: {1})\n", entryNumber, ISO_LOCAL_DATE_TIME.format(timestamp)));
259+
format("Report Entry #{0} (timestamp: {1})\n", entryNumber, ISO_ZONED_DATE_TIME.format(timestamp)));
260260
keyValuePairs.forEach((key, value) -> result.append(format("\t- {0}: {1}\n", key, value)));
261261
}
262262

@@ -273,8 +273,8 @@ private Optional<String> getHostname() {
273273
}
274274
}
275275

276-
private LocalDateTime getCurrentDateTime() {
277-
return LocalDateTime.now(this.reportData.getClock()).withNano(0);
276+
private ZonedDateTime getCurrentDateTime() {
277+
return ZonedDateTime.now(this.reportData.getClock()).withNano(0);
278278
}
279279

280280
private String formatNonStandardAttributesAsString(TestIdentifier testIdentifier) {

Diff for: platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.time.Clock;
3333
import java.time.Duration;
3434
import java.time.Instant;
35-
import java.time.LocalDateTime;
3635
import java.time.Year;
3736
import java.time.ZoneId;
3837
import java.time.ZonedDateTime;
@@ -317,10 +316,11 @@ void writesHostNameAndTimestamp(@TempDir Path tempDirectory) throws Exception {
317316
engine.addTest("test", () -> {
318317
});
319318

320-
LocalDateTime now = LocalDateTime.parse("2016-01-28T14:02:59.123");
321-
ZoneId zone = ZoneId.systemDefault();
319+
String timestamp = "2016-01-28T14:02:59Z[Europe/London]";
320+
ZonedDateTime now = ZonedDateTime.parse(timestamp);
321+
ZoneId zone = now.getZone();
322322

323-
executeTests(engine, tempDirectory, Clock.fixed(ZonedDateTime.of(now, zone).toInstant(), zone));
323+
executeTests(engine, tempDirectory, Clock.fixed(now.toInstant(), zone));
324324

325325
String content = readValidXmlFile(tempDirectory.resolve("TEST-dummy.xml"));
326326

@@ -329,7 +329,7 @@ void writesHostNameAndTimestamp(@TempDir Path tempDirectory) throws Exception {
329329
.containsSubsequence(
330330
"<testsuite",
331331
"hostname=\"" + InetAddress.getLocalHost().getHostName() + "\"",
332-
"timestamp=\"2016-01-28T14:02:59\"",
332+
"timestamp=\"" + timestamp + "\"",
333333
"<testcase",
334334
"</testsuite>");
335335
// @formatter:on

0 commit comments

Comments
 (0)