Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes #47 - inconsistent serialization.

The major issue was the XPath serialization which was broken (still is but is now deprecated). That left nothing really to fix, so instead put in tests which put all the core JDOM types through the serialization wringer.
  • Loading branch information...
commit 7a9d980e2827bd07042f57bedfe33686c99cdaa2 1 parent 5e6b89e
@rolfl rolfl authored
Showing with 103 additions and 0 deletions.
  1. +103 −0 test/src/java/org/jdom2/test/cases/TestSerialization.java
View
103 test/src/java/org/jdom2/test/cases/TestSerialization.java
@@ -60,6 +60,9 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author unascribed
* @version 0.1
*/
+import java.util.Iterator;
+import java.util.List;
+
import org.jdom2.*;
import org.jdom2.filter.ElementFilter;
import org.jdom2.test.util.UnitTestUtil;
@@ -102,4 +105,104 @@ public void test_ElementFilterNamespace() {
public void test_ElementFilterEmpty() {
outAndBack(new ElementFilter());
}
+
+
+ public void compare(final Content a, final Content b) {
+ assertNotNull(a);
+ assertNotNull(b);
+ assertEquals(a.getClass(), b.getClass());
+ assertTrue(a != b);
+
+ switch (a.getCType()) {
+ case Text:
+ assertEquals(a.getValue(), b.getValue());
+ break;
+ case CDATA:
+ assertEquals(a.getValue(), b.getValue());
+ break;
+ case Comment:
+ assertEquals(((Comment)a).getText(), ((Comment)b).getText());
+ break;
+ case DocType:
+ DocType da = (DocType)a;
+ DocType db = (DocType)b;
+ assertEquals(da.getElementName(), db.getElementName());
+ assertEquals(da.getPublicID(), db.getPublicID());
+ assertEquals(da.getSystemID(), db.getSystemID());
+ assertEquals(da.getInternalSubset(), db.getInternalSubset());
+ break;
+ case Element:
+ Element ea = (Element)a;
+ Element eb = (Element)b;
+ assertEquals(ea.getName(), eb.getName());
+ compare(ea.getAttributes(), eb.getAttributes());
+ assertEquals(ea.getNamespacesInScope(), eb.getNamespacesInScope());
+ break;
+ case EntityRef:
+ assertEquals(((EntityRef)a).getName(), ((EntityRef)b).getName());
+ break;
+ case ProcessingInstruction:
+ ProcessingInstruction pa = (ProcessingInstruction)a;
+ ProcessingInstruction pb = (ProcessingInstruction)b;
+ assertEquals(pa.getTarget(), pb.getTarget());
+ assertEquals(pa.getData(), pb.getData());
+ break;
+ }
+ }
+
+
+ private void compare(List<Attribute> a, List<Attribute> b) {
+ assertTrue(a != b);
+ assertTrue(a.size() == b.size());
+ Iterator<Attribute> ait = a.iterator();
+ Iterator<Attribute> bit = b.iterator();
+ while (ait.hasNext() && bit.hasNext()) {
+ Attribute aa = ait.next();
+ Attribute bb = bit.next();
+ assertEquals(aa.getName(), bb.getName());
+ assertEquals(aa.getNamespace(), bb.getNamespace());
+ assertEquals(aa.getValue(), bb.getValue());
+ }
+ assertFalse(ait.hasNext());
+ assertFalse(bit.hasNext());
+
+ }
+
+
+ @Test
+ public void testDocumentSerialization() {
+ // test serialization of all JDOM core types.
+ // Document, Element, DocType, Comment, CDATA, Text,
+ // ProcessingInstruction, EntityRef, Namespace, and Attribute.
+ // Additionally, test ContentList and AttributeList
+ Document doc = new Document();
+ doc.setDocType(new DocType("root", "pubid", "sysid"));
+ doc.getDocType().setInternalSubset("internalss");
+ doc.addContent(new Comment("doccomment1"));
+ doc.addContent(new ProcessingInstruction("target1"));
+ doc.addContent(new ProcessingInstruction("target2").setData("key=value"));
+ doc.addContent(new Comment("doccomment2"));
+ Element root = new Element("root");
+ doc.setRootElement(root);
+ root.setAttribute(new Attribute("att", "value"));
+ root.addContent(new Text(" "));
+ root.addNamespaceDeclaration(Namespace.getNamespace("pfx", "uriupfx"));
+ root.addContent(new Element("child", Namespace.getNamespace("nopfxuri")));
+ root.addContent(new EntityRef("name"));
+ root.addContent(new CDATA("cdata"));
+
+
+ Document ser = UnitTestUtil.deSerialize(doc);
+ Iterator<Content> sit = ser.getDescendants();
+ Iterator<Content> dit = doc.getDescendants();
+ while (sit.hasNext() && dit.hasNext()) {
+ compare(sit.next(), dit.next());
+ }
+ assertFalse(sit.hasNext());
+ assertFalse(dit.hasNext());
+
+ }
+
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.