Skip to content

Commit

Permalink
XMLDocument implements Closeable
Browse files Browse the repository at this point in the history
warnings in tests
  • Loading branch information
SoltauFintel committed Mar 18, 2017
1 parent 5cfd8f1 commit 2991ee2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,32 @@ public void w3cToThis() throws Exception {
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
final org.w3c.dom.Document w3cDoc = builder.parse(new ByteArrayInputStream(XML.getBytes()));

// test this
final XMLDocument doc = new XMLDocument(w3cDoc);

// verify
Assert.assertEquals(XML, shorten(doc.getXML()));
// test this
final XMLDocument doc = new XMLDocument(w3cDoc);
try {

// verify
Assert.assertEquals(XML, shorten(doc.getXML()));
} finally {
doc.close();
}
}

@Test
public void thisToW3c() throws Exception {
// prepare
final XMLDocument doc = new XMLDocument(XML);
try {

// test this
final org.w3c.dom.Document w3cDoc = doc.getW3CDocument();

// verify
final String result = DocumentUtils.toXML(w3cDoc);
Assert.assertEquals(XML, shorten(result));
// test this
final org.w3c.dom.Document w3cDoc = doc.getW3CDocument();

// verify
final String result = DocumentUtils.toXML(w3cDoc);
Assert.assertEquals(XML, shorten(result));
} finally {
doc.close();
}
}

private String shorten(final String xml) {
Expand Down
80 changes: 50 additions & 30 deletions xmldocument/src/test/java/de/mwvb/base/xml/XMLElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,71 @@ public class XMLElementTest {
@Test
public void testCdata1() {
XMLDocument dok = new XMLDocument("<test/>");
XMLElement root = dok.getElement();

XMLElement e = root.add("withCDATA");
e.setCdata("This is &a test.");
try {
XMLElement root = dok.getElement();

Assert.assertEquals("XML is wrong!", "<test><withCDATA><![CDATA[This is &a test.]]></withCDATA></test>", root.getXML());
Assert.assertEquals("There must be no attributes or child elements!", 0, e.getChildrenCount() + e.getAttributeCount());
XMLElement e = root.add("withCDATA");
e.setCdata("This is &a test.");

Assert.assertEquals("XML is wrong!", "<test><withCDATA><![CDATA[This is &a test.]]></withCDATA></test>",
root.getXML());
Assert.assertEquals("There must be no attributes or child elements!", 0,
e.getChildrenCount() + e.getAttributeCount());
} finally {
dok.close();
}
}

@Test
public void testCdata2() {
XMLDocument dok = new XMLDocument("<test><a/></test>");
XMLElement root = dok.getElement();

XMLElement e = root.add("withCDATA");
e.setCdata("This is a test.");

String cdata = dok.selectSingleNode("//withCDATA").getText();
Assert.assertEquals("getting CDATA does not work!", "This is a test.", cdata);
Assert.assertEquals("If there is no CDATA node, then \"\" should be returned!", "", dok.selectSingleNode("//a").getText());
Assert.assertEquals("CDATA content of sub-elements must not be returned!", "", root.getText());

e.setCdata(" & <more/> ]]>text.");
cdata = dok.selectSingleNode("//withCDATA").getText();
Assert.assertEquals("getting CDATA does not work!", "This is a test. & <more/> ]]>text.", cdata);
new XMLDocument(dok.getElement().getXML()); // Test whether the XML can be parsed.
Assert.assertTrue("']]>' split test failed", dok.getElement().getXML().contains("![CDATA[>text"));
try {
XMLElement root = dok.getElement();

XMLElement e = root.add("withCDATA");
e.setCdata("This is a test.");

String cdata = dok.selectSingleNode("//withCDATA").getText();
Assert.assertEquals("getting CDATA does not work!", "This is a test.", cdata);
Assert.assertEquals("If there is no CDATA node, then \"\" should be returned!", "",
dok.selectSingleNode("//a").getText());
Assert.assertEquals("CDATA content of sub-elements must not be returned!", "", root.getText());

e.setCdata(" & <more/> ]]>text.");
cdata = dok.selectSingleNode("//withCDATA").getText();
Assert.assertEquals("getting CDATA does not work!", "This is a test. & <more/> ]]>text.", cdata);
new XMLDocument(dok.getElement().getXML()) // Test whether the XML can be parsed.
.close();
Assert.assertTrue("']]>' split test failed", dok.getElement().getXML().contains("![CDATA[>text"));
} finally {
dok.close();
}
}

@Test
public void testAddManyAttributes() {
XMLDocument dok = new XMLDocument("<test/>");
XMLElement root = dok.getElement();

XMLElement e = root.addWithAttributes("properties", "attr1", "value1", "attr2", "value2");
e.setValue("a", "b");

Assert.assertEquals(3, root.selectSingleNode("properties").getAttributeCount());
try {
XMLElement root = dok.getElement();

XMLElement e = root.addWithAttributes("properties", "attr1", "value1", "attr2", "value2");
e.setValue("a", "b");

Assert.assertEquals(3, root.selectSingleNode("properties").getAttributeCount());
} finally {
dok.close();
}
}

@Test(expected = IllegalArgumentException.class)
public void testAddManyAttributes_odd() {
XMLDocument dok = new XMLDocument("<test/>");
XMLElement root = dok.getElement();

root.addWithAttributes("properties", "attr1"); // error: odd number of attrs-arguments
try {
XMLElement root = dok.getElement();

root.addWithAttributes("properties", "attr1"); // error: odd number of attrs-arguments
} finally {
dok.close();
}
}
}

0 comments on commit 2991ee2

Please sign in to comment.