1/*
2 * Copyright (C) 2011 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 java.io.StringWriter;
20import javax.xml.parsers.DocumentBuilder;
21import javax.xml.parsers.DocumentBuilderFactory;
22import javax.xml.transform.Transformer;
23import javax.xml.transform.TransformerFactory;
24import javax.xml.transform.dom.DOMSource;
25import javax.xml.transform.stream.StreamResult;
26import junit.framework.TestCase;
27import org.w3c.dom.Attr;
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30
31public final class DomSerializationTest extends TestCase {
32    private DocumentBuilder documentBuilder;
33    private Transformer transformer;
34
35    @Override protected void setUp() throws Exception {
36        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
37        documentBuilder = builderFactory.newDocumentBuilder();
38        transformer = TransformerFactory.newInstance().newTransformer();
39    }
40
41    public void testWriteDocument() throws Exception {
42        Document document = documentBuilder.newDocument();
43        Element foo = document.createElement("foo");
44        Attr quux = document.createAttribute("quux");
45        quux.setValue("abc");
46        foo.setAttributeNode(quux);
47        foo.appendChild(document.createElement("bar"));
48        foo.appendChild(document.createElement("baz"));
49        document.appendChild(foo);
50        assertXmlEquals("<foo quux=\"abc\"><bar/><baz/></foo>", document);
51    }
52
53    public void testWriteSpecialCharactersInText() throws Exception {
54        Document document = documentBuilder.newDocument();
55        Element foo = document.createElement("foo");
56        foo.appendChild(document.createTextNode("5'8\", 5 < 6 & 7 > 3!"));
57        document.appendChild(foo);
58        assertXmlEquals("<foo>5'8\", 5 &lt; 6 &amp; 7 &gt; 3!</foo>", document);
59    }
60
61    private String toXml(Document document) throws Exception {
62        StringWriter stringWriter = new StringWriter();
63        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
64        return stringWriter.toString();
65    }
66
67    private void assertXmlEquals(String expectedXml, Document document) throws Exception {
68        String actual = toXml(document);
69        String declarationA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
70        String declarationB = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
71        assertTrue(actual, actual.equals(declarationA + expectedXml)
72                || actual.equals(declarationB + expectedXml));
73    }
74}
75