@@ -29,9 +29,9 @@ public final class MessagesToHtmlWriter implements AutoCloseable {
29
29
private final String template ;
30
30
private final Supplier <InputStream > title ;
31
31
private final Supplier <InputStream > icon ;
32
- private final Supplier <InputStream > css ;
32
+ private final Supplier <InputStream > css = () -> getResource ( "main.css" ) ;
33
33
private final Supplier <InputStream > customCss ;
34
- private final Supplier <InputStream > script ;
34
+ private final Supplier <InputStream > script = () -> getResource ( "main.js" ) ;
35
35
private final Supplier <InputStream > customScript ;
36
36
37
37
private boolean preMessageWritten = false ;
@@ -44,12 +44,10 @@ public MessagesToHtmlWriter(OutputStream outputStream, Serializer serializer) th
44
44
this (
45
45
createWriter (outputStream ),
46
46
requireNonNull (serializer ),
47
- () -> new ByteArrayInputStream ("Cucumber" . getBytes ( UTF_8 ) ),
47
+ () -> createInputStream ("Cucumber" ),
48
48
() -> getResource ("icon.url" ),
49
- () -> getResource ("main.css" ),
50
- MessagesToHtmlWriter ::getEmptyResource ,
51
- () -> getResource ("main.js" ),
52
- MessagesToHtmlWriter ::getEmptyResource
49
+ MessagesToHtmlWriter ::createEmptyInputStream ,
50
+ MessagesToHtmlWriter ::createEmptyInputStream
53
51
);
54
52
}
55
53
@@ -58,9 +56,7 @@ private MessagesToHtmlWriter(
58
56
Serializer serializer ,
59
57
Supplier <InputStream > title ,
60
58
Supplier <InputStream > icon ,
61
- Supplier <InputStream > css ,
62
59
Supplier <InputStream > customCss ,
63
- Supplier <InputStream > script ,
64
60
Supplier <InputStream > customScript
65
61
) {
66
62
this .writer = writer ;
@@ -69,15 +65,18 @@ private MessagesToHtmlWriter(
69
65
this .template = readTemplate ();
70
66
this .title = title ;
71
67
this .icon = icon ;
72
- this .css = css ;
73
68
this .customCss = customCss ;
74
69
this .customScript = customScript ;
75
- this .script = script ;
76
70
}
77
71
78
72
private static String readTemplate () {
79
73
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 );
81
80
} catch (IOException e ) {
82
81
throw new RuntimeException ("Could not read resource index.mustache.html" , e );
83
82
}
@@ -98,8 +97,12 @@ private static OutputStreamWriter createWriter(OutputStream outputStream) {
98
97
public static Builder builder (Serializer serializer ) {
99
98
return new Builder (serializer );
100
99
}
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 () {
103
106
return new ByteArrayInputStream (new byte [0 ]);
104
107
}
105
108
@@ -128,33 +131,24 @@ private static InputStream getResource(String name) {
128
131
return resource ;
129
132
}
130
133
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
-
140
134
private void writePreMessage () throws IOException {
141
135
writeTemplateBetween (writer , template , null , "{{title}}" );
142
136
writeResource (writer , title );
143
137
writeTemplateBetween (writer , template , "{{title}}" , "{{icon}}" );
144
138
writeResource (writer , icon );
145
139
writeTemplateBetween (writer , template , "{{icon}}" , "{{css}}" );
146
140
writeResource (writer , css );
147
- writeTemplateBetween (writer , template , "{{css}}" , "{{customCss }}" );
141
+ writeTemplateBetween (writer , template , "{{css}}" , "{{custom_css }}" );
148
142
writeResource (writer , customCss );
149
- writeTemplateBetween (writer , template , "{{customCss }}" , "{{messages}}" );
143
+ writeTemplateBetween (writer , template , "{{custom_css }}" , "{{messages}}" );
150
144
}
151
145
152
146
private void writePostMessage () throws IOException {
153
147
writeTemplateBetween (writer , template , "{{messages}}" , "{{script}}" );
154
148
writeResource (writer , script );
155
- writeTemplateBetween (writer , template , "{{script}}" , "{{customScript }}" );
149
+ writeTemplateBetween (writer , template , "{{script}}" , "{{custom_script }}" );
156
150
writeResource (writer , customScript );
157
- writeTemplateBetween (writer , template , "{{customScript }}" , null );
151
+ writeTemplateBetween (writer , template , "{{custom_script }}" , null );
158
152
}
159
153
160
154
/**
@@ -241,12 +235,10 @@ public interface Serializer {
241
235
242
236
public static final class Builder {
243
237
private final Serializer serializer ;
244
- private Supplier <InputStream > title = () -> new ByteArrayInputStream ("Cucumber" . getBytes ( UTF_8 ) );
238
+ private Supplier <InputStream > title = () -> createInputStream ("Cucumber" );
245
239
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 ;
250
242
251
243
private Builder (Serializer serializer ) {
252
244
this .serializer = requireNonNull (serializer );
@@ -260,7 +252,7 @@ private Builder(Serializer serializer) {
260
252
*/
261
253
public Builder title (String title ) {
262
254
requireNonNull (title );
263
- this .title = () -> new ByteArrayInputStream (title . getBytes ( UTF_8 ) );
255
+ this .title = () -> createInputStream (title );
264
256
return this ;
265
257
}
266
258
@@ -278,15 +270,16 @@ public Builder icon(Supplier<InputStream> icon) {
278
270
}
279
271
280
272
/**
281
- * Sets default css for the report.
273
+ * Sets a custom icon for the report, default value the cucumber logo .
282
274
* <p>
283
- * The default script styles the cucumber report .
275
+ * The {@code icon} is any valid {@code href} value .
284
276
*
285
- * @param css the custom css .
277
+ * @param icon the custom icon .
286
278
* @return this builder
287
279
*/
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 );
290
283
return this ;
291
284
}
292
285
@@ -303,19 +296,6 @@ public Builder customCss(Supplier<InputStream> customCss) {
303
296
return this ;
304
297
}
305
298
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
-
319
299
/**
320
300
* Sets custom script for the report.
321
301
* <p>
@@ -337,7 +317,7 @@ public Builder customScript(Supplier<InputStream> customScript) {
337
317
* @return a new instance of the messages to html writer.
338
318
*/
339
319
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 );
341
321
}
342
322
}
343
323
0 commit comments