Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue #21 - Thread safety issue in SpecRunner #22

Merged
merged 1 commit into from
May 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/com/googlecode/yatspec/junit/SpecRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
import org.junit.runners.model.Statement;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.googlecode.totallylazy.Sequences.sequence;
import static java.lang.System.getProperty;

public class SpecRunner extends TableRunner {
public static final String OUTPUT_DIR = "yatspec.output.dir";
private final Result testResult;
private Scenario currentScenario;
private Map<String, Scenario> currentScenario = new HashMap<String, Scenario>();

public SpecRunner(Class<?> klass) throws org.junit.runners.model.InitializationError {
super(klass);
Expand All @@ -46,7 +48,7 @@ public boolean matches(FrameworkMethod method) {
@Override
protected Object createTest() throws Exception {
Object instance = super.createTest();
if(instance instanceof WithCustomResultListeners){
if (instance instanceof WithCustomResultListeners) {
listeners = (WithCustomResultListeners) instance;
} else {
listeners = new DefaultResultListeners();
Expand Down Expand Up @@ -80,28 +82,24 @@ protected Statement methodInvoker(final FrameworkMethod method, final Object tes
return new Statement() {
@Override
public void evaluate() throws Throwable {
currentScenario = testResult.getScenario(method.getName());
final String fullyQualifiedTestMethod = test.getClass().getCanonicalName() + "." + method.getName();
final Scenario scenario = testResult.getScenario(method.getName());
currentScenario.put(fullyQualifiedTestMethod, scenario);

if (test instanceof WithTestState) {
TestState testState = ((WithTestState) test).testState();
currentScenario.setTestState(testState);
currentScenario.get(fullyQualifiedTestMethod).setTestState(testState);
}
statement.evaluate();
}
};
}

private boolean isInTest() {
return currentScenario != null;
}

private final class SpecListener extends RunListener {

@Override
public void testFailure(Failure failure) throws Exception {
if (isInTest()) {
currentScenario.setException(failure.getException());
}
String fullyQualifiedTestMethod = failure.getDescription().getClassName() + "." + failure.getDescription().getMethodName();
if (currentScenario.get(fullyQualifiedTestMethod) != null) currentScenario.get(fullyQualifiedTestMethod).setException(failure.getException());
}
}
}