* Tests {@link TreeNode#newChild()}.
*/
@Test
@DependsOnMethod("testGetValue")
public void testNewChild() {
final DefaultCitation citation = metadataWithHierarchy();
final TreeNode node = create(citation, ValueExistencePolicy.NON_EMPTY);
/*
* Ensure that we can not overwrite existing nodes.
*/
TreeTable.Node child = node.newChild();
child.setValue(TableColumn.IDENTIFIER, "title");
try {
child.setValue(TableColumn.VALUE, "A new title");
fail("Attemps to overwrite an existing value shall fail.");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("title"));
}
/*
* Clear the title and try again. This time, it shall work.
*/
citation.setTitle(null);
child = node.newChild();
child.setValue(TableColumn.IDENTIFIER, "title");
child.setValue(TableColumn.VALUE, "A new title");
assertEquals("A new title", citation.getTitle().toString());
assertSame(citation.getTitle(), child.getValue(TableColumn.VALUE));
/*
* Try adding a new element in a collection.
* Note that the code below imply a conversion from String to InternationalString.
*/
child = node.newChild();
child.setValue(TableColumn.IDENTIFIER, "alternateTitle");
child.setValue(TableColumn.VALUE, "Third alternate title");
assertEquals(3, citation.getAlternateTitles().size());
assertEquals("Third alternate title", child.getValue(TableColumn.VALUE).toString());
}