1
/* Copyright 2002-2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
31
* Test that CDATA sections are read and where possible
32
* preserved upon serialization.
35
* @author Elliotte Rusty Harold
39
public class CDATASectionTest extends XOMTestCase {
42
public CDATASectionTest(String name) {
47
private String data = "<test><child1><![CDATA[<&>]]></child1>"
48
+ "<child2> <![CDATA[<&>]]> </child2> "
49
+ "<child3><![CDATA[<&>]]> </child3> "
50
+ "<child4><![CDATA[<&>]]> <![CDATA[<&>]]></child4> "
51
+ "<child5><![CDATA[<&>]]>]]><![CDATA[<&>]]></child5> "
54
private Builder builder;
57
protected void setUp()
58
throws ValidityException, ParsingException, IOException {
59
builder = new Builder();
60
doc = builder.build(data, "http://www.base.com");
64
public void testCopy() {
65
Element child1 = doc.getRootElement().getFirstChildElement("child1");
66
Node cdata = child1.getChild(0);
67
Node copy = cdata.copy();
68
assertTrue(cdata instanceof Text);
69
assertEquals("nu.xom.CDATASection", copy.getClass().getName());
70
assertEquals("<&>", copy.getValue());
74
public void testToXML() {
75
Element child1 = doc.getRootElement().getFirstChildElement("child1");
76
Node cdata = child1.getChild(0);
77
String result = cdata.toXML();
78
assertEquals("<![CDATA[<&>]]>", result);
82
public void testToXMLWhenCDATASectionContainsEndDelimiter() {
83
Element child1 = doc.getRootElement().getFirstChildElement("child1");
84
Text cdata = (Text) child1.getChild(0);
85
cdata.setValue("A]]>A");
86
assertEquals("A]]>A", cdata.toXML());
90
public void testUseCDATAWherePossible() {
91
Element child1 = doc.getRootElement().getFirstChildElement("child1");
92
Node cdata = child1.getChild(0);
93
assertTrue(cdata instanceof Text);
94
assertEquals("nu.xom.CDATASection", cdata.getClass().getName());
95
assertEquals("<&>", cdata.getValue());
99
public void testDontAllowCDATASectionToSplitTextNode() {
100
Element child2 = doc.getRootElement().getFirstChildElement("child2");
101
assertEquals(1, child2.getChildCount());
102
Node data = child2.getChild(0);
103
assertTrue(data instanceof Text);
104
assertEquals("nu.xom.Text", data.getClass().getName());
105
assertEquals(" <&> ", data.getValue());
109
public void testAccumulateTextNodeAfterCDATASection() {
110
Element child3 = doc.getRootElement().getFirstChildElement("child3");
111
assertEquals(1, child3.getChildCount());
112
Node data = child3.getChild(0);
113
assertTrue(data instanceof Text);
114
assertEquals("nu.xom.Text", data.getClass().getName());
115
assertEquals("<&> ", data.getValue());
119
public void testAccumulateTextNodeAcrossMultipleCDATASections() {
120
Element child4 = doc.getRootElement().getFirstChildElement("child4");
121
assertEquals(1, child4.getChildCount());
122
Node data = child4.getChild(0);
123
assertTrue(data instanceof Text);
124
assertEquals("nu.xom.Text", data.getClass().getName());
125
assertEquals("<&> <&>", data.getValue());
129
public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter() {
130
Element child5 = doc.getRootElement().getFirstChildElement("child5");
131
assertEquals(1, child5.getChildCount());
132
Node data = child5.getChild(0);
133
assertTrue(data instanceof Text);
134
assertEquals("<&>]]><&>", data.getValue());
135
assertEquals("<&>]]><&>", data.toXML());
139
public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter2()
141
Element child5 = doc.getRootElement().getFirstChildElement("child5");
142
assertEquals(1, child5.getChildCount());
144
Document doc = new Document(child5);
145
ByteArrayOutputStream out = new ByteArrayOutputStream();
146
Serializer serializer = new Serializer(out);
147
serializer.write(doc);
149
String result = new String(out.toByteArray(), "UTF-8");
150
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
151
+ "<child5><&>]]><&></child5>\r\n", result);
155
public void testSerializeCDATASection() throws IOException {
156
ByteArrayOutputStream out = new ByteArrayOutputStream();
157
Serializer serializer = new Serializer(out);
158
serializer.write(doc);
159
byte[] data = out.toByteArray();
160
String result = new String(data, "UTF8");
161
assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0);
166
public void testSerializeCDATASectionWithOutOfRangeCharacter()
167
throws ValidityException, ParsingException, IOException {
169
String data = "<test><![CDATA[\u0298]]></test>";
170
doc = builder.build(data, "http://www.example.com");
171
ByteArrayOutputStream out = new ByteArrayOutputStream();
172
Serializer serializer = new Serializer(out, "ISO-8859-1");
173
serializer.write(doc);
174
byte[] output = out.toByteArray();
175
String result = new String(output, "8859_1");
176
assertEquals(-1, result.indexOf("<![CDATA[<&>]]>"));
177
assertTrue(result.indexOf("ʘ") > 1);
182
public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding()
183
throws ValidityException, ParsingException, IOException {
185
String data = "<test><![CDATA[abcd]]></test>";
186
doc = builder.build(data, "http://www.example.com");
187
ByteArrayOutputStream out = new ByteArrayOutputStream();
188
Serializer serializer = new Serializer(out, "ISO-8859-1");
189
serializer.write(doc);
190
byte[] output = out.toByteArray();
191
String result = new String(output, "8859_1");
192
assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1);
197
public void testSerializeCDATASectionWithCDATASectionEndDelimiter()
198
throws ValidityException, ParsingException, IOException {
200
String data = "<test><![CDATA[original data]]></test>";
201
doc = builder.build(data, "http://www.example.com");
202
Text content = (Text) (doc.getRootElement().getChild(0));
203
content.setValue("]]>");
204
ByteArrayOutputStream out = new ByteArrayOutputStream();
205
Serializer serializer = new Serializer(out);
206
serializer.write(doc);
207
byte[] output = out.toByteArray();
208
String result = new String(output, "UTF8");
209
assertEquals(-1, result.indexOf("<![CDATA[]]>]]>"));
210
assertTrue(result.indexOf("]]>") > 1);
b'\\ No newline at end of file'