• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 tests.xml;
18 
19 import dalvik.annotation.TestTargets;
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargetClass;
23 
24 import junit.framework.TestCase;
25 
26 import org.xml.sax.Attributes;
27 import org.xml.sax.ContentHandler;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.Locator;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.helpers.DefaultHandler;
32 
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.util.HashMap;
37 import java.util.Map;
38 
39 import javax.xml.parsers.SAXParser;
40 import javax.xml.parsers.SAXParserFactory;
41 
42 @TestTargetClass(SAXParser.class)
43 public class SimpleParserTest extends TestCase implements ContentHandler {
44 
45     private SAXParser parser;
46 
47     private StringBuffer instructions;
48 
49     private Map<String, String> namespaces1;
50     private Map<String, String> namespaces2;
51 
52     private StringBuffer elements1;
53     private StringBuffer elements2;
54 
55     private Map<String, String> attributes1;
56     private Map<String, String> attributes2;
57 
58     private StringBuffer text;
59 
60     @Override
setUp()61     protected void setUp() throws Exception {
62         SAXParserFactory factory = SAXParserFactory.newInstance();
63         factory.setValidating(false);
64         factory.setNamespaceAware(true);
65 
66         parser = factory.newSAXParser();
67         parser.getXMLReader().setContentHandler(this);
68 
69         instructions = new StringBuffer();
70         namespaces1 = new HashMap<String, String>();
71         namespaces2 = new HashMap<String, String>();
72         elements1 = new StringBuffer();
73         elements2 = new StringBuffer();
74         attributes1 = new HashMap<String, String>();
75         attributes2 = new HashMap<String, String>();
76         text = new StringBuffer();
77     }
78 
79     @Override
tearDown()80     protected void tearDown() throws Exception {
81         instructions = null;
82         parser = null;
83         namespaces1 = null;
84         namespaces2 = null;
85         attributes1 = null;
86         attributes2 = null;
87         elements1 = null;
88         elements2 = null;
89         text = null;
90     }
91 
characters(char[] ch, int start, int length)92     public void characters(char[] ch, int start, int length) {
93 
94         String s = new String(ch, start, length).trim();
95         if (s.length() != 0) {
96             if (text.length() != 0) {
97                 text.append(",");
98             }
99 
100             text.append(s);
101         }
102     }
103 
endDocument()104     public void endDocument() {
105     }
106 
endElement(String uri, String localName, String qName)107     public void endElement(String uri, String localName, String qName) {
108     }
109 
endPrefixMapping(String prefix)110     public void endPrefixMapping(String prefix) {
111     }
112 
ignorableWhitespace(char[] ch, int start, int length)113     public void ignorableWhitespace(char[] ch, int start, int length) {
114     }
115 
processingInstruction(String target, String data)116     public void processingInstruction(String target, String data) {
117         String s = target + ":" + data;
118 
119         if (instructions.length() != 0) {
120             instructions.append(",");
121         }
122 
123         instructions.append(s);
124     }
125 
setDocumentLocator(Locator locator)126     public void setDocumentLocator(Locator locator) {
127     }
128 
skippedEntity(String name)129     public void skippedEntity(String name) {
130     }
131 
startDocument()132     public void startDocument() {
133     }
134 
startElement(String uri, String localName, String qName, Attributes atts)135     public void startElement(String uri, String localName, String qName,
136             Attributes atts) {
137 
138         if (elements1.length() != 0) {
139             elements1.append(",");
140         }
141 
142         elements1.append(localName);
143 
144         if (!"".equals(uri)) {
145             namespaces1.put(localName, uri);
146         }
147 
148         for (int i = 0; i < atts.getLength(); i++) {
149             attributes1.put(atts.getLocalName(i), atts.getValue(i));
150         }
151 
152         if (elements2.length() != 0) {
153             elements2.append(",");
154         }
155 
156         elements2.append(qName);
157 
158         if (!"".equals(uri)) {
159             namespaces2.put(qName, uri);
160         }
161 
162         for (int i = 0; i < atts.getLength(); i++) {
163             attributes2.put(atts.getQName(i), atts.getValue(i));
164         }
165     }
166 
startPrefixMapping(String prefix, String uri)167     public void startPrefixMapping(String prefix, String uri) {
168     }
169     @TestTargetNew(
170         level = TestLevel.PARTIAL,
171         notes = "",
172         method = "parse",
173         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
174     )
testWorkingFile1()175     public void testWorkingFile1() throws Exception {
176         SAXParserFactory factory = SAXParserFactory.newInstance();
177         factory.setValidating(false);
178         factory.setNamespaceAware(true);
179 
180         SAXParser parser = factory.newSAXParser();
181         parser.getXMLReader().setContentHandler(this);
182 
183         parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"),
184                 (DefaultHandler) null);
185 
186         assertEquals("The:quick,brown:fox", instructions.toString());
187 
188         assertEquals("stuff,nestedStuff,nestedStuff,nestedStuff", elements1
189                 .toString());
190 
191         assertEquals("Some text here,some more here...", text.toString());
192 
193         assertEquals("eins", attributes1.get("one"));
194         assertEquals("zwei", attributes1.get("two"));
195         assertEquals("drei", attributes1.get("three"));
196 
197         assertEquals("http://www.foobar.org", namespaces1.get("stuff"));
198     }
199     @TestTargetNew(
200         level = TestLevel.PARTIAL,
201         notes = "",
202         method = "parse",
203         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
204     )
testWorkingFile2()205     public void testWorkingFile2() throws Exception {
206         SAXParserFactory factory = SAXParserFactory.newInstance();
207 
208         factory.setValidating(false);
209         factory.setNamespaceAware(false);
210         factory.setFeature("http://xml.org/sax/features/namespace-prefixes",
211                 true);
212 
213         SAXParser parser = factory.newSAXParser();
214         parser.getXMLReader().setContentHandler(this);
215         parser.parse(getClass().getResourceAsStream("/SimpleParserTest.xml"),
216                 (DefaultHandler) null);
217 
218         assertFalse(parser.isNamespaceAware());
219 
220         assertEquals("The:quick,brown:fox", instructions.toString());
221 
222         assertEquals("t:stuff,nestedStuff,nestedStuff,nestedStuff", elements2
223                 .toString());
224 
225         assertEquals("Some text here,some more here...", text.toString());
226 
227         assertEquals("eins", attributes2.get("one"));
228         assertEquals("zwei", attributes2.get("two"));
229         assertEquals("drei", attributes2.get("three"));
230 
231         assertEquals(0, namespaces2.size());
232     }
233     @TestTargetNew(
234         level = TestLevel.PARTIAL,
235         notes = "Doesn't verify exceptions.",
236         method = "parse",
237         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
238     )
testEntityResolver()239     public void testEntityResolver() throws Exception {
240         final StringBuilder text = new StringBuilder();
241         DefaultHandler handler = new DefaultHandler() {
242             public void characters(char[] ch, int start, int length) {
243                 String s = new String(ch, start, length).trim();
244                 if (s.length() != 0) {
245                     if (text.length() != 0) {
246                         text.append(",");
247                     }
248                     text.append(s);
249                 }
250             }
251 
252             public InputSource resolveEntity(String publicId, String systemId)
253                     throws IOException, SAXException {
254                 return new InputSource(new InputStreamReader(
255                         new ByteArrayInputStream("test".getBytes())));
256             }
257         };
258 
259         SAXParserFactory spf = SAXParserFactory.newInstance();
260         spf.setValidating(false);
261         parser = spf.newSAXParser();
262         parser.parse(this.getClass().getResourceAsStream("/staffEntRes.xml"),
263                 handler);
264         assertTrue(
265                 "resolved external entity must be in parser character stream",
266                 text.toString().contains("test"));
267     }
268     @TestTargetNew(
269         level = TestLevel.PARTIAL,
270         notes = "Doesn't verify exceptions.",
271         method = "parse",
272         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
273     )
testGetValue()274     public void testGetValue() throws Exception{
275         parser.parse(getClass().getResourceAsStream("/staffNS.xml"),
276                 new DefaultHandler() {
277             boolean firstAddressElem = true;
278             @Override
279             public void startElement (String uri, String localName,
280                     String qName, Attributes attributes) {
281                 if(firstAddressElem && localName.equals("address")) {
282                     firstAddressElem = false;
283                     assertNotNull(attributes.getValue("http://www.usa.com",
284                             "domestic"));
285                 }
286             }
287         });
288     }
289 }
290