Discover Converter capabilities and validate shared binding drafts

This commit is contained in:
2026-11-13 12:58:08 +01:00
parent 17b1bce704
commit 5a208d2336
35 changed files with 1096 additions and 34 deletions
@@ -0,0 +1,148 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fx.view.Converter;
import de.neitzel.fxdesigner.preview.ConverterCapabilities;
import java.util.*;
import org.junit.jupiter.api.Test;
class ConverterDiscoveryTest {
@ConverterCapabilities(modelType = Integer.class, controlType = String.class, reverse = true)
public static class Valid implements Converter {
static {
System.setProperty("converter.initialized", "yes");
}
public Valid() {
throw new AssertionError("constructed");
}
public Object toControlValue(Object value) {
throw new AssertionError("invoked");
}
public Object toAttributeValue(Object value) {
throw new AssertionError("invoked");
}
}
public static class Legacy extends Valid {}
@ConverterCapabilities(modelType = Integer.class, controlType = String.class, reverse = true)
public static class Broken implements Converter {
public Object toControlValue(Object v) {
return null;
}
}
@ConverterCapabilities(modelType = Integer.class, controlType = String.class)
public static class Forward implements Converter {
public Object toControlValue(Object v) {
return null;
}
}
private ConverterDiscovery discovery() throws Exception {
return new ConverterDiscovery(getClass().getClassLoader());
}
@Test
void discoveryNeverInitializesOrConstructsAndDoesNotInheritCapabilities() throws Exception {
var d = discovery();
assertTrue(
d.candidates(int.class, String.class, "both").stream()
.anyMatch(
c -> c.name().equals(Valid.class.getName()) && c.status().equals("COMPATIBLE")));
assertEquals(
"UNVERIFIED", d.check(Legacy.class.getName(), int.class, String.class, "both").status());
assertNull(System.getProperty("converter.initialized"));
}
@Test
void reverseContractAndConcreteTypeAreChecked() throws Exception {
var d = discovery();
assertEquals(
"ERROR", d.check(Broken.class.getName(), int.class, String.class, "view").status());
assertEquals(
"ERROR", d.check(String.class.getName(), int.class, String.class, "both").status());
assertEquals("ERROR", d.check("missing.Converter", int.class, String.class, "both").status());
assertEquals(
"INCOMPATIBLE",
d.check(Forward.class.getName(), int.class, String.class, "model").status());
assertEquals(
"COMPATIBLE", d.check(Forward.class.getName(), int.class, String.class, "view").status());
}
@Test
void forwardInitializationIsRequiredEvenForModelDirection() throws Exception {
var d = discovery();
assertEquals(
"INCOMPATIBLE",
d.check(Valid.class.getName(), Number.class, String.class, "model").status());
assertEquals(
"COMPATIBLE",
d.check(Valid.class.getName(), int.class, CharSequence.class, "view").status());
assertEquals(
"INCOMPATIBLE",
d.check(Valid.class.getName(), int.class, CharSequence.class, "both").status());
}
private String xml(String path, String property, String converter) {
return "<?neitzel-model "
+ BindingDiscoveryTest.Model.class.getName()
+ "?><?import javafx.scene.control.TextField?><TextField"
+ " xmlns:fx='http://javafx.com/fxml/1'"
+ " fx:id='input'><fx:define><de.neitzel.fx.view.BindingList><de.neitzel.fx.view.DirectBinding"
+ " control='input' property='"
+ property
+ "' path='"
+ path
+ "' direction='both' converter='"
+ converter
+ "'/>"
+ "</de.neitzel.fx.view.BindingList></fx:define></TextField>";
}
private de.neitzel.fxdesigner.protocol.BindingMetadata inspect(
String path, String property, String converter) throws Exception {
return BindingDiscovery.inspect(
xml(path, property, converter),
"rank",
"both",
"input",
"text",
getClass().getClassLoader());
}
@Test
void validatesPathsPropertiesAndConverterContracts() throws Exception {
assertTrue(inspect("rank", "text", Valid.class.getName()).problems().isEmpty());
assertTrue(
BindingDiscovery.inspect(
xml("absent", "text", ""),
"absent",
"both",
"input",
"text",
getClass().getClassLoader())
.problems()
.stream()
.anyMatch(p -> p.severity().equals("ERROR")));
for (var input :
List.of(
new String[] {"absent", "text", ""},
new String[] {"rank", "absent", ""},
new String[] {"rank", "width", ""},
new String[] {"rank", "text", ""},
new String[] {"rank", "text", Broken.class.getName()}))
assertTrue(
inspect(input[0], input[1], input[2]).problems().stream()
.anyMatch(p -> p.severity().equals("ERROR")),
Arrays.toString(input));
assertEquals(
"WARNING",
inspect("rank", "text", Legacy.class.getName()).problems().getFirst().severity());
assertNull(System.getProperty("converter.initialized"));
}
}