From 815372e0b1f2cc9d955e4e2822d76249d75c9d5f Mon Sep 17 00:00:00 2001 From: Konrad Neitzel Date: Thu, 26 Nov 2026 17:26:46 +0100 Subject: [PATCH] Stabilize Spring preview initial value check --- integration-fixtures/spring-di/README.md | 2 + integration-tests/idea/SpringTargetProbe.java | 106 +++++++++++++----- 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/integration-fixtures/spring-di/README.md b/integration-fixtures/spring-di/README.md index 5edf0e9..a7030ba 100644 --- a/integration-fixtures/spring-di/README.md +++ b/integration-fixtures/spring-di/README.md @@ -70,6 +70,8 @@ Components, Repeat and Table release, cleanup after load/readiness failure and cancellation, module resource diagnostics, and the required runtime diagnostic against FX 1.0.6. The host checks use disposably copied project files and exercise the canvas, form preview and interactive preview. +The interactive check captures the initial field value before the native +window becomes visible and exercises editing in the opened window. Each Spring context verifies that the running application or preview cannot load Neitzel's `ApplicationContext` or Reflections and that diff --git a/integration-tests/idea/SpringTargetProbe.java b/integration-tests/idea/SpringTargetProbe.java index 79b6175..9cd743c 100644 --- a/integration-tests/idea/SpringTargetProbe.java +++ b/integration-tests/idea/SpringTargetProbe.java @@ -2,16 +2,20 @@ package qualification; import java.nio.file.Files; import java.nio.file.Path; -import java.util.concurrent.FutureTask; -import java.util.concurrent.TimeUnit; +import java.util.Optional; import javafx.application.Platform; +import javafx.beans.InvalidationListener; +import javafx.beans.Observable; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.stage.Stage; import javafx.stage.Window; +import javafx.stage.WindowEvent; /** Test-only observer of the actual interactive window, inside the target JVM. */ public final class SpringTargetProbe { + private static final String PREVIEW_TITLE = "Neitzel preview — "; + public static void premain(String output) { var command = System.getProperty("sun.java.command", "").split(" ", 2)[0]; if (command.endsWith(".WorkerMain")) @@ -22,45 +26,85 @@ public final class SpringTargetProbe { private static void observe(Path output) { try { while (true) { - Thread.sleep(100); - var inspect = new FutureTask<>(() -> inspect(output)); try { - Platform.runLater(inspect); + Platform.runLater(() -> watchOpening(output)); + return; } catch (IllegalStateException toolkitStarting) { - continue; + Thread.sleep(10); } - if (inspect.get(10, TimeUnit.SECONDS)) return; } } catch (InterruptedException stopped) { Thread.currentThread().interrupt(); - } catch (Throwable error) { - try { - Files.writeString(output.resolve("target-error.txt"), error.toString()); - } catch (Exception writeFailure) { - error.addSuppressed(writeFailure); - error.printStackTrace(); - } } } - private static boolean inspect(Path output) throws Exception { - var stage = Window.getWindows().stream() - .filter(w -> w instanceof Stage window && window.isShowing() && window.getOpacity() > 0 - && window.getTitle() != null && window.getTitle().startsWith("Neitzel preview")) + private static Optional previewWindow() { + return Window.getWindows().stream() + .filter(Stage.class::isInstance) + .map(Stage.class::cast) + .filter(stage -> stage.getOpacity() > 0 && stage.getTitle() != null + && stage.getTitle().startsWith(PREVIEW_TITLE)) .findFirst(); - if (stage.isEmpty()) return false; - String attempt = Files.readString(output.resolve("spring-attempt")); - boolean book = attempt.startsWith("book-"); - var scene = stage.get().getScene(); - var field = (TextField) scene.lookup(book ? "#bookName" : "#entryName"); - String expected = book ? "Preview contact book" : "Preview entry"; - if (field == null || !expected.equals(field.getText())) - throw new AssertionError("Expected fresh " + expected + " in " + attempt); - field.setText("Edited in preview"); - if (book && !"[Edited in preview]".equals(((Label) scene.lookup("#convertedName")).getText())) - throw new AssertionError("Spring Converter did not update the live interactive View"); - Files.writeString(output.resolve(attempt + ".txt"), expected); - return true; + } + + private static void watchOpening(Path output) { + var windows = Window.getWindows(); + if (previewWindow().isPresent()) { + reportFailure(output, new AssertionError("Probe attached after the preview opened")); + return; + } + windows.addListener(new InvalidationListener() { + @Override + public void invalidated(Observable ignored) { + previewWindow().ifPresent(stage -> { + windows.removeListener(this); + checkInitialValue(stage, output); + }); + } + }); + } + + private static void checkInitialValue(Stage stage, Path output) { + try { + String attempt = Files.readString(output.resolve("spring-attempt")); + boolean book = attempt.startsWith("book-"); + String example = book ? "book" : "card"; + if (!stage.getTitle().startsWith(PREVIEW_TITLE + example + " — ")) + throw new AssertionError("Unexpected preview window in " + attempt + ": " + stage.getTitle()); + if (!(stage.getScene().lookup(book ? "#bookName" : "#entryName") instanceof TextField field)) + throw new AssertionError("Missing input field in " + attempt); + // JavaFX adds the window before making its native peer visible. + String initialValue = field.getText(); + String expected = book ? "Preview contact book" : "Preview entry"; + if (!expected.equals(initialValue)) + throw new AssertionError("Expected fresh " + expected + " in " + attempt); + stage.addEventHandler(WindowEvent.WINDOW_SHOWN, + event -> Platform.runLater(() -> checkEditing(stage, field, book, output, attempt, initialValue))); + } catch (Exception | AssertionError failure) { + reportFailure(output, failure); + } + } + + private static void checkEditing(Stage stage, TextField field, boolean book, Path output, + String attempt, String initialValue) { + try { + if (!stage.isShowing()) throw new AssertionError("Preview closed before the edit check in " + attempt); + field.setText("Edited in preview"); + if (book && !"[Edited in preview]".equals(((Label) stage.getScene().lookup("#convertedName")).getText())) + throw new AssertionError("Spring Converter did not update the live interactive View"); + Files.writeString(output.resolve(attempt + ".txt"), initialValue); + } catch (Exception | AssertionError failure) { + reportFailure(output, failure); + } + } + + private static void reportFailure(Path output, Throwable failure) { + try { + Files.writeString(output.resolve("target-error.txt"), failure.toString()); + } catch (Exception writeFailure) { + failure.addSuppressed(writeFailure); + failure.printStackTrace(); + } } } }