Discover custom ViewModels and validate Computed bindings in shared hosts

This commit is contained in:
2026-11-13 13:27:23 +01:00
parent 5a208d2336
commit 99d93a11fc
46 changed files with 1397 additions and 64 deletions
@@ -66,7 +66,8 @@ class BindingDiscoveryTest {
throws Exception {
var metadata = BindingDiscovery.inspect(fxml(), "name", "both", getClass().getClassLoader());
assertEquals(
Set.of("home", "home.street", "name", "rank", "visits"),
Set.of(
"home", "home.street", "name", "rank", "visits", "latestMessage", "home.latestMessage"),
metadata.attributes().stream()
.map(a -> a.path())
.collect(java.util.stream.Collectors.toSet()));
@@ -0,0 +1,329 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fx.view.*;
import de.neitzel.fxdesigner.protocol.BindingMetadata;
import java.util.*;
import javafx.beans.property.*;
import org.junit.jupiter.api.Test;
class ViewModelComputedTest {
public interface A {}
public interface B {}
public static class Model implements A, B {
public String getName() {
throw new AssertionError("getter invoked");
}
public void setName(String s) {}
public int getLatestMessage() {
return 0;
}
public void setLatestMessage(int i) {}
}
public static class Custom extends ViewModel {
static {
System.setProperty("vm.metadata.initialized", "yes");
}
public Custom(Model m) {
super(m);
throw new AssertionError("ViewModel constructed");
}
public StringProperty badgeProperty() {
throw new AssertionError("property invoked");
}
public IntegerProperty nameProperty() {
throw new AssertionError("shadow invoked");
}
public IntegerProperty latestMessageProperty() {
throw new AssertionError("reserved invoked");
}
public ReadOnlyStringProperty readOnlyProperty() {
throw new AssertionError("readonly invoked");
}
}
public static class Replacement {
static {
System.setProperty("replacement.metadata.initialized", "yes");
}
public Replacement(Model m) {
throw new AssertionError("replacement constructed");
}
public IntegerProperty nameProperty() {
throw new AssertionError("property invoked");
}
public ObjectProperty<Model> homeProperty() {
throw new AssertionError("property invoked");
}
public StringProperty badgeProperty() {
throw new AssertionError("property invoked");
}
}
public static class Ambiguous {
public Ambiguous(A a) {}
public Ambiguous(B b) {}
}
@ViewModelClass(Ambiguous.class)
public static class AmbiguousController {}
public static class WrongConstructor {
public WrongConstructor(String s) {}
public StringProperty badgeProperty() {
return null;
}
}
public static class Dynamic extends Custom {
public Dynamic(Model m) {
super(m);
}
@Override
public Property<?> property(String name) {
throw new AssertionError("dynamic invoked");
}
}
public static class ParentController {
public String inherited() {
throw new AssertionError("method invoked");
}
}
@ViewModelClass(Custom.class)
public static class Controller extends ParentController {
static {
System.setProperty("controller.metadata.initialized", "yes");
}
public Controller() {
throw new AssertionError("Controller constructed");
}
public String good() {
throw new AssertionError("method invoked");
}
public Integer wrong() {
throw new AssertionError("method invoked");
}
public Object unknown() {
throw new AssertionError("method invoked");
}
public void action() {
throw new AssertionError("method invoked");
}
public String argument(String s) {
throw new AssertionError("method invoked");
}
@javafx.fxml.FXML
private String hidden() {
throw new AssertionError("method invoked");
}
@Override
public String toString() {
throw new AssertionError("method invoked");
}
}
@ViewModelClass(Replacement.class)
public static class ReplacementController {}
@ViewModelClass(WrongConstructor.class)
public static class WrongController {}
@ViewModelClass(Dynamic.class)
public static class DynamicController {}
@ViewModelClass(Replacement.class)
public static class Component extends javafx.scene.layout.VBox {
static {
System.setProperty("component.metadata.initialized", "yes");
}
public StringProperty captionProperty() {
throw new AssertionError("property invoked");
}
public String caption() {
throw new AssertionError("component method invoked");
}
}
private String xml(Class<?> controller, String binding) {
return "<?neitzel-model "
+ Model.class.getName()
+ "?><?import javafx.scene.control.TextField?><TextField"
+ " xmlns:fx='http://javafx.com/fxml/1' fx:id='input'"
+ (controller == null ? "" : " fx:controller='" + controller.getName() + "'")
+ "><fx:define><de.neitzel.fx.view.BindingList>"
+ binding
+ "</de.neitzel.fx.view.BindingList></fx:define></TextField>";
}
private String computed(String method) {
return "<de.neitzel.fx.view.ComputedBinding control='input' property='text' method='"
+ method
+ "'/>";
}
private String direct(String path) {
return "<de.neitzel.fx.view.DirectBinding control='input' property='text' path='"
+ path
+ "' direction='view'/>";
}
private BindingMetadata inspect(String xml) throws Exception {
return BindingDiscovery.inspect(
xml, "badge", "view", "input", "text", getClass().getClassLoader());
}
@Test
void subclassUsesRuntimePrecedenceAndNeverInitializesApplicationClasses() throws Exception {
var result =
inspect(
xml(
Controller.class,
direct("badge")
+ computed("#good").replace("property='text'", "property='promptText'")));
var types = new HashMap<String, String>();
result.attributes().forEach(a -> types.put(a.path(), a.type()));
assertEquals(String.class.getName(), types.get("badge"));
assertEquals(String.class.getName(), types.get("name"));
assertEquals(String.class.getName(), types.get("latestMessage"));
assertFalse(types.containsKey("readOnly"));
assertEquals(Custom.class.getName(), result.viewModel());
assertTrue(result.problems().isEmpty(), result.problems().toString());
for (String key : List.of("vm.metadata.initialized", "controller.metadata.initialized"))
assertNull(System.getProperty(key));
}
@Test
void replacementDoesNotInheritModelAttributesOrDottedTraversal() throws Exception {
var result = inspect(xml(ReplacementController.class, direct("home.name")));
assertEquals(
Set.of("name", "home", "badge"),
result.attributes().stream()
.map(a -> a.path())
.collect(java.util.stream.Collectors.toSet()));
assertEquals(
Number.class.getName(),
result.attributes().stream()
.filter(a -> a.path().equals("name"))
.findFirst()
.orElseThrow()
.type());
assertTrue(result.problems().stream().anyMatch(p -> p.severity().equals("ERROR")));
assertNull(System.getProperty("replacement.metadata.initialized"));
}
@Test
void computedSignaturesAndTargetsAreCheckedWithoutInvokingMethods() throws Exception {
var valid = inspect(xml(Controller.class, computed("#inherited")));
assertTrue(valid.problems().isEmpty());
assertTrue(
valid.computed().stream()
.anyMatch(m -> m.method().equals("#good") && m.status().equals("COMPATIBLE")));
assertFalse(
valid.computed().stream()
.anyMatch(
m -> Set.of("#action", "#toString", "#argument", "#hidden").contains(m.method())));
for (String method :
List.of("#wrong", "#missing", "#hidden", "#argument", "#action", "#toString"))
assertTrue(
inspect(xml(Controller.class, computed(method))).problems().stream()
.anyMatch(p -> p.severity().equals("ERROR")),
method);
assertEquals(
"WARNING",
inspect(xml(Controller.class, computed("#unknown"))).problems().getFirst().severity());
assertTrue(
inspect(xml(null, computed("#good"))).problems().stream()
.anyMatch(p -> p.message().contains("No Controller")));
assertTrue(
inspect(
xml(Controller.class, computed("#wrong"))
.replaceFirst("<\\?neitzel-model[^>]*>", ""))
.problems()
.stream()
.anyMatch(p -> p.message().contains("incompatible")));
assertTrue(
inspect(
xml(Controller.class, computed("#good"))
.replace("property='text'", "property='width'"))
.problems()
.stream()
.anyMatch(p -> p.message().contains("writable")));
assertNull(System.getProperty("controller.metadata.initialized"));
}
@Test
void constructionAndDynamicResolutionAreReported() throws Exception {
assertTrue(
inspect(xml(AmbiguousController.class, "")).problems().stream()
.anyMatch(p -> p.message().contains("Ambiguous ViewModel constructor")));
assertTrue(
inspect(xml(WrongController.class, direct("badge"))).problems().stream()
.anyMatch(p -> p.message().contains("No constructor")));
assertTrue(
inspect(xml(DynamicController.class, direct("badge"))).problems().stream()
.anyMatch(p -> p.severity().equals("WARNING")));
}
@Test
void componentResourceSelectsItsOwnControllerAndViewModel() throws Exception {
String xml =
"<?neitzel-model "
+ Model.class.getName()
+ "?><fx:root xmlns:fx='http://javafx.com/fxml/1' fx:id='root'"
+ " type='javafx.scene.layout.VBox'/>";
var metadata =
BindingDiscovery.inspect(
xml,
"badge",
"view",
"",
"",
Component.class.getName().replace('.', '/') + ".fxml",
getClass().getClassLoader());
assertTrue(
metadata.targets().stream()
.anyMatch(t -> t.control().equals("root") && t.property().equals("caption")),
"Actual Component root properties missing");
assertEquals(Component.class.getName(), metadata.controller());
assertEquals(Replacement.class.getName(), metadata.viewModel());
assertTrue(metadata.computed().stream().anyMatch(m -> m.method().equals("#caption")));
assertNull(System.getProperty("component.metadata.initialized"));
assertTrue(inspect(xml).diagnostics().stream().anyMatch(d -> d.contains("captured FXML")));
var unresolved = BindingDiscovery.inspect(xml, "name", "view", getClass().getClassLoader());
assertTrue(
unresolved.targets().stream()
.anyMatch(t -> t.property().equals("style") && t.status().equals("UNRESOLVED")),
"Unknown Component ViewModel was certified as generic");
}
}