Skip to content

Commit

Permalink
added support for execution order for "Before" and "After" hook. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhshams committed Dec 13, 2014
1 parent 125b959 commit 5b7a1f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
42 changes: 35 additions & 7 deletions src/main/java/cucumber/api/groovy/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,66 @@
import java.util.List;

public class Hooks {
private static final int DEFAULT_ORDER = 10000;
private static final long DEFAULT_TIMEOUT = 0;

public static void World(Closure body) throws Throwable {
GroovyBackend.getInstance().registerWorld(body);
}

public static void Before(Object... args) throws Throwable {
/**
* Registers a before hook, which is executed before specific, or all, scenarios.
* <p/>
* Following values are expected as hook parameters.
* - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction.
* - Integer order: the order in which this hook should run. Lower numbers are run first. The default is 10000.
* - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty.
* - Closure body: hook body which is executed before scenario. Not null.
*
* @param args the hook parameters
*/
public static void Before(Object... args) {
addHook(args, true);
}

public static void After(Object... args) throws Throwable {
/**
* Registers an after hook, which is executed after specific, or all, scenarios.
* <p/>
* Following values are expected as hook parameters.
* - Long timeoutMillis: max amount of milliseconds this is allowed to run for. The default is 0 which means no restriction.
* - Integer order: the order in which this hook should run. Higher numbers are run first. The default is 10000.
* - String(s) tags: one or more tag expression to filter the certain scenarios. The default is empty.
* - Closure body: hook body which is executed after scenario. Not null.
*
* @param args the hook parameters
*/
public static void After(Object... args) {
addHook(args, false);
}

private static void addHook(Object[] tagsExpressionsAndBody, boolean before) {
List<String> tagExpressions = new ArrayList<String>();
int timeoutMillis = 0;
long timeoutMillis = DEFAULT_TIMEOUT;
int order = DEFAULT_ORDER;
Closure body = null;
List<String> tagExpressions = new ArrayList<String>();

for (Object o : tagsExpressionsAndBody) {
if (o instanceof String) {
tagExpressions.add((String) o);
} else if (o instanceof Long) {
timeoutMillis = (Long) o;
} else if (o instanceof Integer) {
timeoutMillis = (Integer) o;
order = (Integer) o;
} else if (o instanceof Closure) {
body = (Closure) o;
}
}

TagExpression tagExpression = new TagExpression(tagExpressions);
if (before) {
GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, body);
GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, order, body);
} else {
GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, body);
GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, order, body);
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/cucumber/runtime/groovy/GroovyBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ public void registerWorld(Closure closure) {
worldClosures.add(closure);
}

public void addBeforeHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
glue.addBeforeHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), this));
public void addBeforeHook(TagExpression tagExpression, long timeoutMillis, int order, Closure body) {
glue.addBeforeHook(new GroovyHookDefinition(tagExpression, timeoutMillis, order, body, currentLocation(), this));
}

public void addAfterHook(TagExpression tagExpression, long timeoutMillis, Closure body) {
glue.addAfterHook(new GroovyHookDefinition(tagExpression, timeoutMillis, body, currentLocation(), this));
public void addAfterHook(TagExpression tagExpression, long timeoutMillis, int order, Closure body) {
glue.addAfterHook(new GroovyHookDefinition(tagExpression, timeoutMillis, order, body, currentLocation(), this));
}

public void invoke(Closure body, Object[] args) throws Throwable {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/cucumber/runtime/groovy/GroovyHookDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@
public class GroovyHookDefinition implements HookDefinition {
private final TagExpression tagExpression;
private final long timeoutMillis;
private final int order;
private final Closure body;
private final GroovyBackend backend;
private final StackTraceElement location;

public GroovyHookDefinition(TagExpression tagExpression, long timeoutMillis, Closure body, StackTraceElement location, GroovyBackend backend) {
public GroovyHookDefinition(
TagExpression tagExpression,
long timeoutMillis,
int order,
Closure body,
StackTraceElement location,
GroovyBackend backend) {

this.tagExpression = tagExpression;
this.timeoutMillis = timeoutMillis;
this.order = order;
this.body = body;
this.location = location;
this.backend = backend;
Expand Down Expand Up @@ -47,7 +56,7 @@ public boolean matches(Collection<Tag> tags) {

@Override
public int getOrder() {
return location.getFileName() == "env.groovy" ? -1 : 0;
return order;
}
}

0 comments on commit 5b7a1f4

Please sign in to comment.