summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/xml/DeclarationTest.java
blob: 4cd049f7490af97b24e39768d7959bcbefa84063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.xml;

import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Test the parsing of the XML declaration, plus the additional document fields
 * captured during parsing.
 */
public class DeclarationTest extends TestCase {

    private String systemIdA;
    private Document documentA;

    private String systemIdB;
    private Document documentB;

    @Override protected void setUp() throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();

        systemIdA = stringToSystemId(
                "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?><foo />");
        InputSource inputSourceA = new InputSource(systemIdA);
        inputSourceA.setEncoding("US-ASCII");
        documentA = builder.parse(inputSourceA);

        systemIdB = stringToSystemId(
                "<?xml version=\"1.1\" encoding=\"US-ASCII\" standalone=\"yes\" ?><foo />");
        InputSource inputSourceB = new InputSource(systemIdB);
        inputSourceB.setEncoding("ISO-8859-1");
        documentB = builder.parse(inputSourceB);
    }

    private String stringToSystemId(String contents) throws IOException {
        File file = File.createTempFile("temp", "xml");
        file.deleteOnExit();
        OutputStream out = new FileOutputStream(file);
        out.write(contents.getBytes("UTF-8"));
        out.close();
        return "file:" + file;
    }

    /**
     * XML parsers are advised of the document's character set via two channels:
     * via the declaration and also the document's input source. To test that
     * each of these winds up in the correct location in the document model, we
     * supply different names for each. This is only safe because for the subset
     * of characters in the document, the character sets are equivalent.
     */
    public void testGetInputEncoding() throws Exception {
        assertEquals("US-ASCII", documentA.getInputEncoding());
        assertEquals("ISO-8859-1", documentB.getInputEncoding());
    }

    public void testGetXmlEncoding() throws Exception {
        String message = "This implementation doesn't parse the encoding from the XML declaration";
        assertEquals(message, "ISO-8859-1", documentA.getXmlEncoding());
        assertEquals(message, "US-ASCII", documentB.getXmlEncoding());
    }

    public void testGetXmlVersion() throws Exception {
        String message = "This implementation doesn't parse the version from the XML declaration";
        assertEquals(message, "1.0", documentA.getXmlVersion());
        assertEquals(message, "1.1", documentB.getXmlVersion());
    }

    public void testGetXmlStandalone() throws Exception {
        String message = "This implementation doesn't parse standalone from the XML declaration";
        assertEquals(message, false, documentA.getXmlStandalone());
        assertEquals(message, true, documentB.getXmlStandalone());
    }

    public void testGetDocumentUri() throws Exception {
        assertEquals(systemIdA, documentA.getDocumentURI());
        assertEquals(systemIdB, documentB.getDocumentURI());
    }
}