From 12740e2b70c48e478af53de31624c388e1e89e0f Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Tue, 24 Jan 2012 17:05:31 +0000 Subject: Fix XML serialisation. Also port the unit tests from Swiften. diff --git a/src/com/isode/stroke/serializer/xml/XMLElement.java b/src/com/isode/stroke/serializer/xml/XMLElement.java index 43abfea..9512c85 100644 --- a/src/com/isode/stroke/serializer/xml/XMLElement.java +++ b/src/com/isode/stroke/serializer/xml/XMLElement.java @@ -57,11 +57,11 @@ public class XMLElement implements XMLNode { public void setAttribute(String attribute, String value) { String escapedValue = value; - escapedValue.replaceAll("&", "&"); - escapedValue.replaceAll("<", "<"); - escapedValue.replaceAll(">", ">"); - escapedValue.replaceAll("'", "'"); - escapedValue.replaceAll("\"", """); + escapedValue = escapedValue.replaceAll("&", "&"); + escapedValue = escapedValue.replaceAll("<", "<"); + escapedValue = escapedValue.replaceAll(">", ">"); + escapedValue = escapedValue.replaceAll("'", "'"); + escapedValue = escapedValue.replaceAll("\"", """); attributes_.put(attribute, escapedValue); } diff --git a/src/com/isode/stroke/serializer/xml/XMLTextNode.java b/src/com/isode/stroke/serializer/xml/XMLTextNode.java index b6940c4..fa1881e 100644 --- a/src/com/isode/stroke/serializer/xml/XMLTextNode.java +++ b/src/com/isode/stroke/serializer/xml/XMLTextNode.java @@ -10,13 +10,13 @@ package com.isode.stroke.serializer.xml; public class XMLTextNode implements XMLNode { - private final String text_; + private String text_; public XMLTextNode(String text) { text_ = text; - text_.replaceAll("&", "&"); // Should come first - text_.replaceAll("<", "<"); - text_.replaceAll(">", ">"); + text_ = text_.replaceAll("&", "&"); // Should come first + text_ = text_.replaceAll("<", "<"); + text_ = text_.replaceAll(">", ">"); } public String serialize() { diff --git a/test/com/isode/stroke/serializer/xml/XMLElementTest.java b/test/com/isode/stroke/serializer/xml/XMLElementTest.java new file mode 100644 index 0000000..8377193 --- /dev/null +++ b/test/com/isode/stroke/serializer/xml/XMLElementTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.serializer.xml; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class XMLElementTest { + + @Test + public void testSerialize() { + XMLElement testling = new XMLElement("foo", "http://example.com"); + testling.setAttribute("myatt", "myval"); + XMLElement barElement = new XMLElement("bar"); + barElement.addNode(new XMLTextNode("Blo")); + testling.addNode(barElement); + XMLElement bazElement = new XMLElement("baz"); + bazElement.addNode(new XMLTextNode("Bli&")); + testling.addNode(bazElement); + + String result = testling.serialize(); + String expectedResult = + "" + + "Blo" + + "Bli&</stream>" + + ""; + + assertEquals(expectedResult, result); + } + + @Test + public void testSerialize_NoChildren() { + XMLElement testling = new XMLElement("foo", "http://example.com"); + + assertEquals("", testling.serialize()); + } + + @Test + public void testSerialize_SpecialAttributeCharacters() { + XMLElement testling = new XMLElement("foo"); + testling.setAttribute("myatt", "<\"'&>"); + + assertEquals("", testling.serialize()); + } + + @Test + public void testSerialize_EmptyAttributeValue() { + XMLElement testling = new XMLElement("foo"); + testling.setAttribute("myatt", ""); + + assertEquals("", testling.serialize()); + } +} + + -- cgit v0.10.2-6-g49f6