Skip to content

Commit 175620d

Browse files
authored
[Core] Include hook type in cucumber message (#2972)
Utilizes cucumber/messages#102
1 parent 5c990e6 commit 175620d

File tree

9 files changed

+134
-49
lines changed

9 files changed

+134
-49
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
### Changed
1919
- [Archetype] Replace JUnit Jupiter with AssertJ ([#2969](https://github.com/cucumber/cucumber-jvm/pull/2969) M.P. Korstanje)
2020
- [JUnit Platform Engine] Use JUnit Platform 1.11.3 (JUnit Jupiter 5.11.3)
21+
- [Core] Update dependency io.cucumber:gherkin to v31.0.0
22+
- [Core] Update dependency io.cucumber:messages to v27.1.0
2123

2224
### Added
2325
- [Core] Pretty-Print DocStringArgument Step Arguments([#2953](https://github.com/cucumber/cucumber-jvm/pull/2953) Daniel Miladinov)
26+
- [Core] Include hook type in cucumber message ([#2972](https://github.com/cucumber/cucumber-jvm/pull/2972) M.P. Korstanje)
2427

2528
## [7.20.1] - 2024-10-09
2629
### Fixed

Diff for: cucumber-core/src/main/java/io/cucumber/core/backend/HookDefinition.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.apiguardian.api.API;
44

5+
import java.util.Optional;
6+
57
@API(status = API.Status.STABLE)
68
public interface HookDefinition extends Located {
79

@@ -11,4 +13,18 @@ public interface HookDefinition extends Located {
1113

1214
int getOrder();
1315

16+
default Optional<HookType> getHookType() {
17+
return Optional.empty();
18+
}
19+
20+
enum HookType {
21+
22+
BEFORE,
23+
24+
AFTER,
25+
26+
BEFORE_STEP,
27+
28+
AFTER_STEP;
29+
}
1430
}

Diff for: cucumber-core/src/main/java/io/cucumber/core/runner/CachingGlue.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.cucumber.datatable.TableEntryByTypeTransformer;
2929
import io.cucumber.messages.types.Envelope;
3030
import io.cucumber.messages.types.Hook;
31+
import io.cucumber.messages.types.HookType;
3132
import io.cucumber.messages.types.JavaMethod;
3233
import io.cucumber.messages.types.JavaStackTraceElement;
3334
import io.cucumber.messages.types.Location;
@@ -306,7 +307,23 @@ private void emitHook(CoreHookDefinition coreHook) {
306307
coreHook.getDefinitionLocation()
307308
.map(this::createSourceReference)
308309
.orElseGet(this::emptySourceReference),
309-
coreHook.getTagExpression(), null);
310+
coreHook.getTagExpression(),
311+
coreHook.getHookType()
312+
.map(hookType -> {
313+
switch (hookType) {
314+
case BEFORE:
315+
return HookType.BEFORE_TEST_CASE;
316+
case AFTER:
317+
return HookType.AFTER_TEST_CASE;
318+
case BEFORE_STEP:
319+
return HookType.BEFORE_TEST_STEP;
320+
case AFTER_STEP:
321+
return HookType.AFTER_TEST_STEP;
322+
default:
323+
return null;
324+
}
325+
})
326+
.orElse(null));
310327
bus.send(Envelope.of(messagesHook));
311328
}
312329

Diff for: cucumber-core/src/main/java/io/cucumber/core/runner/CoreHookDefinition.java

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ String getTagExpression() {
7171
return delegate.getTagExpression();
7272
}
7373

74+
Optional<HookDefinition.HookType> getHookType() {
75+
return delegate.getHookType();
76+
}
77+
7478
static class ScenarioScopedCoreHookDefinition extends CoreHookDefinition implements ScenarioScoped {
7579

7680
private ScenarioScopedCoreHookDefinition(UUID id, HookDefinition delegate) {

Diff for: cucumber-java/src/main/java/io/cucumber/java/GlueAdaptor.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import java.lang.annotation.Annotation;
77
import java.lang.reflect.Method;
88

9+
import static io.cucumber.core.backend.HookDefinition.HookType.AFTER;
10+
import static io.cucumber.core.backend.HookDefinition.HookType.AFTER_STEP;
11+
import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE;
12+
import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE_STEP;
13+
914
final class GlueAdaptor {
1015

1116
private final Lookup lookup;
@@ -24,25 +29,27 @@ void addDefinition(Method method, Annotation annotation) {
2429
} else if (annotationType.equals(Before.class)) {
2530
Before before = (Before) annotation;
2631
String tagExpression = before.value();
27-
glue.addBeforeHook(new JavaHookDefinition(method, tagExpression, before.order(), lookup));
32+
glue.addBeforeHook(new JavaHookDefinition(BEFORE, method, tagExpression, before.order(), lookup));
2833
} else if (annotationType.equals(BeforeAll.class)) {
2934
BeforeAll beforeAll = (BeforeAll) annotation;
3035
glue.addBeforeAllHook(new JavaStaticHookDefinition(method, beforeAll.order(), lookup));
3136
} else if (annotationType.equals(After.class)) {
3237
After after = (After) annotation;
3338
String tagExpression = after.value();
34-
glue.addAfterHook(new JavaHookDefinition(method, tagExpression, after.order(), lookup));
39+
glue.addAfterHook(new JavaHookDefinition(AFTER, method, tagExpression, after.order(), lookup));
3540
} else if (annotationType.equals(AfterAll.class)) {
3641
AfterAll afterAll = (AfterAll) annotation;
3742
glue.addAfterAllHook(new JavaStaticHookDefinition(method, afterAll.order(), lookup));
3843
} else if (annotationType.equals(BeforeStep.class)) {
3944
BeforeStep beforeStep = (BeforeStep) annotation;
4045
String tagExpression = beforeStep.value();
41-
glue.addBeforeStepHook(new JavaHookDefinition(method, tagExpression, beforeStep.order(), lookup));
46+
glue.addBeforeStepHook(
47+
new JavaHookDefinition(BEFORE_STEP, method, tagExpression, beforeStep.order(), lookup));
4248
} else if (annotationType.equals(AfterStep.class)) {
4349
AfterStep afterStep = (AfterStep) annotation;
4450
String tagExpression = afterStep.value();
45-
glue.addAfterStepHook(new JavaHookDefinition(method, tagExpression, afterStep.order(), lookup));
51+
glue.addAfterStepHook(
52+
new JavaHookDefinition(AFTER_STEP, method, tagExpression, afterStep.order(), lookup));
4653
} else if (annotationType.equals(ParameterType.class)) {
4754
ParameterType parameterType = (ParameterType) annotation;
4855
String pattern = parameterType.value();

Diff for: cucumber-java/src/main/java/io/cucumber/java/JavaHookDefinition.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import io.cucumber.core.backend.HookDefinition;
44
import io.cucumber.core.backend.Lookup;
55
import io.cucumber.core.backend.TestCaseState;
6+
import io.cucumber.messages.types.HookType;
67

78
import java.lang.reflect.Method;
89
import java.lang.reflect.Type;
10+
import java.util.Optional;
911

1012
import static io.cucumber.java.InvalidMethodSignatureException.builder;
1113
import static java.util.Objects.requireNonNull;
@@ -14,9 +16,11 @@ final class JavaHookDefinition extends AbstractGlueDefinition implements HookDef
1416

1517
private final String tagExpression;
1618
private final int order;
19+
private final HookType hookType;
1720

18-
JavaHookDefinition(Method method, String tagExpression, int order, Lookup lookup) {
21+
JavaHookDefinition(HookType hookType, Method method, String tagExpression, int order, Lookup lookup) {
1922
super(requireValidMethod(method), lookup);
23+
this.hookType = requireNonNull(hookType);
2024
this.tagExpression = requireNonNull(tagExpression, "tag-expression may not be null");
2125
this.order = order;
2226
}
@@ -74,4 +78,8 @@ public int getOrder() {
7478
return order;
7579
}
7680

81+
@Override
82+
public Optional<HookType> getHookType() {
83+
return Optional.of(hookType);
84+
}
7785
}

Diff for: cucumber-java/src/test/java/io/cucumber/java/JavaHookDefinitionTest.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.Method;
1313
import java.util.List;
1414

15+
import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE;
1516
import static org.hamcrest.CoreMatchers.startsWith;
1617
import static org.hamcrest.MatcherAssert.assertThat;
1718
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -39,7 +40,7 @@ public <T> T getInstance(Class<T> glueClass) {
3940
@Test
4041
void can_create_with_no_argument() throws Throwable {
4142
Method method = JavaHookDefinitionTest.class.getMethod("no_arguments");
42-
JavaHookDefinition definition = new JavaHookDefinition(method, "", 0, lookup);
43+
JavaHookDefinition definition = new JavaHookDefinition(BEFORE, method, "", 0, lookup);
4344
definition.execute(state);
4445
assertTrue(invoked);
4546
}
@@ -52,7 +53,7 @@ public void no_arguments() {
5253
@Test
5354
void can_create_with_single_scenario_argument() throws Throwable {
5455
Method method = JavaHookDefinitionTest.class.getMethod("single_argument", Scenario.class);
55-
JavaHookDefinition definition = new JavaHookDefinition(method, "", 0, lookup);
56+
JavaHookDefinition definition = new JavaHookDefinition(BEFORE, method, "", 0, lookup);
5657
definition.execute(state);
5758
assertTrue(invoked);
5859
}
@@ -67,7 +68,7 @@ void fails_if_hook_argument_is_not_scenario_result() throws NoSuchMethodExceptio
6768
Method method = JavaHookDefinitionTest.class.getMethod("invalid_parameter", String.class);
6869
InvalidMethodSignatureException exception = assertThrows(
6970
InvalidMethodSignatureException.class,
70-
() -> new JavaHookDefinition(method, "", 0, lookup));
71+
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
7172
assertThat(exception.getMessage(), startsWith("" +
7273
"A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:\n" +
7374
" * public void before_or_after(io.cucumber.java.Scenario scenario)\n" +
@@ -84,7 +85,7 @@ void fails_if_generic_hook_argument_is_not_scenario_result() throws NoSuchMethod
8485
Method method = JavaHookDefinitionTest.class.getMethod("invalid_generic_parameter", List.class);
8586
assertThrows(
8687
InvalidMethodSignatureException.class,
87-
() -> new JavaHookDefinition(method, "", 0, lookup));
88+
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
8889
}
8990

9091
public void invalid_generic_parameter(List<String> badType) {
@@ -96,7 +97,7 @@ void fails_if_too_many_arguments() throws NoSuchMethodException {
9697
Method method = JavaHookDefinitionTest.class.getMethod("too_many_parameters", Scenario.class, String.class);
9798
assertThrows(
9899
InvalidMethodSignatureException.class,
99-
() -> new JavaHookDefinition(method, "", 0, lookup));
100+
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
100101
}
101102

102103
public void too_many_parameters(Scenario arg1, String arg2) {
@@ -108,7 +109,7 @@ void fails_with_non_void_return_type() throws Throwable {
108109
Method method = JavaHookDefinitionTest.class.getMethod("string_return_type");
109110
InvalidMethodSignatureException exception = assertThrows(
110111
InvalidMethodSignatureException.class,
111-
() -> new JavaHookDefinition(method, "", 0, lookup));
112+
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
112113
assertThat(exception.getMessage(), startsWith("" +
113114
"A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:\n" +
114115
" * public void before_or_after(io.cucumber.java.Scenario scenario)\n" +

Diff for: cucumber-java8/src/main/java/io/cucumber/java8/Java8HookDefinition.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33
import io.cucumber.core.backend.HookDefinition;
44
import io.cucumber.core.backend.TestCaseState;
55

6+
import java.util.Optional;
7+
68
import static java.util.Objects.requireNonNull;
79

810
final class Java8HookDefinition extends AbstractGlueDefinition implements HookDefinition {
911

1012
private final String tagExpression;
1113
private final int order;
14+
private final HookType hookType;
1215

13-
Java8HookDefinition(String tagExpression, int order, HookBody hookBody) {
14-
this(tagExpression, order, (Object) hookBody);
16+
Java8HookDefinition(HookType hookType, String tagExpression, int order, HookBody hookBody) {
17+
this(hookType, tagExpression, order, (Object) hookBody);
1518
}
1619

17-
private Java8HookDefinition(String tagExpression, int order, Object body) {
20+
private Java8HookDefinition(HookType hookType, String tagExpression, int order, Object body) {
1821
super(body, new Exception().getStackTrace()[3]);
1922
this.order = order;
2023
this.tagExpression = requireNonNull(tagExpression, "tag-expression may not be null");
24+
this.hookType = requireNonNull(hookType);
2125
}
2226

23-
Java8HookDefinition(String tagExpression, int order, HookNoArgsBody hookNoArgsBody) {
24-
this(tagExpression, order, (Object) hookNoArgsBody);
27+
Java8HookDefinition(HookType hookType, String tagExpression, int order, HookNoArgsBody hookNoArgsBody) {
28+
this(hookType, tagExpression, order, (Object) hookNoArgsBody);
2529
}
2630

2731
@Override
@@ -45,4 +49,8 @@ public int getOrder() {
4549
return order;
4650
}
4751

52+
@Override
53+
public Optional<HookType> getHookType() {
54+
return Optional.of(hookType);
55+
}
4856
}

0 commit comments

Comments
 (0)