KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > CDATASectionTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21
22 package nu.xom.tests;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import nu.xom.*;
28
29 /**
30  * <p>
31  * Test that CDATA sections are read and where possible
32  * preserved upon serialization.
33  * </p>
34  *
35  * @author Elliotte Rusty Harold
36  * @version 1.0
37  *
38  */

39 public class CDATASectionTest extends XOMTestCase {
40
41     
42     public CDATASectionTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     
47     private String JavaDoc data = "<test><child1><![CDATA[<&>]]></child1>"
48      + "<child2> <![CDATA[<&>]]> </child2> "
49      + "<child3><![CDATA[<&>]]> </child3> "
50      + "<child4><![CDATA[<&>]]> <![CDATA[<&>]]></child4> "
51      + "</test>";
52     private Document doc;
53     private Builder builder;
54     
55     
56     protected void setUp()
57       throws ValidityException, ParsingException, IOException JavaDoc {
58         builder = new Builder();
59         doc = builder.build(data, "http://www.base.com");
60     }
61     
62     
63     public void testCopy() {
64         Element child1 = doc.getRootElement().getFirstChildElement("child1");
65         Node cdata = child1.getChild(0);
66         Node copy = cdata.copy();
67         assertTrue(cdata instanceof Text);
68         assertEquals("nu.xom.CDATASection", copy.getClass().getName());
69         assertEquals("<&>", copy.getValue());
70     }
71
72     
73     public void testUseCDATAWherePossible() {
74         Element child1 = doc.getRootElement().getFirstChildElement("child1");
75         Node cdata = child1.getChild(0);
76         assertTrue(cdata instanceof Text);
77         assertEquals("nu.xom.CDATASection", cdata.getClass().getName());
78         assertEquals("<&>", cdata.getValue());
79     }
80
81     
82     public void testDontAllowCDATASectionToSplitTextNode() {
83         Element child2 = doc.getRootElement().getFirstChildElement("child2");
84         assertEquals(1, child2.getChildCount());
85         Node data = child2.getChild(0);
86         assertTrue(data instanceof Text);
87         assertEquals("nu.xom.Text", data.getClass().getName());
88         assertEquals(" <&> ", data.getValue());
89     }
90     
91     
92     public void testAccumulateTextNodeAfterCDATASection() {
93         Element child3 = doc.getRootElement().getFirstChildElement("child3");
94         assertEquals(1, child3.getChildCount());
95         Node data = child3.getChild(0);
96         assertTrue(data instanceof Text);
97         assertEquals("nu.xom.Text", data.getClass().getName());
98         assertEquals("<&> ", data.getValue());
99     }
100     
101     
102     public void testAccumulateTextNodeAcrossMultipleCDATASections() {
103         Element child4 = doc.getRootElement().getFirstChildElement("child4");
104         assertEquals(1, child4.getChildCount());
105         Node data = child4.getChild(0);
106         assertTrue(data instanceof Text);
107         assertEquals("nu.xom.Text", data.getClass().getName());
108         assertEquals("<&> <&>", data.getValue());
109     }
110     
111     
112     public void testSerializeCDATASection() throws IOException JavaDoc {
113         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
114         Serializer serializer = new Serializer(out);
115         serializer.write(doc);
116         byte[] data = out.toByteArray();
117         String JavaDoc result = new String JavaDoc(data, "UTF8");
118         assertTrue(result.indexOf("<![CDATA[<&>]]>") > 0);
119         
120     }
121
122     
123     public void testSerializeCDATASectionWithOutOfRangeCharacter()
124       throws ValidityException, ParsingException, IOException JavaDoc {
125           
126         String JavaDoc data = "<test><![CDATA[\u0298]]></test>";
127         doc = builder.build(data, "http://www.example.com");
128         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
129         Serializer serializer = new Serializer(out, "ISO-8859-1");
130         serializer.write(doc);
131         byte[] output = out.toByteArray();
132         String JavaDoc result = new String JavaDoc(output, "8859_1");
133         assertEquals(-1, result.indexOf("<![CDATA[<&>]]>"));
134         assertTrue(result.indexOf("&#x298;") > 1);
135         
136     }
137
138     
139     public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding()
140       throws ValidityException, ParsingException, IOException JavaDoc {
141           
142         String JavaDoc data = "<test><![CDATA[abcd]]></test>";
143         doc = builder.build(data, "http://www.example.com");
144         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
145         Serializer serializer = new Serializer(out, "ISO-8859-1");
146         serializer.write(doc);
147         byte[] output = out.toByteArray();
148         String JavaDoc result = new String JavaDoc(output, "8859_1");
149         assertTrue(result.indexOf("<![CDATA[abcd]]>") > 1);
150         
151     }
152
153     
154     public void testSerializeCDATASectionWithCDATASectionEndDelimiter()
155       throws ValidityException, ParsingException, IOException JavaDoc {
156           
157         String JavaDoc data = "<test><![CDATA[original data]]></test>";
158         doc = builder.build(data, "http://www.example.com");
159         Text content = (Text) (doc.getRootElement().getChild(0));
160         content.setValue("]]>");
161         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
162         Serializer serializer = new Serializer(out);
163         serializer.write(doc);
164         byte[] output = out.toByteArray();
165         String JavaDoc result = new String JavaDoc(output, "UTF8");
166         assertEquals(-1, result.indexOf("<![CDATA[]]>]]>"));
167         assertTrue(result.indexOf("]]&gt;") > 1);
168     }
169
170
171 }
Popular Tags