Skip to content

Commit

Permalink
[Core] Include hook type in cucumber message (#2972)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje authored Feb 2, 2025
1 parent 5c990e6 commit 175620d
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- [Archetype] Replace JUnit Jupiter with AssertJ ([#2969](https://github.com/cucumber/cucumber-jvm/pull/2969) M.P. Korstanje)
- [JUnit Platform Engine] Use JUnit Platform 1.11.3 (JUnit Jupiter 5.11.3)
- [Core] Update dependency io.cucumber:gherkin to v31.0.0
- [Core] Update dependency io.cucumber:messages to v27.1.0

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

## [7.20.1] - 2024-10-09
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apiguardian.api.API;

import java.util.Optional;

@API(status = API.Status.STABLE)
public interface HookDefinition extends Located {

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

int getOrder();

default Optional<HookType> getHookType() {
return Optional.empty();
}

enum HookType {

BEFORE,

AFTER,

BEFORE_STEP,

AFTER_STEP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.cucumber.datatable.TableEntryByTypeTransformer;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.Hook;
import io.cucumber.messages.types.HookType;
import io.cucumber.messages.types.JavaMethod;
import io.cucumber.messages.types.JavaStackTraceElement;
import io.cucumber.messages.types.Location;
Expand Down Expand Up @@ -306,7 +307,23 @@ private void emitHook(CoreHookDefinition coreHook) {
coreHook.getDefinitionLocation()
.map(this::createSourceReference)
.orElseGet(this::emptySourceReference),
coreHook.getTagExpression(), null);
coreHook.getTagExpression(),
coreHook.getHookType()
.map(hookType -> {
switch (hookType) {
case BEFORE:
return HookType.BEFORE_TEST_CASE;
case AFTER:
return HookType.AFTER_TEST_CASE;
case BEFORE_STEP:
return HookType.BEFORE_TEST_STEP;
case AFTER_STEP:
return HookType.AFTER_TEST_STEP;
default:
return null;
}
})
.orElse(null));
bus.send(Envelope.of(messagesHook));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ String getTagExpression() {
return delegate.getTagExpression();
}

Optional<HookDefinition.HookType> getHookType() {
return delegate.getHookType();
}

static class ScenarioScopedCoreHookDefinition extends CoreHookDefinition implements ScenarioScoped {

private ScenarioScopedCoreHookDefinition(UUID id, HookDefinition delegate) {
Expand Down
15 changes: 11 additions & 4 deletions cucumber-java/src/main/java/io/cucumber/java/GlueAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import static io.cucumber.core.backend.HookDefinition.HookType.AFTER;
import static io.cucumber.core.backend.HookDefinition.HookType.AFTER_STEP;
import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE;
import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE_STEP;

final class GlueAdaptor {

private final Lookup lookup;
Expand All @@ -24,25 +29,27 @@ void addDefinition(Method method, Annotation annotation) {
} else if (annotationType.equals(Before.class)) {
Before before = (Before) annotation;
String tagExpression = before.value();
glue.addBeforeHook(new JavaHookDefinition(method, tagExpression, before.order(), lookup));
glue.addBeforeHook(new JavaHookDefinition(BEFORE, method, tagExpression, before.order(), lookup));
} else if (annotationType.equals(BeforeAll.class)) {
BeforeAll beforeAll = (BeforeAll) annotation;
glue.addBeforeAllHook(new JavaStaticHookDefinition(method, beforeAll.order(), lookup));
} else if (annotationType.equals(After.class)) {
After after = (After) annotation;
String tagExpression = after.value();
glue.addAfterHook(new JavaHookDefinition(method, tagExpression, after.order(), lookup));
glue.addAfterHook(new JavaHookDefinition(AFTER, method, tagExpression, after.order(), lookup));
} else if (annotationType.equals(AfterAll.class)) {
AfterAll afterAll = (AfterAll) annotation;
glue.addAfterAllHook(new JavaStaticHookDefinition(method, afterAll.order(), lookup));
} else if (annotationType.equals(BeforeStep.class)) {
BeforeStep beforeStep = (BeforeStep) annotation;
String tagExpression = beforeStep.value();
glue.addBeforeStepHook(new JavaHookDefinition(method, tagExpression, beforeStep.order(), lookup));
glue.addBeforeStepHook(
new JavaHookDefinition(BEFORE_STEP, method, tagExpression, beforeStep.order(), lookup));
} else if (annotationType.equals(AfterStep.class)) {
AfterStep afterStep = (AfterStep) annotation;
String tagExpression = afterStep.value();
glue.addAfterStepHook(new JavaHookDefinition(method, tagExpression, afterStep.order(), lookup));
glue.addAfterStepHook(
new JavaHookDefinition(AFTER_STEP, method, tagExpression, afterStep.order(), lookup));
} else if (annotationType.equals(ParameterType.class)) {
ParameterType parameterType = (ParameterType) annotation;
String pattern = parameterType.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import io.cucumber.core.backend.HookDefinition;
import io.cucumber.core.backend.Lookup;
import io.cucumber.core.backend.TestCaseState;
import io.cucumber.messages.types.HookType;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Optional;

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

private final String tagExpression;
private final int order;
private final HookType hookType;

JavaHookDefinition(Method method, String tagExpression, int order, Lookup lookup) {
JavaHookDefinition(HookType hookType, Method method, String tagExpression, int order, Lookup lookup) {
super(requireValidMethod(method), lookup);
this.hookType = requireNonNull(hookType);
this.tagExpression = requireNonNull(tagExpression, "tag-expression may not be null");
this.order = order;
}
Expand Down Expand Up @@ -74,4 +78,8 @@ public int getOrder() {
return order;
}

@Override
public Optional<HookType> getHookType() {
return Optional.of(hookType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.lang.reflect.Method;
import java.util.List;

import static io.cucumber.core.backend.HookDefinition.HookType.BEFORE;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -39,7 +40,7 @@ public <T> T getInstance(Class<T> glueClass) {
@Test
void can_create_with_no_argument() throws Throwable {
Method method = JavaHookDefinitionTest.class.getMethod("no_arguments");
JavaHookDefinition definition = new JavaHookDefinition(method, "", 0, lookup);
JavaHookDefinition definition = new JavaHookDefinition(BEFORE, method, "", 0, lookup);
definition.execute(state);
assertTrue(invoked);
}
Expand All @@ -52,7 +53,7 @@ public void no_arguments() {
@Test
void can_create_with_single_scenario_argument() throws Throwable {
Method method = JavaHookDefinitionTest.class.getMethod("single_argument", Scenario.class);
JavaHookDefinition definition = new JavaHookDefinition(method, "", 0, lookup);
JavaHookDefinition definition = new JavaHookDefinition(BEFORE, method, "", 0, lookup);
definition.execute(state);
assertTrue(invoked);
}
Expand All @@ -67,7 +68,7 @@ void fails_if_hook_argument_is_not_scenario_result() throws NoSuchMethodExceptio
Method method = JavaHookDefinitionTest.class.getMethod("invalid_parameter", String.class);
InvalidMethodSignatureException exception = assertThrows(
InvalidMethodSignatureException.class,
() -> new JavaHookDefinition(method, "", 0, lookup));
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
assertThat(exception.getMessage(), startsWith("" +
"A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:\n" +
" * public void before_or_after(io.cucumber.java.Scenario scenario)\n" +
Expand All @@ -84,7 +85,7 @@ void fails_if_generic_hook_argument_is_not_scenario_result() throws NoSuchMethod
Method method = JavaHookDefinitionTest.class.getMethod("invalid_generic_parameter", List.class);
assertThrows(
InvalidMethodSignatureException.class,
() -> new JavaHookDefinition(method, "", 0, lookup));
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
}

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

public void too_many_parameters(Scenario arg1, String arg2) {
Expand All @@ -108,7 +109,7 @@ void fails_with_non_void_return_type() throws Throwable {
Method method = JavaHookDefinitionTest.class.getMethod("string_return_type");
InvalidMethodSignatureException exception = assertThrows(
InvalidMethodSignatureException.class,
() -> new JavaHookDefinition(method, "", 0, lookup));
() -> new JavaHookDefinition(BEFORE, method, "", 0, lookup));
assertThat(exception.getMessage(), startsWith("" +
"A method annotated with Before, After, BeforeStep or AfterStep must have one of these signatures:\n" +
" * public void before_or_after(io.cucumber.java.Scenario scenario)\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
import io.cucumber.core.backend.HookDefinition;
import io.cucumber.core.backend.TestCaseState;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

final class Java8HookDefinition extends AbstractGlueDefinition implements HookDefinition {

private final String tagExpression;
private final int order;
private final HookType hookType;

Java8HookDefinition(String tagExpression, int order, HookBody hookBody) {
this(tagExpression, order, (Object) hookBody);
Java8HookDefinition(HookType hookType, String tagExpression, int order, HookBody hookBody) {
this(hookType, tagExpression, order, (Object) hookBody);
}

private Java8HookDefinition(String tagExpression, int order, Object body) {
private Java8HookDefinition(HookType hookType, String tagExpression, int order, Object body) {
super(body, new Exception().getStackTrace()[3]);
this.order = order;
this.tagExpression = requireNonNull(tagExpression, "tag-expression may not be null");
this.hookType = requireNonNull(hookType);
}

Java8HookDefinition(String tagExpression, int order, HookNoArgsBody hookNoArgsBody) {
this(tagExpression, order, (Object) hookNoArgsBody);
Java8HookDefinition(HookType hookType, String tagExpression, int order, HookNoArgsBody hookNoArgsBody) {
this(hookType, tagExpression, order, (Object) hookNoArgsBody);
}

@Override
Expand All @@ -45,4 +49,8 @@ public int getOrder() {
return order;
}

@Override
public Optional<HookType> getHookType() {
return Optional.of(hookType);
}
}
Loading

0 comments on commit 175620d

Please sign in to comment.