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 
17 package libcore.xml;
18 
19 import java.io.StringWriter;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
26 import junit.framework.TestCase;
27 import org.w3c.dom.Attr;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 
31 public final class DomSerializationTest extends TestCase {
32     private DocumentBuilder documentBuilder;
33     private Transformer transformer;
34 
setUp()35     @Override protected void setUp() throws Exception {
36         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
37         documentBuilder = builderFactory.newDocumentBuilder();
38         transformer = TransformerFactory.newInstance().newTransformer();
39     }
40 
testWriteDocument()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 
testWriteSpecialCharactersInText()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 
toXml(Document document)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 
assertXmlEquals(String expectedXml, Document document)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