Add reviewed FXML View generation in both hosts
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package de.neitzel.fxdesigner.generation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import de.neitzel.fxdesigner.generation.ViewGeneration.*;
|
||||
import de.neitzel.fxdesigner.protocol.BindingMetadata;
|
||||
import java.util.*;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ViewGenerationTest {
|
||||
private Field field(
|
||||
String path, Control control, String component, List<Column> columns, boolean include) {
|
||||
return new Field(
|
||||
path, "Name & <quoted> \"😀\"", control, "both", "", component, columns, include);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emitsOrderedUniqueBindingsSectionsAndEscapedLabels() throws Exception {
|
||||
var fields =
|
||||
List.of(
|
||||
field("name", Control.TextField, "", List.of(), true),
|
||||
field("home", Control.Component, "sample.AddressForm", List.of(), true),
|
||||
field("visits", Control.Repeat, "sample.AddressForm", List.of(), true));
|
||||
String xml = ViewGeneration.generate("sample.Person", "", fields);
|
||||
var doc =
|
||||
DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder()
|
||||
.parse(
|
||||
new java.io.ByteArrayInputStream(
|
||||
xml.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
|
||||
assertEquals("GridPane", doc.getDocumentElement().getTagName());
|
||||
assertEquals(
|
||||
"Name & <quoted> \"😀\"",
|
||||
((org.w3c.dom.Element) doc.getElementsByTagName("Label").item(0)).getAttribute("text"));
|
||||
assertEquals(2, doc.getElementsByTagName("DirectBinding").getLength());
|
||||
assertEquals(1, doc.getElementsByTagName("Repeat").getLength());
|
||||
assertTrue(xml.indexOf("fx:id=\"field1\"") < xml.indexOf("fx:id=\"field2\""));
|
||||
assertFalse(xml.contains("fx:controller"));
|
||||
assertEquals(xml, ViewGeneration.generate("sample.Person", "", fields));
|
||||
}
|
||||
|
||||
@Test
|
||||
void omissionBypassesUnresolvedFieldsButIncludedFieldsMustBeConfigured() {
|
||||
var missing = field("home", Control.Component, "", List.of(), true);
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ViewGeneration.generate("sample.Person", "", List.of(missing)));
|
||||
assertDoesNotThrow(
|
||||
() ->
|
||||
ViewGeneration.generate(
|
||||
"sample.Person",
|
||||
"",
|
||||
List.of(field("home", Control.Component, "", List.of(), false))));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> ViewGeneration.seed("sample.Person?><bad>", ""));
|
||||
assertThrows(IllegalArgumentException.class, () -> ViewGeneration.xml("bad\u0000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tableColumnsKeepExplicitOrderAndValidateWidth() {
|
||||
String xml =
|
||||
ViewGeneration.generate(
|
||||
"sample.Person",
|
||||
"sample.Controller",
|
||||
List.of(
|
||||
field(
|
||||
"visits",
|
||||
Control.Table,
|
||||
"sample.Row",
|
||||
List.of(new Column("street", "Street", "120"), new Column("city", "City", "")),
|
||||
true)));
|
||||
assertTrue(xml.contains("fx:controller=\"sample.Controller\""));
|
||||
assertTrue(xml.indexOf("attribute=\"street\"") < xml.indexOf("attribute=\"city\""));
|
||||
assertTrue(xml.contains("width=\"120\""));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
ViewGeneration.generate(
|
||||
"sample.Person",
|
||||
"",
|
||||
List.of(
|
||||
field(
|
||||
"visits",
|
||||
Control.Table,
|
||||
"sample.Row",
|
||||
List.of(new Column("street", "Street", "NaN")),
|
||||
true))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void proposalsUseSharedMetadataAndExcludeNestedPathsAndRuntimeMessages() {
|
||||
var metadata =
|
||||
new BindingMetadata(
|
||||
"sample.Person",
|
||||
"",
|
||||
"both",
|
||||
List.of(
|
||||
new BindingMetadata.Attribute("name", "java.lang.String", "VALUE", ""),
|
||||
new BindingMetadata.Attribute("home", "sample.Address", "COMPLEX", ""),
|
||||
new BindingMetadata.Attribute("home.street", "java.lang.String", "VALUE", ""),
|
||||
new BindingMetadata.Attribute(
|
||||
"visits", "java.util.List", "COLLECTION", "sample.Address"),
|
||||
new BindingMetadata.Attribute("latestMessage", "java.lang.String", "BUILTIN", "")),
|
||||
List.of(),
|
||||
List.of());
|
||||
var fields = ViewGeneration.propose(metadata);
|
||||
assertEquals(
|
||||
List.of(Control.TextField, Control.Component, Control.Repeat),
|
||||
fields.stream().map(Field::control).toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user