package qualification; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import javafx.application.Platform; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.stage.Stage; import javafx.stage.Window; /** Test-only observer of the actual interactive window, inside the target JVM. */ public final class SpringTargetProbe { public static void premain(String output) { var command = System.getProperty("sun.java.command", "").split(" ", 2)[0]; if (command.endsWith(".WorkerMain")) Thread.ofPlatform().daemon().name("spring-preview-probe").start(() -> Observer.observe(Path.of(output))); } private static class Observer { private static void observe(Path output) { try { while (true) { Thread.sleep(100); var inspect = new FutureTask<>(() -> inspect(output)); try { Platform.runLater(inspect); } catch (IllegalStateException toolkitStarting) { continue; } 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")) .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; } } }