Skip to content

Commit 740da63

Browse files
committed
[Core] Include hook type in cucumber message
Utilizes cucumber/messages#102
1 parent 5c990e6 commit 740da63

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

cucumber-core/src/main/java/io/cucumber/core/backend/HookDefinition.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.cucumber.core.backend;
22

3+
import io.cucumber.messages.types.HookType;
34
import org.apiguardian.api.API;
45

6+
import java.util.Optional;
7+
58
@API(status = API.Status.STABLE)
69
public interface HookDefinition extends Located {
710

@@ -11,4 +14,7 @@ public interface HookDefinition extends Located {
1114

1215
int getOrder();
1316

17+
default Optional<HookType> getHookType() {
18+
return Optional.empty();
19+
}
1420
}

cucumber-core/src/main/java/io/cucumber/core/runner/CachingGlue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ private void emitHook(CoreHookDefinition coreHook) {
306306
coreHook.getDefinitionLocation()
307307
.map(this::createSourceReference)
308308
.orElseGet(this::emptySourceReference),
309-
coreHook.getTagExpression(), null);
309+
coreHook.getTagExpression(),
310+
coreHook.getHookType().orElse(null));
310311
bus.send(Envelope.of(messagesHook));
311312
}
312313

cucumber-core/src/main/java/io/cucumber/core/runner/CoreHookDefinition.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.cucumber.core.backend.ScenarioScoped;
55
import io.cucumber.core.backend.SourceReference;
66
import io.cucumber.core.backend.TestCaseState;
7+
import io.cucumber.messages.types.HookType;
78
import io.cucumber.tagexpressions.Expression;
89
import io.cucumber.tagexpressions.TagExpressionException;
910
import io.cucumber.tagexpressions.TagExpressionParser;
@@ -71,6 +72,10 @@ String getTagExpression() {
7172
return delegate.getTagExpression();
7273
}
7374

75+
Optional<HookType> getHookType() {
76+
return delegate.getHookType();
77+
}
78+
7479
static class ScenarioScopedCoreHookDefinition extends CoreHookDefinition implements ScenarioScoped {
7580

7681
private ScenarioScopedCoreHookDefinition(UUID id, HookDefinition delegate) {

cucumber-java/src/main/java/io/cucumber/java/GlueAdaptor.java

Lines changed: 11 additions & 4 deletions
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.messages.types.HookType.AFTER_TEST_CASE;
10+
import static io.cucumber.messages.types.HookType.AFTER_TEST_STEP;
11+
import static io.cucumber.messages.types.HookType.BEFORE_TEST_CASE;
12+
import static io.cucumber.messages.types.HookType.BEFORE_TEST_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_TEST_CASE, 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_TEST_CASE, 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_TEST_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_TEST_STEP, method, tagExpression, afterStep.order(), lookup));
4653
} else if (annotationType.equals(ParameterType.class)) {
4754
ParameterType parameterType = (ParameterType) annotation;
4855
String pattern = parameterType.value();

cucumber-java/src/main/java/io/cucumber/java/JavaHookDefinition.java

Lines changed: 9 additions & 1 deletion
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
}

cucumber-java/src/test/java/io/cucumber/java/JavaHookDefinitionTest.java

Lines changed: 7 additions & 6 deletions
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.messages.types.HookType.BEFORE_TEST_CASE;
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_TEST_CASE, 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_TEST_CASE, 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_TEST_CASE, 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_TEST_CASE, 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_TEST_CASE, 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_TEST_CASE, 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" +

0 commit comments

Comments
 (0)