Skip to content

Commit

Permalink
[WFCORE-6728] Make it possible to run Junit Assume from a ServerSetup…
Browse files Browse the repository at this point in the history
…Task.setup() method
  • Loading branch information
kabir committed Mar 12, 2024
1 parent 572393c commit 711a6bb
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;
import org.junit.AssumptionViolatedException;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
Expand Down Expand Up @@ -64,6 +65,14 @@ public class WildFlyRunner extends BlockJUnit4ClassRunner {

private final ParameterDescriptions parameterDescriptions = new ParameterDescriptions();

// If a ServerSetupTask.setup() method fails, record the error here
private org.junit.internal.AssumptionViolatedException assumptionFailedInServerSetup;

//Keeps track of the number of setup tasks that have been run, which is needed when tearing them down again
private int runSetupTasks = 0;



/**
* Creates a BlockJUnit4ClassRunner to run {@code klass}
*
Expand Down Expand Up @@ -154,6 +163,8 @@ protected Statement methodInvoker(final FrameworkMethod method, final Object tes

@Override
public void run(final RunNotifier notifier) {
assumptionFailedInServerSetup = null;
int setupTasks = 0;
notifier.addListener(new RunListener() {
@Override
public void testRunFinished(Result result) throws Exception {
Expand All @@ -173,10 +184,15 @@ public void testRunFinished(Result result) throws Exception {
Security.insertProviderAt(ELYTRON_PROVIDER, 0);
providerInstalled = true;
}
if (automaticServerControl) {
runSetupTasks();
try {
if (automaticServerControl) {
runSetupTasks();
}
} catch (AssumptionViolatedException e) {
assumptionFailedInServerSetup = e;
}
super.run(notifier);

if (automaticServerControl) {
runTearDownTasks();
}
Expand All @@ -189,14 +205,17 @@ private void runSetupTasks() {
for (ServerSetupTask task : serverSetupTasks) {
try {
task.setup(controller.getClient());
runSetupTasks++;
} catch (AssumptionViolatedException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(String.format("Could not run setup task '%s'", task), e);
}
}
}

private void runTearDownTasks() {
List<ServerSetupTask> reverseServerSetupTasks = new LinkedList<>(serverSetupTasks);
List<ServerSetupTask> reverseServerSetupTasks = new LinkedList<>(serverSetupTasks.subList(0, runSetupTasks));
Collections.reverse(reverseServerSetupTasks);
for (ServerSetupTask task : reverseServerSetupTasks) {
try {
Expand Down Expand Up @@ -300,4 +319,25 @@ private static List<Class<? extends Annotation>> getInjectClasses() {
return INJECT_CLASSES;
}

@Override
protected Statement classBlock(RunNotifier notifier) {
Statement statement = super.classBlock(notifier);
return new WrappedStatement(statement);
}

private class WrappedStatement extends Statement {

private final Statement delegate;
public WrappedStatement(Statement delegate) {
this.delegate = delegate;
}

@Override
public void evaluate() throws Throwable {
if (assumptionFailedInServerSetup != null) {
throw assumptionFailedInServerSetup;
}
delegate.evaluate();
}
}
}

0 comments on commit 711a6bb

Please sign in to comment.