• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 package com.android.utils;
17 
18 import com.android.SdkConstants;
19 
20 import org.w3c.dom.Attr;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 
27 import junit.framework.TestCase;
28 
29 @SuppressWarnings("javadoc")
30 public class XmlUtilsTest extends TestCase {
testlookupNamespacePrefix()31     public void testlookupNamespacePrefix() throws Exception {
32         // Setup
33         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
34         factory.setNamespaceAware(true);
35         factory.setValidating(false);
36         DocumentBuilder builder = factory.newDocumentBuilder();
37         Document document = builder.newDocument();
38         Element rootElement = document.createElement("root");
39         Attr attr = document.createAttributeNS(SdkConstants.XMLNS_URI,
40                 "xmlns:customPrefix");
41         attr.setValue(SdkConstants.ANDROID_URI);
42         rootElement.getAttributes().setNamedItemNS(attr);
43         document.appendChild(rootElement);
44         Element root = document.getDocumentElement();
45         root.appendChild(document.createTextNode("    "));
46         Element foo = document.createElement("foo");
47         root.appendChild(foo);
48         root.appendChild(document.createTextNode("    "));
49         Element bar = document.createElement("bar");
50         root.appendChild(bar);
51         Element baz = document.createElement("baz");
52         root.appendChild(baz);
53 
54         String prefix = XmlUtils.lookupNamespacePrefix(baz, SdkConstants.ANDROID_URI);
55         assertEquals("customPrefix", prefix);
56 
57         prefix = XmlUtils.lookupNamespacePrefix(baz,
58                 "http://schemas.android.com/tools", "tools");
59         assertEquals("tools", prefix);
60     }
61 
testToXmlAttributeValue()62     public void testToXmlAttributeValue() throws Exception {
63         assertEquals("", XmlUtils.toXmlAttributeValue(""));
64         assertEquals("foo", XmlUtils.toXmlAttributeValue("foo"));
65         assertEquals("foo&lt;bar", XmlUtils.toXmlAttributeValue("foo<bar"));
66         assertEquals("foo>bar", XmlUtils.toXmlAttributeValue("foo>bar"));
67 
68         assertEquals("&quot;", XmlUtils.toXmlAttributeValue("\""));
69         assertEquals("&apos;", XmlUtils.toXmlAttributeValue("'"));
70         assertEquals("foo&quot;b&apos;&apos;ar",
71                 XmlUtils.toXmlAttributeValue("foo\"b''ar"));
72         assertEquals("&lt;&quot;&apos;>&amp;", XmlUtils.toXmlAttributeValue("<\"'>&"));
73     }
74 
testAppendXmlAttributeValue()75     public void testAppendXmlAttributeValue() throws Exception {
76         StringBuilder sb = new StringBuilder();
77         XmlUtils.appendXmlAttributeValue(sb, "<\"'>&");
78         assertEquals("&lt;&quot;&apos;>&amp;", sb.toString());
79     }
80 
testToXmlTextValue()81     public void testToXmlTextValue() throws Exception {
82         assertEquals("&lt;\"'>&amp;", XmlUtils.toXmlTextValue("<\"'>&"));
83     }
84 
testAppendXmlTextValue()85     public void testAppendXmlTextValue() throws Exception {
86         StringBuilder sb = new StringBuilder();
87         XmlUtils.appendXmlTextValue(sb, "<\"'>&");
88         assertEquals("&lt;\"'>&amp;", sb.toString());
89     }
90 }
91