Skip to content

Commit d221d3c

Browse files
committed
Add changes from cucumber/common#1879
Fixes #67
1 parent 3ff9860 commit d221d3c

File tree

10 files changed

+150
-151
lines changed

10 files changed

+150
-151
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
*.iml

java/pom.xml

+32-7
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,55 @@
3434
<type>pom</type>
3535
<scope>import</scope>
3636
</dependency>
37+
38+
<dependency>
39+
<groupId>com.fasterxml.jackson</groupId>
40+
<artifactId>jackson-bom</artifactId>
41+
<version>2.13.1</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
3745
</dependencies>
3846
</dependencyManagement>
3947

4048
<dependencies>
4149
<dependency>
4250
<groupId>io.cucumber</groupId>
4351
<artifactId>messages</artifactId>
44-
<version>[17.1.1,18.0.0)</version>
52+
<version>[18.0.0,19.0.0)</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-databind</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>com.fasterxml.jackson.datatype</groupId>
63+
<artifactId>jackson-datatype-jdk8</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>com.fasterxml.jackson.module</groupId>
69+
<artifactId>jackson-module-parameter-names</artifactId>
70+
<scope>test</scope>
4571
</dependency>
72+
4673
<dependency>
4774
<groupId>org.hamcrest</groupId>
4875
<artifactId>hamcrest</artifactId>
4976
<version>2.2</version>
5077
<scope>test</scope>
5178
</dependency>
79+
5280
<dependency>
5381
<groupId>org.junit.jupiter</groupId>
5482
<artifactId>junit-jupiter-engine</artifactId>
5583
<scope>test</scope>
5684
</dependency>
85+
5786
<dependency>
5887
<groupId>org.junit.jupiter</groupId>
5988
<artifactId>junit-jupiter-params</artifactId>
@@ -65,13 +94,9 @@
6594
<plugins>
6695
<plugin>
6796
<groupId>org.apache.maven.plugins</groupId>
68-
<artifactId>maven-jar-plugin</artifactId>
97+
<artifactId>maven-compiler-plugin</artifactId>
6998
<configuration>
70-
<archive>
71-
<manifest>
72-
<mainClass>io.cucumber.htmlformatter.Main</mainClass>
73-
</manifest>
74-
</archive>
99+
<parameters>true</parameters>
75100
</configuration>
76101
</plugin>
77102
</plugins>

java/src/main/java/io/cucumber/htmlformatter/Main.java

-24
This file was deleted.

java/src/main/java/io/cucumber/htmlformatter/MessagesToHtmlWriter.java

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.cucumber.htmlformatter;
22

3-
import io.cucumber.messages.JSON;
43
import io.cucumber.messages.types.Envelope;
54

65
import java.io.BufferedReader;
@@ -9,8 +8,10 @@
98
import java.io.IOException;
109
import java.io.InputStream;
1110
import java.io.InputStreamReader;
11+
import java.io.OutputStream;
1212
import java.io.OutputStreamWriter;
1313
import java.io.Writer;
14+
import java.nio.charset.StandardCharsets;
1415

