package net.exoego.queen.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import net.exoego.function.Func1;
import net.exoego.function.helper.OnString;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
public class XDocumentTest {
private XDocument doc;
private TemporaryFolder folder;
@Before
public void setUp() throws IOException {
doc = new XDocument();
folder = new TemporaryFolder();
folder.create();
}
@After
public void tearDown() {
folder.delete();
}
@Test
public void testXDocumentIsADocument() throws Exception {
assertThat(new XDocument(), isA(Document.class));
}
@Test
public void testCreateEmptyDocument() throws Exception {
final XDocument d = new XDocument();
assertThat(d.getChildNodes().getLength(), is(0));
assertThat(d.toString(), is(""));
}
@Test
public void testAddXmlDeclaration() throws Exception {
final XDocument doc = new XDocument(new XDeclaration());
// declaration is not a node
assertThat(doc.getChildNodes().getLength(), is(0));
assertThat(doc.toString(), is("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"));
}
@Test
public void testAddXmlDeclaration2() throws Exception {
final XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-16"), new XElement("root"));
assertThat(doc.getChildNodes().getLength(), is(1));
assertThat(doc.toString(), is("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root/>"));
}
@Test(expected = DOMException.class)
public void testAdd2RootElement() throws Exception {
new XDocument(new XElement("root"), new XElement("root2"));
}
@Test
public void testAddProcessingInstruction() throws Exception {
final XDocument doc = new XDocument(new XProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='diff.xsl'"),
new XElement("foo"));
assertThat(doc.getChildNodes().getLength(), is(2));
assertThat(doc.toString(), is("<?xml-stylesheet type='text/xsl' href='diff.xsl'?><foo/>"));
}
@Test
public void testAddSingleElement() throws Exception {
final XDocument d = new XDocument(new XElement("root"));
assertThat(d.getChildNodes().getLength(), is(1));
assertThat(d.toString(), is("<root/>"));
}
@Test(expected = NullPointerException.class)
public void testAddNullOutsideOfRootElementFail() throws Exception {
final String s = null;
new XDocument(s);
}
@Test
public void testAppendChild() throws Exception {
final XDocument doc = new XDocument();
doc.appendChild(new XComment("This is a comment"));
doc.appendChild(new XElement("Root", "content"));
assertThat(doc.getChildNodes().getLength(), is(2));
assertThat(doc.toString(), is("<!--This is a comment--><Root>content</Root>"));
}
@Test
public void testAddContentFromIterable() throws Exception {
final XDocument src = new XDocument(new XComment("This is a comment"),
new XElement("Root",
new XElement("Child1", "data1"),
new XElement("Child2", "data2"),
new XElement("Child3", "data3"),
new XElement("Child2", "data4"),
new XElement("Info5", "info5"),
new XElement("Info7", "info7"),
new XElement("Info8", "info8")));
final XDocument doc = new XDocument(new XComment("This is a comment"),
new XElement("Root",
NodeSeq.newInstance(src.getDocumentElement().getChildNodes())
.filter(((Func1<Node, String>) Node::getTextContent).then(
OnString.startsWith("data")))));
assertThat(doc.toString(), is("<!--This is a comment--><Root>" +
"<Child1>data1</Child1>" +
"<Child2>data2</Child2>" +
"<Child3>data3</Child3>" +
"<Child2>data4</Child2></Root>"));
}
@Test
public void testGetNodeType() throws Exception {
assertThat(new XDocument().getType(), CoreMatchers.is(XType.Document));
}
@Test
public void testClone() {
final XDocument origin = new XDocument(new XElement("root"));
final XDocument clone = origin.cloneNode(true);
assertThat(origin.isEqualNode(clone), is(true));
assertThat(origin.isSameNode(clone), is(false));
assertThat(origin == clone, is(false));
}
@Test
public void testCloneConstructor() {
final XDocument origin = new XDocument(new XElement("root"));
final XDocument clone = new XDocument(origin);
assertThat(origin.isEqualNode(clone), is(true));
assertThat(origin.isSameNode(clone), is(false));
assertThat(origin == clone, is(false));
}
@Test
public void testSetAndGetXmlStandalone() throws Exception {
final XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", false));
assertThat(doc.getXmlStandalone(), is(false));
assertThat(doc.toString(), is("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"));
doc.setXmlStandalone(true);
assertThat(doc.getXmlStandalone(), is(true));
assertThat(doc.toString(), is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
doc.setXmlStandalone(false);
assertThat(doc.getXmlStandalone(), is(false));
assertThat(doc.toString(), is("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"));
}
@Test
public void testSetAndGetXmlVersion() throws Exception {
assertThat(doc.getXmlVersion(), is("1.0"));
doc.setXmlVersion("1.1");
assertThat(doc.getXmlVersion(), is("1.1"));
}
@Test(expected = DOMException.class)
public void testSetXmlVersionFail() throws Exception {
doc.setXmlVersion("unknown version");
}
@Test
public void testSetAndGetStrictErrorChecking() throws Exception {
assertThat(doc.getStrictErrorChecking(), is(true));
doc.setStrictErrorChecking(false);
assertThat(doc.getStrictErrorChecking(), is(false));
doc.setStrictErrorChecking(true);
assertThat(doc.getStrictErrorChecking(), is(true));
}
@Test
public void testAdd() throws Exception {
final XDocument doc = new XDocument();
doc.add(new XComment("This is a comment"));
doc.add(new XElement("Root", "content"));
assertThat(doc.getChildNodes().getLength(), is(2));
assertThat(doc.toString(), is("<!--This is a comment--><Root>content</Root>"));
}
@Test
public void testSetAndGetDocumentURI() throws Exception {
assertThat(doc.getDocumentURI(), is(nullValue()));
doc.setDocumentURI("");
assertThat(doc.getDocumentURI(), is(""));
doc.setDocumentURI("// abc //");
assertThat(doc.getDocumentURI(), is("// abc //"));
}
@Test
public void testAdoptNode() throws Exception {
final XElement a = new XElement("a");
doc.adoptNode(a);
assertThat(doc.toString(), is(""));
doc.appendChild(a);
assertThat(doc.toString(), is("<a/>"));
}
@Test
public void testGetDomConfig() throws Exception {
final DOMConfiguration config = doc.getDomConfig();
assertThat(config.getParameter("comments"), is((Object) true));
assertThat(config.getParameter("entities"), is((Object) true));
}
@Test
public void testNormalizeDocument() throws Exception {
doc.appendChild(new XElement("root", new XElement("child"), "must", " be ", "concated"));
assertThat(doc.getDocumentElement().getChildNodes().getLength(), is(4));
doc.normalizeDocument();
assertThat(doc.getDocumentElement().getChildNodes().getLength(), is(2));
assertThat(doc.getDocumentElement().getLastChild().getTextContent(), is("must be concated"));
}
@Test
public void testRenameNodeNoNameSpace() throws Exception {
final Element root = doc.createElement("root");
doc.appendChild(root);
doc.renameNode(root, null, "newRoot");
assertThat(doc.toString(), is("<newRoot/>"));
}
@Test
public void testRenameNodeQualifiedName() throws Exception {
final Element root = doc.createElement("root");
doc.appendChild(root);
doc.renameNode(root, "// abc //", "abc:newRoot");
assertThat(doc.toString(), is("<abc:newRoot xmlns:abc=\"// abc //\"/>"));
}
@Test(expected = DOMException.class)
public void testRenameNodeFailQualifiedName() throws Exception {
final Element root = doc.createElement("root");
doc.appendChild(root);
final String nullNamespaceURI = null;
doc.renameNode(root, nullNamespaceURI, "abc:newRoot");
}
@Test
public void testCreateElement() throws Exception {
doc.appendChild(doc.createElement("root"));
assertThat(doc.toString(), is("<root/>"));
}
@Test
public void testCreateDocumentFragment() throws Exception {
final DocumentFragment documentFragment = doc.createDocumentFragment();
assertThat(documentFragment, is(notNullValue()));
}
@Test
public void testCreateTextNode() throws Exception {
assertThat(doc.createTextNode("text").getWholeText(), is("text"));
}
@Test
public void testCreateComment() throws Exception {
assertThat(doc.createComment("comment").getData(), is("comment"));
}
@Test
public void testCreateCDATASection() throws Exception {
assertThat(doc.createCDATASection("comment").getData(), is("comment"));
}
@Test
public void testCreateProcessingInstruction() throws Exception {
assertThat(doc.createProcessingInstruction("target", "a").getTarget(), is("target"));
}
@Test
public void testCreateAttribute() throws Exception {
final Attr a = doc.createAttribute("a");
a.setValue("val");
assertThat(a.getName(), is("a"));
assertThat(a.getValue(), is("val"));
}
@Test
public void testCreateEntityReference() throws Exception {
final EntityReference entityReference = doc.createEntityReference("a");
assertThat(entityReference, is(notNullValue()));
}
@Test
public void testImportNodeDeep() throws Exception {
final XElement fromOther = new XElement("other", new XElement("child"));
final Node node = doc.importNode(fromOther, true);
assertThat(node.getLocalName(), is("other"));
assertThat(node.getChildNodes().getLength(), is(1));
}
@Test
public void testImportNodeShallow() throws Exception {
final XElement fromOther = new XElement("other", new XElement("child"));
final Node node = doc.importNode(fromOther, false);
assertThat(node.getLocalName(), is("other"));
assertThat(node.getChildNodes().getLength(), is(0));
}
@Test
public void testCreateElementNS() throws Exception {
final Element e = doc.createElementNS("uri", "a");
assertThat(e.getLocalName(), is("a"));
assertThat(e.getNamespaceURI(), is("uri"));
}
@Test
public void testCreateAttributeNS() throws Exception {
final Attr attr = doc.createAttributeNS("uri", "key");
attr.setValue("1");
assertThat(new XElement("tag", attr).getAttribute("key"), is("1"));
}
@Test
public void testGetElementsByTagNameNS() throws Exception {
final XNamespace namespace = XNamespace.namespace("// uri //");
final XDocument doc = new XDocument(new XElement("root",
new XElement("child", new XElement("grandChild")),
new XElement("child", new XElement(namespace, "grandChild"))));
assertThat(doc.getElementsByTagName("grandChild").getLength(), is(2));
assertThat(doc.getElementsByTagNameNS("*", "grandChild").getLength(), is(2));
assertThat(doc.getElementsByTagNameNS(null, "grandChild").getLength(), is(1));
assertThat(doc.getElementsByTagNameNS(namespace.getURI(), "grandChild").getLength(), is(1));
}
@Test
public void testGetElementById() throws Exception {
final Element e = new XElement("child");
doc.appendChild(e);
e.setIdAttributeNode(new XAttr("id", "asdfva4wt"), true);
assertThat(e.getAttributeNode("id").isId(), is(true));
assertThat(doc.toString(), is("<child id=\"asdfva4wt\"/>"));
assertThat(doc.getElementById("asdfva4wt").isEqualNode(e), is(true));
}
@Test
public void testGetInputEncodingReturnNullIfCreatedOnMemory() throws Exception {
assertThat(new XDocument().getInputEncoding(), is(nullValue()));
}
private final String path = XDocumentTest.class.getClassLoader().getResource("simple.xml").getPath();
@Test
public void testGetInputEncodingReturnNonNullIfParsed() throws Exception {
final XDocument doc = XDocument.load(path);
assertThat(doc.getInputEncoding(), is("UTF-8"));
}
@Test
public void testInsertBefore() throws Exception {
final Comment c1 = doc.createComment("1");
final Comment c2 = doc.createComment("2");
doc.appendChild(c2);
doc.insertBefore(c1, c2);
assertThat(doc.toString(), is("<!--1--><!--2-->"));
}
@Test
public void testReplaceChild() throws Exception {
final Comment c1 = doc.createComment("1");
final Comment c2 = doc.createComment("2");
doc.appendChild(c1);
doc.replaceChild(c2, c1);
assertThat(doc.toString(), is("<!--2-->"));
}
@Test
public void testCompareDocumentPosition() throws Exception {
assertThat(doc.compareDocumentPosition(doc), is((short) 0));
final XElement root = new XElement("root");
doc.appendChild(root);
final short contain = Node.DOCUMENT_POSITION_FOLLOWING | Node.DOCUMENT_POSITION_CONTAINED_BY;
assertThat(doc.compareDocumentPosition(root), is(contain));
final short contained = Node.DOCUMENT_POSITION_PRECEDING | Node.DOCUMENT_POSITION_CONTAINS;
assertThat(root.compareDocumentPosition(doc), is(contained));
}
@Test
public void testGetDoctype() throws Exception {
assertThat(new XDocument().getDoctype(), is(nullValue()));
assertThat(new XDocument(new XDeclaration()).getDoctype(), is(nullValue()));
}
@Test
public void testGetImplementation() throws Exception {
final DOMImplementation impl = doc.getImplementation();
assertThat(impl, is(notNullValue()));
}
@Test
public void testLoadFromStringPath() throws Exception {
testPreserveSpace(XDocument.load(path));
}
@Test
public void testLoadFromFileStringPathIgnoreWhiteSpace() throws Exception {
testIgnoreSpace(XDocument.load(path, WhiteSpaceOption.Ignore));
}
@Test
public void testLoadFromFile() throws Exception {
final File file = new File(path);
testPreserveSpace(XDocument.load(file));
}
@Test
public void testLoadFromFileIgnoreWhiteSpace() throws Exception {
final File file = new File(path);
testIgnoreSpace(XDocument.load(file, WhiteSpaceOption.Ignore));
}
@Test
public void testLoadFromInputStream() throws Exception {
final InputStream stream = new FileInputStream(path);
testPreserveSpace(XDocument.load(stream));
}
@Test
public void testLoadFromInputStreamIgnoringSpace() throws Exception {
final InputStream stream = new FileInputStream(path);
testIgnoreSpace(XDocument.load(stream, WhiteSpaceOption.Ignore));
}
@Test
public void testLoadFromReader() throws Exception {
final Reader reader = new FileReader(path);
testPreserveSpace(XDocument.load(reader));
}
@Test
public void testLoadFromReaderIgnoringSpace() throws Exception {
final Reader reader = new FileReader(path);
testIgnoreSpace(XDocument.load(reader, WhiteSpaceOption.Ignore));
}
private void testIgnoreSpace(final XDocument document) {
final XDocument expected = new XDocument(new XElement("root",
new XElement("child1"),
new XElement("child2"),
new XElement("child3")));
assertThat(document.getDocumentElement().getChildNodes().getLength(),
is(expected.getDocumentElement().getChildNodes().getLength()));
assertThat(document.toString(), is(expected.toString()));
}
private void testPreserveSpace(final XDocument document) {
final XDocument expected = new XDocument(new XElement("root",
new XText("\n "),
new XElement("child1"),
new XText("\n "),
new XElement("child2"),
new XText("\n "),
new XElement("child3"),
new XText("\n")));
assertThat(document.getDocumentElement().getChildNodes().getLength(),
is(expected.getDocumentElement().getChildNodes().getLength()));
assertThat(document.toString(), is(expected.toString()));
}
@Test
public void testSaveFile() throws Exception {
final XDocument doc = XDocument.load(path);
final File tempFile = folder.newFile();
doc.save(tempFile);
final XDocument doc2 = XDocument.load(tempFile);
assertThat(doc.isEqualNode(doc2), is(true));
}
@Test
public void testSaveFilePath() throws Exception {
final XDocument doc = XDocument.load(path);
final File tempFile = folder.newFile("dummy.xml");
doc.save(tempFile.getPath());
final XDocument doc2 = XDocument.load(tempFile);
assertThat(doc.isEqualNode(doc2), is(true));
}
@Test
public void testSaveOutputStream() throws Exception {
final XDocument doc = XDocument.load(path);
final File tempFile = folder.newFile("dummy.xml");
final OutputStream stream = new FileOutputStream(tempFile, false);
doc.save(stream);
final XDocument doc2 = XDocument.load(tempFile);
assertThat(doc.isEqualNode(doc2), is(true));
}
}