001: /* Copyright 2002-2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026:
027: import nu.xom.*;
028:
029: /**
030: * <p>
031: * Test that CDATA sections are read and where possible
032: * preserved upon serialization.
033: * </p>
034: *
035: * @author Elliotte Rusty Harold
036: * @version 1.1b4
037: *
038: */
039: public class CDATASectionTest extends XOMTestCase {
040:
041: public CDATASectionTest(String name) {
042: super (name);
043: }
044:
045: private String data = "<test><child1><![CDATA[<&>]]></child1>"
046: + "<child2> <![CDATA[<&>]]> </child2> "
047: + "<child3><![CDATA[<&>]]> </child3> "
048: + "<child4><![CDATA[<&>]]> <![CDATA[<&>]]></child4> "
049: + "<child5><![CDATA[<&>]]>]]><![CDATA[<&>]]></child5> "
050: + "</test>";
051: private Document doc;
052: private Builder builder;
053:
054: protected void setUp() throws ValidityException, ParsingException,
055: IOException {
056: builder = new Builder();
057: doc = builder.build(data, "http://www.base.com");
058: }
059:
060: public void testCopy() {
061: Element child1 = doc.getRootElement().getFirstChildElement(
062: "child1");
063: Node cdata = child1.getChild(0);
064: Node copy = cdata.copy();
065: assertTrue(cdata instanceof Text);
066: assertEquals("nu.xom.CDATASection", copy.getClass().getName());
067: assertEquals("<&>", copy.getValue());
068: }
069:
070: public void testToXML() {
071: Element child1 = doc.getRootElement().getFirstChildElement(
072: "child1");
073: Node cdata = child1.getChild(0);
074: String result = cdata.toXML();
075: assertEquals("<![CDATA[<&>]]>", result);
076: }
077:
078: public void testToXMLWhenCDATASectionContainsEndDelimiter() {
079: Element child1 = doc.getRootElement().getFirstChildElement(
080: "child1");
081: Text cdata = (Text) child1.getChild(0);
082: cdata.setValue("A]]>A");
083: assertEquals("A]]>A", cdata.toXML());
084: }
085:
086: public void testUseCDATAWherePossible() {
087: Element child1 = doc.getRootElement().getFirstChildElement(
088: "child1");
089: Node cdata = child1.getChild(0);
090: assertTrue(cdata instanceof Text);
091: assertEquals("nu.xom.CDATASection", cdata.getClass().getName());
092: assertEquals("<&>", cdata.getValue());
093: }
094:
095: public void testDontAllowCDATASectionToSplitTextNode() {
096: Element child2 = doc.getRootElement().getFirstChildElement(
097: "child2");
098: assertEquals(1, child2.getChildCount());
099: Node data = child2.getChild(0);
100: assertTrue(data instanceof Text);
101: assertEquals("nu.xom.Text", data.getClass().getName());
102: assertEquals(" <&> ", data.getValue());
103: }
104:
105: public void testAccumulateTextNodeAfterCDATASection() {
106: Element child3 = doc.getRootElement().getFirstChildElement(
107: "child3");
108: assertEquals(1, child3.getChildCount());
109: Node data = child3.getChild(0);
110: assertTrue(data instanceof Text);
111: assertEquals("nu.xom.Text", data.getClass().getName());
112: assertEquals("<&> ", data.getValue());
113: }
114:
115: public void testAccumulateTextNodeAcrossMultipleCDATASections() {
116: Element child4 = doc.getRootElement().getFirstChildElement(
117: "child4");
118: assertEquals(1, child4.getChildCount());
119: Node data = child4.getChild(0);
120: assertTrue(data instanceof Text);
121: assertEquals("nu.xom.Text", data.getClass().getName());
122: assertEquals("<&> <&>", data.getValue());
123: }
124:
125: public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter() {
126: Element child5 = doc.getRootElement().getFirstChildElement(
127: "child5");
128: assertEquals(1, child5.getChildCount());
129: Node data = child5.getChild(0);
130: assertTrue(data instanceof Text);
131: assertEquals("<&>]]><&>", data.getValue());
132: assertEquals("<&>]]><&>", data.toXML());
133: }
134:
135: public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter2()
136: throws IOException {
137: Element child5 = doc.getRootElement().getFirstChildElement(
138: "child5");
139: assertEquals(1, child5.getChildCount());
140: child5.detach();
141: Document doc = new Document(child5);
142: ByteArrayOutputStream out = new ByteArrayOutputStream();
143: Serializer serializer = new Serializer(out);
144: serializer.write(doc);
145: serializer.flush();
146: String result = new String(out.toByteArray(), "UTF-8");
147: assertEquals(
148: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
149: + "<child5><&>]]><&></child5>\r\n",
150: result);
151: }
152:
153: public void testSerializeCDATASection() throws IOException {
154: ByteArrayOutputStream out = new ByteArrayOutputStream();
155: Serializer serializer = new Serializer(out);
156: serializer.write(doc);
157: byte[] data = out.toByteArray();
158: String result = new String(data, "UTF8");
159: assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0);
160:
161: }
162:
163: public void testSerializeCDATASectionWithOutOfRangeCharacter()
164: throws ValidityException, ParsingException, IOException {
165:
166: String data = "<test><![CDATA[\u0298]]></test>";
167: doc = builder.build(data, "http://www.example.com");
168: ByteArrayOutputStream out = new ByteArrayOutputStream();
169: Serializer serializer = new Serializer(out, "ISO-8859-1");
170: serializer.write(doc);
171: byte[] output = out.toByteArray();
172: String result = new String(output, "8859_1");
173: assertEquals(-1, result.indexOf("<![CDATA[<&>]]>"));
174: assertTrue(result.indexOf("ʘ") > 1);
175:
176: }
177:
178: public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding()
179: throws ValidityException, ParsingException, IOException {
180:
181: String data = "<test><![CDATA[abcd]]></test>";
182: doc = builder.build(data, "http://www.example.com");
183: ByteArrayOutputStream out = new ByteArrayOutputStream();
184: Serializer serializer = new Serializer(out, "ISO-8859-1");
185: serializer.write(doc);
186: byte[] output = out.toByteArray();
187: String result = new String(output, "8859_1");
188: assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1);
189:
190: }
191:
192: public void testSerializeCDATASectionWithCDATASectionEndDelimiter()
193: throws ValidityException, ParsingException, IOException {
194:
195: String data = "<test><![CDATA[original data]]></test>";
196: doc = builder.build(data, "http://www.example.com");
197: Text content = (Text) (doc.getRootElement().getChild(0));
198: content.setValue("]]>");
199: ByteArrayOutputStream out = new ByteArrayOutputStream();
200: Serializer serializer = new Serializer(out);
201: serializer.write(doc);
202: byte[] output = out.toByteArray();
203: String result = new String(output, "UTF8");
204: assertEquals(-1, result.indexOf("<![CDATA[]]>]]>"));
205: assertTrue(result.indexOf("]]>") > 1);
206: }
207:
208: }
|