use of org.structr.common.error.FrameworkException in project structr by structr.
the class DOMNodeTest method testAppendWithFragment.
@Test
public void testAppendWithFragment() {
try (final Tx tx = app.tx()) {
Document document = getDocument();
org.w3c.dom.DocumentFragment fragment = document.createDocumentFragment();
assertNotNull(document);
assertNotNull(fragment);
Text test1 = document.createTextNode("test1");
Text test2 = document.createTextNode("test2");
Text test3 = document.createTextNode("test3");
Text test4 = document.createTextNode("test4");
Text test5 = document.createTextNode("test5");
Text test6 = document.createTextNode("test6");
Text test7 = document.createTextNode("test7");
Text test8 = document.createTextNode("test8");
Text test9 = document.createTextNode("test9");
assertNotNull(test1);
assertNotNull(test2);
assertNotNull(test3);
assertNotNull(test4);
assertNotNull(test5);
assertNotNull(test6);
assertNotNull(test7);
assertNotNull(test8);
assertNotNull(test9);
Element div = document.createElement("div");
assertNotNull(div);
// add children
div.appendChild(test1);
div.appendChild(test2);
div.appendChild(test3);
div.appendChild(test4);
div.appendChild(test5);
// examine children
NodeList children1 = div.getChildNodes();
assertEquals(test1, children1.item(0));
assertEquals(test2, children1.item(1));
assertEquals(test3, children1.item(2));
assertEquals(test4, children1.item(3));
assertEquals(test5, children1.item(4));
// add children to document fragment
fragment.appendChild(test6);
fragment.appendChild(test7);
fragment.appendChild(test8);
fragment.appendChild(test9);
// test replace child
div.appendChild(fragment);
// examine children
NodeList children2 = div.getChildNodes();
assertEquals(test1, children2.item(0));
assertEquals(test2, children2.item(1));
assertEquals(test3, children2.item(2));
assertEquals(test4, children2.item(3));
assertEquals(test5, children2.item(4));
assertEquals(test6, children2.item(5));
assertEquals(test7, children2.item(6));
assertEquals(test8, children2.item(7));
assertEquals(test9, children2.item(8));
tx.success();
} catch (FrameworkException fex) {
fail("unexpected exception");
}
}
use of org.structr.common.error.FrameworkException in project structr by structr.
the class DOMNodeTest method testRemoveChildNode.
@Test
public void testRemoveChildNode() {
try (final Tx tx = app.tx()) {
Document document = getDocument();
assertNotNull(document);
Text test1 = document.createTextNode("test1");
Text test2 = document.createTextNode("test2");
Text test3 = document.createTextNode("test3");
Text test4 = document.createTextNode("test4");
Text test5 = document.createTextNode("test5");
Text test6 = document.createTextNode("test6");
assertNotNull(test1);
assertNotNull(test2);
assertNotNull(test3);
assertNotNull(test4);
assertNotNull(test5);
assertNotNull(test6);
Element div = document.createElement("div");
assertNotNull(div);
// add children
div.appendChild(test1);
div.appendChild(test2);
div.appendChild(test3);
div.appendChild(test4);
div.appendChild(test5);
// note that we do NOT add test6 as a child!
NodeList children1 = div.getChildNodes();
assertEquals(test1, children1.item(0));
assertEquals(test2, children1.item(1));
assertEquals(test3, children1.item(2));
assertEquals(test4, children1.item(3));
assertEquals(test5, children1.item(4));
// test remove child node method
div.removeChild(test3);
NodeList children2 = div.getChildNodes();
assertEquals(test1, children2.item(0));
assertEquals(test2, children2.item(1));
assertEquals(test4, children2.item(2));
assertEquals(test5, children2.item(3));
// test remove child node method
div.removeChild(test1);
NodeList children3 = div.getChildNodes();
assertEquals(test2, children3.item(0));
assertEquals(test4, children3.item(1));
assertEquals(test5, children3.item(2));
// and finally, test errors that should be raised
try {
div.removeChild(test6);
fail("Removing a node that is not a child of the given node should raise a DOMException");
} catch (DOMException dex) {
assertEquals(DOMException.NOT_FOUND_ERR, dex.code);
}
tx.success();
} catch (FrameworkException fex) {
fail("unexpected exception");
}
}
use of org.structr.common.error.FrameworkException in project structr by structr.
the class PageTest method testAdoptNodes.
@Test
public void testAdoptNodes() {
try {
try (final Tx tx = app.tx()) {
Page srcPage = Page.createNewPage(securityContext, "srcPage");
assertTrue(srcPage != null);
assertTrue(srcPage instanceof Page);
Node html = srcPage.createElement("html");
Node head = srcPage.createElement("head");
Node body = srcPage.createElement("body");
Node title = srcPage.createElement("title");
Node h1 = srcPage.createElement("h1");
Node div = srcPage.createElement("div");
Node p = srcPage.createElement("p");
// add HTML element to page
srcPage.appendChild(html);
// add HEAD and BODY elements to HTML
html.appendChild(head);
html.appendChild(body);
// add TITLE element to HEAD
head.appendChild(title);
// add H1 element to BODY
body.appendChild(h1);
// add DIV element to BODY
body.appendChild(div);
div.appendChild(p);
// add text element to P
p.appendChild(srcPage.createTextNode("First Paragraph"));
Page dstPage = Page.createNewPage(securityContext, "dstPage");
assertNotNull(dstPage);
// test adopt method
dstPage.adoptNode(html);
// has parent been removed for the source element?
assertNull(html.getParentNode());
// has owner changed for all elements?
assertEquals(dstPage, html.getOwnerDocument());
assertEquals(dstPage, head.getOwnerDocument());
assertEquals(dstPage, body.getOwnerDocument());
assertEquals(dstPage, title.getOwnerDocument());
assertEquals(dstPage, h1.getOwnerDocument());
assertEquals(dstPage, div.getOwnerDocument());
assertEquals(dstPage, p.getOwnerDocument());
// have parents been kept for all other elements?
assertEquals(html, head.getParentNode());
assertEquals(html, body.getParentNode());
assertEquals(head, title.getParentNode());
assertEquals(body, h1.getParentNode());
assertEquals(body, div.getParentNode());
assertEquals(div, p.getParentNode());
// srcPage should not have a document element any more
assertNull(srcPage.getDocumentElement());
// srcPage should have exactly one child element
assertEquals(1, srcPage.getChildNodes().getLength());
tx.success();
} catch (DOMException dex) {
throw new FrameworkException(422, dex.getMessage());
}
} catch (FrameworkException ex) {
fail("Unexpected exception");
}
}
use of org.structr.common.error.FrameworkException in project structr by structr.
the class PageTest method testGetElementsByTagName.
@Test
public void testGetElementsByTagName() {
final String pageName = "page-01";
try {
try (final Tx tx = app.tx()) {
Page page = Page.createNewPage(securityContext, pageName);
assertTrue(page != null);
assertTrue(page instanceof Page);
DOMNode html = (DOMNode) page.createElement("html");
DOMNode head = (DOMNode) page.createElement("head");
DOMNode body = (DOMNode) page.createElement("body");
DOMNode title = (DOMNode) page.createElement("title");
DOMNode h1 = (DOMNode) page.createElement("h1");
DOMNode div1 = (DOMNode) page.createElement("div");
DOMNode p1 = (DOMNode) page.createElement("p");
DOMNode div2 = (DOMNode) page.createElement("div");
DOMNode p2 = (DOMNode) page.createElement("p");
DOMNode div3 = (DOMNode) page.createElement("div");
DOMNode p3 = (DOMNode) page.createElement("p");
// add HTML element to page
page.appendChild(html);
// add HEAD and BODY elements to HTML
html.appendChild(head);
html.appendChild(body);
// add TITLE element to HEAD
head.appendChild(title);
// add H1 element to BODY
body.appendChild(h1);
// add DIV element 1 to BODY
body.appendChild(div1);
div1.appendChild(p1);
// add DIV element 2 to DIV
div1.appendChild(div2);
div2.appendChild(p2);
// add DIV element 3 to DIV
div2.appendChild(div3);
div3.appendChild(p3);
NodeList divs = page.getElementsByTagName("p");
assertEquals(p1, divs.item(0));
assertEquals(p2, divs.item(1));
assertEquals(p3, divs.item(2));
tx.success();
} catch (Exception ex) {
throw new FrameworkException(422, ex.getMessage());
}
} catch (FrameworkException ex) {
fail("Unexpected exception");
}
}
use of org.structr.common.error.FrameworkException in project structr by structr.
the class SyncCommand method exportToFile.
// ----- static methods -----
/**
* Exports the whole structr database to a file with the given name.
*
* @param graphDb
* @param fileName
* @param includeFiles
* @throws FrameworkException
*/
public static void exportToFile(final DatabaseService graphDb, final String fileName, final String query, final boolean includeFiles) throws FrameworkException {
final App app = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
final Set<AbstractNode> nodes = new HashSet<>();
final Set<AbstractRelationship> rels = new HashSet<>();
boolean conditionalIncludeFiles = includeFiles;
if (query != null) {
logger.info("Using Cypher query {} to determine export set, disabling export of files", query);
conditionalIncludeFiles = false;
final List<GraphObject> result = StructrApp.getInstance().cypher(query, null);
for (final GraphObject obj : result) {
if (obj.isNode()) {
nodes.add((AbstractNode) obj.getSyncNode());
} else {
rels.add((AbstractRelationship) obj.getSyncRelationship());
}
}
logger.info("Query returned {} nodes and {} relationships.", new Object[] { nodes.size(), rels.size() });
} else {
nodes.addAll(nodeFactory.bulkInstantiate(graphDb.getAllNodes()));
rels.addAll(relFactory.bulkInstantiate(graphDb.getAllRelationships()));
}
try (final FileOutputStream fos = new FileOutputStream(fileName)) {
exportToStream(fos, nodes, rels, null, conditionalIncludeFiles);
}
tx.success();
} catch (Throwable t) {
logger.warn("", t);
throw new FrameworkException(500, t.getMessage());
}
}
Aggregations