Skip to content

Commit a91a764

Browse files
committed
Harmonize with #406
1 parent 20afb81 commit a91a764

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

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

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public final class MessagesToHtmlWriter implements AutoCloseable {
2929
private final String template;
3030
private final Supplier<InputStream> title;
3131
private final Supplier<InputStream> icon;
32-
private final Supplier<InputStream> css;
32+
private final Supplier<InputStream> css = () -> getResource("main.css");
3333
private final Supplier<InputStream> customCss;
34-
private final Supplier<InputStream> script;
34+
private final Supplier<InputStream> script = () -> getResource("main.js");
3535
private final Supplier<InputStream> customScript;
3636

3737
private boolean preMessageWritten = false;
@@ -44,12 +44,10 @@ public MessagesToHtmlWriter(OutputStream outputStream, Serializer serializer) th
4444
this(
4545
createWriter(outputStream),
4646
requireNonNull(serializer),
47-
() -> new ByteArrayInputStream("Cucumber".getBytes(UTF_8)),
47+
() -> createInputStream("Cucumber"),
4848
() -> getResource("icon.url"),
49-
() -> getResource("main.css"),
50-
MessagesToHtmlWriter::getEmptyResource,
51-
() -> getResource("main.js"),
52-
MessagesToHtmlWriter::getEmptyResource
49+
MessagesToHtmlWriter::createEmptyInputStream,
50+
MessagesToHtmlWriter::createEmptyInputStream
5351
);
5452
}
5553

@@ -58,9 +56,7 @@ private MessagesToHtmlWriter(
5856
Serializer serializer,
5957
Supplier<InputStream> title,
6058
Supplier<InputStream> icon,
61-
Supplier<InputStream> css,
6259
Supplier<InputStream> customCss,
63-
Supplier<InputStream> script,
6460
Supplier<InputStream> customScript
6561
) {
6662
this.writer = writer;
@@ -69,15 +65,18 @@ private MessagesToHtmlWriter(
6965
this.template = readTemplate();
7066
this.title = title;
7167
this.icon = icon;
72-
this.css = css;
7368
this.customCss = customCss;
7469
this.customScript = customScript;
75-
this.script = script;
7670
}
7771

7872
private static String readTemplate() {
7973
try {
80-
return readResource("index.mustache.html");
74+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
75+
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos, UTF_8))) {
76+
InputStream resource = getResource("index.mustache.html");
77+
writeResource(writer, resource);
78+
}
79+
return new String(baos.toByteArray(), UTF_8);
8180
} catch (IOException e) {
8281
throw new RuntimeException("Could not read resource index.mustache.html", e);
8382
}
@@ -98,8 +97,12 @@ private static OutputStreamWriter createWriter(OutputStream outputStream) {
9897
public static Builder builder(Serializer serializer) {
9998
return new Builder(serializer);
10099
}
101-
102-
private static ByteArrayInputStream getEmptyResource() {
100+
101+
private static InputStream createInputStream(String text) {
102+
return new ByteArrayInputStream(text.getBytes(UTF_8));
103+
}
104+
105+
private static InputStream createEmptyInputStream() {
103106
return new ByteArrayInputStream(new byte[0]);
104107
}
105108

@@ -128,33 +131,24 @@ private static InputStream getResource(String name) {
128131
return resource;
129132
}
130133

131-
private static String readResource(String name) throws IOException {
132-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
133-
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos, UTF_8))) {
134-
InputStream resource = getResource(name);
135-
writeResource(writer, resource);
136-
}
137-
return new String(baos.toByteArray(), UTF_8);
138-
}
139-
140134
private void writePreMessage() throws IOException {
141135
writeTemplateBetween(writer, template, null, "{{title}}");
142136
writeResource(writer, title);
143137
writeTemplateBetween(writer, template, "{{title}}", "{{icon}}");
144138
writeResource(writer, icon);
145139
writeTemplateBetween(writer, template, "{{icon}}", "{{css}}");
146140
writeResource(writer, css);
147-
writeTemplateBetween(writer, template, "{{css}}", "{{customCss}}");
141+
writeTemplateBetween(writer, template, "{{css}}", "{{custom_css}}");
148142
writeResource(writer, customCss);
149-
writeTemplateBetween(writer, template, "{{customCss}}", "{{messages}}");
143+
writeTemplateBetween(writer, template, "{{custom_css}}", "{{messages}}");
150144
}
151145

