Skip to content

Commit 5d03555

Browse files
authored
[Core] Add hook method name to TeamCityPlugin (#2881)
The TeamCityPlugin for IntelliJ IDEA now uses the hook's method name for the name of the hook itself. For example, if we have a hook with the method name `com.example.HookDefinition.beforeHook()``, the generated name will be `Before(beforeHook)`. If we have a hook with the method name `com.example.HookDefinition.<init>(HookDefinition.java:12)`, the generated name will be `Before(HookDefinition)`. If the hook's method can't be identified then fallback to simple hook name. Closes #2798
1 parent 3f4c299 commit 5d03555

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1010
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111

1212
## [Unreleased]
13+
### Changed
14+
- [Core] The TeamCityPlugin for IntelliJ IDEA now uses the hook's method name for the name of the hook itself. ([#2798](https://github.com/cucumber/cucumber-jvm/issues/2798) V.V. Belov)
1315

1416
## [7.17.0] - 2024-04-18
1517
### Added

cucumber-core/src/main/java/io/cucumber/core/plugin/TeamCityPlugin.java

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.util.Locale;
3838
import java.util.Map;
3939
import java.util.Optional;
40+
import java.util.function.BiFunction;
41+
import java.util.function.Function;
4042
import java.util.function.Predicate;
4143
import java.util.function.Supplier;
4244
import java.util.regex.Matcher;
@@ -251,18 +253,31 @@ private String extractLocation(TestStepStarted event) {
251253
PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) testStep;
252254
return pickleStepTestStep.getUri() + ":" + pickleStepTestStep.getStep().getLine();
253255
}
254-
return extractSourceLocation(testStep);
256+
if (testStep instanceof HookTestStep) {
257+
return formatHookStepLocation(
258+
(HookTestStep) testStep,
259+
javaTestLocationUri(),
260+
TestStep::getCodeLocation);
261+
}
262+
return testStep.getCodeLocation();
255263
}
256264

257-
private String extractSourceLocation(TestStep testStep) {
265+
private static BiFunction<String, String, String> javaTestLocationUri() {
266+
return (fqDeclaringClassName, classOrMethodName) -> String.format("java:test://%s/%s", fqDeclaringClassName,
267+
classOrMethodName);
268+
}
258269

259-
Matcher javaMatcher = ANNOTATION_GLUE_CODE_LOCATION_PATTERN.matcher(testStep.getCodeLocation());
270+
private String formatHookStepLocation(
271+
HookTestStep hookTestStep, BiFunction<String, String, String> hookStepCase,
272+
Function<HookTestStep, String> defaultHookName
273+
) {
274+
Matcher javaMatcher = ANNOTATION_GLUE_CODE_LOCATION_PATTERN.matcher(hookTestStep.getCodeLocation());
260275
if (javaMatcher.matches()) {
261276
String fqDeclaringClassName = javaMatcher.group(1);
262277
String methodName = javaMatcher.group(2);
263-
return String.format("java:test://%s/%s", fqDeclaringClassName, methodName);
278+
return hookStepCase.apply(fqDeclaringClassName, methodName);
264279
}
265-
Matcher java8Matcher = LAMBDA_GLUE_CODE_LOCATION_PATTERN.matcher(testStep.getCodeLocation());
280+
Matcher java8Matcher = LAMBDA_GLUE_CODE_LOCATION_PATTERN.matcher(hookTestStep.getCodeLocation());
266281
if (java8Matcher.matches()) {
267282
String fqDeclaringClassName = java8Matcher.group(1);
268283
String declaringClassName;
@@ -272,10 +287,9 @@ private String extractSourceLocation(TestStep testStep) {
272287
} else {
273288
declaringClassName = fqDeclaringClassName;
274289
}
275-
return String.format("java:test://%s/%s", fqDeclaringClassName, declaringClassName);
290+
return hookStepCase.apply(fqDeclaringClassName, declaringClassName);
276291
}
277-
278-
return testStep.getCodeLocation();
292+
return defaultHookName.apply(hookTestStep);
279293
}
280294

281295
private void printTestStepFinished(TestStepFinished event) {
@@ -324,30 +338,42 @@ private void printTestStepFinished(TestStepFinished event) {
324338
print(TEMPLATE_TEST_FINISHED, timeStamp, duration, name);
325339
}
326340

327-
private String extractName(TestStep step) {
328-
if (step instanceof PickleStepTestStep) {
329-
PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) step;
341+
private String getHookName(HookTestStep hook) {
342+
HookType hookType = hook.getHookType();
343+
switch (hookType) {
344+
case BEFORE:
345+
return "Before";
346+
case AFTER:
347+
return "After";
348+
case BEFORE_STEP:
349+
return "BeforeStep";
350+
case AFTER_STEP:
351+
return "AfterStep";
352+
default:
353+
return hookType.name().toLowerCase(Locale.US);
354+
}
355+
}
356+
357+
private String extractName(TestStep testStep) {
358+
if (testStep instanceof PickleStepTestStep) {
359+
PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) testStep;
330360
return pickleStepTestStep.getStep().getText();
331361
}
332-
if (step instanceof HookTestStep) {
333-
HookTestStep hook = (HookTestStep) step;
334-
HookType hookType = hook.getHookType();
335-
switch (hookType) {
336-
case BEFORE:
337-
return "Before";
338-
case AFTER:
339-
return "After";
340-
case BEFORE_STEP:
341-
return "BeforeStep";
342-
case AFTER_STEP:
343-
return "AfterStep";
344-
default:
345-
return hookType.name().toLowerCase(Locale.US);
346-
}
362+
if (testStep instanceof HookTestStep) {
363+
HookTestStep hookTestStep = (HookTestStep) testStep;
364+
return formatHookStepLocation(
365+
hookTestStep,
366+
hookNameFormat(hookTestStep),
367+
this::getHookName);
347368
}
348369
return "Unknown step";
349370
}
350371

372+
private BiFunction<String, String, String> hookNameFormat(HookTestStep hookTestStep) {
373+
return (fqDeclaringClassName, classOrMethodName) -> String.format("%s(%s)", getHookName(hookTestStep),
374+
classOrMethodName);
375+
}
376+
351377
private String getSnippets(TestCase testCase) {
352378
URI uri = testCase.getUri();
353379
Location location = testCase.getLocation();

cucumber-core/src/test/java/io/cucumber/core/plugin/TeamCityPluginTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void should_print_location_hint_for_java_hooks() {
287287
.run();
288288

289289
assertThat(out, bytes(containsString("" +
290-
"##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = 'java:test://com.example.HookDefinition/beforeHook' captureStandardOutput = 'true' name = 'Before']\n")));
290+
"##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = 'java:test://com.example.HookDefinition/beforeHook' captureStandardOutput = 'true' name = 'Before(beforeHook)']\n")));
291291
}
292292

293293
@Test
@@ -310,7 +310,7 @@ void should_print_location_hint_for_lambda_hooks() {
310310
.run();
311311

312312
assertThat(out, bytes(containsString("" +
313-
"##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = 'java:test://com.example.HookDefinition/HookDefinition' captureStandardOutput = 'true' name = 'Before']\n")));
313+
"##teamcity[testStarted timestamp = '1970-01-01T12:00:00.000+0000' locationHint = 'java:test://com.example.HookDefinition/HookDefinition' captureStandardOutput = 'true' name = 'Before(HookDefinition)']\n")));
314314
}
315315

316316
@Test

0 commit comments

Comments
 (0)