Generate versioned Model metadata and consume it in shared binding discovery

This commit is contained in:
2026-11-13 19:08:30 +01:00
parent 9bc4569407
commit adf432b98b
20 changed files with 700 additions and 15 deletions
@@ -0,0 +1,103 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.protocol.ModelMetadata;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class GeneratedModelMetadataTest {
@TempDir Path root;
public static class Model {
static {
System.setProperty("generated.model.initialized", "yes");
}
public Model() {
throw new AssertionError();
}
public String getName() {
throw new AssertionError();
}
public void setName(String v) {
throw new AssertionError();
}
}
String fxml() {
return "<?neitzel-model "
+ Model.class.getName()
+ "?><?import javafx.scene.control.TextField?><TextField"
+ " xmlns:fx='http://javafx.com/fxml/1' fx:id='input'/>";
}
ModelMetadata metadata() {
return new ModelMetadata(
Model.class.getName(),
List.of(new ModelMetadata.Property("name", "java.lang.String", "", "getName", "setName")));
}
void write(Path base, byte[] bytes) throws Exception {
Path p = base.resolve(ModelMetadata.resource(Model.class.getName()));
Files.createDirectories(p.getParent());
Files.write(p, bytes);
}
@Test
void generatedAndFallbackHaveSameAuthoringSemanticsWithoutInitialization() throws Exception {
var fallback = BindingDiscovery.inspect(fxml(), "name", "both", getClass().getClassLoader());
write(root, metadata().encode());
try (var loader =
new URLClassLoader(new URL[] {root.toUri().toURL()}, getClass().getClassLoader())) {
var generated = BindingDiscovery.inspect(fxml(), "name", "both", loader);
assertEquals(fallback.attributes(), generated.attributes());
assertEquals(fallback.targets(), generated.targets());
assertEquals(fallback.problems(), generated.problems());
assertTrue(
generated.diagnostics().stream()
.anyMatch(d -> d.startsWith("Generated Model metadata 1.0:")));
assertNull(System.getProperty("generated.model.initialized"));
}
}
@Test
void staleMalformedAndDuplicateMetadataAreErrorsNotSilentFallback() throws Exception {
for (byte[] bytes :
List.of(
"schemaVersion=9.0".getBytes(),
new ModelMetadata(Model.class.getName(), List.of()).encode(),
new ModelMetadata(
Model.class.getName(),
List.of(new ModelMetadata.Property("name", "int", "", "getName", "setName")))
.encode())) {
write(root, bytes);
try (var loader =
new URLClassLoader(new URL[] {root.toUri().toURL()}, getClass().getClassLoader())) {
var result = BindingDiscovery.inspect(fxml(), "name", "both", loader);
assertTrue(
result.problems().stream()
.anyMatch(
p ->
p.severity().equals("ERROR") && p.message().contains("metadata rejected")));
assertFalse(result.attributes().stream().anyMatch(a -> a.path().equals("name")));
}
}
write(root, metadata().encode());
Path second = root.resolve("second");
write(second, metadata().encode());
try (var loader =
new URLClassLoader(
new URL[] {root.toUri().toURL(), second.toUri().toURL()},
getClass().getClassLoader())) {
assertTrue(
BindingDiscovery.inspect(fxml(), "name", "both", loader).problems().stream()
.anyMatch(p -> p.message().contains("Duplicate Model metadata")));
}
}
}