Make configuration and compatibility contracts executable

This commit is contained in:
2026-11-12 08:27:53 +01:00
parent 306266ae40
commit 85a75383a0
28 changed files with 2284 additions and 8 deletions
@@ -0,0 +1,58 @@
package de.neitzel.fxdesigner.config;
import de.neitzel.fxdesigner.contracts.*;
import de.neitzel.fxdesigner.host.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
/** Same behavioral contract executed with real standalone and IntelliJ document stores. */
public final class ConfigurationHostContract {
public static final String VALID =
"""
{"schemaVersion":"1.0","contexts":{"desktop":{"applicationModule":".","launchMode":"classpath","buildRuntime":"build","targetRuntime":"target","preview":{"sourceSet":"test","provider":"fixtures.Examples"}}}}
""";
public static ContextCatalog fixture(Path root) throws Exception {
Files.createDirectories(root.resolve("src"));
Files.writeString(root.resolve("pom.xml"), "<project/>");
Files.writeString(root.resolve("src/View.fxml"), "original");
Path file = root.resolve("neitzel-fx-designer.json");
Files.writeString(file, VALID);
return new ContextCatalog(
file,
ConfigurationFiles.load(file),
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of())));
}
public static void exercise(HostServices host, ContextCatalog catalog) throws Exception {
Path file = catalog.configuration().getParent().resolve("src/View.fxml");
var document =
host.documents().open(file.toUri()).toCompletableFuture().get(10, TimeUnit.SECONDS);
try {
var choice = catalog.select(file, Optional.empty(), Optional.empty());
if (!choice.selected().equals(Optional.of("desktop")))
throw new AssertionError("Context not selected");
var gate = new ConfigurationGate(host);
try {
gate.check(catalog, "desktop", Set.of()).toCompletableFuture().get(10, TimeUnit.SECONDS);
throw new AssertionError("Missing runtime accepted");
} catch (ExecutionException expected) {
}
var draft = document.snapshot().toCompletableFuture().get(10, TimeUnit.SECONDS);
var changed =
document
.apply(
new EditBatch(
draft.revision(),
"Edit despite setup failure",
List.of(new EditBatch.Replacement(0, 8, "editable"))))
.toCompletableFuture()
.get(10, TimeUnit.SECONDS);
if (!changed.writable() || !changed.text().equals("editable"))
throw new AssertionError("Configuration failure disabled source editing");
} finally {
document.close();
}
}
}
@@ -0,0 +1,323 @@
package de.neitzel.fxdesigner.config;
import static org.junit.jupiter.api.Assertions.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;
import de.neitzel.fxdesigner.host.*;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
class ConfigurationTest {
@TempDir Path root;
private final ObjectMapper json = new ObjectMapper();
private ObjectNode valid() throws Exception {
return (ObjectNode) json.readTree(ConfigurationHostContract.VALID);
}
private ObjectNode context(ObjectNode n) {
return (ObjectNode) n.path("contexts").path("desktop");
}
private void validate(ObjectNode n) {
ConfigurationFiles.validate(
n.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8), "configuration");
}
@Test
void packagedFixturesValidate() throws Exception {
for (String name : List.of("classpath-module", "module-path-test")) {
try (var in = getClass().getResourceAsStream("/configuration/" + name + ".json")) {
assertNotNull(in);
ConfigurationFiles.validate(in.readAllBytes(), "configuration");
}
}
}
@Test
void strictSchemaRejectsVersionsUnknownFieldsMissingConditionalFieldsAndDuplicateKeys()
throws Exception {
validate(valid());
var n = valid();
n.put("schemaVersion", "2.0");
assertThrows(ConfigurationException.class, () -> validate(n));
var unknown = valid();
context(unknown).put("typo", true);
assertThrows(ConfigurationException.class, () -> validate(unknown));
var module = valid();
context(module).put("launchMode", "module-path");
assertThrows(ConfigurationException.class, () -> validate(module));
context(module).putObject("modulePath").put("entryModule", "app");
validate(module);
var preview = valid();
((ObjectNode) context(preview).get("preview")).put("sourceSet", "module");
assertThrows(ConfigurationException.class, () -> validate(preview));
((ObjectNode) context(preview).get("preview")).put("module", "preview");
validate(preview);
assertThrows(
ConfigurationException.class,
() ->
ConfigurationFiles.validate(
"{\"schemaVersion\":\"1.0\",\"schemaVersion\":\"1.0\"}".getBytes(),
"configuration"));
assertThrows(
ConfigurationException.class,
() ->
ConfigurationFiles.validate(
(ConfigurationHostContract.VALID + " {}").getBytes(), "configuration"));
assertThrows(
ConfigurationException.class,
() ->
ConfigurationFiles.validate(
new byte[ConfigurationFiles.MAX_BYTES + 1], "configuration"));
}
@Test
void emptyNullAndOverNestedJsonFailAsConfigurationErrors() {
for (String value : List.of("", "null", "[".repeat(40) + "0" + "]".repeat(40)))
assertThrows(
ConfigurationException.class,
() -> ConfigurationFiles.validate(value.getBytes(), "configuration"));
assertThrows(
ConfigurationException.class,
() -> new ContextCatalog.MavenModule(Path.of("relative"), List.of(), Set.of()));
}
@Test
void selectAmbiguousRememberedInheritedAndNoMatch() throws Exception {
var base = ConfigurationHostContract.fixture(root);
var n = valid();
((ObjectNode) n.get("contexts")).set("other", context(n).deepCopy());
Files.writeString(base.configuration(), n.toString());
var catalog =
new ContextCatalog(
base.configuration(),
ConfigurationFiles.load(base.configuration()),
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of())));
Path view = root.resolve("src/View.fxml");
assertEquals(2, catalog.select(view, Optional.empty(), Optional.empty()).candidates().size());
assertTrue(catalog.select(view, Optional.empty(), Optional.empty()).selected().isEmpty());
assertEquals(
"other",
catalog.select(view, Optional.empty(), Optional.of("other")).selected().orElseThrow());
assertTrue(catalog.select(view, Optional.empty(), Optional.of("removed")).selected().isEmpty());
Path external = Files.writeString(root.resolve("Component.fxml"), "component");
assertTrue(catalog.select(external, Optional.empty(), Optional.empty()).candidates().isEmpty());
assertEquals(
"desktop",
catalog
.select(external, Optional.of("desktop"), Optional.of("other"))
.selected()
.orElseThrow());
assertThrows(
ConfigurationException.class,
() -> catalog.select(view, Optional.of("removed"), Optional.empty()));
}
@Test
void modulePathsMustBelongToEffectiveReactorIncludingSymlinkEscapes() throws Exception {
var base = ConfigurationHostContract.fixture(root);
Path fake = Files.createDirectories(root.resolve("unlisted"));
Files.writeString(fake.resolve("pom.xml"), "<project/>");
var n = valid();
context(n).put("applicationModule", "unlisted");
Files.writeString(base.configuration(), n.toString());
var model =
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of()));
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(), ConfigurationFiles.load(base.configuration()), model));
context(n).put("applicationModule", "missing");
Files.writeString(base.configuration(), n.toString());
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(), ConfigurationFiles.load(base.configuration()), model));
context(n).put("applicationModule", root.toString());
Files.writeString(base.configuration(), n.toString());
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(), ConfigurationFiles.load(base.configuration()), model));
Path outside = Files.createTempDirectory("designer-outside-");
try {
Files.createSymbolicLink(root.resolve("escape"), outside);
context(n).put("applicationModule", "escape");
Files.writeString(base.configuration(), n.toString());
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(), ConfigurationFiles.load(base.configuration()), model));
} finally {
Files.deleteIfExists(outside);
}
}
@Test
void dependencySourceRootsAndPreviewModuleParticipateInMatching() throws Exception {
var base = ConfigurationHostContract.fixture(root);
Path app = Files.createDirectories(root.resolve("app")),
shared = Files.createDirectories(root.resolve("shared")),
preview = Files.createDirectories(root.resolve("preview"));
for (Path p : List.of(app, shared, preview)) {
Files.writeString(p.resolve("pom.xml"), "<project/>");
Files.createDirectories(p.resolve("src"));
}
var n = valid();
context(n).put("applicationModule", "app");
var p = (ObjectNode) context(n).get("preview");
p.put("sourceSet", "module");
p.put("module", "preview");
Files.writeString(base.configuration(), n.toString());
var model =
List.of(
new ContextCatalog.MavenModule(root, List.of(), Set.of()),
new ContextCatalog.MavenModule(app, List.of(app.resolve("src")), Set.of(shared)),
new ContextCatalog.MavenModule(shared, List.of(shared.resolve("src")), Set.of()),
new ContextCatalog.MavenModule(preview, List.of(preview.resolve("src")), Set.of()));
var catalog =
new ContextCatalog(
base.configuration(), ConfigurationFiles.load(base.configuration()), model);
Path component = Files.writeString(shared.resolve("src/Card.fxml"), "component");
assertEquals(
"desktop",
catalog.select(component, Optional.empty(), Optional.empty()).selected().orElseThrow());
}
@Test
void profileSpecificReactorViewsAndExplicitModuleAccessAreRequired() throws Exception {
var base = ConfigurationHostContract.fixture(root);
var n = valid();
context(n).putArray("mavenProfiles").add("preview");
context(n).put("launchMode", "module-path");
var access = context(n).putObject("modulePath").put("entryModule", "example.app");
access.putArray("addOpens").add("example.app/example.model=ALL-UNNAMED");
Files.writeString(base.configuration(), n.toString());
var config = ConfigurationFiles.load(base.configuration());
var modules =
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of()));
assertThrows(
ConfigurationException.class,
() -> new ContextCatalog(base.configuration(), config, modules));
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(),
config,
Map.of("desktop", new ContextCatalog.ReactorView(List.of("wrong"), modules))));
assertNotNull(
new ContextCatalog(
base.configuration(),
config,
Map.of("desktop", new ContextCatalog.ReactorView(List.of("preview"), modules))));
access.putArray("addOpens").add("--add-opens=anything");
Files.writeString(base.configuration(), n.toString());
assertThrows(
ConfigurationException.class,
() ->
new ContextCatalog(
base.configuration(),
ConfigurationFiles.load(base.configuration()),
Map.of("desktop", new ContextCatalog.ReactorView(List.of("preview"), modules))));
}
@Test
void localMappingsVerifyInstallationsRolesAndPreserveRememberedChoice() throws Exception {
Files.writeString(root.resolve("neitzel-fx-designer.json"), ConfigurationHostContract.VALID);
Path home = Path.of(System.getProperty("java.home"));
Path local = root.resolve("local.json");
var n = json.createObjectNode().put("schemaVersion", "1.0");
n.putObject("runtimes").putObject("java").put("build", home.toString());
n.putObject("rememberedContexts")
.put(root.resolve("neitzel-fx-designer.json").toRealPath().toUri().toString(), "desktop");
Files.writeString(local, n.toString());
var settings = LocalSettings.load(local);
assertEquals(
Runtime.version().feature(),
settings
.resolve("java", HostServices.RuntimeRole.BUILD)
.toCompletableFuture()
.get()
.javaFeature());
assertThrows(
ExecutionException.class,
() ->
settings.resolve("java", HostServices.RuntimeRole.TARGET).toCompletableFuture().get());
assertEquals(
"desktop", settings.remembered(root.resolve("neitzel-fx-designer.json")).orElseThrow());
LocalSettings.remember(local, root.resolve("neitzel-fx-designer.json"), "other");
assertEquals(
"other",
LocalSettings.load(local)
.remembered(root.resolve("neitzel-fx-designer.json").toRealPath())
.orElseThrow());
((ObjectNode) n.path("runtimes").path("java")).put("target", root.toString());
Files.writeString(local, n.toString());
assertThrows(
ExecutionException.class,
() ->
LocalSettings.load(local)
.resolve("java", HostServices.RuntimeRole.TARGET)
.toCompletableFuture()
.get());
((ObjectNode) n.path("runtimes").path("java")).put("target", "relative");
Files.writeString(local, n.toString());
assertThrows(ConfigurationException.class, () -> LocalSettings.load(local));
}
@Test
void gateReportsCapabilitiesAndRuntimeRequirementsSeparatelyWithoutFallback() throws Exception {
var catalog = ConfigurationHostContract.fixture(root);
var n = valid();
context(n).put("targetJava", 99);
Files.writeString(catalog.configuration(), n.toString());
catalog =
new ContextCatalog(
catalog.configuration(),
ConfigurationFiles.load(catalog.configuration()),
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of())));
var diagnostics = new ArrayList<HostServices.Diagnostic>();
var install =
new HostServices.RuntimeInstallation(Path.of(System.getProperty("java.home")), 25);
var host =
new HostServices(
uri ->
CompletableFuture.failedFuture(
new AssertionError("Documents must not be accessed")),
(r, o) -> {
throw new AssertionError("Build must not run");
},
(name, role) -> CompletableFuture.completedFuture(install),
location -> CompletableFuture.completedFuture(null),
diagnostics::add,
new SessionScope());
var gate = new ConfigurationGate(host);
var checked = catalog;
assertThrows(
ExecutionException.class,
() -> gate.check(checked, "desktop", Set.of()).toCompletableFuture().get());
assertEquals(1, diagnostics.size());
context(n).put("targetJava", 21);
context(n).putArray("requiredCapabilities").add("unknown");
Files.writeString(catalog.configuration(), n.toString());
var capCatalog =
new ContextCatalog(
catalog.configuration(),
ConfigurationFiles.load(catalog.configuration()),
List.of(new ContextCatalog.MavenModule(root, List.of(root.resolve("src")), Set.of())));
assertThrows(
ExecutionException.class,
() -> gate.check(capCatalog, "desktop", Set.of()).toCompletableFuture().get());
assertNotNull(gate.check(capCatalog, "desktop", Set.of("unknown")).toCompletableFuture().get());
}
}
@@ -0,0 +1,26 @@
{
"schemaVersion": "1.0",
"contexts": {
"desktop": {
"applicationModule": "app",
"mavenProfiles": [
"designer-preview"
],
"launchMode": "classpath",
"buildRuntime": "build-java",
"targetRuntime": "application-java",
"buildJava": 25,
"targetJava": 21,
"requiredCapabilities": [
"discover-v1",
"render-rgba-v1"
],
"preview": {
"sourceSet": "module",
"module": "preview",
"provider": "example.preview.PersonExamples",
"defaultExample": "person.standard"
}
}
}
}
@@ -0,0 +1,33 @@
{
"schemaVersion": "1.0",
"contexts": {
"desktop": {
"applicationModule": "app",
"mavenProfiles": [
"designer-preview"
],
"launchMode": "module-path",
"buildRuntime": "build-java",
"targetRuntime": "application-java",
"buildJava": 25,
"targetJava": 21,
"requiredCapabilities": [
"discover-v1",
"render-rgba-v1"
],
"preview": {
"sourceSet": "test",
"provider": "example.preview.PersonExamples"
},
"modulePath": {
"entryModule": "example.app",
"addReads": [
"example.app=example.preview"
],
"addOpens": [
"example.app/example.model=ALL-UNNAMED"
]
}
}
}
}