152146
private void writePostMessage() throws IOException {
153147
writeTemplateBetween(writer, template, "{{messages}}", "{{script}}");
154148
writeResource(writer, script);
155-
writeTemplateBetween(writer, template, "{{script}}", "{{customScript}}");
149+
writeTemplateBetween(writer, template, "{{script}}", "{{custom_script}}");
156150
writeResource(writer, customScript);
157-
writeTemplateBetween(writer, template, "{{customScript}}", null);
151+
writeTemplateBetween(writer, template, "{{custom_script}}", null);
158152
}
159153

160154
/**
@@ -241,12 +235,10 @@ public interface Serializer {
241235

242236
public static final class Builder {
243237
private final Serializer serializer;
244-
private Supplier<InputStream> title = () -> new ByteArrayInputStream("Cucumber".getBytes(UTF_8));
238+
private Supplier<InputStream> title = () -> createInputStream("Cucumber");
245239
private Supplier<InputStream> icon = () -> getResource("icon.url");
246-
private Supplier<InputStream> css = () -> getResource("main.css");
247-
private Supplier<InputStream> customCss = MessagesToHtmlWriter::getEmptyResource;
248-
private Supplier<InputStream> script = () -> getResource("main.js");
249-
private Supplier<InputStream> customScript = MessagesToHtmlWriter::getEmptyResource;
240+
private Supplier<InputStream> customCss = MessagesToHtmlWriter::createEmptyInputStream;
241+
private Supplier<InputStream> customScript = MessagesToHtmlWriter::createEmptyInputStream;
250242

251243
private Builder(Serializer serializer) {
252244
this.serializer = requireNonNull(serializer);
@@ -260,7 +252,7 @@ private Builder(Serializer serializer) {
260252
*/
261253
public Builder title(String title) {
262254
requireNonNull(title);
263-
this.title = () -> new ByteArrayInputStream(title.getBytes(UTF_8));
255+
this.title = () -> createInputStream(title);
264256
return this;
265257
}
266258

@@ -278,15 +270,16 @@ public Builder icon(Supplier<InputStream> icon) {
278270
}
279271

280272
/**
281-
* Sets default css for the report.
273+
* Sets a custom icon for the report, default value the cucumber logo.
282274
* <p>
283-
* The default script styles the cucumber report.
275+
* The {@code icon} is any valid {@code href} value.
284276
*
285-
* @param css the custom css.
277+
* @param icon the custom icon.
286278
* @return this builder
287279
*/
288-
public Builder css(Supplier<InputStream> css) {
289-
this.css = requireNonNull(css);
280+
public Builder icon(String icon) {
281+
requireNonNull(icon);
282+
this.icon = () -> createInputStream(icon);
290283
return this;
291284
}
292285

@@ -303,19 +296,6 @@ public Builder customCss(Supplier<InputStream> customCss) {
303296
return this;
304297
}
305298

306-
/**
307-
* Replaces default script for the report.
308-
* <p>
309-
* The default script renders the cucumber messages into a report.
310-
*
311-
* @param script the custom script.
312-
* @return this builder
313-
*/
314-
public Builder script(Supplier<InputStream> script) {
315-
this.script = requireNonNull(script);
316-
return this;
317-
}
318-
319299
/**
320300
* Sets custom script for the report.
321301
* <p>
@@ -337,7 +317,7 @@ public Builder customScript(Supplier<InputStream> customScript) {
337317
* @return a new instance of the messages to html writer.
338318
*/
339319
public MessagesToHtmlWriter build(OutputStream out) {
340-
return new MessagesToHtmlWriter(createWriter(out), serializer, title, icon, css, customCss, script, customScript);
320+
return new MessagesToHtmlWriter(createWriter(out), serializer, title, icon, customCss, customScript);
341321
}
342322
}
343323

javascript/src/index.mustache.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{{css}}
1010
</style>
1111
<style>
12-
{{customCss}}
12+
{{custom_css}}
1313
</style>
1414
</head>
1515
<body>
@@ -22,7 +22,7 @@
2222
{{script}}
2323
</script>
2424
<script>
25-
{{customScript}}
25+
{{custom_script}}
2626
</script>
2727
</body>
2828
</html>

0 commit comments

Comments
 (0)