• 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 package org.apache.harmony.tests.javax.xml.parsers;
17 
18 import dalvik.annotation.KnownFailure;
19 
20 import junit.framework.TestCase;
21 
22 import org.apache.harmony.tests.javax.xml.parsers.SAXParserTestSupport.MyDefaultHandler;
23 import org.apache.harmony.tests.javax.xml.parsers.SAXParserTestSupport.MyHandler;
24 import org.apache.harmony.tests.org.xml.sax.support.BrokenInputStream;
25 import org.apache.harmony.tests.org.xml.sax.support.MethodLogger;
26 import org.apache.harmony.tests.org.xml.sax.support.MockHandler;
27 import org.xml.sax.HandlerBase;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.Parser;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.SAXNotRecognizedException;
32 import org.xml.sax.SAXNotSupportedException;
33 import org.xml.sax.XMLReader;
34 import org.xml.sax.ext.LexicalHandler;
35 import org.xml.sax.helpers.DefaultHandler;
36 
37 import tests.support.resource.Support_Resources;
38 
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.util.HashMap;
46 import java.util.Vector;
47 
48 import javax.xml.parsers.SAXParser;
49 import javax.xml.parsers.SAXParserFactory;
50 
51 @SuppressWarnings("deprecation")
52 public class SAXParserTest extends TestCase {
53 
54     private class MockSAXParser extends SAXParser {
MockSAXParser()55         public MockSAXParser() {
56             super();
57         }
58 
59         /*
60          * @see javax.xml.parsers.SAXParser#getParser()
61          */
62         @Override
getParser()63         public Parser getParser() throws SAXException {
64             // it is a fake
65             return null;
66         }
67 
68         /*
69          * @see javax.xml.parsers.SAXParser#getProperty(java.lang.String)
70          */
71         @Override
getProperty(String name)72         public Object getProperty(String name) throws SAXNotRecognizedException,
73                 SAXNotSupportedException {
74             // it is a fake
75             return null;
76         }
77 
78         /*
79          * @see javax.xml.parsers.SAXParser#getXMLReader()
80          */
81         @Override
getXMLReader()82         public XMLReader getXMLReader() throws SAXException {
83             // it is a fake
84             return null;
85         }
86 
87         /*
88          * @see javax.xml.parsers.SAXParser#isNamespaceAware()
89          */
90         @Override
isNamespaceAware()91         public boolean isNamespaceAware() {
92             // it is a fake
93             return false;
94         }
95 
96         /*
97          * @see javax.xml.parsers.SAXParser#isValidating()
98          */
99         @Override
isValidating()100         public boolean isValidating() {
101             // it is a fake
102             return false;
103         }
104 
105         /*
106          * @see javax.xml.parsers.SAXParser#setProperty(java.lang.String,
107          * java.lang.Object)
108          */
109         @Override
setProperty(String name, Object value)110         public void setProperty(String name, Object value) throws
111                 SAXNotRecognizedException, SAXNotSupportedException {
112             // it is a fake
113         }
114     }
115 
116     private static final String LEXICAL_HANDLER_PROPERTY
117             = "http://xml.org/sax/properties/lexical-handler";
118 
119     SAXParserFactory spf;
120 
121     SAXParser parser;
122 
123     static HashMap<String, String> ns;
124 
125     static Vector<String> el;
126 
127     static HashMap<String, String> attr;
128 
129     SAXParserTestSupport sp = new SAXParserTestSupport();
130 
131     File [] list_wf;
132     File [] list_nwf;
133     File [] list_out_dh;
134     File [] list_out_hb;
135 
136     boolean validating = false;
137 
getResource(String name)138     private InputStream getResource(String name) {
139         return this.getClass().getResourceAsStream(name);
140     }
141 
initFiles()142     public void initFiles() throws Exception {
143         // we differntiate between a validating and a non validating parser
144         try {
145             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
146             validating = parser.isValidating();
147         } catch (Exception e) {
148             fail("could not obtain a SAXParser");
149         }
150 
151         // Create the temp directories again despite the static init in SAXParserTestSupport.
152         // http://b/263794383
153         SAXParserTestSupport.createTempDirectories();
154 
155         String tmpPath = System.getProperty("java.io.tmpdir");
156 
157         // nwf = not well formed, wf = well formed
158         list_wf = new File[] {new File(tmpPath + "/" +
159                 SAXParserTestSupport.XML_WF + "staff.xml")};
160         list_nwf = new File[] {new File(tmpPath + "/" +
161                 SAXParserTestSupport.XML_NWF + "staff.xml")};
162         list_out_dh = new File[] {new File(tmpPath + "/" +
163                 SAXParserTestSupport.XML_WF_OUT_DH + "staff.out")};
164         list_out_hb = new File[] {new File(tmpPath + "/" +
165                 SAXParserTestSupport.XML_WF_OUT_HB + "staff.out")};
166 
167         list_wf[0].deleteOnExit();
168         list_nwf[0].deleteOnExit();
169         list_out_hb[0].deleteOnExit();
170         list_out_dh[0].deleteOnExit();
171 
172 
173         Support_Resources.copyLocalFileto(list_wf[0],
174                 getResource(SAXParserTestSupport.XML_WF + "staff.xml"));
175         Support_Resources.copyLocalFileto(new File(
176                 tmpPath + "/" + SAXParserTestSupport.XML_WF + "staff.dtd"),
177                 getResource(SAXParserTestSupport.XML_WF + "staff.dtd"));
178 
179         Support_Resources.copyLocalFileto(list_nwf[0],
180                 getResource(SAXParserTestSupport.XML_NWF + "staff.xml"));
181         Support_Resources.copyLocalFileto(new File(
182                 tmpPath + "/" + SAXParserTestSupport.XML_NWF + "staff.dtd"),
183                 getResource(SAXParserTestSupport.XML_NWF + "staff.dtd"));
184 
185         Support_Resources.copyLocalFileto(list_out_dh[0],
186                 getResource(SAXParserTestSupport.XML_WF_OUT_DH + "staff.out"));
187         Support_Resources.copyLocalFileto(list_out_hb[0],
188                 getResource(SAXParserTestSupport.XML_WF_OUT_HB + "staff.out"));
189     }
190 
191     @Override
setUp()192     protected void setUp() throws Exception {
193         spf = SAXParserFactory.newInstance();
194         parser = spf.newSAXParser();
195         assertNotNull(parser);
196 
197         ns = new HashMap<String, String>();
198         attr = new HashMap<String, String>();
199         el = new Vector<String>();
200         initFiles();
201     }
202 
203     @Override
tearDown()204     protected void tearDown() throws Exception {
205     }
206 
207 //    public static void main(String[] args) throws Exception {
208 //        SAXParserTest st = new SAXParserTest();
209 //        st.setUp();
210 //        st.generateDataFromReferenceImpl();
211 //
212 //    }
213 //
214 //    private void generateDataFromReferenceImpl() {
215 //        try {
216 //            for(int i = 0; i < list_wf.length; i++) {
217 //                MyDefaultHandler dh = new MyDefaultHandler();
218 //                InputStream is = new FileInputStream(list_wf[i]);
219 //                parser.parse(is, dh, ParsingSupport.XML_SYSTEM_ID);
220 //                HashMap refHm = dh.createData();
221 //
222 //                StringBuilder sb = new StringBuilder();
223 //                for (int j = 0; j < ParsingSupport.KEYS.length; j++) {
224 //                    String key = ParsingSupport.KEYS[j];
225 //                    sb.append(refHm.get(key)).append(
226 //                            ParsingSupport.SEPARATOR_DATA);
227 //                }
228 //                FileWriter fw = new FileWriter("/tmp/build_dh"+i+".out");
229 //                fw.append(sb.toString());
230 //                fw.close();
231 //            }
232 //
233 //            for(int i = 0; i < list_nwf.length; i++) {
234 //                MyHandler hb = new MyHandler();
235 //                InputStream is = new FileInputStream(list_wf[i]);
236 //                parser.parse(is, hb, ParsingSupport.XML_SYSTEM_ID);
237 //                HashMap refHm = hb.createData();
238 //
239 //                StringBuilder sb = new StringBuilder();
240 //                for (int j = 0; j < ParsingSupport.KEYS.length; j++) {
241 //                    String key = ParsingSupport.KEYS[j];
242 //                    sb.append(refHm.get(key)).append(
243 //                            ParsingSupport.SEPARATOR_DATA);
244 //                }
245 //                FileWriter fw = new FileWriter("/tmp/build_hb"+i+".out");
246 //                fw.append(sb.toString());
247 //                fw.close();
248 //            }
249 //
250 //
251 //        } catch (Exception e) {
252 //            e.printStackTrace();
253 //        }
254 //    }
255 
testSAXParser()256     public void testSAXParser() {
257         try {
258             new MockSAXParser();
259         } catch (Exception e) {
260             fail("unexpected exception " + e.toString());
261         }
262     }
263 
264     /**
265      * javax.xml.parser.SAXParser#getSchema().
266      * TODO getSchema() IS NOT SUPPORTED
267      */
268     /*   public void test_getSchema() {
269         assertNull(parser.getSchema());
270         SchemaFactory sf =
271             SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
272         try {
273             Schema schema = sf.newSchema();
274             spf.setSchema(schema);
275             assertNotNull(spf.newSAXParser().getSchema());
276         } catch (ParserConfigurationException pce) {
277             fail("Unexpected ParserConfigurationException " + pce.toString());
278         } catch (SAXException sax) {
279             fail("Unexpected SAXException " + sax.toString());
280         }
281     }
282      */
283 
testIsNamespaceAware()284     public void testIsNamespaceAware() {
285         try {
286             spf.setNamespaceAware(true);
287             assertTrue(spf.newSAXParser().isNamespaceAware());
288             spf.setNamespaceAware(false);
289             assertFalse(spf.newSAXParser().isNamespaceAware());
290         } catch (Exception e) {
291             throw new RuntimeException("Unexpected exception", e);
292         }
293     }
294 
testIsValidating()295     public void testIsValidating() {
296         try {
297             spf.setValidating(false);
298             assertFalse(spf.newSAXParser().isValidating());
299         } catch (Exception e) {
300             throw new RuntimeException("Unexpected exception", e);
301         }
302     }
303 
testIsXIncludeAware()304     public void testIsXIncludeAware() {
305         try {
306             spf.setXIncludeAware(false);
307             assertFalse(spf.newSAXParser().isXIncludeAware());
308         } catch (Exception e) {
309             throw new RuntimeException("Unexpected exception", e);
310         }
311     }
312 
test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler()313     public void test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler() throws Exception {
314 
315         for(int i = 0; i < list_wf.length; i++) {
316             HashMap<String, String> hm =
317                 new SAXParserTestSupport().readFile(list_out_dh[i].getPath());
318             MyDefaultHandler dh = new MyDefaultHandler();
319             parser.parse(list_wf[i], dh);
320             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
321         }
322 
323         for(int i = 0; i < list_nwf.length; i++) {
324             try {
325                 MyDefaultHandler dh = new MyDefaultHandler();
326                 parser.parse(list_nwf[i], dh);
327                 fail("SAXException is not thrown");
328             } catch(org.xml.sax.SAXException se) {
329                 //expected
330             }
331         }
332 
333         try {
334             MyDefaultHandler dh = new MyDefaultHandler();
335             parser.parse((File) null, dh);
336             fail("java.lang.IllegalArgumentException is not thrown");
337         } catch(java.lang.IllegalArgumentException iae) {
338             //expected
339         }
340 
341         try {
342             parser.parse(list_wf[0], (DefaultHandler) null);
343         } catch(java.lang.IllegalArgumentException iae) {
344             fail("java.lang.IllegalArgumentException is thrown");
345         }
346     }
347 
testParseFileHandlerBase()348     public void testParseFileHandlerBase() {
349         for(int i = 0; i < list_wf.length; i++) {
350             try {
351                 HashMap<String, String> hm = sp.readFile(
352                         list_out_hb[i].getPath());
353                 MyHandler dh = new MyHandler();
354                 parser.parse(list_wf[i], dh);
355                 assertTrue(SAXParserTestSupport.equalsMaps(hm,
356                         dh.createData()));
357             } catch (IOException ioe) {
358                 fail("Unexpected IOException " + ioe.toString());
359             } catch (SAXException sax) {
360                 fail("Unexpected SAXException " + sax.toString());
361             }
362         }
363 
364         for(int i = 0; i < list_nwf.length; i++) {
365             try {
366                 MyHandler dh = new MyHandler();
367                 parser.parse(list_nwf[i], dh);
368                 fail("SAXException is not thrown");
369             } catch(org.xml.sax.SAXException se) {
370                 //expected
371             } catch (FileNotFoundException fne) {
372                 fail("Unexpected FileNotFoundException " + fne.toString());
373             } catch (IOException ioe) {
374                 fail("Unexpected IOException " + ioe.toString());
375             }
376         }
377 
378         try {
379             MyHandler dh = new MyHandler();
380             parser.parse((File) null, dh);
381             fail("java.lang.IllegalArgumentException is not thrown");
382         } catch(java.lang.IllegalArgumentException iae) {
383             //expected
384         } catch (IOException ioe) {
385             fail("Unexpected IOException " + ioe.toString());
386         } catch(SAXException sax) {
387             fail("Unexpected SAXException " + sax.toString());
388         }
389 
390         try {
391             parser.parse(list_wf[0], (HandlerBase) null);
392         } catch(java.lang.IllegalArgumentException iae) {
393             fail("java.lang.IllegalArgumentException is thrown");
394         } catch (FileNotFoundException fne) {
395             fail("Unexpected FileNotFoundException " + fne.toString());
396         } catch(IOException ioe) {
397             fail("Unexpected IOException " + ioe.toString());
398         } catch(SAXException sax) {
399             fail("Unexpected SAXException " + sax.toString());
400         }
401     }
402 
test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler()403     public void test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler()
404             throws Exception {
405         for(int i = 0; i < list_wf.length; i++) {
406             HashMap<String, String> hm = new SAXParserTestSupport().readFile(
407                     list_out_dh[i].getPath());
408             MyDefaultHandler dh = new MyDefaultHandler();
409             InputSource is = new InputSource(new FileInputStream(list_wf[i]));
410             parser.parse(is, dh);
411             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
412         }
413 
414         for (File file : list_nwf) {
415             try {
416                 MyDefaultHandler dh = new MyDefaultHandler();
417                 InputSource is = new InputSource(new FileInputStream(file));
418                 parser.parse(is, dh);
419                 fail("SAXException is not thrown");
420             } catch (SAXException expected) {
421             }
422         }
423 
424         try {
425             MyDefaultHandler dh = new MyDefaultHandler();
426             parser.parse((InputSource) null, dh);
427             fail("java.lang.IllegalArgumentException is not thrown");
428         } catch (IllegalArgumentException expected) {
429         }
430 
431         InputSource is = new InputSource(new FileInputStream(list_wf[0]));
432         parser.parse(is, (DefaultHandler) null);
433 
434         InputStream in = null;
435         try {
436             in = new BrokenInputStream(new FileInputStream(list_wf[0]), 10);
437             is = new InputSource(in);
438             parser.parse(is, (DefaultHandler) null);
439             fail("IOException expected");
440         } catch(IOException expected) {
441         } finally {
442             in.close();
443         }
444     }
445 
testParseInputSourceHandlerBase()446     public void testParseInputSourceHandlerBase() throws Exception {
447         for(int i = 0; i < list_wf.length; i++) {
448             HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath());
449             MyHandler dh = new MyHandler();
450             InputSource is = new InputSource(new FileInputStream(list_wf[i]));
451             parser.parse(is, dh);
452             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
453         }
454 
455         for (File file : list_nwf) {
456             try {
457                 MyHandler dh = new MyHandler();
458                 InputSource is = new InputSource(new FileInputStream(file));
459                 parser.parse(is, dh);
460                 fail("SAXException is not thrown");
461             } catch (SAXException expected) {
462             }
463         }
464 
465         try {
466             MyHandler dh = new MyHandler();
467             parser.parse((InputSource) null, dh);
468             fail("java.lang.IllegalArgumentException is not thrown");
469         } catch(IllegalArgumentException expected) {
470         }
471 
472         InputSource is = new InputSource(new FileInputStream(list_wf[0]));
473         parser.parse(is, (HandlerBase) null);
474 
475         // Reader case
476         is = new InputSource(new InputStreamReader(new FileInputStream(list_wf[0])));
477         parser.parse(is, (HandlerBase) null);
478 
479         // SystemID case
480         is = new InputSource(list_wf[0].toURI().toString());
481         parser.parse(is, (HandlerBase) null);
482 
483         // Inject IOException
484         InputStream in = null;
485         try {
486             in = new BrokenInputStream(new FileInputStream(list_wf[0]), 10);
487             parser.parse(in, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID);
488             fail("IOException expected");
489         } catch(IOException expected) {
490         } finally {
491             in.close();
492         }
493     }
494 
test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler()495     public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler() throws Exception {
496 
497         for(int i = 0; i < list_wf.length; i++) {
498 
499             HashMap<String, String> hm = new SAXParserTestSupport().readFile(
500                     list_out_dh[i].getPath());
501             MyDefaultHandler dh = new MyDefaultHandler();
502             InputStream is = new FileInputStream(list_wf[i]);
503             parser.parse(is, dh);
504             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
505         }
506 
507         for(int i = 0; i < list_nwf.length; i++) {
508             try {
509                 MyDefaultHandler dh = new MyDefaultHandler();
510                 InputStream is = new FileInputStream(list_nwf[i]);
511                 parser.parse(is, dh);
512                 fail("SAXException is not thrown");
513             } catch(org.xml.sax.SAXException se) {
514                 //expected
515             }
516         }
517 
518         try {
519             MyDefaultHandler dh = new MyDefaultHandler();
520             parser.parse((InputStream) null, dh);
521             fail("java.lang.IllegalArgumentException is not thrown");
522         } catch(java.lang.IllegalArgumentException iae) {
523             //expected
524         }
525 
526         try {
527             InputStream is = new FileInputStream(list_wf[0]);
528             parser.parse(is, (DefaultHandler) null);
529         } catch(java.lang.IllegalArgumentException iae) {
530             fail("java.lang.IllegalArgumentException is thrown");
531         }
532     }
533 
534     @KnownFailure("We supply optional qnames, but this test doesn't expect them")
test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String()535     public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String() {
536         for(int i = 0; i < list_wf.length; i++) {
537             try {
538                 HashMap<String, String> hm = sp.readFile(
539                         list_out_hb[i].getPath());
540                 MyDefaultHandler dh = new MyDefaultHandler();
541                 InputStream is = new FileInputStream(list_wf[i]);
542                 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
543                 assertEquals(hm, dh.createData());
544             } catch (IOException ioe) {
545                 fail("Unexpected IOException " + ioe.toString());
546             } catch (SAXException sax) {
547                 fail("Unexpected SAXException " + sax.toString());
548             }
549         }
550 
551         for(int i = 0; i < list_nwf.length; i++) {
552             try {
553                 MyDefaultHandler dh = new MyDefaultHandler();
554                 InputStream is = new FileInputStream(list_nwf[i]);
555                 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
556                 fail("SAXException is not thrown");
557             } catch(org.xml.sax.SAXException se) {
558                 //expected
559             } catch (FileNotFoundException fne) {
560                 fail("Unexpected FileNotFoundException " + fne.toString());
561             } catch (IOException ioe) {
562                 fail("Unexpected IOException " + ioe.toString());
563             }
564         }
565 
566         try {
567             MyDefaultHandler dh = new MyDefaultHandler();
568             parser.parse((InputStream) null, dh,
569                     SAXParserTestSupport.XML_SYSTEM_ID);
570             fail("java.lang.IllegalArgumentException is not thrown");
571         } catch(java.lang.IllegalArgumentException iae) {
572             //expected
573         } catch (IOException ioe) {
574             fail("Unexpected IOException " + ioe.toString());
575         } catch(SAXException sax) {
576             fail("Unexpected SAXException " + sax.toString());
577         }
578 
579         try {
580             InputStream is = new FileInputStream(list_wf[0]);
581             parser.parse(is, (DefaultHandler) null,
582                     SAXParserTestSupport.XML_SYSTEM_ID);
583         } catch(java.lang.IllegalArgumentException iae) {
584             fail("java.lang.IllegalArgumentException is thrown");
585         } catch (FileNotFoundException fne) {
586             fail("Unexpected FileNotFoundException " + fne.toString());
587         } catch(IOException ioe) {
588             fail("Unexpected IOException " + ioe.toString());
589         } catch(SAXException sax) {
590             fail("Unexpected SAXException " + sax.toString());
591         }
592 //
593 //        for(int i = 0; i < list_wf.length; i++) {
594 //
595 //            HashMap<String, String> hm = new SAXParserTestSupport().readFile(
596 //                    list_out_dh[i].getPath());
597 //            MyDefaultHandler dh = new MyDefaultHandler();
598 //            InputStream is = new FileInputStream(list_wf[i]);
599 //            parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
600 //            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
601 //        }
602 //
603 //        for(int i = 0; i < list_nwf.length; i++) {
604 //            try {
605 //                MyDefaultHandler dh = new MyDefaultHandler();
606 //                InputStream is = new FileInputStream(list_nwf[i]);
607 //                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
608 //                fail("SAXException is not thrown");
609 //            } catch(org.xml.sax.SAXException se) {
610 //                //expected
611 //            }
612 //        }
613 //
614 //        try {
615 //            MyDefaultHandler dh = new MyDefaultHandler();
616 //            parser.parse((InputStream) null, dh,
617 //                    SAXParserTestSupport.XML_SYSTEM_ID);
618 //            fail("java.lang.IllegalArgumentException is not thrown");
619 //        } catch(java.lang.IllegalArgumentException iae) {
620 //            //expected
621 //        }
622 //
623 //        try {
624 //            InputStream is = new FileInputStream(list_wf[0]);
625 //            parser.parse(is, (DefaultHandler) null,
626 //                    SAXParserTestSupport.XML_SYSTEM_ID);
627 //        } catch(java.lang.IllegalArgumentException iae) {
628 //            fail("java.lang.IllegalArgumentException is thrown");
629 //        }
630 //
631 //        // TODO commented out since our parser is nonvalidating and thus never
632 //        // tries to load staff.dtd in "/" ... and therefore never can fail with
633 //        // an IOException
634 //        /*try {
635 //            MyDefaultHandler dh = new MyDefaultHandler();
636 //            InputStream is = new FileInputStream(list_wf[0]);
637 //            parser.parse(is, dh, "/");
638 //            fail("Expected IOException was not thrown");
639 //        } catch(IOException ioe) {
640 //            // expected
641 //        }*/
642     }
643 
testParseInputStreamHandlerBase()644     public void testParseInputStreamHandlerBase() throws Exception {
645         for(int i = 0; i < list_wf.length; i++) {
646             HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath());
647             MyHandler dh = new MyHandler();
648             InputStream is = new FileInputStream(list_wf[i]);
649             parser.parse(is, dh);
650             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
651         }
652 
653         for (File file : list_nwf) {
654             try {
655                 MyHandler dh = new MyHandler();
656                 InputStream is = new FileInputStream(file);
657                 parser.parse(is, dh);
658                 fail("SAXException is not thrown");
659             } catch (SAXException expected) {
660             }
661         }
662 
663         try {
664             MyHandler dh = new MyHandler();
665             parser.parse((InputStream) null, dh);
666             fail("java.lang.IllegalArgumentException is not thrown");
667         } catch (IllegalArgumentException expected) {
668         }
669 
670         InputStream is = new FileInputStream(list_wf[0]);
671         parser.parse(is, (HandlerBase) null);
672 
673         // Inject IOException
674         try {
675             is = new BrokenInputStream(new FileInputStream(list_wf[0]), 10);
676             parser.parse(is, (HandlerBase) null);
677             fail("IOException expected");
678         } catch(IOException e) {
679             // Expected
680         } finally {
681             is.close();
682         }
683     }
684 
testParseInputStreamHandlerBaseString()685     public void testParseInputStreamHandlerBaseString() throws Exception {
686         for(int i = 0; i < list_wf.length; i++) {
687             HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath());
688             MyHandler dh = new MyHandler();
689             InputStream is = new FileInputStream(list_wf[i]);
690             parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
691             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
692         }
693 
694         for (File file : list_nwf) {
695             try {
696                 MyHandler dh = new MyHandler();
697                 InputStream is = new FileInputStream(file);
698                 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
699                 fail("SAXException is not thrown");
700             } catch (SAXException expected) {
701             }
702         }
703 
704         try {
705             MyHandler dh = new MyHandler();
706             parser.parse(null, dh, SAXParserTestSupport.XML_SYSTEM_ID);
707             fail("java.lang.IllegalArgumentException is not thrown");
708         } catch(IllegalArgumentException expected) {
709         }
710 
711         InputStream is = new FileInputStream(list_wf[0]);
712         parser.parse(is, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID);
713 
714         // Inject IOException
715         try {
716             is = new BrokenInputStream(new FileInputStream(list_wf[0]), 10);
717             parser.parse(is, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID);
718             fail("IOException expected");
719         } catch(IOException expected) {
720         } finally {
721             is.close();
722         }
723     }
724 
test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler()725     public void test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler() throws Exception {
726 
727         for(int i = 0; i < list_wf.length; i++) {
728 
729             HashMap<String, String> hm = new SAXParserTestSupport().readFile(
730                     list_out_dh[i].getPath());
731             MyDefaultHandler dh = new MyDefaultHandler();
732             parser.parse(list_wf[i].toURI().toString(), dh);
733             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
734         }
735 
736         for(int i = 0; i < list_nwf.length; i++) {
737             try {
738                 MyDefaultHandler dh = new MyDefaultHandler();
739                 parser.parse(list_nwf[i].toURI().toString(), dh);
740                 fail("SAXException is not thrown");
741             } catch(org.xml.sax.SAXException se) {
742                 //expected
743             }
744         }
745 
746         try {
747             MyDefaultHandler dh = new MyDefaultHandler();
748             parser.parse((String) null, dh);
749             fail("java.lang.IllegalArgumentException is not thrown");
750         } catch(java.lang.IllegalArgumentException iae) {
751             //expected
752         }
753 
754         try {
755             parser.parse(list_wf[0].toURI().toString(), (DefaultHandler) null);
756         } catch(java.lang.IllegalArgumentException iae) {
757             fail("java.lang.IllegalArgumentException is thrown");
758         }
759     }
760 
testParseStringHandlerBase()761     public void testParseStringHandlerBase() {
762         for(int i = 0; i < list_wf.length; i++) {
763             try {
764                 HashMap<String, String> hm = sp.readFile(
765                         list_out_hb[i].getPath());
766                 MyHandler dh = new MyHandler();
767                 parser.parse(list_wf[i].toURI().toString(), dh);
768                 assertTrue(SAXParserTestSupport.equalsMaps(hm,
769                         dh.createData()));
770             } catch (IOException ioe) {
771                 fail("Unexpected IOException " + ioe.toString());
772             } catch (SAXException sax) {
773                 fail("Unexpected SAXException " + sax.toString());
774             }
775         }
776 
777         for(int i = 0; i < list_nwf.length; i++) {
778             try {
779                 MyHandler dh = new MyHandler();
780                 parser.parse(list_nwf[i].toURI().toString(), dh);
781                 fail("SAXException is not thrown");
782             } catch(org.xml.sax.SAXException se) {
783                 //expected
784             } catch (FileNotFoundException fne) {
785                 fail("Unexpected FileNotFoundException " + fne.toString());
786             } catch (IOException ioe) {
787                 fail("Unexpected IOException " + ioe.toString());
788             }
789         }
790 
791         try {
792             MyHandler dh = new MyHandler();
793             parser.parse((String) null, dh);
794             fail("java.lang.IllegalArgumentException is not thrown");
795         } catch(java.lang.IllegalArgumentException iae) {
796             //expected
797         } catch (IOException ioe) {
798             fail("Unexpected IOException " + ioe.toString());
799         } catch(SAXException sax) {
800             fail("Unexpected SAXException " + sax.toString());
801         }
802 
803         try {
804             parser.parse(list_wf[0].toURI().toString(), (HandlerBase) null);
805         } catch(java.lang.IllegalArgumentException iae) {
806             fail("java.lang.IllegalArgumentException is thrown");
807         } catch (FileNotFoundException fne) {
808             fail("Unexpected FileNotFoundException " + fne.toString());
809         } catch(IOException ioe) {
810             fail("Unexpected IOException " + ioe.toString());
811         } catch(SAXException sax) {
812             fail("Unexpected SAXException " + sax.toString());
813         }
814     }
815 
testReset()816     public void testReset() {
817         try {
818             spf = SAXParserFactory.newInstance();
819             parser = spf.newSAXParser();
820 
821             parser.setProperty(LEXICAL_HANDLER_PROPERTY, new MockHandler(new MethodLogger()));
822             parser.reset();
823             assertEquals(null, parser.getProperty(LEXICAL_HANDLER_PROPERTY));
824         } catch (Exception e) {
825             throw new RuntimeException("Unexpected exception", e);
826         }
827     }
828 
testGetParser()829     public void testGetParser() {
830         spf = SAXParserFactory.newInstance();
831         try {
832             Parser parser = spf.newSAXParser().getParser();
833             assertNotNull(parser);
834         } catch (Exception e) {
835             throw new RuntimeException("Unexpected exception", e);
836         }
837     }
838 
testGetReader()839     public void testGetReader() {
840         spf = SAXParserFactory.newInstance();
841         try {
842             XMLReader reader = spf.newSAXParser().getXMLReader();
843             assertNotNull(reader);
844         } catch (Exception e) {
845             throw new RuntimeException("Unexpected exception", e);
846         }
847     }
848 
testSetGetProperty()849     public void testSetGetProperty() {
850         // Ordinary case
851         String validName = "http://xml.org/sax/properties/lexical-handler";
852         LexicalHandler validValue = new MockHandler(new MethodLogger());
853 
854         try {
855             SAXParser parser = spf.newSAXParser();
856             parser.setProperty(validName, validValue);
857             assertEquals(validValue, parser.getProperty(validName));
858 
859             parser.setProperty(validName, null);
860             assertEquals(null, parser.getProperty(validName));
861         } catch (Exception e) {
862             throw new RuntimeException("Unexpected exception", e);
863         }
864 
865         // Unsupported property
866         try {
867             SAXParser parser = spf.newSAXParser();
868             parser.setProperty("foo", "bar");
869             fail("SAXNotRecognizedException expected");
870         } catch (SAXNotRecognizedException e) {
871             // Expected
872         } catch (Exception e) {
873             throw new RuntimeException("Unexpected exception", e);
874         }
875 
876         try {
877             SAXParser parser = spf.newSAXParser();
878             parser.getProperty("foo");
879             fail("SAXNotRecognizedException expected");
880         } catch (SAXNotRecognizedException e) {
881             // Expected
882         } catch (Exception e) {
883             throw new RuntimeException("Unexpected exception", e);
884         }
885 
886         // No name case
887         try {
888             SAXParser parser = spf.newSAXParser();
889             parser.setProperty(null, "bar");
890             fail("NullPointerException expected");
891         } catch (NullPointerException e) {
892             // Expected
893         } catch (Exception e) {
894             throw new RuntimeException("Unexpected exception", e);
895         }
896 
897         try {
898             SAXParser parser = spf.newSAXParser();
899             parser.getProperty(null);
900             fail("NullPointerException expected");
901         } catch (NullPointerException e) {
902             // Expected
903         } catch (Exception e) {
904             throw new RuntimeException("Unexpected exception", e);
905         }
906     }
907 
908 }
909