|
3 | 3 | import io.cucumber.core.backend.Status;
|
4 | 4 | import io.cucumber.core.eventbus.EventBus;
|
5 | 5 | import io.cucumber.messages.Messages;
|
| 6 | +import io.cucumber.messages.Messages.Attachment; |
6 | 7 | import io.cucumber.messages.Messages.Attachment.ContentEncoding;
|
7 | 8 | import io.cucumber.plugin.event.EmbedEvent;
|
8 | 9 | import io.cucumber.plugin.event.Result;
|
@@ -66,54 +67,61 @@ public boolean isFailed() {
|
66 | 67 |
|
67 | 68 | @Override
|
68 | 69 | public void attach(byte[] data, String mediaType, String name) {
|
| 70 | + requireNonNull(data); |
| 71 | + requireNonNull(mediaType); |
| 72 | + |
69 | 73 | requireActiveTestStep();
|
70 | 74 | bus.send(new EmbedEvent(bus.getInstant(), testCase, data, mediaType, name));
|
| 75 | + Attachment.Builder attachment = createAttachment() |
| 76 | + .setBody(Base64.getEncoder().encodeToString(data)) |
| 77 | + .setContentEncoding(ContentEncoding.BASE64) |
| 78 | + .setMediaType(mediaType); |
| 79 | + if (name != null) { |
| 80 | + attachment.setFileName(name); |
| 81 | + } |
71 | 82 | bus.send(Messages.Envelope.newBuilder()
|
72 |
| - .setAttachment( |
73 |
| - Messages.Attachment.newBuilder() |
74 |
| - .setTestCaseStartedId(testExecutionId.toString()) |
75 |
| - .setTestStepId(currentTestStepId.toString()) |
76 |
| - .setBody(Base64.getEncoder().encodeToString(data)) |
77 |
| - .setContentEncoding(ContentEncoding.BASE64) |
78 |
| - // Add file name to message protocol |
79 |
| - // https://github.com/cucumber/cucumber/issues/945 |
80 |
| - .setMediaType(mediaType)) |
| 83 | + .setAttachment(attachment) |
81 | 84 | .build());
|
82 | 85 | }
|
83 | 86 |
|
84 | 87 | @Override
|
85 | 88 | public void attach(String data, String mediaType, String name) {
|
| 89 | + requireNonNull(data); |
| 90 | + requireNonNull(mediaType); |
| 91 | + |
86 | 92 | requireActiveTestStep();
|
87 | 93 | bus.send(new EmbedEvent(bus.getInstant(), testCase, data.getBytes(UTF_8), mediaType, name));
|
| 94 | + Attachment.Builder attachment = createAttachment() |
| 95 | + .setBody(data) |
| 96 | + .setContentEncoding(ContentEncoding.IDENTITY) |
| 97 | + .setMediaType(mediaType); |
| 98 | + if (name != null) { |
| 99 | + attachment.setFileName(name); |
| 100 | + } |
88 | 101 | bus.send(Messages.Envelope.newBuilder()
|
89 |
| - .setAttachment( |
90 |
| - Messages.Attachment.newBuilder() |
91 |
| - .setTestCaseStartedId(testExecutionId.toString()) |
92 |
| - .setTestStepId(currentTestStepId.toString()) |
93 |
| - .setBody(data) |
94 |
| - .setContentEncoding(ContentEncoding.IDENTITY) |
95 |
| - // Add file name to message protocol |
96 |
| - // https://github.com/cucumber/cucumber/issues/945 |
97 |
| - .setMediaType(mediaType)) |
| 102 | + .setAttachment(attachment) |
98 | 103 | .build());
|
99 | 104 | }
|
100 | 105 |
|
101 | 106 | @Override
|
102 | 107 | public void log(String text) {
|
103 | 108 | requireActiveTestStep();
|
104 | 109 | bus.send(new WriteEvent(bus.getInstant(), testCase, text));
|
| 110 | + Attachment.Builder attachment = createAttachment() |
| 111 | + .setBody(text) |
| 112 | + .setContentEncoding(ContentEncoding.IDENTITY) |
| 113 | + .setMediaType("text/x.cucumber.log+plain"); |
105 | 114 | bus.send(Messages.Envelope.newBuilder()
|
106 |
| - .setAttachment( |
107 |
| - Messages.Attachment.newBuilder() |
108 |
| - .setTestCaseStartedId(testExecutionId.toString()) |
109 |
| - .setTestStepId(currentTestStepId.toString()) |
110 |
| - .setBody(text) |
111 |
| - .setContentEncoding(ContentEncoding.IDENTITY) |
112 |
| - .setMediaType("text/x.cucumber.log+plain") |
113 |
| - .build()) |
| 115 | + .setAttachment(attachment) |
114 | 116 | .build());
|
115 | 117 | }
|
116 | 118 |
|
| 119 | + private Attachment.Builder createAttachment() { |
| 120 | + return Attachment.newBuilder() |
| 121 | + .setTestCaseStartedId(testExecutionId.toString()) |
| 122 | + .setTestStepId(currentTestStepId.toString()); |
| 123 | + } |
| 124 | + |
117 | 125 | @Override
|
118 | 126 | public String getName() {
|
119 | 127 | return testCase.getName();
|
|
0 commit comments