Let an application load a View for an ordinary Model and edit one Attribute in place.

The control shows the current value, typing writes it immediately, and a later entity setter no longer moves the control.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-30 17:17:44 +01:00
co-authored by Cursor
parent 1b1be747bb
commit 99886efa8b
13 changed files with 840 additions and 0 deletions
@@ -0,0 +1,160 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.injection.ApplicationContext;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class ShowAndEditOneAttributeTest {
@BeforeAll
static void startJavaFx() throws InterruptedException {
CountDownLatch started = new CountDownLatch(1);
try {
Platform.startup(started::countDown);
} catch (IllegalStateException alreadyStarted) {
started.countDown();
}
if (!started.await(10, TimeUnit.SECONDS)) {
fail("JavaFX toolkit did not start");
}
}
@Test
void bothWaysTextFieldShowsTheNameAndTypingUpdatesTheModel() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("name-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
TextField nameField = (TextField) loaded.getRoot().lookup("#nameField");
assertEquals("Ada", nameField.getText());
nameField.setText("Grace");
assertEquals("Grace", person.getName());
});
}
@Test
void towardViewLabelShowsTheNameFollowsTheFieldAndDoesNotWriteTheModel() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("name-both-and-view.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
TextField nameField = (TextField) loaded.getRoot().lookup("#nameField");
Label nameLabel = (Label) loaded.getRoot().lookup("#nameLabel");
assertEquals("Ada", nameLabel.getText());
nameField.setText("Grace");
assertEquals("Grace", nameLabel.getText());
assertEquals("Grace", person.getName());
nameLabel.setText("Hopper");
assertEquals("Grace", person.getName());
});
}
@Test
void entitySetterAfterLoadLeavesTheTextControlUnchanged() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("name-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
person.setName("Grace");
TextField nameField = (TextField) loaded.getRoot().lookup("#nameField");
assertEquals("Ada", nameField.getText());
});
}
@Test
void bindingAReadOnlyGetterFailsTheLoad() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("read-only-title.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
() -> ViewLoader.load(fxml, person, context));
assertTrue(failure.getMessage().contains("title"));
});
}
@Test
void bindingAMethodDeclaredOnObjectFailsTheLoad() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("object-class.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
() -> ViewLoader.load(fxml, person, context));
assertTrue(failure.getMessage().contains("class"));
});
}
@Test
void nullIntegerAttributeUsesAnIntegerProperty() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowAndEditOneAttributeTest.class.getResource("name-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
assertInstanceOf(IntegerProperty.class, loaded.property("age"));
});
}
private static void onFx(FxAction action) throws Exception {
if (Platform.isFxApplicationThread()) {
action.run();
return;
}
AtomicReference<Throwable> thrown = new AtomicReference<>();
CountDownLatch done = new CountDownLatch(1);
Platform.runLater(() -> {
try {
action.run();
} catch (Throwable failure) {
thrown.set(failure);
} finally {
done.countDown();
}
});
if (!done.await(10, TimeUnit.SECONDS)) {
fail("JavaFX thread did not finish");
}
Throwable failure = thrown.get();
if (failure instanceof Exception exception) {
throw exception;
}
if (failure instanceof Error error) {
throw error;
}
if (failure != null) {
throw new AssertionError(failure);
}
}
@FunctionalInterface
private interface FxAction {
void run() throws Exception;
}
}
@@ -0,0 +1,64 @@
package de.neitzel.fx.view.fixture;
/**
* Ordinary model used by view-loading tests.
*/
public class Person {
private String name;
private Integer age;
/**
* Creates a person with the given name.
*
* @param name the current name
*/
public Person(String name) {
this.name = name;
}
/**
* Returns the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns a title that cannot be written.
*
* @return the title
*/
public String getTitle() {
return "Doctor";
}
/**
* Returns the age. The value may be null.
*
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* Sets the age.
*
* @param age the age
*/
public void setAge(Integer age) {
this.age = age;
}
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="nameField" property="text" path="name" direction="both"/>
<DirectBinding control="nameLabel" property="text" path="name" direction="view"/>
</BindingList>
</fx:define>
<TextField fx:id="nameField" id="nameField"/>
<Label fx:id="nameLabel" id="nameLabel"/>
</VBox>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="nameField" property="text" path="name" direction="both"/>
</BindingList>
</fx:define>
<TextField fx:id="nameField" id="nameField"/>
</VBox>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="classField" property="text" path="class" direction="both"/>
</BindingList>
</fx:define>
<TextField fx:id="classField" id="classField"/>
</VBox>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="titleField" property="text" path="title" direction="both"/>
</BindingList>
</fx:define>
<TextField fx:id="titleField" id="titleField"/>
</VBox>