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