DocumentBuilderTest.java revision 3758ae700d96e0a0ae2781599b6fb9688976c280
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 junit.framework.Test;
20import junit.framework.TestSuite;
21
22import static tests.support.Support_Xml.*;
23
24public class DocumentBuilderTest extends junit.framework.TestCase {
25    // http://code.google.com/p/android/issues/detail?id=2607
26    public void test_characterReferences() throws Exception {
27        assertEquals("aAb", firstChildTextOf(domOf("<p>a&#65;b</p>")));
28        assertEquals("aAb", firstChildTextOf(domOf("<p>a&#x41;b</p>")));
29    }
30
31    // http://code.google.com/p/android/issues/detail?id=2607
32    public void test_predefinedEntities() throws Exception {
33        assertEquals("a<b", firstChildTextOf(domOf("<p>a&lt;b</p>")));
34        assertEquals("a>b", firstChildTextOf(domOf("<p>a&gt;b</p>")));
35        assertEquals("a&b", firstChildTextOf(domOf("<p>a&amp;b</p>")));
36        assertEquals("a'b", firstChildTextOf(domOf("<p>a&apos;b</p>")));
37        assertEquals("a\"b", firstChildTextOf(domOf("<p>a&quot;b</p>")));
38    }
39
40    // http://code.google.com/p/android/issues/detail?id=2487
41    public void test_cdata_attributes() throws Exception {
42        assertEquals("hello & world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &amp; world\" />"))));
43        try {
44            domOf("<?xml version=\"1.0\"?><root attr=\"hello <![CDATA[ some-cdata ]]> world\" />");
45            fail("SAXParseException not thrown");
46        } catch (org.xml.sax.SAXParseException ex) {
47            // Expected.
48        }
49        assertEquals("hello <![CDATA[ some-cdata ]]> world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &lt;![CDATA[ some-cdata ]]&gt; world\" />"))));
50        assertEquals("hello <![CDATA[ some-cdata ]]> world", attrOf(firstElementOf(domOf("<?xml version=\"1.0\"?><root attr=\"hello &lt;![CDATA[ some-cdata ]]> world\" />"))));
51    }
52
53    // http://code.google.com/p/android/issues/detail?id=2487
54    public void test_cdata_body() throws Exception {
55        assertEquals("hello & world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello &amp; world</root>")));
56        assertEquals("hello  some-cdata  world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello <![CDATA[ some-cdata ]]> world</root>")));
57        assertEquals("hello <![CDATA[ some-cdata ]]> world", firstChildTextOf(domOf("<?xml version=\"1.0\"?><root>hello &lt;![CDATA[ some-cdata ]]&gt; world</root>")));
58        try {
59            domOf("<?xml version=\"1.0\"?><root>hello &lt;![CDATA[ some-cdata ]]> world</root>");
60            fail("SAXParseException not thrown");
61        } catch (org.xml.sax.SAXParseException ex) {
62            // Expected.
63        }
64    }
65}
66