Skip to content

Commit 6006deb

Browse files
committed
[Core] Include file_name in attachment message
Fixes: #2072
1 parent 7756506 commit 6006deb

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111
* [Core] Include SourceReferences in message output ([#2064](https://github.com/cucumber/cucumber-jvm/issues/2064) M.P. Korstanje)
1212
* [Core] Enable searching and filtering in html report ([cucumber/#1111](https://github.com/cucumber/cucumber/pull/1111) Vincent Psarga)
13+
* [Core] Include `file_name` in `attachment` message ([cucumber/#2072](https://github.com/cucumber/cucumber/pull/2072) M.P. Korstanje)
1314

1415
### Changed
1516

@@ -18,7 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1819
### Removed
1920

2021
### Fixed
21-
* Use Unicode symbols as a parameter boundary in snippets ([cucumber/#1108](https://github.com/cucumber/cucumber/pull/1108) M.P. Korstnaje)
22+
* [Core] Use Unicode symbols as a parameter boundary in snippets ([cucumber/#1108](https://github.com/cucumber/cucumber/pull/1108) M.P. Korstnaje)
2223

2324
## [6.3.0] (2020-07-24)
2425

core/src/main/java/io/cucumber/core/runner/TestCaseState.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.cucumber.core.backend.Status;
44
import io.cucumber.core.eventbus.EventBus;
55
import io.cucumber.messages.Messages;
6+
import io.cucumber.messages.Messages.Attachment;
67
import io.cucumber.messages.Messages.Attachment.ContentEncoding;
78
import io.cucumber.plugin.event.EmbedEvent;
89
import io.cucumber.plugin.event.Result;
@@ -66,54 +67,61 @@ public boolean isFailed() {
6667

6768
@Override
6869
public void attach(byte[] data, String mediaType, String name) {
70+
requireNonNull(data);
71+
requireNonNull(mediaType);
72+
6973
requireActiveTestStep();
7074
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+
}
7182
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)
8184
.build());
8285
}
8386

8487
@Override
8588
public void attach(String data, String mediaType, String name) {
89+
requireNonNull(data);
90+
requireNonNull(mediaType);
91+
8692
requireActiveTestStep();
8793
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+
}
88101
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)
98103
.build());
99104
}
100105

101106
@Override
102107
public void log(String text) {
103108
requireActiveTestStep();
104109
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");
105114
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)
114116
.build());
115117
}
116118

119+
private Attachment.Builder createAttachment() {
120+
return Attachment.newBuilder()
121+
.setTestCaseStartedId(testExecutionId.toString())
122+
.setTestStepId(currentTestStepId.toString());
123+
}
124+
117125
@Override
118126
public String getName() {
119127
return testCase.getName();

core/src/test/java/io/cucumber/core/runner/TestCaseStateTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.cucumber.core.feature.TestFeatureParser;
55
import io.cucumber.core.gherkin.Feature;
66
import io.cucumber.core.runtime.TimeServiceEventBus;
7+
import io.cucumber.messages.Messages.Attachment.ContentEncoding;
78
import io.cucumber.messages.Messages.Envelope;
89
import io.cucumber.plugin.event.EmbedEvent;
910
import org.junit.jupiter.api.Assertions;
@@ -13,6 +14,7 @@
1314
import java.nio.charset.StandardCharsets;
1415
import java.time.Clock;
1516
import java.util.ArrayList;
17+
import java.util.Base64;
1618
import java.util.Collections;
1719
import java.util.List;
1820
import java.util.UUID;
@@ -100,7 +102,39 @@ void provides_the_uri_and_example_row_line_as_unique_id_for_scenarios_from_scena
100102
}
101103

102104
@Test
103-
void attach_emits_event_on_bus() {
105+
void attach_bytes_emits_event_on_bus() {
106+
Feature feature = TestFeatureParser.parse("" +
107+
"Feature: Test feature\n" +
108+
" Scenario: Test scenario\n" +
109+
" Given I have 4 cukes in my belly\n");
110+
TestCaseState state = createTestCaseState(feature);
111+
112+
List<EmbedEvent> embedEvents = new ArrayList<>();
113+
List<Envelope> envelopes = new ArrayList<>();
114+
bus.registerHandlerFor(EmbedEvent.class, embedEvents::add);
115+
bus.registerHandlerFor(Envelope.class, envelopes::add);
116+
117+
UUID activeTestStep = UUID.randomUUID();
118+
state.setCurrentTestStepId(activeTestStep);
119+
state.attach("Hello World".getBytes(UTF_8), "text/plain", "hello.txt");
120+
121+
EmbedEvent embedEvent = embedEvents.get(0);
122+
assertThat(embedEvent.getData(), is("Hello World".getBytes(UTF_8)));
123+
assertThat(embedEvent.getMediaType(), is("text/plain"));
124+
assertThat(embedEvent.getName(), is("hello.txt"));
125+
126+
Envelope envelope = envelopes.get(0);
127+
assertThat(envelope.getAttachment().getBody(),
128+
is(Base64.getEncoder().encodeToString("Hello World".getBytes(UTF_8))));
129+
assertThat(envelope.getAttachment().getContentEncoding(), is(ContentEncoding.BASE64));
130+
assertThat(envelope.getAttachment().getMediaType(), is("text/plain"));
131+
assertThat(envelope.getAttachment().getFileName(), is("hello.txt"));
132+
assertThat(envelope.getAttachment().getTestStepId(), is(activeTestStep.toString()));
133+
assertThat(envelope.getAttachment().getTestCaseStartedId(), is(state.getTestExecutionId().toString()));
134+
}
135+
136+
@Test
137+
void attach_string_emits_event_on_bus() {
104138
Feature feature = TestFeatureParser.parse("" +
105139
"Feature: Test feature\n" +
106140
" Scenario: Test scenario\n" +
@@ -123,7 +157,9 @@ void attach_emits_event_on_bus() {
123157

124158
Envelope envelope = envelopes.get(0);
125159
assertThat(envelope.getAttachment().getBody(), is("Hello World"));
160+
assertThat(envelope.getAttachment().getContentEncoding(), is(ContentEncoding.IDENTITY));
126161
assertThat(envelope.getAttachment().getMediaType(), is("text/plain"));
162+
assertThat(envelope.getAttachment().getFileName(), is("hello.txt"));
127163
assertThat(envelope.getAttachment().getTestStepId(), is(activeTestStep.toString()));
128164
assertThat(envelope.getAttachment().getTestCaseStartedId(), is(state.getTestExecutionId().toString()));
129165
}

0 commit comments

Comments
 (0)