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 java.io.File;
19import java.io.FileInputStream;
20import java.io.FileNotFoundException;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.OutputStream;
26import java.util.HashMap;
27import java.util.Vector;
28
29import javax.xml.parsers.SAXParser;
30import javax.xml.parsers.SAXParserFactory;
31
32import junit.framework.TestCase;
33
34import org.xml.sax.HandlerBase;
35import org.xml.sax.InputSource;
36import org.xml.sax.Parser;
37import org.xml.sax.SAXException;
38import org.xml.sax.SAXNotRecognizedException;
39import org.xml.sax.SAXNotSupportedException;
40import org.xml.sax.XMLReader;
41import org.xml.sax.ext.LexicalHandler;
42import org.xml.sax.helpers.DefaultHandler;
43
44import tests.api.javax.xml.parsers.SAXParserTestSupport.MyDefaultHandler;
45import tests.api.javax.xml.parsers.SAXParserTestSupport.MyHandler;
46import tests.api.org.xml.sax.support.BrokenInputStream;
47import tests.api.org.xml.sax.support.MethodLogger;
48import tests.api.org.xml.sax.support.MockHandler;
49import tests.support.resource.Support_Resources;
50import dalvik.annotation.KnownFailure;
51import dalvik.annotation.TestLevel;
52import dalvik.annotation.TestTargetClass;
53import dalvik.annotation.TestTargetNew;
54import dalvik.annotation.TestTargets;
55
56@SuppressWarnings("deprecation")
57@TestTargetClass(SAXParser.class)
58public class SAXParserTest extends TestCase {
59
60    private class MockSAXParser extends SAXParser {
61        public MockSAXParser() {
62            super();
63        }
64
65        /*
66         * @see javax.xml.parsers.SAXParser#getParser()
67         */
68        @Override
69        public Parser getParser() throws SAXException {
70            // it is a fake
71            return null;
72        }
73
74        /*
75         * @see javax.xml.parsers.SAXParser#getProperty(java.lang.String)
76         */
77        @Override
78        public Object getProperty(String name) throws SAXNotRecognizedException,
79                SAXNotSupportedException {
80            // it is a fake
81            return null;
82        }
83
84        /*
85         * @see javax.xml.parsers.SAXParser#getXMLReader()
86         */
87        @Override
88        public XMLReader getXMLReader() throws SAXException {
89            // it is a fake
90            return null;
91        }
92
93        /*
94         * @see javax.xml.parsers.SAXParser#isNamespaceAware()
95         */
96        @Override
97        public boolean isNamespaceAware() {
98            // it is a fake
99            return false;
100        }
101
102        /*
103         * @see javax.xml.parsers.SAXParser#isValidating()
104         */
105        @Override
106        public boolean isValidating() {
107            // it is a fake
108            return false;
109        }
110
111        /*
112         * @see javax.xml.parsers.SAXParser#setProperty(java.lang.String,
113         * java.lang.Object)
114         */
115        @Override
116        public void setProperty(String name, Object value) throws
117                SAXNotRecognizedException, SAXNotSupportedException {
118            // it is a fake
119        }
120    }
121
122    private static final String LEXICAL_HANDLER_PROPERTY
123            = "http://xml.org/sax/properties/lexical-handler";
124
125    SAXParserFactory spf;
126
127    SAXParser parser;
128
129    static HashMap<String, String> ns;
130
131    static Vector<String> el;
132
133    static HashMap<String, String> attr;
134
135    SAXParserTestSupport sp = new SAXParserTestSupport();
136
137    File [] list_wf;
138    File [] list_nwf;
139    File [] list_out_dh;
140    File [] list_out_hb;
141
142    boolean validating = false;
143
144    private InputStream getResource(String name) {
145        return this.getClass().getResourceAsStream(name);
146    }
147
148    public void initFiles() throws Exception {
149        // we differntiate between a validating and a non validating parser
150        try {
151            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
152            validating = parser.isValidating();
153        } catch (Exception e) {
154            fail("could not obtain a SAXParser");
155        }
156
157        String tmpPath = System.getProperty("java.io.tmpdir");
158
159        // nwf = not well formed, wf = well formed
160        list_wf = new File[] {new File(tmpPath + "/" +
161                SAXParserTestSupport.XML_WF + "staff.xml")};
162        list_nwf = new File[] {new File(tmpPath + "/" +
163                SAXParserTestSupport.XML_NWF + "staff.xml")};
164        list_out_dh = new File[] {new File(tmpPath + "/" +
165                SAXParserTestSupport.XML_WF_OUT_DH + "staff.out")};
166        list_out_hb = new File[] {new File(tmpPath + "/" +
167                SAXParserTestSupport.XML_WF_OUT_HB + "staff.out")};
168
169        list_wf[0].deleteOnExit();
170        list_nwf[0].deleteOnExit();
171        list_out_hb[0].deleteOnExit();
172        list_out_dh[0].deleteOnExit();
173
174
175        Support_Resources.copyLocalFileto(list_wf[0],
176                getResource(SAXParserTestSupport.XML_WF + "staff.xml"));
177        Support_Resources.copyLocalFileto(new File(
178                tmpPath + "/" + SAXParserTestSupport.XML_WF + "staff.dtd"),
179                getResource(SAXParserTestSupport.XML_WF + "staff.dtd"));
180
181        Support_Resources.copyLocalFileto(list_nwf[0],
182                getResource(SAXParserTestSupport.XML_NWF + "staff.xml"));
183        Support_Resources.copyLocalFileto(new File(
184                tmpPath + "/" + SAXParserTestSupport.XML_NWF + "staff.dtd"),
185                getResource(SAXParserTestSupport.XML_NWF + "staff.dtd"));
186
187        Support_Resources.copyLocalFileto(list_out_dh[0],
188                getResource(SAXParserTestSupport.XML_WF_OUT_DH + "staff.out"));
189        Support_Resources.copyLocalFileto(list_out_hb[0],
190                getResource(SAXParserTestSupport.XML_WF_OUT_HB + "staff.out"));
191    }
192
193    @Override
194    protected void setUp() throws Exception {
195        spf = SAXParserFactory.newInstance();
196        parser = spf.newSAXParser();
197        assertNotNull(parser);
198
199        ns = new HashMap<String, String>();
200        attr = new HashMap<String, String>();
201        el = new Vector<String>();
202        initFiles();
203    }
204
205    @Override
206    protected void tearDown() throws Exception {
207    }
208
209//    public static void main(String[] args) throws Exception {
210//        SAXParserTest st = new SAXParserTest();
211//        st.setUp();
212//        st.generateDataFromReferenceImpl();
213//
214//    }
215//
216//    private void generateDataFromReferenceImpl() {
217//        try {
218//            for(int i = 0; i < list_wf.length; i++) {
219//                MyDefaultHandler dh = new MyDefaultHandler();
220//                InputStream is = new FileInputStream(list_wf[i]);
221//                parser.parse(is, dh, ParsingSupport.XML_SYSTEM_ID);
222//                HashMap refHm = dh.createData();
223//
224//                StringBuilder sb = new StringBuilder();
225//                for (int j = 0; j < ParsingSupport.KEYS.length; j++) {
226//                    String key = ParsingSupport.KEYS[j];
227//                    sb.append(refHm.get(key)).append(
228//                            ParsingSupport.SEPARATOR_DATA);
229//                }
230//                FileWriter fw = new FileWriter("/tmp/build_dh"+i+".out");
231//                fw.append(sb.toString());
232//                fw.close();
233//            }
234//
235//            for(int i = 0; i < list_nwf.length; i++) {
236//                MyHandler hb = new MyHandler();
237//                InputStream is = new FileInputStream(list_wf[i]);
238//                parser.parse(is, hb, ParsingSupport.XML_SYSTEM_ID);
239//                HashMap refHm = hb.createData();
240//
241//                StringBuilder sb = new StringBuilder();
242//                for (int j = 0; j < ParsingSupport.KEYS.length; j++) {
243//                    String key = ParsingSupport.KEYS[j];
244//                    sb.append(refHm.get(key)).append(
245//                            ParsingSupport.SEPARATOR_DATA);
246//                }
247//                FileWriter fw = new FileWriter("/tmp/build_hb"+i+".out");
248//                fw.append(sb.toString());
249//                fw.close();
250//            }
251//
252//
253//        } catch (Exception e) {
254//            e.printStackTrace();
255//        }
256//    }
257
258    @TestTargetNew(
259        level = TestLevel.COMPLETE,
260        notes = "",
261        method = "SAXParser",
262        args = {}
263    )
264    public void testSAXParser() {
265        try {
266            new MockSAXParser();
267        } catch (Exception e) {
268            fail("unexpected exception " + e.toString());
269        }
270    }
271
272    /**
273     * @tests javax.xml.parser.SAXParser#getSchema().
274     * TODO getSchema() IS NOT SUPPORTED
275     */
276    /*   public void test_getSchema() {
277        assertNull(parser.getSchema());
278        SchemaFactory sf =
279            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
280        try {
281            Schema schema = sf.newSchema();
282            spf.setSchema(schema);
283            assertNotNull(spf.newSAXParser().getSchema());
284        } catch (ParserConfigurationException pce) {
285            fail("Unexpected ParserConfigurationException " + pce.toString());
286        } catch (SAXException sax) {
287            fail("Unexpected SAXException " + sax.toString());
288        }
289    }
290     */
291
292    @TestTargetNew(
293        level = TestLevel.COMPLETE,
294        notes = "",
295        method = "isNamespaceAware",
296        args = {}
297    )
298    public void testIsNamespaceAware() {
299        try {
300            spf.setNamespaceAware(true);
301            assertTrue(spf.newSAXParser().isNamespaceAware());
302            spf.setNamespaceAware(false);
303            assertFalse(spf.newSAXParser().isNamespaceAware());
304        } catch (Exception e) {
305            throw new RuntimeException("Unexpected exception", e);
306        }
307    }
308
309    @TestTargetNew(
310        level = TestLevel.SUFFICIENT,
311        notes = "No validating parser in Android, hence not tested",
312        method = "isValidating",
313        args = {}
314    )
315    public void testIsValidating() {
316        try {
317            spf.setValidating(false);
318            assertFalse(spf.newSAXParser().isValidating());
319        } catch (Exception e) {
320            throw new RuntimeException("Unexpected exception", e);
321        }
322    }
323
324    @TestTargetNew(
325        level = TestLevel.SUFFICIENT,
326        notes = "No XInclude-aware parser in Android, hence not tested",
327        method = "isXIncludeAware",
328        args = {}
329    )
330    @KnownFailure("Should handle XIncludeAware flag more gracefully")
331    public void testIsXIncludeAware() {
332        try {
333            spf.setXIncludeAware(false);
334            assertFalse(spf.newSAXParser().isXIncludeAware());
335        } catch (Exception e) {
336            throw new RuntimeException("Unexpected exception", e);
337        }
338    }
339
340    /**
341     * @test javax.xml.parsers.SAXParser#parse(java.io.File,
342     *     org.xml.sax.helpers.DefaultHandler)
343     */
344    @TestTargetNew(
345        level = TestLevel.PARTIAL,
346        notes = "Doesn't verify positive functionality properly; not all exceptions are verified.",
347        method = "parse",
348        args = {java.io.File.class, org.xml.sax.helpers.DefaultHandler.class}
349    )
350    public void test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler()
351    throws Exception {
352
353        for(int i = 0; i < list_wf.length; i++) {
354            HashMap<String, String> hm =
355                new SAXParserTestSupport().readFile(list_out_dh[i].getPath());
356            MyDefaultHandler dh = new MyDefaultHandler();
357            parser.parse(list_wf[i], dh);
358            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
359        }
360
361        for(int i = 0; i < list_nwf.length; i++) {
362            try {
363                MyDefaultHandler dh = new MyDefaultHandler();
364                parser.parse(list_nwf[i], dh);
365                fail("SAXException is not thrown");
366            } catch(org.xml.sax.SAXException se) {
367                //expected
368            }
369        }
370
371        try {
372            MyDefaultHandler dh = new MyDefaultHandler();
373            parser.parse((File) null, dh);
374            fail("java.lang.IllegalArgumentException is not thrown");
375        } catch(java.lang.IllegalArgumentException iae) {
376            //expected
377        }
378
379        try {
380            parser.parse(list_wf[0], (DefaultHandler) null);
381        } catch(java.lang.IllegalArgumentException iae) {
382            fail("java.lang.IllegalArgumentException is thrown");
383        }
384    }
385
386    @TestTargetNew(
387        level = TestLevel.SUFFICIENT,
388        notes = "Sufficient while XML parser situation is still unclear",
389        method = "parse",
390        args = {java.io.File.class, org.xml.sax.HandlerBase.class}
391    )
392    public void testParseFileHandlerBase() {
393        for(int i = 0; i < list_wf.length; i++) {
394            try {
395                HashMap<String, String> hm = sp.readFile(
396                        list_out_hb[i].getPath());
397                MyHandler dh = new MyHandler();
398                parser.parse(list_wf[i], dh);
399                assertTrue(SAXParserTestSupport.equalsMaps(hm,
400                        dh.createData()));
401            } catch (IOException ioe) {
402                fail("Unexpected IOException " + ioe.toString());
403            } catch (SAXException sax) {
404                fail("Unexpected SAXException " + sax.toString());
405            }
406        }
407
408        for(int i = 0; i < list_nwf.length; i++) {
409            try {
410                MyHandler dh = new MyHandler();
411                parser.parse(list_nwf[i], dh);
412                fail("SAXException is not thrown");
413            } catch(org.xml.sax.SAXException se) {
414                //expected
415            } catch (FileNotFoundException fne) {
416                fail("Unexpected FileNotFoundException " + fne.toString());
417            } catch (IOException ioe) {
418                fail("Unexpected IOException " + ioe.toString());
419            }
420        }
421
422        try {
423            MyHandler dh = new MyHandler();
424            parser.parse((File) null, dh);
425            fail("java.lang.IllegalArgumentException is not thrown");
426        } catch(java.lang.IllegalArgumentException iae) {
427            //expected
428        } catch (IOException ioe) {
429            fail("Unexpected IOException " + ioe.toString());
430        } catch(SAXException sax) {
431            fail("Unexpected SAXException " + sax.toString());
432        }
433
434        try {
435            parser.parse(list_wf[0], (HandlerBase) null);
436        } catch(java.lang.IllegalArgumentException iae) {
437            fail("java.lang.IllegalArgumentException is thrown");
438        } catch (FileNotFoundException fne) {
439            fail("Unexpected FileNotFoundException " + fne.toString());
440        } catch(IOException ioe) {
441            fail("Unexpected IOException " + ioe.toString());
442        } catch(SAXException sax) {
443            fail("Unexpected SAXException " + sax.toString());
444        }
445    }
446
447    /**
448     * @test javax.xml.parsers.SAXParser#parse(org.xml.sax.InputSource,
449     *     org.xml.sax.helpers.DefaultHandler)
450     */
451    @TestTargetNew(
452        level = TestLevel.PARTIAL,
453        notes = "Doesn't verify IOException.",
454        method = "parse",
455        args = {org.xml.sax.InputSource.class, org.xml.sax.helpers.DefaultHandler.class}
456    )
457    public void test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler()
458    throws Exception {
459
460        for(int i = 0; i < list_wf.length; i++) {
461
462            HashMap<String, String> hm = new SAXParserTestSupport().readFile(
463                    list_out_dh[i].getPath());
464            MyDefaultHandler dh = new MyDefaultHandler();
465            InputSource is = new InputSource(new FileInputStream(list_wf[i]));
466            parser.parse(is, dh);
467            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
468        }
469
470        for(int i = 0; i < list_nwf.length; i++) {
471            try {
472                MyDefaultHandler dh = new MyDefaultHandler();
473                InputSource is = new InputSource(
474                        new FileInputStream(list_nwf[i]));
475                parser.parse(is, dh);
476                fail("SAXException is not thrown");
477            } catch(org.xml.sax.SAXException se) {
478                //expected
479            }
480        }
481
482        try {
483            MyDefaultHandler dh = new MyDefaultHandler();
484            parser.parse((InputSource) null, dh);
485            fail("java.lang.IllegalArgumentException is not thrown");
486        } catch(java.lang.IllegalArgumentException iae) {
487            //expected
488        }
489
490        try {
491            InputSource is = new InputSource(new FileInputStream(list_wf[0]));
492            parser.parse(is, (DefaultHandler) null);
493        } catch(java.lang.IllegalArgumentException iae) {
494            fail("java.lang.IllegalArgumentException is thrown");
495        }
496
497        try {
498            InputSource is = new InputSource(new BrokenInputStream(new FileInputStream(list_wf[0]), 10));
499            parser.parse(is, (DefaultHandler) null);
500            fail("IOException expected");
501        } catch(IOException e) {
502            // Expected
503        }
504    }
505
506    @TestTargetNew(
507        level = TestLevel.SUFFICIENT,
508        notes = "Sufficient while XML parser situation is still unclear",
509        method = "parse",
510        args = {org.xml.sax.InputSource.class, org.xml.sax.HandlerBase.class}
511    )
512    public void testParseInputSourceHandlerBase() {
513        for(int i = 0; i < list_wf.length; i++) {
514            try {
515                HashMap<String, String> hm = sp.readFile(
516                        list_out_hb[i].getPath());
517                MyHandler dh = new MyHandler();
518                InputSource is = new InputSource(new FileInputStream(list_wf[i]));
519                parser.parse(is, dh);
520                assertTrue(SAXParserTestSupport.equalsMaps(hm,
521                        dh.createData()));
522            } catch (IOException ioe) {
523                fail("Unexpected IOException " + ioe.toString());
524            } catch (SAXException sax) {
525                fail("Unexpected SAXException " + sax.toString());
526            }
527        }
528
529        for(int i = 0; i < list_nwf.length; i++) {
530            try {
531                MyHandler dh = new MyHandler();
532                InputSource is = new InputSource(new FileInputStream(list_nwf[i]));
533                parser.parse(is, dh);
534                fail("SAXException is not thrown");
535            } catch(org.xml.sax.SAXException se) {
536                //expected
537            } catch (FileNotFoundException fne) {
538                fail("Unexpected FileNotFoundException " + fne.toString());
539            } catch (IOException ioe) {
540                fail("Unexpected IOException " + ioe.toString());
541            }
542        }
543
544        try {
545            MyHandler dh = new MyHandler();
546            parser.parse((InputSource) null, dh);
547            fail("java.lang.IllegalArgumentException is not thrown");
548        } catch(java.lang.IllegalArgumentException iae) {
549            //expected
550        } catch (IOException ioe) {
551            fail("Unexpected IOException " + ioe.toString());
552        } catch(SAXException sax) {
553            fail("Unexpected SAXException " + sax.toString());
554        }
555
556        try {
557            InputSource is = new InputSource(new FileInputStream(list_wf[0]));
558            parser.parse(is, (HandlerBase) null);
559        } catch(java.lang.IllegalArgumentException iae) {
560            fail("java.lang.IllegalArgumentException is thrown");
561        } catch (FileNotFoundException fne) {
562            fail("Unexpected FileNotFoundException " + fne.toString());
563        } catch(IOException ioe) {
564            fail("Unexpected IOException " + ioe.toString());
565        } catch(SAXException sax) {
566            fail("Unexpected SAXException " + sax.toString());
567        }
568
569        // Reader case
570        try {
571            InputSource is = new InputSource(new InputStreamReader(
572                    new FileInputStream(list_wf[0])));
573            parser.parse(is, (HandlerBase) null);
574        } catch(java.lang.IllegalArgumentException iae) {
575            fail("java.lang.IllegalArgumentException is thrown");
576        } catch (FileNotFoundException fne) {
577            fail("Unexpected FileNotFoundException " + fne.toString());
578        } catch(IOException ioe) {
579            fail("Unexpected IOException " + ioe.toString());
580        } catch(SAXException sax) {
581            fail("Unexpected SAXException " + sax.toString());
582        }
583
584        // SystemID case
585        try {
586            InputSource is = new InputSource(list_wf[0].toURI().toString());
587            parser.parse(is, (HandlerBase) null);
588        } catch(java.lang.IllegalArgumentException iae) {
589            fail("java.lang.IllegalArgumentException is thrown");
590        } catch (FileNotFoundException fne) {
591            fail("Unexpected FileNotFoundException " + fne.toString());
592        } catch(IOException ioe) {
593            fail("Unexpected IOException " + ioe.toString());
594        } catch(SAXException sax) {
595            fail("Unexpected SAXException " + sax.toString());
596        }
597
598        // Inject IOException
599        try {
600            InputStream is = new BrokenInputStream(
601                    new FileInputStream(list_wf[0]), 10);
602            parser.parse(is, (HandlerBase) null,
603                    SAXParserTestSupport.XML_SYSTEM_ID);
604            fail("IOException expected");
605        } catch(IOException e) {
606            // Expected
607        } catch (Exception e) {
608            throw new RuntimeException("Unexpected exception", e);
609        }
610    }
611
612    /**
613     * @test javax.xml.parsers.SAXParser#parse(java.io.InputStream,
614     *     org.xml.sax.helpers.DefaultHandler)
615     */
616    @TestTargetNew(
617        level = TestLevel.PARTIAL,
618        notes = "Doesn't verify IOException.",
619        method = "parse",
620        args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
621    )
622    public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler()
623    throws Exception {
624
625        for(int i = 0; i < list_wf.length; i++) {
626
627            HashMap<String, String> hm = new SAXParserTestSupport().readFile(
628                    list_out_dh[i].getPath());
629            MyDefaultHandler dh = new MyDefaultHandler();
630            InputStream is = new FileInputStream(list_wf[i]);
631            parser.parse(is, dh);
632            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
633        }
634
635        for(int i = 0; i < list_nwf.length; i++) {
636            try {
637                MyDefaultHandler dh = new MyDefaultHandler();
638                InputStream is = new FileInputStream(list_nwf[i]);
639                parser.parse(is, dh);
640                fail("SAXException is not thrown");
641            } catch(org.xml.sax.SAXException se) {
642                //expected
643            }
644        }
645
646        try {
647            MyDefaultHandler dh = new MyDefaultHandler();
648            parser.parse((InputStream) null, dh);
649            fail("java.lang.IllegalArgumentException is not thrown");
650        } catch(java.lang.IllegalArgumentException iae) {
651            //expected
652        }
653
654        try {
655            InputStream is = new FileInputStream(list_wf[0]);
656            parser.parse(is, (DefaultHandler) null);
657        } catch(java.lang.IllegalArgumentException iae) {
658            fail("java.lang.IllegalArgumentException is thrown");
659        }
660    }
661
662    /**
663     * @test javax.xml.parsers.SAXParser#parse(java.io.InputStream,
664     *     org.xml.sax.helpers.DefaultHandler, java.lang.String)
665     */
666    @TestTargetNew(
667        level = TestLevel.PARTIAL,
668        notes = "Doesn't verify  IOException.",
669        method = "parse",
670        args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class, java.lang.String.class}
671    )
672    @KnownFailure("We supply optional qnames, but this test doesn't expect them")
673    public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String() {
674        for(int i = 0; i < list_wf.length; i++) {
675            try {
676                HashMap<String, String> hm = sp.readFile(
677                        list_out_hb[i].getPath());
678                MyDefaultHandler dh = new MyDefaultHandler();
679                InputStream is = new FileInputStream(list_wf[i]);
680                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
681                assertEquals(hm, dh.createData());
682            } catch (IOException ioe) {
683                fail("Unexpected IOException " + ioe.toString());
684            } catch (SAXException sax) {
685                fail("Unexpected SAXException " + sax.toString());
686            }
687        }
688
689        for(int i = 0; i < list_nwf.length; i++) {
690            try {
691                MyDefaultHandler dh = new MyDefaultHandler();
692                InputStream is = new FileInputStream(list_nwf[i]);
693                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
694                fail("SAXException is not thrown");
695            } catch(org.xml.sax.SAXException se) {
696                //expected
697            } catch (FileNotFoundException fne) {
698                fail("Unexpected FileNotFoundException " + fne.toString());
699            } catch (IOException ioe) {
700                fail("Unexpected IOException " + ioe.toString());
701            }
702        }
703
704        try {
705            MyDefaultHandler dh = new MyDefaultHandler();
706            parser.parse((InputStream) null, dh,
707                    SAXParserTestSupport.XML_SYSTEM_ID);
708            fail("java.lang.IllegalArgumentException is not thrown");
709        } catch(java.lang.IllegalArgumentException iae) {
710            //expected
711        } catch (IOException ioe) {
712            fail("Unexpected IOException " + ioe.toString());
713        } catch(SAXException sax) {
714            fail("Unexpected SAXException " + sax.toString());
715        }
716
717        try {
718            InputStream is = new FileInputStream(list_wf[0]);
719            parser.parse(is, (DefaultHandler) null,
720                    SAXParserTestSupport.XML_SYSTEM_ID);
721        } catch(java.lang.IllegalArgumentException iae) {
722            fail("java.lang.IllegalArgumentException is thrown");
723        } catch (FileNotFoundException fne) {
724            fail("Unexpected FileNotFoundException " + fne.toString());
725        } catch(IOException ioe) {
726            fail("Unexpected IOException " + ioe.toString());
727        } catch(SAXException sax) {
728            fail("Unexpected SAXException " + sax.toString());
729        }
730//
731//        for(int i = 0; i < list_wf.length; i++) {
732//
733//            HashMap<String, String> hm = new SAXParserTestSupport().readFile(
734//                    list_out_dh[i].getPath());
735//            MyDefaultHandler dh = new MyDefaultHandler();
736//            InputStream is = new FileInputStream(list_wf[i]);
737//            parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
738//            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
739//        }
740//
741//        for(int i = 0; i < list_nwf.length; i++) {
742//            try {
743//                MyDefaultHandler dh = new MyDefaultHandler();
744//                InputStream is = new FileInputStream(list_nwf[i]);
745//                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
746//                fail("SAXException is not thrown");
747//            } catch(org.xml.sax.SAXException se) {
748//                //expected
749//            }
750//        }
751//
752//        try {
753//            MyDefaultHandler dh = new MyDefaultHandler();
754//            parser.parse((InputStream) null, dh,
755//                    SAXParserTestSupport.XML_SYSTEM_ID);
756//            fail("java.lang.IllegalArgumentException is not thrown");
757//        } catch(java.lang.IllegalArgumentException iae) {
758//            //expected
759//        }
760//
761//        try {
762//            InputStream is = new FileInputStream(list_wf[0]);
763//            parser.parse(is, (DefaultHandler) null,
764//                    SAXParserTestSupport.XML_SYSTEM_ID);
765//        } catch(java.lang.IllegalArgumentException iae) {
766//            fail("java.lang.IllegalArgumentException is thrown");
767//        }
768//
769//        // TODO commented out since our parser is nonvalidating and thus never
770//        // tries to load staff.dtd in "/" ... and therefore never can fail with
771//        // an IOException
772//        /*try {
773//            MyDefaultHandler dh = new MyDefaultHandler();
774//            InputStream is = new FileInputStream(list_wf[0]);
775//            parser.parse(is, dh, "/");
776//            fail("Expected IOException was not thrown");
777//        } catch(IOException ioe) {
778//            // expected
779//        }*/
780    }
781
782    @TestTargetNew(
783        level = TestLevel.SUFFICIENT,
784        notes = "Sufficient while XML parser situation is still unclear",
785        method = "parse",
786        args = {java.io.InputStream.class, org.xml.sax.HandlerBase.class}
787    )
788    public void testParseInputStreamHandlerBase() {
789        for(int i = 0; i < list_wf.length; i++) {
790            try {
791                HashMap<String, String> hm = sp.readFile(
792                        list_out_hb[i].getPath());
793                MyHandler dh = new MyHandler();
794                InputStream is = new FileInputStream(list_wf[i]);
795                parser.parse(is, dh);
796                assertTrue(SAXParserTestSupport.equalsMaps(hm,
797                        dh.createData()));
798            } catch (IOException ioe) {
799                fail("Unexpected IOException " + ioe.toString());
800            } catch (SAXException sax) {
801                fail("Unexpected SAXException " + sax.toString());
802            }
803        }
804
805        for(int i = 0; i < list_nwf.length; i++) {
806            try {
807                MyHandler dh = new MyHandler();
808                InputStream is = new FileInputStream(list_nwf[i]);
809                parser.parse(is, dh);
810                fail("SAXException is not thrown");
811            } catch(org.xml.sax.SAXException se) {
812                //expected
813            } catch (FileNotFoundException fne) {
814                fail("Unexpected FileNotFoundException " + fne.toString());
815            } catch (IOException ioe) {
816                fail("Unexpected IOException " + ioe.toString());
817            }
818        }
819
820        try {
821            MyHandler dh = new MyHandler();
822            parser.parse((InputStream) null, dh);
823            fail("java.lang.IllegalArgumentException is not thrown");
824        } catch(java.lang.IllegalArgumentException iae) {
825            //expected
826        } catch (IOException ioe) {
827            fail("Unexpected IOException " + ioe.toString());
828        } catch(SAXException sax) {
829            fail("Unexpected SAXException " + sax.toString());
830        }
831
832        try {
833            InputStream is = new FileInputStream(list_wf[0]);
834            parser.parse(is, (HandlerBase) null);
835        } catch(java.lang.IllegalArgumentException iae) {
836            fail("java.lang.IllegalArgumentException is thrown");
837        } catch (FileNotFoundException fne) {
838            fail("Unexpected FileNotFoundException " + fne.toString());
839        } catch(IOException ioe) {
840            fail("Unexpected IOException " + ioe.toString());
841        } catch(SAXException sax) {
842            fail("Unexpected SAXException " + sax.toString());
843        }
844
845        // Inject IOException
846        try {
847            InputStream is = new BrokenInputStream(
848                    new FileInputStream(list_wf[0]), 10);
849            parser.parse(is, (HandlerBase) null);
850            fail("IOException expected");
851        } catch(IOException e) {
852            // Expected
853        } catch (Exception e) {
854            throw new RuntimeException("Unexpected exception", e);
855        }
856    }
857
858    @TestTargetNew(
859        level = TestLevel.SUFFICIENT,
860        notes = "Sufficient while XML parser situation is still unclear",
861        method = "parse",
862        args = {java.io.InputStream.class, org.xml.sax.HandlerBase.class, java.lang.String.class}
863    )
864    public void testParseInputStreamHandlerBaseString() {
865        for(int i = 0; i < list_wf.length; i++) {
866            try {
867                HashMap<String, String> hm = sp.readFile(
868                        list_out_hb[i].getPath());
869                MyHandler dh = new MyHandler();
870                InputStream is = new FileInputStream(list_wf[i]);
871                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
872                assertTrue(SAXParserTestSupport.equalsMaps(hm,
873                        dh.createData()));
874            } catch (IOException ioe) {
875                fail("Unexpected IOException " + ioe.toString());
876            } catch (SAXException sax) {
877                fail("Unexpected SAXException " + sax.toString());
878            }
879        }
880
881        for(int i = 0; i < list_nwf.length; i++) {
882            try {
883                MyHandler dh = new MyHandler();
884                InputStream is = new FileInputStream(list_nwf[i]);
885                parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID);
886                fail("SAXException is not thrown");
887            } catch(org.xml.sax.SAXException se) {
888                //expected
889            } catch (FileNotFoundException fne) {
890                fail("Unexpected FileNotFoundException " + fne.toString());
891            } catch (IOException ioe) {
892                fail("Unexpected IOException " + ioe.toString());
893            }
894        }
895
896        try {
897            MyHandler dh = new MyHandler();
898            parser.parse((InputStream) null, dh,
899                    SAXParserTestSupport.XML_SYSTEM_ID);
900            fail("java.lang.IllegalArgumentException is not thrown");
901        } catch(java.lang.IllegalArgumentException iae) {
902            //expected
903        } catch (IOException ioe) {
904            fail("Unexpected IOException " + ioe.toString());
905        } catch(SAXException sax) {
906            fail("Unexpected SAXException " + sax.toString());
907        }
908
909        try {
910            InputStream is = new FileInputStream(list_wf[0]);
911            parser.parse(is, (HandlerBase) null,
912                    SAXParserTestSupport.XML_SYSTEM_ID);
913        } catch(java.lang.IllegalArgumentException iae) {
914            fail("java.lang.IllegalArgumentException is thrown");
915        } catch (FileNotFoundException fne) {
916            fail("Unexpected FileNotFoundException " + fne.toString());
917        } catch(IOException ioe) {
918            fail("Unexpected IOException " + ioe.toString());
919        } catch(SAXException sax) {
920            fail("Unexpected SAXException " + sax.toString());
921        }
922
923        // Inject IOException
924        try {
925            InputStream is = new BrokenInputStream(
926                    new FileInputStream(list_wf[0]), 10);
927            parser.parse(is, (HandlerBase) null,
928                    SAXParserTestSupport.XML_SYSTEM_ID);
929            fail("IOException expected");
930        } catch(IOException e) {
931            // Expected
932        } catch (Exception e) {
933            throw new RuntimeException("Unexpected exception", e);
934        }
935    }
936
937    /**
938     * @test javax.xml.parsers.SAXParser#parse(java.lang.String,
939     *     org.xml.sax.helpers.DefaultHandler)
940     */
941    @TestTargetNew(
942        level = TestLevel.PARTIAL,
943        notes = "Doesn't verify IOException.",
944        method = "parse",
945        args = {java.lang.String.class, org.xml.sax.helpers.DefaultHandler.class}
946    )
947    public void test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler()
948    throws Exception {
949
950        for(int i = 0; i < list_wf.length; i++) {
951
952            HashMap<String, String> hm = new SAXParserTestSupport().readFile(
953                    list_out_dh[i].getPath());
954            MyDefaultHandler dh = new MyDefaultHandler();
955            parser.parse(list_wf[i].toURI().toString(), dh);
956            assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
957        }
958
959        for(int i = 0; i < list_nwf.length; i++) {
960            try {
961                MyDefaultHandler dh = new MyDefaultHandler();
962                parser.parse(list_nwf[i].toURI().toString(), dh);
963                fail("SAXException is not thrown");
964            } catch(org.xml.sax.SAXException se) {
965                //expected
966            }
967        }
968
969        try {
970            MyDefaultHandler dh = new MyDefaultHandler();
971            parser.parse((String) null, dh);
972            fail("java.lang.IllegalArgumentException is not thrown");
973        } catch(java.lang.IllegalArgumentException iae) {
974            //expected
975        }
976
977        try {
978            parser.parse(list_wf[0].toURI().toString(), (DefaultHandler) null);
979        } catch(java.lang.IllegalArgumentException iae) {
980            fail("java.lang.IllegalArgumentException is thrown");
981        }
982    }
983
984    @TestTargetNew(
985        level = TestLevel.SUFFICIENT,
986        notes = "Sufficient while XML parser situation is still unclear",
987        method = "parse",
988        args = {java.lang.String.class, org.xml.sax.HandlerBase.class}
989    )
990    public void testParseStringHandlerBase() {
991        for(int i = 0; i < list_wf.length; i++) {
992            try {
993                HashMap<String, String> hm = sp.readFile(
994                        list_out_hb[i].getPath());
995                MyHandler dh = new MyHandler();
996                parser.parse(list_wf[i].toURI().toString(), dh);
997                assertTrue(SAXParserTestSupport.equalsMaps(hm,
998                        dh.createData()));
999            } catch (IOException ioe) {
1000                fail("Unexpected IOException " + ioe.toString());
1001            } catch (SAXException sax) {
1002                fail("Unexpected SAXException " + sax.toString());
1003            }
1004        }
1005
1006        for(int i = 0; i < list_nwf.length; i++) {
1007            try {
1008                MyHandler dh = new MyHandler();
1009                parser.parse(list_nwf[i].toURI().toString(), dh);
1010                fail("SAXException is not thrown");
1011            } catch(org.xml.sax.SAXException se) {
1012                //expected
1013            } catch (FileNotFoundException fne) {
1014                fail("Unexpected FileNotFoundException " + fne.toString());
1015            } catch (IOException ioe) {
1016                fail("Unexpected IOException " + ioe.toString());
1017            }
1018        }
1019
1020        try {
1021            MyHandler dh = new MyHandler();
1022            parser.parse((String) null, dh);
1023            fail("java.lang.IllegalArgumentException is not thrown");
1024        } catch(java.lang.IllegalArgumentException iae) {
1025            //expected
1026        } catch (IOException ioe) {
1027            fail("Unexpected IOException " + ioe.toString());
1028        } catch(SAXException sax) {
1029            fail("Unexpected SAXException " + sax.toString());
1030        }
1031
1032        try {
1033            parser.parse(list_wf[0].toURI().toString(), (HandlerBase) null);
1034        } catch(java.lang.IllegalArgumentException iae) {
1035            fail("java.lang.IllegalArgumentException is thrown");
1036        } catch (FileNotFoundException fne) {
1037            fail("Unexpected FileNotFoundException " + fne.toString());
1038        } catch(IOException ioe) {
1039            fail("Unexpected IOException " + ioe.toString());
1040        } catch(SAXException sax) {
1041            fail("Unexpected SAXException " + sax.toString());
1042        }
1043    }
1044
1045    @TestTargetNew(
1046        level = TestLevel.COMPLETE,
1047        method = "reset",
1048        args = { }
1049    )
1050    @KnownFailure("Android DocumentBuilder should implement reset() properly")
1051    public void testReset() {
1052        try {
1053            spf = SAXParserFactory.newInstance();
1054            parser = spf.newSAXParser();
1055
1056            parser.setProperty(LEXICAL_HANDLER_PROPERTY, new MockHandler(new MethodLogger()));
1057            parser.reset();
1058            assertEquals(null, parser.getProperty(LEXICAL_HANDLER_PROPERTY));
1059        } catch (Exception e) {
1060            throw new RuntimeException("Unexpected exception", e);
1061        }
1062    }
1063
1064    @TestTargetNew(
1065        level = TestLevel.COMPLETE,
1066        method = "getParser",
1067        args = { }
1068    )
1069    public void testGetParser() {
1070        spf = SAXParserFactory.newInstance();
1071        try {
1072            Parser parser = spf.newSAXParser().getParser();
1073            assertNotNull(parser);
1074        } catch (Exception e) {
1075            throw new RuntimeException("Unexpected exception", e);
1076        }
1077    }
1078
1079    @TestTargetNew(
1080        level = TestLevel.COMPLETE,
1081        method = "getXMLReader",
1082        args = { }
1083    )
1084    public void testGetReader() {
1085        spf = SAXParserFactory.newInstance();
1086        try {
1087            XMLReader reader = spf.newSAXParser().getXMLReader();
1088            assertNotNull(reader);
1089        } catch (Exception e) {
1090            throw new RuntimeException("Unexpected exception", e);
1091        }
1092    }
1093
1094    @TestTargets({
1095        @TestTargetNew(
1096            level = TestLevel.COMPLETE,
1097            method = "getProperty",
1098            args = { String.class }
1099        ),
1100        @TestTargetNew(
1101            level = TestLevel.COMPLETE,
1102            method = "setProperty",
1103            args = { String.class, Object.class }
1104        )
1105    })
1106    @KnownFailure("ExpatParser should allow to clear properties")
1107    public void testSetGetProperty() {
1108        // Ordinary case
1109        String validName = "http://xml.org/sax/properties/lexical-handler";
1110        LexicalHandler validValue = new MockHandler(new MethodLogger());
1111
1112        try {
1113            SAXParser parser = spf.newSAXParser();
1114            parser.setProperty(validName, validValue);
1115            assertEquals(validValue, parser.getProperty(validName));
1116
1117            parser.setProperty(validName, null);
1118            assertEquals(null, parser.getProperty(validName));
1119        } catch (Exception e) {
1120            throw new RuntimeException("Unexpected exception", e);
1121        }
1122
1123        // Unsupported property
1124        try {
1125            SAXParser parser = spf.newSAXParser();
1126            parser.setProperty("foo", "bar");
1127            fail("SAXNotRecognizedException expected");
1128        } catch (SAXNotRecognizedException e) {
1129            // Expected
1130        } catch (Exception e) {
1131            throw new RuntimeException("Unexpected exception", e);
1132        }
1133
1134        try {
1135            SAXParser parser = spf.newSAXParser();
1136            parser.getProperty("foo");
1137            fail("SAXNotRecognizedException expected");
1138        } catch (SAXNotRecognizedException e) {
1139            // Expected
1140        } catch (Exception e) {
1141            throw new RuntimeException("Unexpected exception", e);
1142        }
1143
1144        // No name case
1145        try {
1146            SAXParser parser = spf.newSAXParser();
1147            parser.setProperty(null, "bar");
1148            fail("NullPointerException expected");
1149        } catch (NullPointerException e) {
1150            // Expected
1151        } catch (Exception e) {
1152            throw new RuntimeException("Unexpected exception", e);
1153        }
1154
1155        try {
1156            SAXParser parser = spf.newSAXParser();
1157            parser.getProperty(null);
1158            fail("NullPointerException expected");
1159        } catch (NullPointerException e) {
1160            // Expected
1161        } catch (Exception e) {
1162            throw new RuntimeException("Unexpected exception", e);
1163        }
1164    }
1165
1166}
1167