Discover Repeat collections and compatible Components in both hosts

This commit is contained in:
2026-11-13 14:47:21 +01:00
parent 0300062fb6
commit 0d14ac14e0
28 changed files with 969 additions and 44 deletions
@@ -0,0 +1,170 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fx.view.*;
import java.util.*;
import javafx.beans.property.*;
import javafx.scene.layout.VBox;
import org.junit.jupiter.api.Test;
class RepeatDiscoveryTest {
public static class Item {}
public static class Model {
static {
System.setProperty("repeat.model.initialized", "yes");
}
public List<Item> getItems() {
throw new AssertionError("getter");
}
public void setItems(List<Item> v) {}
public List getRaw() {
throw new AssertionError("getter");
}
public void setRaw(List v) {}
public String getName() {
throw new AssertionError("getter");
}
public void setName(String v) {}
}
public static class Replacement {
public Replacement(Model m) {
throw new AssertionError("constructor");
}
public ListProperty<Item> itemsProperty() {
throw new AssertionError("accessor");
}
public ObjectProperty<List<Item>> uncertainProperty() {
throw new AssertionError("accessor");
}
public ObjectProperty<String> nameProperty() {
throw new AssertionError("accessor");
}
}
@ViewModelClass(Replacement.class)
public static class Controller {}
public static class Good extends VBox {
static {
System.setProperty("repeat.component.initialized", "yes");
}
public ObjectProperty<Item> modelProperty() {
throw new AssertionError("accessor");
}
}
public static class Wrong extends VBox {
public ObjectProperty<Model> modelProperty() {
throw new AssertionError("accessor");
}
}
public static class Unknown extends VBox {
public ObjectProperty<Object> modelProperty() {
throw new AssertionError("accessor");
}
}
public static class ReadOnly extends VBox {
public ReadOnlyObjectProperty<Item> modelProperty() {
throw new AssertionError("accessor");
}
}
private String xml(String collection, String component, String pane, String controller) {
return "<?neitzel-model "
+ Model.class.getName()
+ "?><?import javafx.scene.layout.VBox?><?import javafx.scene.control.Label?>"
+ "<VBox xmlns:fx='http://javafx.com/fxml/1'"
+ (controller.isEmpty() ? "" : " fx:controller='" + controller + "'")
+ "><fx:define><de.neitzel.fx.view.BindingList>"
+ "<de.neitzel.fx.view.Repeat pane='"
+ pane
+ "' collection='"
+ collection
+ "' component='"
+ component
+ "'/></de.neitzel.fx.view.BindingList></fx:define><VBox fx:id='rows'/><Label"
+ " fx:id='label'/></VBox>";
}
private de.neitzel.fxdesigner.protocol.BindingMetadata inspect(
String path, Class<?> type, String pane, String controller) throws Exception {
return BindingDiscovery.inspect(
xml(path, type.getName(), pane, controller), path, "view", getClass().getClassLoader());
}
@Test
void acceptsDeclaredTypesWithoutInitializingApplicationCode() throws Exception {
var m = inspect("items", Good.class, "rows", "");
assertTrue(m.problems().isEmpty(), m.problems().toString());
assertTrue(
m.collections().stream()
.anyMatch(
c ->
c.name().equals("items")
&& c.type().equals(Item.class.getName())
&& c.status().equals("COMPATIBLE")));
assertTrue(
m.panes().stream()
.anyMatch(p -> p.name().equals("label") && p.status().equals("INCOMPATIBLE")));
assertNull(System.getProperty("repeat.model.initialized"));
assertNull(System.getProperty("repeat.component.initialized"));
}
@Test
void rejectsWrongModelPaneAndMissingCollection() throws Exception {
assertTrue(
inspect("items", Wrong.class, "rows", "").problems().stream()
.anyMatch(p -> p.severity().equals("ERROR") && p.message().contains("incompatible")));
assertTrue(
inspect("items", Good.class, "label", "").problems().stream()
.anyMatch(p -> p.message().contains("JavaFX Pane")));
assertTrue(
inspect("name", Good.class, "rows", "").problems().stream()
.anyMatch(p -> p.severity().equals("ERROR") && p.message().contains("collection")));
assertTrue(
inspect("absent", Good.class, "rows", "").problems().stream()
.anyMatch(p -> p.severity().equals("ERROR")));
assertTrue(
inspect("items", ReadOnly.class, "rows", "").problems().stream()
.anyMatch(p -> p.message().contains("modelProperty")));
}
@Test
void keepsUnknownElementAndComponentModelsUnverified() throws Exception {
for (var m :
List.of(
inspect("raw", Good.class, "rows", ""), inspect("items", Unknown.class, "rows", ""))) {
assertTrue(m.problems().stream().anyMatch(p -> p.severity().equals("WARNING")));
assertTrue(
m.problems().stream().noneMatch(p -> p.severity().equals("ERROR")),
m.problems().toString());
}
}
@Test
void discoversReplacementObservableCollectionsAndWarnsForPlainListBacking() throws Exception {
var m = inspect("items", Good.class, "rows", Controller.class.getName());
assertTrue(m.problems().isEmpty(), m.problems().toString());
assertTrue(
m.collections().stream()
.anyMatch(c -> c.name().equals("items") && c.status().equals("COMPATIBLE")));
assertTrue(m.collections().stream().noneMatch(c -> c.name().equals("raw")));
var uncertain = inspect("uncertain", Good.class, "rows", Controller.class.getName());
assertTrue(uncertain.problems().stream().anyMatch(p -> p.severity().equals("WARNING")));
}
}