Merge Spring preview initial value fix

This commit is contained in:
2026-11-26 17:26:52 +01:00
2 changed files with 77 additions and 31 deletions
+2
View File
@@ -70,6 +70,8 @@ Components, Repeat and Table release, cleanup after load/readiness failure
and cancellation, module resource diagnostics, and the required runtime and cancellation, module resource diagnostics, and the required runtime
diagnostic against FX 1.0.6. The host checks use disposably copied project diagnostic against FX 1.0.6. The host checks use disposably copied project
files and exercise the canvas, form preview and interactive preview. 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 Each Spring context verifies that the running application or preview
cannot load Neitzel's `ApplicationContext` or Reflections and that cannot load Neitzel's `ApplicationContext` or Reflections and that
+75 -31
View File
@@ -2,16 +2,20 @@ package qualification;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.concurrent.FutureTask; import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.Window; import javafx.stage.Window;
import javafx.stage.WindowEvent;
/** Test-only observer of the actual interactive window, inside the target JVM. */ /** Test-only observer of the actual interactive window, inside the target JVM. */
public final class SpringTargetProbe { public final class SpringTargetProbe {
private static final String PREVIEW_TITLE = "Neitzel preview — ";
public static void premain(String output) { public static void premain(String output) {
var command = System.getProperty("sun.java.command", "").split(" ", 2)[0]; var command = System.getProperty("sun.java.command", "").split(" ", 2)[0];
if (command.endsWith(".WorkerMain")) if (command.endsWith(".WorkerMain"))
@@ -22,45 +26,85 @@ public final class SpringTargetProbe {
private static void observe(Path output) { private static void observe(Path output) {
try { try {
while (true) { while (true) {
Thread.sleep(100);
var inspect = new FutureTask<>(() -> inspect(output));
try { try {
Platform.runLater(inspect); Platform.runLater(() -> watchOpening(output));
return;
} catch (IllegalStateException toolkitStarting) { } catch (IllegalStateException toolkitStarting) {
continue; Thread.sleep(10);
} }
if (inspect.get(10, TimeUnit.SECONDS)) return;
} }
} catch (InterruptedException stopped) { } catch (InterruptedException stopped) {
Thread.currentThread().interrupt(); 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 { private static Optional<Stage> previewWindow() {
var stage = Window.getWindows().stream() return Window.getWindows().stream()
.filter(w -> w instanceof Stage window && window.isShowing() && window.getOpacity() > 0 .filter(Stage.class::isInstance)
&& window.getTitle() != null && window.getTitle().startsWith("Neitzel preview")) .map(Stage.class::cast)
.filter(stage -> stage.getOpacity() > 0 && stage.getTitle() != null
&& stage.getTitle().startsWith(PREVIEW_TITLE))
.findFirst(); .findFirst();
if (stage.isEmpty()) return false; }
String attempt = Files.readString(output.resolve("spring-attempt"));
boolean book = attempt.startsWith("book-"); private static void watchOpening(Path output) {
var scene = stage.get().getScene(); var windows = Window.getWindows();
var field = (TextField) scene.lookup(book ? "#bookName" : "#entryName"); if (previewWindow().isPresent()) {
String expected = book ? "Preview contact book" : "Preview entry"; reportFailure(output, new AssertionError("Probe attached after the preview opened"));
if (field == null || !expected.equals(field.getText())) return;
throw new AssertionError("Expected fresh " + expected + " in " + attempt); }
field.setText("Edited in preview"); windows.addListener(new InvalidationListener() {
if (book && !"[Edited in preview]".equals(((Label) scene.lookup("#convertedName")).getText())) @Override
throw new AssertionError("Spring Converter did not update the live interactive View"); public void invalidated(Observable ignored) {
Files.writeString(output.resolve(attempt + ".txt"), expected); previewWindow().ifPresent(stage -> {
return true; 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();
}
} }
} }
} }