Skip to content

Add changes from cucumber/common#1879 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-javascript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
cache: "npm"
cache-dependency-path: javascript/package-lock.json

- run: npm install-test
- run: npm install-ci-test
working-directory: javascript

- run: npm run build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
*.iml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ javascript/dist/src/index.mustache.html: javascript/dist/main.js
javascript/dist/main.css: javascript/dist/main.js

javascript/dist/main.js: javascript/package.json $(javascript_source)
cd javascript && npm install-test && npm run build
cd javascript && npm install-ci-test && npm run build
41 changes: 33 additions & 8 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>html-formatter</artifactId>
<version>19.0.0</version>
<version>19.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cucumber HTML Formatter</name>
<description>Renders Cucumber Messages as HTML</description>
Expand All @@ -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