DocumentBuilderTest.java revision 094afca45544ca924da51f359ebfdd174ba3ba08
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package javax.xml.parsers;
18
19import java.io.ByteArrayInputStream;
20import org.w3c.dom.Document;
21import org.w3c.dom.Element;
22import org.w3c.dom.Node;
23import org.w3c.dom.NodeList;
24
25import junit.framework.Test;
26import junit.framework.TestSuite;
27
28public class DocumentBuilderTest extends junit.framework.TestCase {
29    private static Document domOf(String xml) throws Exception {
30        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
31        dbf.setCoalescing(true);
32        dbf.setExpandEntityReferences(true);
33
34        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
35        DocumentBuilder builder = dbf.newDocumentBuilder();
36
37        return builder.parse(stream);
38    }
39
40    private static String firstChildTextOf(Document doc) throws Exception {
41        NodeList children = doc.getFirstChild().getChildNodes();
42        assertEquals(1, children.getLength());
43        return children.item(0).getNodeValue();
44    }
45
46    // http://code.google.com/p/android/issues/detail?id=2607
47    public void test_characterReferences() throws Exception {
48        assertEquals("aAb", firstChildTextOf(domOf("<p>a&#65;b</p>")));
49        assertEquals("aAb", firstChildTextOf(domOf("<p>a&#x41;b</p>")));
50    }
51
52    // http://code.google.com/p/android/issues/detail?id=2607
53    public void test_predefinedEntities() throws Exception {
54        assertEquals("a<b", firstChildTextOf(domOf("<p>a&lt;b</p>")));
55        assertEquals("a>b", firstChildTextOf(domOf("<p>a&gt;b</p>")));
56        assertEquals("a&b", firstChildTextOf(domOf("<p>a&amp;b</p>")));
57        assertEquals("a'b", firstChildTextOf(domOf("<p>a&apos;b</p>")));
58        assertEquals("a\"b", firstChildTextOf(domOf("<p>a&quot;b</p>")));
59    }
60
61    private static Element firstElementOf(Document doc) throws Exception {
62        return (Element) doc.getFirstChild();
63    }
64
65    private static String attrOf(Element e) throws Exception {
66        return e.getAttribute("attr");
67    }
68
69    // http://code.google.com/p/android/issues/detail?id=2487
70    public void test_cdata_attributes() throws Exception {
71        assertEquals("hello & world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &amp; world\" />"))));
72        try {
73            domOf("<?xml version=\"1.0\"?><root attr=\"hello <![CDATA[ some-cdata ]]> world\" />");
74            fail("SAXParseException not thrown");
75        } catch (org.xml.sax.SAXParseException ex) {
76            // Expected.
77        }
78        assertEquals("hello <![CDATA[ some-cdata ]]> world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &lt;![CDATA[ some-cdata ]]&gt; world\" />"))));
79        assertEquals("hello <![CDATA[ some-cdata ]]> world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &lt;![CDATA[ some-cdata ]]> world\" />"))));
80    }
81
82    // http://code.google.com/p/android/issues/detail?id=2487
83    public void test_cdata_body() throws Exception {
84        assertEquals("hello & world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello &amp; world</root>")));
85        assertEquals("hello  some-cdata  world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello <![CDATA[ some-cdata ]]> world</root>")));
86        assertEquals("hello <![CDATA[ some-cdata ]]> world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello &lt;![CDATA[ some-cdata ]]&gt; world</root>")));
87        try {
88            domOf("<?xml version=\"1.0\"?><root>hello &lt;![CDATA[ some-cdata ]]> world</root>");
89            fail("SAXParseException not thrown");
90        } catch (org.xml.sax.SAXParseException ex) {
91            // Expected.
92        }
93    }
94}
95