package bluej;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import javax.swing.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
| A JUnit {}link Rule} for running tests on the JavaFX thread and performing
| JavaFX initialisation. To include in your test case, add the following code:
|
| <pre>
| {}literal @}Rule
| public JavaFXThreadingRule jfxRule = new JavaFXThreadingRule();
| </pre>
|
| @author Andy Till
|
| From: http://andrewtill.blogspot.co.uk/2012/10/junit-rule-for-javafx-controller-testing.html
|
public class JavaFXThreadingRule
implements TestRule{
| Flag for setting up the JavaFX, we only need to do this once for all tests.
|
private static boolean jfxIsSetup;
@Override
public Statement apply(Statement statement, Description description)
{
return new OnJFXThreadStatement(statement);
}
private static class OnJFXThreadStatement
extends Statement
{
private final Statement statement;
public OnJFXThreadStatement(Statement aStatement)
{
statement = aStatement;
}
private Throwable rethrownException = null;
@Override
public void evaluate() throws Throwable
{
if (!jfxIsSetup) {
setupJavaFX();
jfxIsSetup = true;
}
final CountDownLatch countDownLatch = new CountDownLatch(1);
rethrownException = null;
Platform.runLater(new Runnable() {
@Override
public void run()
{
try {
statement.evaluate();
} catch (Throwable e) {
rethrownException = e;
}
countDownLatch.countDown();
}
});
countDownLatch.await(60, TimeUnit.SECONDS);
if (rethrownException != null) {
throw rethrownException;
}
}
protected void setupJavaFX() throws Throwable
{
System.out.println("javafx initialising...");
System.out.flush();
long timeMillis = System.currentTimeMillis();
final CountDownLatch latch = new CountDownLatch(1);
rethrownException = null;
SwingUtilities.invokeLater(() -> {
try
{
new JFXPanel();
}
catch (Throwable e)
{
rethrownException = e;
}
latch.countDown();
});
if (!latch.await(10, TimeUnit.SECONDS))
{
if (rethrownException != null)
{
throw rethrownException;
}
else
{
throw new RuntimeException("Timed out awaiting JavaFX initialisation");
}
}
System.out.println("javafx is initialised in " + (System.currentTimeMillis() - timeMillis) + "ms");
}
}
}
top,
use,
map,
class JavaFXThreadingRule
. apply
top,
use,
map,
class JavaFXThreadingRule . OnJFXThreadStatement
. OnJFXThreadStatement
. evaluate
. run
. setupJavaFX
126 neLoCode
+ 9 LoComm