Validate preview sessions against real classpath and module-path fixtures

This commit is contained in:
2026-11-12 09:47:59 +01:00
parent e97af99412
commit 28c1e47c95
52 changed files with 1867 additions and 9 deletions
@@ -0,0 +1,110 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.net.URL;
import java.nio.file.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class ExampleCatalogTest {
@TempDir Path temp;
private static final String VALID =
"""
format=1
count=1
example.0.id=person
example.0.label=Person
example.0.modelType=missing.Person
example.0.providerClass=missing.Provider
example.0.viewResource=views/Person.fxml
example.0.componentPackage=missing.components
""";
URL resource(String text) throws Exception {
Path file = Files.createTempFile(temp, "catalog", ".properties");
Files.writeString(file, text);
return file.toUri().toURL();
}
@Test
void readsMetadataWithoutLoadingClasses() throws Exception {
var examples = ExampleCatalog.read(resource(VALID));
assertEquals("missing.Provider", examples.getFirst().providerClass());
assertThrows(UnsupportedOperationException.class, () -> examples.clear());
}
@Test
void rejectsUnknownDuplicateAndMissingFields() throws Exception {
for (String value :
List.of(
VALID + "extra=value\n",
VALID + "format=1\n",
VALID.replace("example.0.id=person\n", ""))) {
URL url = resource(value);
assertThrows(IOException.class, () -> ExampleCatalog.read(url));
}
}
@Test
void rejectsVersionsCountsAndUnsafeResources() throws Exception {
for (String value :
List.of(
VALID.replace("format=1", "format=2"),
VALID.replace("count=1", "count=0"),
VALID.replace("count=1", "count=1025"),
VALID.replace("views/Person.fxml", "../Person.fxml"))) {
URL url = resource(value);
assertThrows(IOException.class, () -> ExampleCatalog.read(url));
}
}
@Test
void rejectsOversizeAndMalformedUtf8() throws Exception {
URL oversized = resource("x".repeat(1024 * 1024 + 1));
assertThrows(IOException.class, () -> ExampleCatalog.read(oversized));
Path malformed = temp.resolve("malformed");
Files.write(malformed, new byte[] {(byte) 0xc3, 0x28});
assertThrows(IOException.class, () -> ExampleCatalog.read(malformed.toUri().toURL()));
}
@Test
void rejectsDuplicateIdsAcrossResources() throws Exception {
var urls = List.of(resource(VALID), resource(VALID));
var loader =
new ClassLoader(null) {
public Enumeration<URL> getResources(String name) {
assertEquals(ExampleCatalog.RESOURCE, name);
return Collections.enumeration(urls);
}
};
assertThrows(IOException.class, () -> ExampleCatalog.discover(loader));
}
@Test
void discoveryIsSortedAndBounded() throws Exception {
var urls =
List.of(
resource(VALID.replace("id=person", "id=z")),
resource(VALID.replace("id=person", "id=a")));
var loader =
new ClassLoader(null) {
public Enumeration<URL> getResources(String name) {
return Collections.enumeration(urls);
}
};
assertEquals(
List.of("a", "z"), ExampleCatalog.discover(loader).stream().map(e -> e.id()).toList());
var many = new ArrayList<URL>();
for (int i = 0; i < 129; i++) many.add(resource(VALID.replace("id=person", "id=p" + i)));
var excessive =
new ClassLoader(null) {
public Enumeration<URL> getResources(String name) {
return Collections.enumeration(many);
}
};
assertThrows(IOException.class, () -> ExampleCatalog.discover(excessive));
}
}