Skip to content

Commit

Permalink
Reworked test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-Loeffler committed Jan 10, 2022
1 parent 0d6789e commit 656f1f5
Showing 1 changed file with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Callable;

import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -49,6 +48,7 @@
import com.oracle.javafx.scenebuilder.kit.JfxInitializer;

import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;

public class FXOMDocumentTest {
Expand Down Expand Up @@ -147,43 +147,35 @@ public void that_fxml_with_defines_loads_without_error_without_normalization() t
URL resource = getClass().getResource("DynamicScreenSize.fxml");
String validFxmlText = FXOMDocument.readContentFromURL(resource);

invokeAndWait(() -> {
try {
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, resource, null, null, false);
String beforeNormalization = classUnderTest.getFxmlText(false);
assertFalse(beforeNormalization.isBlank());
} catch (IOException e) {
throw new AssertionError("unexpected error: " + e);
}
});
FXOMDocument classUnderTest = waitFor(() -> new FXOMDocument(validFxmlText, resource, null, null, false));
String beforeNormalization = classUnderTest.getFxmlText(false);
assertFalse(beforeNormalization.isBlank());
}

@Test
public void that_missing_imports_during_defines_resolution_cause_exception() throws Exception {
URL resource = getClass().getResource("DynamicScreenSize.fxml");
String validFxmlText = FXOMDocument.readContentFromURL(resource);

Throwable t = assertThrows(ExecutionException.class,
() -> invokeAndWait(
() -> {
try {
/* normalization enabled */
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, resource, null, null, true);
String beforeNormalization = classUnderTest.getFxmlText(false);
assertFalse(beforeNormalization.isBlank());
} catch (IOException e) {
throw new AssertionError("unexpected error: " + e);
}
}));
Throwable t = assertThrows(Throwable.class,
() -> waitFor(() -> new FXOMDocument(validFxmlText, resource, null, null, true /* normalization enabled */)));

assertTrue(t.getCause() instanceof IllegalStateException);
String message = t.getCause().getMessage();
assertTrue(message.startsWith("Bug in FXOMRefresher: FXML dumped in "));
if (t.getCause() != null) {
t = t.getCause();
}

assertEquals(IllegalStateException.class, t.getClass());
assertTrue(t.getMessage().contains("Bug in FXOMRefresher"));
}

void invokeAndWait(Runnable r) throws Exception {
FutureTask<?> task = new FutureTask<>(r, null);
Platform.runLater(task);
task.get();

private <T> T waitFor(Callable<T> callable) throws Exception {
Task<T> task = new Task<T>() {
@Override
protected T call() throws Exception {
return callable.call();
}
};
Platform.runLater(()->task.run());
return task.get();
}
}

0 comments on commit 656f1f5

Please sign in to comment.