Add shared source-preserving Computed binding inspector

This commit is contained in:
2026-11-13 13:42:19 +01:00
parent 99d93a11fc
commit 510ce35b60
12 changed files with 639 additions and 224 deletions
@@ -0,0 +1,240 @@
package de.neitzel.fxdesigner.kit;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.contracts.*;
import java.net.URI;
import java.util.*;
import org.junit.jupiter.api.Test;
class BindingDocumentTest {
private static final String P = "de.neitzel.fx.view.";
private static final BindingDocument.Values VALUE =
new BindingDocument.Values("label", "text", "name", "view", "");
private String apply(String text, List<EditBatch.Replacement> edits) throws Exception {
String result =
new EditBatch(7, "binding", edits)
.applyTo(new DocumentSnapshot(URI.create("file:///test.fxml"), 7, text, true, false));
new BindingDocument(result);
return result;
}
@Test
void createsRequiredContainersWithoutSerializingTheDocument() throws Exception {
for (String root :
List.of(
"<VBox/>",
"<VBox xmlns:fx='urn:other'/>",
"<VBox><!-- untouched -->\r\n</VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define/></VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define><"
+ P
+ "BindingList/></f:define></VBox>")) {
String text = "<?neitzel-model app.Person?>\n" + root;
var doc = new BindingDocument(text);
String result = apply(text, doc.write(null, VALUE, false));
var created = new BindingDocument(result);
assertEquals(1, created.entries.size());
assertEquals(VALUE, created.entries.getFirst().values());
assertTrue(result.startsWith("<?neitzel-model app.Person?>\n"));
assertFalse(result.contains("<?import"));
if (text.contains("untouched")) assertTrue(result.contains("<!-- untouched -->\r\n"));
if (text.contains("urn:other"))
assertTrue(result.contains("xmlns:fx1=\"http://javafx.com/fxml/1\""));
}
}
private String existing() {
return "<?neitzel-model app.Person?>\n"
+ "<?import de.neitzel.fx.view.*?>\n"
+ "<VBox xmlns:fx='http://javafx.com/fxml/1'><!-- before"
+ " --><fx:define><BindingList><!-- preserve --><DirectBinding control = 'label'"
+ " property='text' path = 'name' direction='view' converter='app.Old'"
+ " custom='untouched'/><!-- after --><app.Unknown"
+ " value='keep'/></BindingList></fx:define><Label fx:id='label'/></VBox>";
}
@Test
void changesOnlyRequestedAttributeRangesAndRetainsUnknownContent() throws Exception {
String text = existing();
var doc = new BindingDocument(text);
int index = doc.entries.getFirst().index();
var value = new BindingDocument.Values("label", "text", "home.street", "both", "app.New");
String updated = apply(text, doc.write(index, value, false));
assertEquals(
text.replace("path = 'name'", "path = 'home.street'")
.replace("direction='view'", "direction='both'")
.replace("converter='app.Old'", "converter='app.New'"),
updated);
assertTrue(new BindingDocument(updated).write(index, value, false).isEmpty());
String escaped =
apply(
text,
doc.write(
index, new BindingDocument.Values("label", "text", "a'&<", "view", ""), false));
assertEquals("a'&<", new BindingDocument(escaped).entries.getFirst().values().path());
assertFalse(escaped.contains("converter='app.Old'"));
assertFalse(new BindingDocument(escaped).errors().isEmpty());
}
@Test
void removesOnlyTheSelectedElementIncludingPairedTags() throws Exception {
String text =
existing()
.replace("custom='untouched'/>", "custom='untouched'><!-- inner --></DirectBinding>");
var doc = new BindingDocument(text);
var tag = doc.source.tags.get(doc.entries.getFirst().index());
String result = apply(text, doc.remove(doc.entries.getFirst().index()));
assertEquals(text.substring(0, tag.start()) + text.substring(tag.end()), result);
assertTrue(result.contains("<!-- preserve --><!-- after -->"));
assertTrue(result.contains("app.Unknown"));
}
@Test
void replacesAllConflictingDirectAndComputedBindingsAtomically() throws Exception {
String text =
existing()
.replace(
"<!-- after -->",
"<ComputedBinding control='label' property='text' method='#caption'/><!-- after"
+ " -->");
var doc = new BindingDocument(text);
assertTrue(doc.errors().stream().anyMatch(s -> s.contains("Duplicate")));
assertThrows(BindingDocument.Conflict.class, () -> doc.write(null, VALUE, false));
String replaced = apply(text, doc.write(null, VALUE, true));
var parsed = new BindingDocument(replaced);
assertEquals(1, parsed.entries.size());
assertFalse(replaced.contains("<ComputedBinding"));
assertTrue(parsed.errors().isEmpty());
assertTrue(replaced.contains("app.Unknown"));
}
@Test
void supportsExplicitElementsWrapperAndRefusesNestedPropertyEdits() throws Exception {
String text =
"<?import de.neitzel.fx.view.*?><VBox"
+ " xmlns:fx='http://javafx.com/fxml/1'><fx:define><BindingList><elements><DirectBinding"
+ " control='label'><property>text</property></DirectBinding></elements></BindingList></fx:define></VBox>";
var doc = new BindingDocument(text);
assertFalse(doc.entries.getFirst().editable());
assertThrows(
IllegalArgumentException.class,
() -> doc.write(doc.entries.getFirst().index(), VALUE, false));
String result =
apply(
text,
doc.write(
null, new BindingDocument.Values("other", "text", "name", "view", ""), false));
assertEquals(2, new BindingDocument(result).entries.size());
}
@Test
void usesOnlyTheLibraryDeclarationsAndRejectsMalformedXml() throws Exception {
String text =
existing().replace("<?import de.neitzel.fx.view.*?>", "<?import app.DirectBinding?>");
assertTrue(new BindingDocument(text).entries.isEmpty());
assertThrows(Exception.class, () -> new BindingDocument("<VBox>"));
assertThrows(
Exception.class, () -> new BindingDocument("<!DOCTYPE x SYSTEM 'file:///etc/passwd'><x/>"));
}
private BindingDocument computed(String text) throws Exception {
return new BindingDocument(text, BindingDocument.Kind.COMPUTED);
}
@Test
void computedCreatesContainersAndRoundTripsEscapedMethods() throws Exception {
for (String root :
List.of(
"<VBox/>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define/></VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define><"
+ P
+ "BindingList><elements/></"
+ P
+ "BindingList></f:define></VBox>")) {
String text = "<?neitzel-model app.Person?>\r\n" + root;
var value = BindingDocument.Values.computed("label", "text", "#a'&<");
String result = apply(text, computed(text).write(null, value, false));
assertEquals(value, computed(result).entries.getFirst().values());
assertTrue(result.startsWith("<?neitzel-model app.Person?>\r\n"));
assertFalse(result.contains(" direction="));
assertFalse(result.contains(" path="));
assertTrue(computed(result).errors().stream().anyMatch(e -> e.contains("Controller method")));
}
}
@Test
void computedChangesOnlyItsFieldsAndRemovesPairedDeclaration() throws Exception {
String text =
existing()
.replace(
"<app.Unknown",
"<ComputedBinding control = 'other' property='text' method = '#old'"
+ " custom='keep'><!-- inner --></ComputedBinding><Label"
+ " fx:id='other'/><app.Unknown");
var doc = computed(text);
int index = doc.entries.getFirst().index();
var value = BindingDocument.Values.computed("label", "accessibleText", "greeting");
String changed = apply(text, doc.write(index, value, false));
assertEquals(
text.replace("control = 'other'", "control = 'label'")
.replace("property='text' method", "property='accessibleText' method")
.replace("method = '#old'", "method = 'greeting'"),
changed);
assertTrue(computed(changed).write(index, value, false).isEmpty());
String removed = apply(changed, computed(changed).remove(index));
assertTrue(computed(removed).entries.isEmpty());
assertEquals(1, new BindingDocument(removed).entries.size());
assertTrue(removed.contains("<!-- preserve -->"));
}
@Test
void computedReplacementRemovesDirectAndComputedConflictsTogether() throws Exception {
String text =
existing()
.replace(
"<!-- after -->",
"<ComputedBinding control='label' property='text' method='#caption'/><!-- after"
+ " -->");
var doc = computed(text);
var value = BindingDocument.Values.computed("label", "text", "#greeting");
assertThrows(BindingDocument.Conflict.class, () -> doc.write(null, value, false));
String result = apply(text, doc.write(null, value, true));
assertTrue(new BindingDocument(result).entries.isEmpty());
assertEquals(1, computed(result).entries.size());
assertTrue(computed(result).errors().isEmpty());
assertTrue(result.contains("<!-- preserve -->"));
// Reverse replacement uses the same conflict policy.
assertThrows(
BindingDocument.Conflict.class,
() -> new BindingDocument(result).write(null, VALUE, false));
String direct = apply(result, new BindingDocument(result).write(null, VALUE, true));
assertTrue(computed(direct).entries.isEmpty());
assertEquals(1, new BindingDocument(direct).entries.size());
}
@Test
void computedNestedSyntaxIsVisibleRemovableButNotFlattened() throws Exception {
String text =
existing()
.replace(
"<app.Unknown",
"<ComputedBinding control='other'"
+ " property='text'><method>#caption</method></ComputedBinding><app.Unknown");
var doc = computed(text);
var entry = doc.entries.getFirst();
assertFalse(entry.editable());
assertThrows(
IllegalArgumentException.class,
() ->
doc.write(
entry.index(), BindingDocument.Values.computed("other", "text", "#new"), false));
assertTrue(computed(apply(text, doc.remove(entry.index()))).entries.isEmpty());
assertTrue(
computed(text.replace("<?import de.neitzel.fx.view.*?>", "<?import app.ComputedBinding?>"))
.entries
.isEmpty());
}
}
@@ -1,146 +0,0 @@
package de.neitzel.fxdesigner.kit;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.contracts.*;
import java.net.URI;
import java.util.*;
import org.junit.jupiter.api.Test;
class DirectBindingDocumentTest {
private static final String P = "de.neitzel.fx.view.";
private static final DirectBindingDocument.Values VALUE =
new DirectBindingDocument.Values("label", "text", "name", "view", "");
private String apply(String text, List<EditBatch.Replacement> edits) throws Exception {
String result =
new EditBatch(7, "binding", edits)
.applyTo(new DocumentSnapshot(URI.create("file:///test.fxml"), 7, text, true, false));
new DirectBindingDocument(result);
return result;
}
@Test
void createsRequiredContainersWithoutSerializingTheDocument() throws Exception {
for (String root :
List.of(
"<VBox/>",
"<VBox xmlns:fx='urn:other'/>",
"<VBox><!-- untouched -->\r\n</VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define/></VBox>",
"<VBox xmlns:f='http://javafx.com/fxml/1'><f:define><"
+ P
+ "BindingList/></f:define></VBox>")) {
String text = "<?neitzel-model app.Person?>\n" + root;
var doc = new DirectBindingDocument(text);
String result = apply(text, doc.write(null, VALUE, false));
var created = new DirectBindingDocument(result);
assertEquals(1, created.entries.size());
assertEquals(VALUE, created.entries.getFirst().values());
assertTrue(result.startsWith("<?neitzel-model app.Person?>\n"));
assertFalse(result.contains("<?import"));
if (text.contains("untouched")) assertTrue(result.contains("<!-- untouched -->\r\n"));
if (text.contains("urn:other"))
assertTrue(result.contains("xmlns:fx1=\"http://javafx.com/fxml/1\""));
}
}
private String existing() {
return "<?neitzel-model app.Person?>\n"
+ "<?import de.neitzel.fx.view.*?>\n"
+ "<VBox xmlns:fx='http://javafx.com/fxml/1'><!-- before"
+ " --><fx:define><BindingList><!-- preserve --><DirectBinding control = 'label'"
+ " property='text' path = 'name' direction='view' converter='app.Old'"
+ " custom='untouched'/><!-- after --><app.Unknown"
+ " value='keep'/></BindingList></fx:define><Label fx:id='label'/></VBox>";
}
@Test
void changesOnlyRequestedAttributeRangesAndRetainsUnknownContent() throws Exception {
String text = existing();
var doc = new DirectBindingDocument(text);
int index = doc.entries.getFirst().index();
var value = new DirectBindingDocument.Values("label", "text", "home.street", "both", "app.New");
String updated = apply(text, doc.write(index, value, false));
assertEquals(
text.replace("path = 'name'", "path = 'home.street'")
.replace("direction='view'", "direction='both'")
.replace("converter='app.Old'", "converter='app.New'"),
updated);
assertTrue(new DirectBindingDocument(updated).write(index, value, false).isEmpty());
String escaped =
apply(
text,
doc.write(
index,
new DirectBindingDocument.Values("label", "text", "a'&<", "view", ""),
false));
assertEquals("a'&<", new DirectBindingDocument(escaped).entries.getFirst().values().path());
assertFalse(escaped.contains("converter='app.Old'"));
assertFalse(new DirectBindingDocument(escaped).errors().isEmpty());
}
@Test
void removesOnlyTheSelectedElementIncludingPairedTags() throws Exception {
String text =
existing()
.replace("custom='untouched'/>", "custom='untouched'><!-- inner --></DirectBinding>");
var doc = new DirectBindingDocument(text);
var tag = doc.source.tags.get(doc.entries.getFirst().index());
String result = apply(text, doc.remove(doc.entries.getFirst().index()));
assertEquals(text.substring(0, tag.start()) + text.substring(tag.end()), result);
assertTrue(result.contains("<!-- preserve --><!-- after -->"));
assertTrue(result.contains("app.Unknown"));
}
@Test
void replacesAllConflictingDirectAndComputedBindingsAtomically() throws Exception {
String text =
existing()
.replace(
"<!-- after -->",
"<ComputedBinding control='label' property='text' method='#caption'/><!-- after"
+ " -->");
var doc = new DirectBindingDocument(text);
assertTrue(doc.errors().stream().anyMatch(s -> s.contains("Duplicate")));
assertThrows(DirectBindingDocument.Conflict.class, () -> doc.write(null, VALUE, false));
String replaced = apply(text, doc.write(null, VALUE, true));
var parsed = new DirectBindingDocument(replaced);
assertEquals(1, parsed.entries.size());
assertFalse(replaced.contains("<ComputedBinding"));
assertTrue(parsed.errors().isEmpty());
assertTrue(replaced.contains("app.Unknown"));
}
@Test
void supportsExplicitElementsWrapperAndRefusesNestedPropertyEdits() throws Exception {
String text =
"<?import de.neitzel.fx.view.*?><VBox"
+ " xmlns:fx='http://javafx.com/fxml/1'><fx:define><BindingList><elements><DirectBinding"
+ " control='label'><property>text</property></DirectBinding></elements></BindingList></fx:define></VBox>";
var doc = new DirectBindingDocument(text);
assertFalse(doc.entries.getFirst().editable());
assertThrows(
IllegalArgumentException.class,
() -> doc.write(doc.entries.getFirst().index(), VALUE, false));
String result =
apply(
text,
doc.write(
null,
new DirectBindingDocument.Values("other", "text", "name", "view", ""),
false));
assertEquals(2, new DirectBindingDocument(result).entries.size());
}
@Test
void usesOnlyTheLibraryDeclarationsAndRejectsMalformedXml() throws Exception {
String text =
existing().replace("<?import de.neitzel.fx.view.*?>", "<?import app.DirectBinding?>");
assertTrue(new DirectBindingDocument(text).entries.isEmpty());
assertThrows(Exception.class, () -> new DirectBindingDocument("<VBox>"));
assertThrows(
Exception.class,
() -> new DirectBindingDocument("<!DOCTYPE x SYSTEM 'file:///etc/passwd'><x/>"));
}
}
@@ -482,6 +482,168 @@ public final class ProjectionHostChecks {
await(() -> fx(() -> source(pane).getText().equals(full)));
}
private static void computedField(DesignerPane pane, String field, String value)
throws Exception {
fx(
() -> {
((TextField) pane.lookup("#computed-" + field)).setText(value);
return null;
});
}
private static void selectComputed(DesignerPane pane, String control) throws Exception {
fx(
() -> {
var list = (ListView<Object>) pane.lookup("#computed-bindings");
list.getSelectionModel()
.select(
list.getItems().stream()
.filter(e -> e.toString().startsWith(control + "."))
.findFirst()
.orElseThrow());
return null;
});
}
private static void computedBindingEdits(DesignerPane pane, DocumentSession document, String full)
throws Exception {
bindingTab(pane, "Computed bindings");
click(pane, "New Computed binding");
computedField(pane, "control", "action");
computedField(pane, "property", "text");
click(pane, "Find methods / Validate");
await(() -> fx(() -> !((ComboBox<?>) pane.lookup("#computed-sources")).getItems().isEmpty()));
fx(
() -> {
var methods =
(ComboBox<de.neitzel.fxdesigner.protocol.BindingMetadata.Computed>)
pane.lookup("#computed-sources");
var greeting =
methods.getItems().stream()
.filter(m -> m.method().equals("#greeting"))
.findFirst()
.orElseThrow();
check(greeting.status().equals("COMPATIBLE"), "Computed method type comparison failed");
methods.setValue(greeting);
return null;
});
check(documentText(document).equals(full), "Method choice changed source before Apply");
click(pane, "Apply Computed binding");
await(() -> !documentText(document).equals(full));
String created = documentText(document);
check(
created.contains(
"ComputedBinding control=\"action\" property=\"text\" method=\"#greeting\""),
"Computed creation failed");
click(pane, "Undo");
await(() -> documentText(document).equals(full));
click(pane, "Redo");
await(() -> documentText(document).equals(created));
await(() -> fx(() -> source(pane).getText().equals(created)));
selectComputed(pane, "action");
computedField(pane, "property", "accessibleText");
computedField(pane, "method", "greeting");
click(pane, "Apply Computed binding");
String changed =
created.replace(
"property=\"text\" method=\"#greeting\"",
"property=\"accessibleText\" method=\"greeting\"");
await(() -> documentText(document).equals(changed));
click(pane, "Undo");
await(() -> documentText(document).equals(created));
click(pane, "Redo");
await(() -> documentText(document).equals(changed));
await(() -> fx(() -> source(pane).getText().equals(changed)));
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
.get();
check(
java.nio.file.Files.readString(
java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document()))
.equals(changed),
"Computed save changed source");
click(pane, "Render drafts");
await(
() ->
fx(
() ->
object(pane, "home").getSceneGraphObject() instanceof SnapshotRegion r
&& r.image() != null
&& r.statusText().equals("Target runtime")));
selectComputed(pane, "action");
click(pane, "Remove Computed binding");
await(() -> !documentText(document).equals(changed));
String removed = documentText(document);
check(!removed.contains("ComputedBinding"), "Computed removal failed");
click(pane, "Undo");
await(() -> documentText(document).equals(changed));
click(pane, "Redo");
await(() -> documentText(document).equals(removed));
await(() -> fx(() -> source(pane).getText().equals(removed)));
click(pane, "New Computed binding");
computedField(pane, "control", "rankInput");
computedField(pane, "property", "text");
computedField(pane, "method", "#greeting");
click(pane, "Apply Computed binding");
await(
() ->
fx(
() ->
((Label) pane.lookup("#computed-status"))
.getText()
.contains("Replace conflicting bindings")));
check(documentText(document).equals(removed), "Computed silently replaced Direct binding");
fx(
() -> {
((CheckBox) pane.lookup("#computed-replace")).setSelected(true);
return null;
});
click(pane, "Apply Computed binding");
await(() -> !documentText(document).equals(removed));
String replaced = documentText(document);
check(
directDeclarations(replaced).stream()
.noneMatch(e -> e.getAttribute("control").equals("rankInput")),
"Computed replacement retained Direct conflict");
click(pane, "Undo");
await(() -> documentText(document).equals(removed));
click(pane, "Redo");
await(() -> documentText(document).equals(replaced));
await(() -> fx(() -> source(pane).getText().equals(replaced)));
selectComputed(pane, "rankInput");
computedField(pane, "method", "#invalidComputed");
click(pane, "Apply Computed binding");
await(() -> documentText(document).contains("method=\"#invalidComputed\""));
String invalid = documentText(document);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()
.get();
click(pane, "Render drafts");
await(
() ->
fx(
() ->
pane.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label l
&& l.getText() != null
&& l.getText().startsWith("Preview blocked:"))));
check(
java.nio.file.Files.readString(
java.nio.file.Path.of(document.snapshot().toCompletableFuture().get().document()))
.equals(invalid),
"Invalid Computed draft not saved");
click(pane, "Undo");
await(() -> documentText(document).equals(replaced));
click(pane, "Redo");
await(() -> documentText(document).equals(invalid));
set(document, full);
await(() -> fx(() -> source(pane).getText().equals(full)));
}
private static void viewModelBindings(
DesignerPane pane, DocumentSession document, String original) throws Exception {
bindingTab(pane, "Discover targets");
@@ -859,6 +1021,7 @@ public final class ProjectionHostChecks {
check(documentText(document).equals(full), "Discovery handoff changed source before Apply");
converterEdits(pane, document, full);
directBindingEdits(pane, document, full);
computedBindingEdits(pane, document, full);
document
.save(document.snapshot().toCompletableFuture().get().revision())
.toCompletableFuture()