Skip to content

Commit

Permalink
Add changes from cucumber/common#1879
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
mpkorstanje committed Apr 3, 2022
1 parent 3ff9860 commit 203e83a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 151 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
*.iml
39 changes: 32 additions & 7 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,55 @@
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.13.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>messages</artifactId>
<version>[17.1.1,18.0.0)</version>
<version>[18.0.0,19.0.0)</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand All @@ -65,13 +94,9 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>io.cucumber.htmlformatter.Main</mainClass>
</manifest>
</archive>
<parameters>true</parameters>
</configuration>
</plugin>
</plugins>
Expand Down
24 changes: 0 additions & 24 deletions java/src/main/java/io/cucumber/htmlformatter/Main.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.cucumber.htmlformatter;

import io.cucumber.messages.JSON;
import io.cucumber.messages.types.Envelope;

import java.io.BufferedReader;
Expand All @@ -9,8 +8,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
Expand All @@ -19,17 +20,26 @@
* Writes the message output of a test run as single page html report.
*/
public final class MessagesToHtmlWriter implements AutoCloseable {

private final String template;

private final Writer writer;
private final Serializer serializer;
private boolean preMessageWritten = false;
private boolean postMessageWritten = false;
private boolean firstMessageWritten = false;
private boolean streamClosed = false;

public MessagesToHtmlWriter(Writer writer) throws IOException {
public MessagesToHtmlWriter(OutputStream outputStream, Serializer serializer) throws IOException {
this(
new OutputStreamWriter(
requireNonNull(outputStream),
StandardCharsets.UTF_8),
requireNonNull(serializer)
);
}

private MessagesToHtmlWriter(Writer writer, Serializer serializer) throws IOException {
this.writer = writer;
this.serializer = serializer;
this.template = readResource("index.mustache.html");
}

Expand Down Expand Up @@ -67,7 +77,7 @@ public void write(Envelope envelope) throws IOException {
writer.write(",");
}

JSON.writeValue(writer, envelope);
serializer.writeValue(writer, envelope);
}

/**
Expand All @@ -79,7 +89,7 @@ public void write(Envelope envelope) throws IOException {
*/
@Override
public void close() throws IOException {
if(streamClosed){
if (streamClosed) {
return;
}

Expand All @@ -93,11 +103,15 @@ public void close() throws IOException {
writePostMessage();
postMessageWritten = true;
}
writer.close();
streamClosed = true;
try {
writer.close();
} finally {
streamClosed = true;
}
}

private static void writeTemplateBetween(Writer writer, String template, String begin, String end) throws IOException {
private static void writeTemplateBetween(Writer writer, String template, String begin, String end)
throws IOException {
int beginIndex = begin == null ? 0 : template.indexOf(begin) + begin.length();
int endIndex = end == null ? template.length() : template.indexOf(end);
writer.write(template.substring(beginIndex, endIndex));
Expand All @@ -120,4 +134,12 @@ private static String readResource(String name) throws IOException {
}
return new String(baos.toByteArray(), UTF_8);
}

@FunctionalInterface
public interface Serializer {

void writeValue(Writer writer, Envelope value) throws IOException;

}

}

This file was deleted.

30 changes: 30 additions & 0 deletions java/src/test/java/io/cucumber/htmlformatter/Jackson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.cucumber.htmlformatter;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

final class Jackson {
public static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder()
.addModule(new Jdk8Module())
.addModule(new ParameterNamesModule(Mode.PROPERTIES))
.serializationInclusion(Include.NON_ABSENT)
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
.enable(DeserializationFeature.USE_LONG_FOR_INTS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
.build();

private Jackson() {
}
}

35 changes: 35 additions & 0 deletions java/src/test/java/io/cucumber/htmlformatter/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.cucumber.htmlformatter;

import io.cucumber.htmlformatter.MessagesToHtmlWriter.Serializer;
import io.cucumber.messages.NdjsonToMessageIterable;
import io.cucumber.messages.NdjsonToMessageIterable.Deserializer;
import io.cucumber.messages.types.Envelope;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static io.cucumber.htmlformatter.Jackson.OBJECT_MAPPER;

public final class Main {
private static final Deserializer deserializer = (json) -> OBJECT_MAPPER.readValue(json, Envelope.class);
private static final Serializer serializer = OBJECT_MAPPER::writeValue;

public static void main(String[] args) throws IOException {
InputStream in = System.in;
if (args.length == 1) {
in = new FileInputStream(args[0]);
}
try (NdjsonToMessageIterable envelopes = new NdjsonToMessageIterable(in, deserializer)) {
try (MessagesToHtmlWriter htmlWriter = new MessagesToHtmlWriter(System.out, serializer)) {
for (Envelope envelope : envelopes) {
htmlWriter.write(envelope);
}
}
} catch (Throwable e) {
// Workaround for https://github.com/mojohaus/exec-maven-plugin/issues/141
e.printStackTrace();
System.exit(1);
}
}
}
Loading

0 comments on commit 203e83a

Please sign in to comment.