-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] Add BeforeAll and AfterAll hooks
Work in progress to fix #515. TODO: - [ ] How to deal with failure? - [ ] Test with JUnit4 - [ ] Test with JUnit5 - [ ] Test with TestNG - [ ] Test with CLI - [ ] Invoke around semantics? - [ ] How to report execution results? - [ ] TeamCity Plugin - [ ] Pretty formatter - [ ] Messages/Events
- Loading branch information
1 parent
218dcae
commit 2f4e9da
Showing
17 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/src/main/java/io/cucumber/core/backend/StaticHookDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE) | ||
public interface StaticHookDefinition extends Located { | ||
|
||
void execute(); | ||
|
||
int getOrder(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.cucumber.java; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Executes a method before all scenarios | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
@API(status = API.Status.STABLE) | ||
public @interface AfterAll { | ||
|
||
/** | ||
* The order in which this hook should run. Higher numbers are run first. | ||
* The default order is 10000. | ||
* | ||
* @return the order in which this hook should run. | ||
*/ | ||
int order() default 10000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.cucumber.java; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Executes a method after all scenarios | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
@API(status = API.Status.STABLE) | ||
public @interface BeforeAll { | ||
|
||
/** | ||
* The order in which this hook should run. Lower numbers are run first. | ||
* The default order is 10000. | ||
* | ||
* @return the order in which this hook should run. | ||
*/ | ||
int order() default 10000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
java/src/main/java/io/cucumber/java/JavaStaticHookDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.cucumber.java; | ||
|
||
import io.cucumber.core.backend.Lookup; | ||
import io.cucumber.core.backend.StaticHookDefinition; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static io.cucumber.java.InvalidMethodSignatureException.builder; | ||
import static java.lang.reflect.Modifier.isStatic; | ||
|
||
final class JavaStaticHookDefinition extends AbstractGlueDefinition implements StaticHookDefinition { | ||
|
||
private final int order; | ||
private final Lookup lookup; | ||
|
||
JavaStaticHookDefinition(Method method, int order, Lookup lookup) { | ||
super(requireValidMethod(method), lookup); | ||
this.order = order; | ||
this.lookup = lookup; | ||
} | ||
|
||
private static Method requireValidMethod(Method method) { | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
if (parameterTypes.length != 0) { | ||
throw createInvalidSignatureException(method); | ||
} | ||
|
||
if (!isStatic(method.getModifiers())) { | ||
throw createInvalidSignatureException(method); | ||
} | ||
|
||
return method; | ||
} | ||
|
||
private static InvalidMethodSignatureException createInvalidSignatureException(Method method) { | ||
return builder(method) | ||
.addAnnotation(BeforeAll.class) | ||
.addAnnotation(AfterAll.class) | ||
.addSignature("public static void before_or_after_all()") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
Invoker.invoke(this, lookup.getInstance(method.getDeclaringClass()), method); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return order; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.