~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/CDATASectionTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002-2005 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;
 
25
import java.io.IOException;
 
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.1b4
 
37
 *
 
38
 */
 
39
public class CDATASectionTest extends XOMTestCase {
 
40
 
 
41
    
 
42
    public CDATASectionTest(String name) {
 
43
        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[<&>]]>]]&gt;<![CDATA[<&>]]></child5> "
 
52
     + "</test>";
 
53
    private Document doc;
 
54
    private Builder builder;
 
55
    
 
56
    
 
57
    protected void setUp() 
 
58
      throws ValidityException, ParsingException, IOException {
 
59
        builder = new Builder();
 
60
        doc = builder.build(data, "http://www.base.com");   
 
61
    }
 
62
    
 
63
    
 
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());  
 
71
    }
 
72
 
 
73
    
 
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);  
 
79
    }
 
80
 
 
81
    
 
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]]&gt;A", cdata.toXML());  
 
87
    }
 
88
 
 
89
    
 
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());  
 
96
    }
 
97
 
 
98
    
 
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());  
 
106
    }
 
107
    
 
108
    
 
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());  
 
116
    }
 
117
    
 
118
    
 
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());  
 
126
    }
 
127
    
 
128
    
 
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("&lt;&amp;&gt;]]&gt;&lt;&amp;&gt;", data.toXML());
 
136
    }
 
137
    
 
138
    
 
139
    public void testDontAllowCDATASectionToContainCDATASectionEndDelimiter2() 
 
140
      throws IOException {
 
141
        Element child5 = doc.getRootElement().getFirstChildElement("child5");
 
142
        assertEquals(1, child5.getChildCount());
 
143
        child5.detach();
 
144
        Document doc = new Document(child5);
 
145
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
146
        Serializer serializer = new Serializer(out);
 
147
        serializer.write(doc);
 
148
        serializer.flush();
 
149
        String result = new String(out.toByteArray(), "UTF-8");
 
150
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 
151
          + "<child5>&lt;&amp;&gt;]]&gt;&lt;&amp;&gt;</child5>\r\n", result);
 
152
    }
 
153
    
 
154
    
 
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);
 
162
        
 
163
    }
 
164
 
 
165
    
 
166
    public void testSerializeCDATASectionWithOutOfRangeCharacter() 
 
167
      throws ValidityException, ParsingException, IOException {  
 
168
          
 
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("&#x298;") > 1);
 
178
        
 
179
    }
 
180
 
 
181
    
 
182
    public void testSerializeCDATASectionWithInRangeCharactersAndANonUnicodeEncoding() 
 
183
      throws ValidityException, ParsingException, IOException {  
 
184
          
 
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);
 
193
        
 
194
    }
 
195
 
 
196
    
 
197
    public void testSerializeCDATASectionWithCDATASectionEndDelimiter() 
 
198
      throws ValidityException, ParsingException, IOException {  
 
199
          
 
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("]]&gt;") > 1);
 
211
    }
 
212
 
 
213
 
 
214
}
 
 
b'\\ No newline at end of file'