use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRSwapInsertTargetTest method testCloneInsert.
@Test
public void testCloneInsert() throws JaxenException, JDOMException {
String x = "mods:mods[mods:name[@type='personal']='p1'][mods:name[@type='personal'][2]='p2'][mods:name[@type='corporate']='c1']";
Element template = new MCRNodeBuilder().buildElement(x, null, null);
Document doc = new Document(template);
MCRBinding root = new MCRBinding(doc);
MCRRepeatBinding repeat = new MCRRepeatBinding("mods:mods/mods:name[@type='personal']", root, 3, 10, "clone");
assertEquals("mods:name[(@type = \"personal\")]", repeat.getElementNameWithPredicates());
assertEquals(3, repeat.getBoundNodes().size());
assertEquals("p1", ((Element) (repeat.getBoundNodes().get(0))).getText());
assertEquals("p2", ((Element) (repeat.getBoundNodes().get(1))).getText());
assertEquals("p2", ((Element) (repeat.getBoundNodes().get(2))).getText());
assertEquals("name", ((Element) (repeat.getBoundNodes().get(2))).getName());
assertEquals("personal", ((Element) (repeat.getBoundNodes().get(2))).getAttributeValue("type"));
repeat.insert(1);
assertEquals(4, repeat.getBoundNodes().size());
assertEquals("p1", ((Element) (repeat.getBoundNodes().get(0))).getText());
assertEquals("p1", ((Element) (repeat.getBoundNodes().get(1))).getText());
assertEquals("name", ((Element) (repeat.getBoundNodes().get(1))).getName());
assertEquals("personal", ((Element) (repeat.getBoundNodes().get(1))).getAttributeValue("type"));
assertEquals("p2", ((Element) (repeat.getBoundNodes().get(2))).getText());
repeat = new MCRRepeatBinding("mods:mods/mods:name[@type='corporate']", root, 1, 10, "build");
assertEquals(1, repeat.getBoundNodes().size());
assertEquals("mods:name[(@type = \"corporate\")]", repeat.getElementNameWithPredicates());
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRSwapInsertTargetTest method testSwap.
@Test
public void testSwap() throws JaxenException, JDOMException {
Element template = new MCRNodeBuilder().buildElement("parent[name='a'][note][foo][name='b'][note[2]]", null, null);
Document doc = new Document(template);
MCRBinding root = new MCRBinding(doc);
MCRRepeatBinding repeat = new MCRRepeatBinding("parent/name", root, 2, 0, "build");
assertEquals(2, repeat.getBoundNodes().size());
assertEquals("a", doc.getRootElement().getChildren().get(0).getText());
assertEquals("b", doc.getRootElement().getChildren().get(3).getText());
assertEquals("a", ((Element) (repeat.getBoundNodes().get(0))).getText());
assertEquals("b", ((Element) (repeat.getBoundNodes().get(1))).getText());
repeat.bindRepeatPosition();
String swapParam = MCRSwapTarget.getSwapParameter(repeat, MCRSwapTarget.MOVE_DOWN);
new MCRSwapTarget().handle(swapParam, root);
assertEquals("b", doc.getRootElement().getChildren().get(0).getText());
assertEquals("a", doc.getRootElement().getChildren().get(3).getText());
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testSetAttributeValue.
@Test
public void testSetAttributeValue() throws JaxenException {
Document doc = new Document(new MCRNodeBuilder().buildElement("document[@id='foo']", null, null));
MCRChangeTracker tracker = new MCRChangeTracker();
Attribute id = doc.getRootElement().getAttribute("id");
tracker.track(MCRSetAttributeValue.setValue(id, "bar"));
assertEquals("bar", id.getValue());
tracker.undoChanges(doc);
assertEquals("foo", doc.getRootElement().getAttributeValue("id"));
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testSwapElements.
@Test
public void testSwapElements() throws JaxenException {
Element root = new MCRNodeBuilder().buildElement("parent[name='a'][note][foo][name[2]='b'][note[2]]", null, null);
Document doc = new Document(root);
assertEquals("a", root.getChildren().get(0).getText());
assertEquals("b", root.getChildren().get(3).getText());
MCRChangeTracker tracker = new MCRChangeTracker();
tracker.track(MCRSwapElements.swap(root, 0, 3));
assertEquals("b", root.getChildren().get(0).getText());
assertEquals("a", root.getChildren().get(3).getText());
tracker.undoChanges(doc);
assertEquals("a", root.getChildren().get(0).getText());
assertEquals("b", root.getChildren().get(3).getText());
}
use of org.mycore.common.xml.MCRNodeBuilder in project mycore by MyCoRe-Org.
the class MCRChangeTrackerTest method testRemoveElement.
@Test
public void testRemoveElement() throws JaxenException {
String template = "document[title][title[2][@type='main'][subTitle]][title[3]]";
Document doc = new Document(new MCRNodeBuilder().buildElement(template, null, null));
MCRChangeTracker tracker = new MCRChangeTracker();
Element title = doc.getRootElement().getChildren().get(1);
tracker.track(MCRRemoveElement.remove(title));
assertEquals(2, doc.getRootElement().getChildren().size());
assertFalse(doc.getRootElement().getChildren().contains(title));
tracker.undoChanges(doc);
assertEquals(3, doc.getRootElement().getChildren().size());
assertEquals("main", doc.getRootElement().getChildren().get(1).getAttributeValue("type"));
assertNotNull(doc.getRootElement().getChildren().get(1).getChild("subTitle"));
}
Aggregations