1516
import static java.nio.charset.StandardCharsets.UTF_8;
1617
import static java.util.Objects.requireNonNull;
@@ -19,17 +20,26 @@
1920
* Writes the message output of a test run as single page html report.
2021
*/
2122
public final class MessagesToHtmlWriter implements AutoCloseable {
22-
2323
private final String template;
24-
2524
private final Writer writer;
25+
private final Serializer serializer;
2626
private boolean preMessageWritten = false;
2727
private boolean postMessageWritten = false;
2828
private boolean firstMessageWritten = false;
2929
private boolean streamClosed = false;
3030

31-
public MessagesToHtmlWriter(Writer writer) throws IOException {
31+
public MessagesToHtmlWriter(OutputStream outputStream, Serializer serializer) throws IOException {
32+
this(
33+
new OutputStreamWriter(
34+
requireNonNull(outputStream),
35+
StandardCharsets.UTF_8),
36+
requireNonNull(serializer)
37+
);
38+
}
39+
40+
private MessagesToHtmlWriter(Writer writer, Serializer serializer) throws IOException {
3241
this.writer = writer;
42+
this.serializer = serializer;
3343
this.template = readResource("index.mustache.html");
3444
}
3545

@@ -67,7 +77,7 @@ public void write(Envelope envelope) throws IOException {
6777
writer.write(",");
6878
}
6979

70-
JSON.writeValue(writer, envelope);
80+
serializer.writeValue(writer, envelope);
7181
}
7282

7383
/**
@@ -79,7 +89,7 @@ public void write(Envelope envelope) throws IOException {
7989
*/
8090
@Override
8191
public void close() throws IOException {
82-
if(streamClosed){
92+
if (streamClosed) {
8393
return;
8494
}
8595

@@ -93,11 +103,15 @@ public void close() throws IOException {
93103
writePostMessage();
94104
postMessageWritten = true;
95105
}
96-
writer.close();
97-
streamClosed = true;
106+
try {
107+
writer.close();
108+
} finally {
109+
streamClosed = true;
110+
}
98111
}
99112

100-
private static void writeTemplateBetween(Writer writer, String template, String begin, String end) throws IOException {
113+
private static void writeTemplateBetween(Writer writer, String template, String begin, String end)
114+
throws IOException {
101115
int beginIndex = begin == null ? 0 : template.indexOf(begin) + begin.length();
102116
int endIndex = end == null ? template.length() : template.indexOf(end);
103117
writer.write(template.substring(beginIndex, endIndex));
@@ -120,4 +134,12 @@ private static String readResource(String name) throws IOException {
120134
}
121135
return new String(baos.toByteArray(), UTF_8);
122136
}
137+
138+
@FunctionalInterface
139+
public interface Serializer {
140+
141+
void writeValue(Writer writer, Envelope value) throws IOException;
142+
143+
}
144+
123145
}

java/src/test/java/io/cucumber/htmlformatter/HtmlFormatterAcceptanceTest.java

-60
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.cucumber.htmlformatter;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import com.fasterxml.jackson.core.JsonGenerator;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.SerializationFeature;
9+
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
10+
import com.fasterxml.jackson.databind.json.JsonMapper;
11+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
12+
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
13+
14+
final class Jackson {
15+
public static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder()
16+
.addModule(new Jdk8Module())
17+
.addModule(new ParameterNamesModule(Mode.PROPERTIES))
18+
.serializationInclusion(Include.NON_ABSENT)
19+
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
20+
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
21+
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
22+
.enable(DeserializationFeature.USE_LONG_FOR_INTS)
23+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
24+
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
25+
.build();
26+
27+
private Jackson() {
28+
}
29+
}
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.cucumber.htmlformatter;
2+
3+
import io.cucumber.htmlformatter.MessagesToHtmlWriter.Serializer;
4+
import io.cucumber.messages.NdjsonToMessageIterable;
5+
import io.cucumber.messages.NdjsonToMessageIterable.Deserializer;
6+
import io.cucumber.messages.types.Envelope;
7+
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
12+
import static io.cucumber.htmlformatter.Jackson.OBJECT_MAPPER;
13+
14+
public final class Main {
15+
private static final Deserializer deserializer = (json) -> OBJECT_MAPPER.readValue(json, Envelope.class);
16+
private static final Serializer serializer = OBJECT_MAPPER::writeValue;
17+
18+
public static void main(String[] args) throws IOException {
19+
InputStream in = System.in;
20+
if (args.length == 1) {
21+
in = new FileInputStream(args[0]);
22+
}
23+
try (NdjsonToMessageIterable envelopes = new NdjsonToMessageIterable(in, deserializer)) {
24+
try (MessagesToHtmlWriter htmlWriter = new MessagesToHtmlWriter(System.out, serializer)) {
25+
for (Envelope envelope : envelopes) {
26+
htmlWriter.write(envelope);
27+
}
28+
}
29+
} catch (Throwable e) {
30+
// Workaround for https://github.com/mojohaus/exec-maven-plugin/issues/141
31+
e.printStackTrace();
32+
System.exit(1);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)