Skip to content

Commit 1c079c7

Browse files
committed
Use Java 17
1 parent 5bc21c1 commit 1c079c7

File tree

9 files changed

+50
-51
lines changed

9 files changed

+50
-51
lines changed

java/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@
8585
<scope>test</scope>
8686
</dependency>
8787

88-
<dependency>
89-
<groupId>org.hamcrest</groupId>
90-
<artifactId>hamcrest</artifactId>
91-
<version>3.0</version>
92-
<scope>test</scope>
93-
</dependency>
94-
9588
<dependency>
9689
<groupId>org.assertj</groupId>
9790
<artifactId>assertj-core</artifactId>

java/src/main/java/io/cucumber/testngxmlformatter/EscapingXmlStreamWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void close() throws XMLStreamException {
2020
writer.close();
2121
}
2222

23+
@SuppressWarnings("SameParameterValue")
2324
void writeStartDocument(String encoding, String version) throws XMLStreamException {
2425
writer.writeStartDocument(encoding, version);
2526
}
@@ -56,7 +57,7 @@ void writeAttribute(String localName, String value) throws XMLStreamException {
5657

5758
void writeCData(String data) throws XMLStreamException {
5859
// https://stackoverflow.com/questions/223652/is-there-a-way-to-escape-a-cdata-end-token-in-xml
59-
for (String part : CDATA_TERMINATOR_SPLIT.split(data)) {
60+
for (String part : CDATA_TERMINATOR_SPLIT.split(data, -1)) {
6061
// see https://www.w3.org/TR/xml/#dt-cdsection
6162
writer.writeCData(escapeIllegalChars(part));
6263
}
@@ -88,6 +89,7 @@ private static String escapeIllegalChars(String value) {
8889
return escaped.toString();
8990
}
9091

92+
@SuppressWarnings("UnnecessaryParentheses")
9193
private static boolean isLegal(int codePoint) {
9294
// see https://www.w3.org/TR/xml/#charsets
9395
return codePoint == 0x9

java/src/main/java/io/cucumber/testngxmlformatter/XmlReportData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
class XmlReportData {
4242

4343
private static final io.cucumber.messages.types.Duration ZERO_DURATION =
44-
new io.cucumber.messages.types.Duration(0L, 0L);
44+
new io.cucumber.messages.types.Duration(0L, 0);
4545
// By definition, but see https://github.com/cucumber/gherkin/issues/11
4646
private static final TestStepResult SCENARIO_WITH_NO_STEPS = new TestStepResult(ZERO_DURATION, null, PASSED, null);
4747
private final Repository repository = Repository.builder()

java/src/main/java/io/cucumber/testngxmlformatter/XmlReportWriter.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ private void writeTestngResultsAttributes(EscapingXmlStreamWriter writer) throws
5050
Map<TestStepResultStatus, Long> counts = data.getTestCaseStatusCounts();
5151

5252
writer.writeAttribute("failed", String.valueOf(countFailures(counts)));
53-
writer.writeAttribute("passed", counts.get(PASSED).toString());
54-
writer.writeAttribute("skipped", counts.get(SKIPPED).toString());
53+
writer.writeAttribute("passed", String.valueOf(counts.get(PASSED)));
54+
writer.writeAttribute("skipped", String.valueOf(counts.get(SKIPPED)));
5555
writer.writeAttribute("total", String.valueOf(data.getTestCaseCount()));
5656
}
5757

@@ -142,14 +142,11 @@ private void writeTestMethodAttributes(EscapingXmlStreamWriter writer, TestCaseS
142142
}
143143

144144
private String writeStatus(TestStepResult status) {
145-
switch (status.getStatus()) {
146-
case PASSED:
147-
return "PASS";
148-
case SKIPPED:
149-
return "SKIP";
150-
default:
151-
return "FAIL";
152-
}
145+
return switch (status.getStatus()) {
146+
case PASSED -> "PASS";
147+
case SKIPPED -> "SKIP";
148+
default -> "FAIL";
149+
};
153150
}
154151

155152
private void writeException(EscapingXmlStreamWriter writer, TestCaseStarted testCaseStarted, TestStepResult result) throws XMLStreamException {
@@ -216,9 +213,7 @@ private String createStepResultList(List<Entry<String, String>> results) {
216213
String status = r.getValue();
217214
sb.append(stepText);
218215
sb.append(".");
219-
for (int i = 75 - stepText.length(); i > 0; i--) {
220-
sb.append(".");
221-
}
216+
sb.append(".".repeat(Math.max(0, 75 - stepText.length())));
222217
sb.append(status);
223218
sb.append("\n");
224219
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package io.cucumber.testngxmlformatter;
3+
4+
import org.jspecify.annotations.NullMarked;

java/src/test/java/io/cucumber/testngxmlformatter/EscapingXmlStreamWriterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void shouldEscapeCDataElement() throws XMLStreamException {
6464
}
6565

6666
private static String asString(ByteArrayOutputStream out) {
67-
String s = new String(out.toByteArray(), UTF_8);
67+
String s = out.toString(UTF_8);
6868
return removeXmlHeader(s);
6969
}
7070

java/src/test/java/io/cucumber/testngxmlformatter/Jackson.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.cucumber.testngxmlformatter;
22

33
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
4-
import com.fasterxml.jackson.annotation.JsonInclude.Include;
54
import com.fasterxml.jackson.core.JsonGenerator;
65
import com.fasterxml.jackson.databind.DeserializationFeature;
76
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -11,6 +10,9 @@
1110
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
1211
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
1312

13+
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_ABSENT;
14+
import static com.fasterxml.jackson.annotation.JsonInclude.Value.construct;
15+
1416
final class Jackson {
1517
public static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder()
1618
.addModule(new Jdk8Module())

java/src/test/java/io/cucumber/testngxmlformatter/MessagesToTestngXmlWriterAcceptanceTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.util.stream.Stream;
2323

2424
import static io.cucumber.testngxmlformatter.Jackson.OBJECT_MAPPER;
25+
import static java.util.Objects.requireNonNull;
2526
import static org.xmlunit.assertj.XmlAssert.assertThat;
2627

2728
class MessagesToTestngXmlWriterAcceptanceTest {
28-
private static final NdjsonToMessageIterable.Deserializer deserializer = (json) -> OBJECT_MAPPER.readValue(json, Envelope.class);
29+
private static final NdjsonToMessageIterable.Deserializer deserializer = json -> OBJECT_MAPPER.readValue(json, Envelope.class);
2930

3031
static List<TestCase> acceptance() throws IOException {
3132
try (Stream<Path> paths = Files.list(Paths.get("../testdata/src"))) {
@@ -68,17 +69,16 @@ private static <T extends OutputStream> T writeTestngXmlReport(TestCase testCase
6869
return out;
6970
}
7071

71-
static class TestCase {
72+
private static final class TestCase {
7273
private final Path source;
7374
private final Path expected;
74-
7575
private final String name;
7676

7777
TestCase(Path source) {
7878
this.source = source;
7979
String fileName = source.getFileName().toString();
8080
this.name = fileName.substring(0, fileName.lastIndexOf(".ndjson"));
81-
this.expected = source.getParent().resolve(name + ".xml");
81+
this.expected = requireNonNull(source.getParent()).resolve(name + ".xml");
8282
}
8383

8484
@Override
@@ -88,15 +88,13 @@ public String toString() {
8888

8989
@Override
9090
public boolean equals(Object o) {
91-
if (this == o) return true;
92-
if (o == null || getClass() != o.getClass()) return false;
93-
TestCase testCase = (TestCase) o;
94-
return source.equals(testCase.source);
91+
if (!(o instanceof TestCase testCase)) return false;
92+
return Objects.equals(source, testCase.source) && Objects.equals(expected, testCase.expected) && Objects.equals(name, testCase.name);
9593
}
9694

9795
@Override
9896
public int hashCode() {
99-
return Objects.hash(source);
97+
return Objects.hash(source, expected, name);
10098
}
10199
}
102100

java/src/test/java/io/cucumber/testngxmlformatter/MessagesToTestngXmlWriterTest.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.cucumber.messages.types.Envelope;
44
import io.cucumber.messages.types.TestRunFinished;
55
import io.cucumber.messages.types.TestRunStarted;
6+
import io.cucumber.messages.types.Timestamp;
67
import org.junit.jupiter.api.Test;
78

89
import java.io.ByteArrayOutputStream;
@@ -26,28 +27,30 @@ void it_writes_two_messages_to_xml() throws IOException {
2627
Envelope.of(new TestRunStarted(toMessage(started), null)),
2728
Envelope.of(new TestRunFinished(null, true, toMessage(finished), null, null)));
2829

29-
assertThat(html).isEqualTo("" +
30-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
31-
"<testng-results failed=\"0\" passed=\"0\" skipped=\"0\" total=\"0\">\n" +
32-
"<suite name=\"Cucumber\" duration-ms=\"20000\">\n" +
33-
"<test name=\"Cucumber\" duration-ms=\"20000\">\n" +
34-
"</test>\n" +
35-
"</suite>\n" +
36-
"</testng-results>\n"
30+
assertThat(html).isEqualTo("""
31+
<?xml version="1.0" encoding="UTF-8"?>
32+
<testng-results failed="0" passed="0" skipped="0" total="0">
33+
<suite name="Cucumber" duration-ms="20000">
34+
<test name="Cucumber" duration-ms="20000">
35+
</test>
36+
</suite>
37+
</testng-results>
38+
"""
3739
);
3840
}
3941

4042
@Test
4143
void it_writes_no_message_to_xml() throws IOException {
4244
String html = renderAsJunitXml();
43-
assertThat(html).isEqualTo("" +
44-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
45-
"<testng-results failed=\"0\" passed=\"0\" skipped=\"0\" total=\"0\">\n" +
46-
"<suite name=\"Cucumber\" duration-ms=\"0\">\n" +
47-
"<test name=\"Cucumber\" duration-ms=\"0\">\n" +
48-
"</test>\n" +
49-
"</suite>\n" +
50-
"</testng-results>\n"
45+
assertThat(html).isEqualTo("""
46+
<?xml version="1.0" encoding="UTF-8"?>
47+
<testng-results failed="0" passed="0" skipped="0" total="0">
48+
<suite name="Cucumber" duration-ms="0">
49+
<test name="Cucumber" duration-ms="0">
50+
</test>
51+
</suite>
52+
</testng-results>
53+
"""
5154
);
5255
}
5356

@@ -56,7 +59,9 @@ void it_throws_when_writing_after_close() throws IOException {
5659
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
5760
MessagesToTestngXmlWriter messagesToHtmlWriter = new MessagesToTestngXmlWriter(bytes);
5861
messagesToHtmlWriter.close();
59-
assertThrows(IOException.class, () -> messagesToHtmlWriter.write(null));
62+
assertThrows(IOException.class, () -> messagesToHtmlWriter.write(
63+
Envelope.of(new TestRunStarted(new Timestamp(0L, 0), ""))
64+
));
6065
}
6166

6267
@Test
@@ -92,6 +97,6 @@ private static String renderAsJunitXml(Envelope... messages) throws IOException
9297
}
9398
}
9499

95-
return new String(bytes.toByteArray(), UTF_8);
100+
return bytes.toString(UTF_8);
96101
}
97102
}

0 commit comments

Comments
 (0)