Skip to content

Commit 7f62e96

Browse files
committed
Enable NullAway in junit-platform-reporting
1 parent 55beb75 commit 7f62e96

File tree

7 files changed

+36
-11
lines changed

7 files changed

+36
-11
lines changed

junit-platform-reporting/junit-platform-reporting.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import junitbuild.extensions.javaModuleName
22

33
plugins {
44
id("junitbuild.java-library-conventions")
5+
id("junitbuild.java-nullability-conventions")
56
id("junitbuild.shadow-conventions")
67
`java-test-fixtures`
78
}
@@ -13,6 +14,8 @@ dependencies {
1314
api(projects.junitPlatformLauncher)
1415

1516
compileOnlyApi(libs.apiguardian)
17+
compileOnly(libs.jspecify)
18+
1619
compileOnlyApi(libs.openTestReporting.tooling.spi)
1720
compileOnly(libs.immutables.valueAnnotations) {
1821
because("package-info references enum")

junit-platform-reporting/src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
* https://www.eclipse.org/legal/epl-v20.html
99
*/
1010

11+
import org.jspecify.annotations.NullMarked;
12+
1113
/**
1214
* Defines the JUnit Platform Reporting API.
1315
*
1416
* @since 1.4
1517
*/
18+
@NullMarked
1619
module org.junit.platform.reporting {
1720
requires java.xml;
1821
requires static transitive org.apiguardian.api;

junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/LegacyReportingUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.apiguardian.api.API.Status.MAINTAINED;
1414

1515
import org.apiguardian.api.API;
16+
import org.jspecify.annotations.Nullable;
1617
import org.junit.platform.commons.util.Preconditions;
1718
import org.junit.platform.engine.support.descriptor.ClassSource;
1819
import org.junit.platform.launcher.TestIdentifier;
@@ -62,10 +63,12 @@ public static String getClassName(TestPlan testPlan, TestIdentifier testIdentifi
6263
return getParentLegacyReportingName(testPlan, testIdentifier);
6364
}
6465

66+
@Nullable
6567
private static TestIdentifier getParent(TestPlan testPlan, TestIdentifier testIdentifier) {
6668
return testPlan.getParent(testIdentifier).orElse(null);
6769
}
6870

71+
@Nullable
6972
private static ClassSource getClassSource(TestIdentifier current) {
7073
// @formatter:off
7174
return current.getSource()

junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListener.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.reporting.legacy.xml;
1212

13+
import static java.util.Objects.requireNonNull;
1314
import static org.apiguardian.api.API.Status.STABLE;
1415

1516
import java.io.IOException;
@@ -22,6 +23,7 @@
2223
import javax.xml.stream.XMLStreamException;
2324

2425
import org.apiguardian.api.API;
26+
import org.jspecify.annotations.Nullable;
2527
import org.junit.platform.engine.TestExecutionResult;
2628
import org.junit.platform.engine.reporting.ReportEntry;
2729
import org.junit.platform.launcher.TestExecutionListener;
@@ -48,6 +50,7 @@ public class LegacyXmlReportGeneratingListener implements TestExecutionListener
4850
private final PrintWriter out;
4951
private final Clock clock;
5052

53+
@Nullable
5154
private XmlReportData reportData;
5255

5356
public LegacyXmlReportGeneratingListener(Path reportsDir, PrintWriter out) {
@@ -83,23 +86,23 @@ public void testPlanExecutionFinished(TestPlan testPlan) {
8386

8487
@Override
8588
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
86-
this.reportData.markSkipped(testIdentifier, reason);
89+
requiredReportData().markSkipped(testIdentifier, reason);
8790
writeXmlReportInCaseOfRoot(testIdentifier);
8891
}
8992

9093
@Override
9194
public void executionStarted(TestIdentifier testIdentifier) {
92-
this.reportData.markStarted(testIdentifier);
95+
requiredReportData().markStarted(testIdentifier);
9396
}
9497

9598
@Override
9699
public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry entry) {
97-
this.reportData.addReportEntry(testIdentifier, entry);
100+
requiredReportData().addReportEntry(testIdentifier, entry);
98101
}
99102

100103
@Override
101104
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult result) {
102-
this.reportData.markFinished(testIdentifier, result);
105+
requiredReportData().markFinished(testIdentifier, result);
103106
writeXmlReportInCaseOfRoot(testIdentifier);
104107
}
105108

@@ -113,13 +116,17 @@ private void writeXmlReportInCaseOfRoot(TestIdentifier testIdentifier) {
113116
private void writeXmlReportSafely(TestIdentifier testIdentifier, String rootName) {
114117
Path xmlFile = this.reportsDir.resolve("TEST-" + rootName + ".xml");
115118
try (Writer fileWriter = Files.newBufferedWriter(xmlFile)) {
116-
new XmlReportWriter(this.reportData).writeXmlReport(testIdentifier, fileWriter);
119+
new XmlReportWriter(requiredReportData()).writeXmlReport(testIdentifier, fileWriter);
117120
}
118121
catch (XMLStreamException | IOException e) {
119122
printException("Could not write XML report: " + xmlFile, e);
120123
}
121124
}
122125

126+
private XmlReportData requiredReportData() {
127+
return requireNonNull(this.reportData);
128+
}
129+
123130
private boolean isRoot(TestIdentifier testIdentifier) {
124131
return testIdentifier.getParentIdObject().isEmpty();
125132
}

junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/XmlReportData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.function.Predicate;
2727

28+
import org.jspecify.annotations.Nullable;
2829
import org.junit.platform.commons.util.ExceptionUtils;
2930
import org.junit.platform.engine.TestExecutionResult;
3031
import org.junit.platform.engine.reporting.ReportEntry;
@@ -60,7 +61,7 @@ Clock getClock() {
6061
return this.clock;
6162
}
6263

63-
void markSkipped(TestIdentifier testIdentifier, String reason) {
64+
void markSkipped(TestIdentifier testIdentifier, @Nullable String reason) {
6465
this.skippedTests.put(testIdentifier, reason == null ? "" : reason);
6566
}
6667

@@ -94,6 +95,7 @@ boolean wasSkipped(TestIdentifier testIdentifier) {
9495
return Duration.between(startInstant, endInstant).toMillis() / (double) MILLIS_PER_SECOND;
9596
}
9697

98+
@Nullable
9799
String getSkipReason(TestIdentifier testIdentifier) {
98100
return findSkippedAncestor(testIdentifier).map(skippedTestIdentifier -> {
99101
String reason = this.skippedTests.get(skippedTestIdentifier);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static java.util.Collections.emptyList;
1616
import static java.util.Collections.unmodifiableMap;
1717
import static java.util.Comparator.naturalOrder;
18+
import static java.util.Objects.requireNonNull;
1819
import static java.util.function.Function.identity;
1920
import static java.util.stream.Collectors.counting;
2021
import static java.util.stream.Collectors.groupingBy;
@@ -55,6 +56,7 @@
5556
import javax.xml.stream.XMLStreamException;
5657
import javax.xml.stream.XMLStreamWriter;
5758

59+
import org.jspecify.annotations.Nullable;
5860
import org.junit.platform.engine.TestExecutionResult;
5961
import org.junit.platform.engine.reporting.ReportEntry;
6062
import org.junit.platform.launcher.TestIdentifier;
@@ -244,10 +246,10 @@ private void writeSkippedOrErrorOrFailureElement(TestIdentifier testIdentifier,
244246
}
245247
}
246248

247-
private void writeSkippedElement(String reason, XMLStreamWriter writer) throws XMLStreamException {
249+
private void writeSkippedElement(@Nullable String reason, XMLStreamWriter writer) throws XMLStreamException {
248250
if (isNotBlank(reason)) {
249251
writer.writeStartElement("skipped");
250-
writeCDataSafely(reason);
252+
writeCDataSafely(requireNonNull(reason));
251253
writer.writeEndElement();
252254
}
253255
else {
@@ -256,7 +258,7 @@ private void writeSkippedElement(String reason, XMLStreamWriter writer) throws X
256258
newLine();
257259
}
258260

259-
private void writeErrorOrFailureElement(Type type, Throwable throwable, XMLStreamWriter writer)
261+
private void writeErrorOrFailureElement(Type type, @Nullable Throwable throwable, XMLStreamWriter writer)
260262
throws XMLStreamException {
261263

262264
String elementName = type == FAILURE ? "failure" : "error";

junit-platform-reporting/src/main/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.reporting.open.xml;
1212

13+
import static java.util.Objects.requireNonNull;
1314
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
1415
import static org.junit.platform.commons.util.StringUtils.isNotBlank;
1516
import static org.junit.platform.launcher.LauncherConstants.STDERR_REPORT_ENTRY_KEY;
@@ -71,6 +72,7 @@
7172
import java.util.function.BiConsumer;
7273

7374
import org.apiguardian.api.API;
75+
import org.jspecify.annotations.Nullable;
7476
import org.junit.platform.commons.JUnitException;
7577
import org.junit.platform.commons.util.ExceptionUtils;
7678
import org.junit.platform.commons.util.StringUtils;
@@ -114,6 +116,8 @@ public class OpenTestReportGeneratingListener implements TestExecutionListener {
114116
private final Map<UniqueId, String> inProgressIds = new ConcurrentHashMap<>();
115117
private DocumentWriter<Events> eventsFileWriter = DocumentWriter.noop();
116118
private final Path workingDir;
119+
120+
@Nullable
117121
private Path outputDir;
118122

119123
@SuppressWarnings("unused") // Used via ServiceLoader
@@ -361,7 +365,8 @@ public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry e
361365
}));
362366
}
363367

364-
private static void attachOutput(Attachments attachments, LocalDateTime timestamp, String content, String source) {
368+
private static void attachOutput(Attachments attachments, LocalDateTime timestamp, @Nullable String content,
369+
String source) {
365370
if (content != null) {
366371
attachments.append(output(timestamp), output -> output.withSource(source).withContent(content));
367372
}
@@ -373,7 +378,7 @@ public void fileEntryPublished(TestIdentifier testIdentifier, FileEntry entry) {
373378
eventsFileWriter.append(reported(id, Instant.now()), //
374379
reported -> reported.append(attachments(), attachments -> attachments.append(file(entry.getTimestamp()), //
375380
file -> {
376-
file.withPath(outputDir.relativize(entry.getPath()).toString());
381+
file.withPath(requireNonNull(outputDir).relativize(entry.getPath()).toString());
377382
entry.getMediaType().ifPresent(file::withMediaType);
378383
})));
379384
}

0 commit comments

Comments
 (0)