DocumentBuilderTest.java revision ef4b3bb4dc2c1c5b0b29209f6d3ea2126be3be0d
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.Node;
22import org.w3c.dom.NodeList;
23
24import junit.framework.Test;
25import junit.framework.TestSuite;
26
27public class DocumentBuilderTest extends junit.framework.TestCase {
28    private static String parse(String xml) throws Exception {
29        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
30        dbf.setCoalescing(true);
31        dbf.setExpandEntityReferences(true);
32
33        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
34        DocumentBuilder builder = dbf.newDocumentBuilder();
35
36        Document doc = builder.parse(stream);
37
38        Node titleNode = doc.getFirstChild();
39        NodeList children = titleNode.getChildNodes();
40        assertEquals(1, children.getLength());
41        return children.item(0).getNodeValue();
42    }
43
44    // http://code.google.com/p/android/issues/detail?id=2607
45    public void test_characterReferences() throws Exception {
46        assertEquals("aAb", parse("<p>a&#65;b</p>"));
47        assertEquals("aAb", parse("<p>a&#x41;b</p>"));
48    }
49
50    // http://code.google.com/p/android/issues/detail?id=2607
51    public void test_predefinedEntities() throws Exception {
52        assertEquals("a<b", parse("<p>a&lt;b</p>"));
53        assertEquals("a>b", parse("<p>a&gt;b</p>"));
54        assertEquals("a&b", parse("<p>a&amp;b</p>"));
55        assertEquals("a'b", parse("<p>a&apos;b</p>"));
56        assertEquals("a\"b", parse("<p>a&quot;b</p>"));
57    }
58}
59