DeclarationTest.java revision 1c92541d38ed4d0b6e958f478cd7b129f140cd44
1/*
2 * Copyright (C) 2010 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 libcore.xml;
18
19import dalvik.annotation.KnownFailure;
20import junit.framework.TestCase;
21import org.w3c.dom.Document;
22import org.xml.sax.InputSource;
23
24import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.DocumentBuilderFactory;
26import java.io.File;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import java.io.OutputStream;
30
31/**
32 * Test the parsing of the XML declaration, plus the additional document fields
33 * captured during parsing.
34 */
35public class DeclarationTest extends TestCase {
36
37    private String systemIdA;
38    private Document documentA;
39
40    private String systemIdB;
41    private Document documentB;
42
43    @Override protected void setUp() throws Exception {
44        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45        factory.setNamespaceAware(true);
46        DocumentBuilder builder = factory.newDocumentBuilder();
47
48        systemIdA = stringToSystemId(
49                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><foo />");
50        InputSource inputSourceA = new InputSource(systemIdA);
51        inputSourceA.setEncoding("US-ASCII");
52        documentA = builder.parse(inputSourceA);
53
54        systemIdB = stringToSystemId(
55                "<?xml version=\"1.1\" encoding=\"US-ASCII\" standalone=\"yes\" ?><foo />");
56        InputSource inputSourceB = new InputSource(systemIdB);
57        inputSourceB.setEncoding("ISO-8859-1");
58        documentB = builder.parse(inputSourceB);
59    }
60
61    private String stringToSystemId(String contents) throws IOException {
62        File file = File.createTempFile("temp", "xml");
63        file.deleteOnExit();
64        OutputStream out = new FileOutputStream(file);
65        out.write(contents.getBytes("UTF-8"));
66        out.close();
67        return "file:" + file;
68    }
69
70    /**
71     * XML parsers are advised of the document's character set via two channels:
72     * via the declaration and also the document's input source. To test that
73     * each of these winds up in the correct location in the document model, we
74     * supply different names for each. This is only safe because for the subset
75     * of characters in the document, the character sets are equivalent.
76     */
77    public void testGetInputEncoding() throws Exception {
78        assertEquals("US-ASCII", documentA.getInputEncoding());
79        assertEquals("ISO-8859-1", documentB.getInputEncoding());
80    }
81
82    @KnownFailure("Dalvik doesn't parse the XML declaration")
83    public void testGetXmlEncoding() throws Exception {
84        String message = "This implementation doesn't parse the encoding from the XML declaration";
85        assertEquals(message, "ISO-8859-1", documentA.getXmlEncoding());
86        assertEquals(message, "US-ASCII", documentB.getXmlEncoding());
87    }
88
89    @KnownFailure("Dalvik doesn't parse the XML declaration")
90    public void testGetXmlVersion() throws Exception {
91        String message = "This implementation doesn't parse the version from the XML declaration";
92        assertEquals(message, "1.0", documentA.getXmlVersion());
93        assertEquals(message, "1.1", documentB.getXmlVersion());
94    }
95
96    @KnownFailure("Dalvik doesn't parse the XML declaration")
97    public void testGetXmlStandalone() throws Exception {
98        String message = "This implementation doesn't parse standalone from the XML declaration";
99        assertEquals(message, false, documentA.getXmlStandalone());
100        assertEquals(message, true, documentB.getXmlStandalone());
101    }
102
103    public void testGetDocumentUri() throws Exception {
104        assertEquals(systemIdA, documentA.getDocumentURI());
105        assertEquals(systemIdB, documentB.getDocumentURI());
106    }
107}
108