|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import java.io.ByteArrayOutputStream; |
|
25 |
| import java.io.IOException; |
|
26 |
| |
|
27 |
| import nu.xom.*; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public class CDATASectionTest extends XOMTestCase { |
|
40 |
| |
|
41 |
| |
|
42 |
13
| public CDATASectionTest(String name) {
|
|
43 |
13
| super(name);
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
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> " |
|
52 |
| + "</test>"; |
|
53 |
| private Document doc; |
|
54 |
| private Builder builder; |
|
55 |
| |
|
56 |
| |
|
57 |
13
| protected void setUp()
|
|
58 |
| throws ValidityException, ParsingException, IOException { |
|
59 |
13
| builder = new Builder();
|
|
60 |
13
| doc = builder.build(data, "http://www.base.com");
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
1
| public void testCopy() {
|
|
65 |
1
| Element child1 = doc.getRootElement().getFirstChildElement("child1");
|
|
66 |
1
| Node cdata = child1.getChild(0);
|
|
67 |
1
| Node copy = cdata.copy();
|
|
68 |
1
| assertTrue(cdata instanceof Text);
|
|
69 |
1
| assertEquals("nu.xom.CDATASection", copy.getClass().getName());
|
|
70 |
1
| assertEquals("<&>", copy.getValue());
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
1
| public void testToXML() {
|
|
75 |
1
| Element child1 = doc.getRootElement().getFirstChildElement("child1");
|
|
76 |
1
| Node cdata = child1.getChild(0);
|
|
77 |
1
| String result = cdata.toXML();
|
|
78 |
1
| assertEquals("<![CDATA[<&>]]>", result);
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
1
| public void testToXMLWhenCDATASectionContainsEndDelimiter() {
|
|
83 |
1
| Element child1 = doc.getRootElement().getFirstChildElement("child1");
|
|
84 |
1
| Text cdata = (Text) child1.getChild(0);
|
|
85 |
1
| cdata.setValue("A]]>A");
|
|
86 |
1
| assertEquals("A]]>A", cdata.toXML());
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
1
| public void testUseCDATAWherePossible() {
|
|
91 |
1
| Element child1 = doc.getRootElement().getFirstChildElement("child1");
|
|
92 |
1
| Node cdata = child1.getChild(0);
|
|
93 |
1
| assertTrue(cdata instanceof Text);
|
|
94 |
1
| assertEquals("nu.xom.CDATASection", cdata.getClass().getName());
|
|
95 |
1
| assertEquals("<&>", cdata.getValue());
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
1
| public void testDontAllowCDATASectionToSplitTextNode() {
|
|
100 |
1
| Element child2 = doc.getRootElement().getFirstChildElement("child2");
|
|
101 |
1
| assertEquals(1, child2.getChildCount());
|
|
102 |
1
| Node data = child2.getChild(0);
|
|
103 |
1
| assertTrue(data instanceof Text);
|
|
104 |
1
| assertEquals("nu.xom.Text", data.getClass().getName());
|
|
105 |
1
| assertEquals(" <&> ", data.getValue());
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
1
| public void testAccumulateTextNodeAfterCDATASection() {
|
|
110 |
1
| Element child3 = doc.getRootElement().getFirstChildElement("child3");
|
|
111 |
1
| assertEquals(1, child3.getChildCount());
|
|
112 |
1
| Node data = child3.getChild(0);
|
|
113 |
1
| assertTrue(data instanceof Text);
|
|
114 |
1
| assertEquals("nu.xom.Text", data.getClass().getName());
|
|
115 |
1
| assertEquals("<&> ", data.getValue());
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
1
| public void testAccumulateTextNodeAcrossMultipleCDATASections() {
|
|
120 |
1
| Element child4 = doc.getRootElement().getFirstChildElement("child4");
|
|
121 |
1
| assertEquals(1, child4.getChildCount());
|
|
122 |
1
| Node data = child4.getChild(0);
|
|
123 |
1
| assertTrue(data instanceof Text);
|
|
124 |
1
| assertEquals("nu.xom.Text", data.getClass().getName());
|
|
125 |
1
| assertEquals("<&> <&>", data.getValue());
|
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
1
| public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter() {
|
|
130 |
1
| Element child5 = doc.getRootElement().getFirstChildElement("child5");
|
|
131 |
1
| assertEquals(1, child5.getChildCount());
|
|
132 |
1
| Node data = child5.getChild(0);
|
|
133 |
1
| assertTrue(data instanceof Text);
|
|
134 |
1
| assertEquals("<&>]]><&>", data.getValue());
|
|
135 |
1
| assertEquals("<&>]]><&>", data.toXML());
|
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
1
| public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter2()
|
|
140 |
| throws IOException { |
|
141 |
1
| Element child5 = doc.getRootElement().getFirstChildElement("child5");
|
|
142 |
1
| assertEquals(1, child5.getChildCount());
|
|
143 |
1
| child5.detach();
|
|
144 |
1
| Document doc = new Document(child5);
|
|
145 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
146 |
1
| Serializer serializer = new Serializer(out);
|
|
147 |
1
| serializer.write(doc);
|
|
148 |
1
| serializer.flush();
|
|
149 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
150 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
151 |
| + "<child5><&>]]><&></child5>\r\n", result); |
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
1
| public void testSerializeCDATASection() throws IOException {
|
|
156 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
157 |
1
| Serializer serializer = new Serializer(out);
|
|
158 |
1
| serializer.write(doc);
|
|
159 |
1
| byte[] data = out.toByteArray();
|
|
160 |
1
| String result = new String(data, "UTF8");
|
|
161 |
1
| assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0);
|
|
162 |
| |
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
1
| public void testSerializeCDATASectionWithOutOfRangeCharacter()
|
|
167 |
| throws ValidityException, ParsingException, IOException { |
|
168 |
| |
|
169 |
1
| String data = "<test><![CDATA[\u0298]]></test>";
|
|
170 |
1
| doc = builder.build(data, "http://www.example.com");
|
|
171 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
172 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
173 |
1
| serializer.write(doc);
|
|
174 |
1
| byte[] output = out.toByteArray();
|
|
175 |
1
| String result = new String(output, "8859_1");
|
|
176 |
1
| assertEquals(-1, result.indexOf("<![CDATA[<&>]]>"));
|
|
177 |
1
| assertTrue(result.indexOf("ʘ") > 1);
|
|
178 |
| |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
1
| public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding()
|
|
183 |
| throws ValidityException, ParsingException, IOException { |
|
184 |
| |
|
185 |
1
| String data = "<test><![CDATA[abcd]]></test>";
|
|
186 |
1
| doc = builder.build(data, "http://www.example.com");
|
|
187 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
188 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
189 |
1
| serializer.write(doc);
|
|
190 |
1
| byte[] output = out.toByteArray();
|
|
191 |
1
| String result = new String(output, "8859_1");
|
|
192 |
1
| assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1);
|
|
193 |
| |
|
194 |
| } |
|
195 |
| |
|
196 |
| |
|
197 |
1
| public void testSerializeCDATASectionWithCDATASectionEndDelimiter()
|
|
198 |
| throws ValidityException, ParsingException, IOException { |
|
199 |
| |
|
200 |
1
| String data = "<test><![CDATA[original data]]></test>";
|
|
201 |
1
| doc = builder.build(data, "http://www.example.com");
|
|
202 |
1
| Text content = (Text) (doc.getRootElement().getChild(0));
|
|
203 |
1
| content.setValue("]]>");
|
|
204 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
205 |
1
| Serializer serializer = new Serializer(out);
|
|
206 |
1
| serializer.write(doc);
|
|
207 |
1
| byte[] output = out.toByteArray();
|
|
208 |
1
| String result = new String(output, "UTF8");
|
|
209 |
1
| assertEquals(-1, result.indexOf("<![CDATA[]]>]]>"));
|
|
210 |
1
| assertTrue(result.indexOf("]]>") > 1);
|
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| } |