247 lines
11 KiB
Java
247 lines
11 KiB
Java
import de.neitzel.fx.view.ViewDependencies;
|
|
import de.neitzel.fx.view.ViewLoader;
|
|
import de.neitzel.fx.view.ViewModelContext;
|
|
import de.neitzel.fx.view.ViewObject;
|
|
import de.neitzel.fxdesigner.preview.ExampleDescriptor;
|
|
import de.neitzel.fxdesigner.preview.PreviewProvider;
|
|
import de.neitzel.fxdesigner.preview.PreviewSession;
|
|
import de.neitzel.fxdesigner.target.ExampleCatalog;
|
|
import de.neitzel.fxdesigner.target.PreviewFailure;
|
|
import de.neitzel.fxdesigner.target.TargetPreview;
|
|
import fixture.spring.app.FixtureService;
|
|
import fixture.spring.app.SpringBook;
|
|
import fixture.spring.app.SpringBookContext;
|
|
import fixture.spring.model.Book;
|
|
import fixture.spring.preview.BookExamples;
|
|
import fixture.spring.ui.BookController;
|
|
import java.time.Duration;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.FutureTask;
|
|
import java.util.concurrent.TimeUnit;
|
|
import javafx.application.Platform;
|
|
import javafx.scene.Parent;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.TextField;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.stage.Stage;
|
|
|
|
public final class SpringChecks {
|
|
private static final String UI = "fixture/spring/ui/";
|
|
private static int assertions;
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
Platform.startup(() -> Platform.setImplicitExit(false));
|
|
try {
|
|
if (args[0].equals("application")) {
|
|
fx(() -> { application(); return null; });
|
|
} else {
|
|
preview(args[0]);
|
|
}
|
|
System.out.println("PASS " + args[0] + " " + assertions);
|
|
} finally {
|
|
Platform.exit();
|
|
}
|
|
}
|
|
|
|
private static void application() throws Exception {
|
|
var app = new SpringBook();
|
|
var stage = new Stage();
|
|
try {
|
|
app.start(stage);
|
|
check(stage.isShowing() && name(stage.getScene().getRoot()).getText().equals("Spring contact book"),
|
|
"normal application launch");
|
|
} finally {
|
|
stage.close();
|
|
app.stop();
|
|
}
|
|
|
|
FixtureService service;
|
|
try (var spring = SpringBookContext.open("Application check"); var models = new ViewModelContext()) {
|
|
service = spring.service();
|
|
var fxml = BookController.class.getResource("Book.fxml");
|
|
var model = Book.sample("Local contact book");
|
|
var first = ViewLoader.loadWithDependencies(fxml, model, spring.dependencies(), models);
|
|
var second = ViewLoader.loadWithDependencies(fxml, model, spring.dependencies(), models);
|
|
var firstRoot = first.getRoot();
|
|
var secondRoot = second.getRoot();
|
|
checkBook(firstRoot, "Local contact book", "Application check");
|
|
var firstController = (BookController) name(firstRoot).getUserData();
|
|
var otherController = (BookController) name(secondRoot).getUserData();
|
|
check(firstController != otherController && firstController.service() == otherController.service(),
|
|
"prototype controllers share one injected service");
|
|
check(firstRoot.lookup("#featured") != secondRoot.lookup("#featured"), "distinct embedded Components");
|
|
check(service.liveCount() == 14, "seven UI instances per view");
|
|
model.getEntries().removeFirst();
|
|
check(service.releasedCount() == 4 && service.liveCount() == 10, "Repeat and Table entries released");
|
|
first.close();
|
|
first.close();
|
|
check(service.releasedCount() == 9 && otherController.service() == spring.service(),
|
|
"one close preserves the other View and the service");
|
|
try {
|
|
ViewLoader.loadWithDependencies(BookController.class.getResource("Invalid.fxml"),
|
|
model, spring.dependencies(), models);
|
|
throw new AssertionError("Invalid child loaded");
|
|
} catch (IllegalStateException expected) {
|
|
check(service.releasedCount() == 11 && service.liveCount() == 5, "child and controller rollback");
|
|
}
|
|
}
|
|
checkClosed(service);
|
|
}
|
|
|
|
private static void preview(String mode) throws Exception {
|
|
var loader = SpringChecks.class.getClassLoader();
|
|
var descriptors = ExampleCatalog.discover(loader);
|
|
check(descriptors.size() == 2, "exact fixture descriptors");
|
|
var listed = descriptors.stream().filter(d -> d.id().equals(mode.equals("card") ? "card" : "book"))
|
|
.findFirst().orElseThrow();
|
|
check(BookExamples.lastService() == null, "discovery did not open provider");
|
|
var selected = switch (mode) {
|
|
case "invalid" -> with(listed, mode, listed.providerClass(), UI + "Invalid.fxml");
|
|
case "release-failure" -> with(listed, mode, FailingReleaseExample.class.getName(), listed.viewResource());
|
|
case "wrong-context" -> with(listed, mode, WrongExample.class.getName(), listed.viewResource());
|
|
case "missing-resource" -> with(listed, mode, listed.providerClass(), "missing/Book.fxml");
|
|
case "closed-resource" -> with(listed, mode, listed.providerClass(), "fixture/spring/app/Closed.fxml");
|
|
default -> with(listed, mode, listed.providerClass(), listed.viewResource());
|
|
};
|
|
try (var target = TargetPreview.open(selected, loader, Duration.ofSeconds(20))) {
|
|
if (mode.equals("cancel")) {
|
|
check(fx(() -> BookExamples.lastService().liveCount()) == 7, "waited for live View");
|
|
target.cancel();
|
|
}
|
|
switch (mode) {
|
|
case "release-failure" -> {
|
|
target.ready().toCompletableFuture().get(20, TimeUnit.SECONDS);
|
|
target.close();
|
|
try {
|
|
target.cleanup().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
|
throw new AssertionError("Expected View cleanup failure");
|
|
} catch (ExecutionException failure) {
|
|
check(((PreviewFailure) failure.getCause()).code() == PreviewFailure.Code.CLEANUP,
|
|
"failed release reported");
|
|
}
|
|
}
|
|
case "book", "card" -> {
|
|
target.ready().toCompletableFuture().get(20, TimeUnit.SECONDS);
|
|
fx(() -> {
|
|
Parent root = target.loadedView().getRoot();
|
|
if (mode.equals("book")) {
|
|
checkBook(root, "Preview contact book", "Preview service");
|
|
name(root).setText("Edited in preview");
|
|
check(((Label) root.lookup("#convertedName")).getText().equals("[Edited in preview]"),
|
|
"live binding in preview");
|
|
} else {
|
|
check(((TextField) root.lookup("#entryName")).getText().equals("Preview entry"),
|
|
"standalone fx:root Component loaded");
|
|
}
|
|
check(target.render(420, 240).length == 420 * 240 * 4, "target rendered");
|
|
return null;
|
|
});
|
|
}
|
|
default -> expectFailure(target, mode);
|
|
}
|
|
}
|
|
// The call to close() from the test thread posts to FX. The next FX action follows it.
|
|
if (java.util.Set.of("book", "invalid", "readiness", "cancel", "missing-resource", "closed-resource",
|
|
"release-failure").contains(mode))
|
|
fx(() -> { checkClosed(BookExamples.lastService()); return null; });
|
|
}
|
|
|
|
private static ExampleDescriptor with(ExampleDescriptor d, String mode, String provider, String resource) {
|
|
return new ExampleDescriptor(mode, d.label(), d.modelType(), provider, resource, d.componentPackage());
|
|
}
|
|
|
|
private static void expectFailure(TargetPreview target, String mode) throws Exception {
|
|
var expected = switch (mode) {
|
|
case "cancel" -> PreviewFailure.Code.CANCELLED;
|
|
case "readiness" -> PreviewFailure.Code.READINESS;
|
|
case "invalid" -> PreviewFailure.Code.LOAD;
|
|
case "closed-resource" -> PreviewFailure.Code.MODULE_ACCESS;
|
|
case "missing-resource" -> PreviewFailure.Code.RESOURCE;
|
|
default -> PreviewFailure.Code.CONTEXT_TYPE;
|
|
};
|
|
try {
|
|
target.ready().toCompletableFuture().get(20, TimeUnit.SECONDS);
|
|
throw new AssertionError("Expected " + expected);
|
|
} catch (ExecutionException failure) {
|
|
var diagnostic = (PreviewFailure) failure.getCause();
|
|
check(diagnostic.code() == expected, "expected " + expected + ", got " + diagnostic);
|
|
}
|
|
target.cleanup().toCompletableFuture().get(5, TimeUnit.SECONDS);
|
|
}
|
|
|
|
private static void checkBook(Parent root, String text, String service) {
|
|
check(name(root).getText().equals(text) && name(root).getPromptText().equals(service), "FXML and injected service");
|
|
check(((Label) root.lookup("#convertedName")).getText().equals("[" + text + "]"), "Spring Converter");
|
|
var panel = (Parent) root.lookup("#featured");
|
|
check(((TextField) panel.lookup("#entryName")).getText().equals("Nested entry"), "nested Component");
|
|
check(((VBox) root.lookup("#entries")).getChildren().size() == 2, "Repeat children");
|
|
check(root.lookup("#entriesTable") != null, "Table loaded");
|
|
}
|
|
|
|
private static TextField name(Parent root) { return (TextField) root.lookup("#bookName"); }
|
|
|
|
private static void checkClosed(FixtureService service) {
|
|
check(service.liveAtShutdown() == 0 && service.shutdownCount() == 1,
|
|
"context closed once, after all View objects were destroyed");
|
|
}
|
|
|
|
private static <T> T fx(Callable<T> action) throws Exception {
|
|
var task = new FutureTask<T>(action);
|
|
Platform.runLater(task);
|
|
return task.get(20, TimeUnit.SECONDS);
|
|
}
|
|
|
|
private static void check(boolean ok, String message) {
|
|
if (!ok) throw new AssertionError(message);
|
|
assertions++;
|
|
}
|
|
|
|
public static final class FailingReleaseExample implements PreviewProvider<Book, ViewDependencies> {
|
|
@Override
|
|
public Class<Book> modelType() { return Book.class; }
|
|
|
|
@Override
|
|
public PreviewSession<Book, ViewDependencies> open(String id) {
|
|
var delegate = new BookExamples().open(id);
|
|
var dependencies = delegate.context();
|
|
return new PreviewSession<>() {
|
|
public Book model() { return delegate.model(); }
|
|
|
|
public ViewDependencies context() {
|
|
return new ViewDependencies() {
|
|
public boolean isComponent(Class<?> type) { return dependencies.isComponent(type); }
|
|
|
|
public <T> Optional<T> findService(Class<T> type) { return dependencies.findService(type); }
|
|
|
|
public <T> ViewObject<T> createViewObject(Class<T> type) {
|
|
var acquired = dependencies.createViewObject(type);
|
|
return new ViewObject<>(acquired.instance(), () -> {
|
|
acquired.release().run();
|
|
if (type == BookController.class) throw new IllegalStateException("release failed");
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
public void close() throws Exception { delegate.close(); }
|
|
};
|
|
}
|
|
}
|
|
|
|
public static final class WrongExample implements PreviewProvider<Book, Object> {
|
|
@Override
|
|
public Class<Book> modelType() { return Book.class; }
|
|
|
|
@Override
|
|
public PreviewSession<Book, Object> open(String id) {
|
|
return new PreviewSession<>() {
|
|
public Book model() { return new Book(); }
|
|
public Object context() { return new Object(); }
|
|
public void close() {}
|
|
};
|
|
}
|
|
}
|
|
}
|