• 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 junit.framework.TestCase;
19 
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
23 import org.xml.sax.ErrorHandler;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.SAXParseException;
26 
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.FactoryConfigurationError;
36 import javax.xml.parsers.ParserConfigurationException;
37 
38 public class DocumentBuilderFactoryTest extends TestCase {
39 
40     DocumentBuilderFactory dbf;
41 
42     List<String> cdataElements;
43 
44     List<String> textElements;
45 
46     List<String> commentElements;
47 
setUp()48     protected void setUp() throws Exception {
49         super.setUp();
50         dbf = DocumentBuilderFactory.newInstance();
51 
52         cdataElements = new ArrayList<String>();
53         textElements = new ArrayList<String>();
54         commentElements = new ArrayList<String>();
55     }
56 
tearDown()57     protected void tearDown() throws Exception {
58         dbf = null;
59         cdataElements = null;
60         textElements = null;
61         commentElements = null;
62         super.tearDown();
63     }
64 
65     /**
66      * javax.xml.parsers.DocumentBuilderFactory#DocumentBuilderFactory().
67      */
test_Constructor()68     public void test_Constructor() {
69         try {
70             new DocumentBuilderFactoryChild();
71         } catch (Exception e) {
72             fail("Unexpected exception " + e.toString());
73         }
74     }
75 
76     /**
77      * javax.xml.parsers.DocumentBuilderFactory#getAttribute(String).
78      */
79 //    public void test_getAttributeLjava_lang_String() {
80 //        String[] attributes = {
81 //                "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
82 //                "http://java.sun.com/xml/jaxp/properties/schemaSource" };
83 //        Object[] values = { "http://www.w3.org/2001/XMLSchema", "source" };
84 //
85 //        try {
86 //            for (int i = 0; i < attributes.length; i++) {
87 //                dbf.setAttribute(attributes[i], values[i]);
88 //                assertEquals(values[i], dbf.getAttribute(attributes[i]));
89 //            }
90 //        } catch (IllegalArgumentException e) {
91 //            fail("Unexpected IllegalArgumentException" + e.getMessage());
92 //        } catch (Exception e) {
93 //            fail("Unexpected exception" + e.getMessage());
94 //        }
95 //
96 //        try {
97 //            for (int i = 0; i < attributes.length; i++) {
98 //                dbf.setAttribute(null, null);
99 //                fail("NullPointerException expected");
100 //            }
101 //        } catch (NullPointerException e) {
102 //            // expected
103 //        }
104 //
105 //        String[] badAttributes = {"bad1", "bad2", ""};
106 //        try {
107 //            for (int i = 0; i < badAttributes.length; i++) {
108 //                dbf.getAttribute(badAttributes[i]);
109 //                fail("IllegalArgumentException expected");
110 //            }
111 //        } catch (IllegalArgumentException e) {
112 //            // expected
113 //        }
114 //    }
115 
116     /**
117      * javax.xml.parsers.DocumentBuilderFactory#getFeature(String).
118      */
119 // TODO Fails on JDK. Why?
120 //    public void test_getFeatureLjava_lang_String() {
121 //        String[] features = { "http://xml.org/sax/features/namespaces",
122 //                "http://xml.org/sax/features/validation",
123 //                "http://xml.org/sax/features/external-general-entities" };
124 //        try {
125 //            for (int i = 0; i < features.length; i++) {
126 //                dbf.setFeature(features[i], true);
127 //                assertTrue(dbf.getFeature(features[i]));
128 //            }
129 //        } catch (ParserConfigurationException e) {
130 //            fail("Unexpected ParserConfigurationException " + e.getMessage());
131 //        }
132 //
133 //        try {
134 //            for (int i = 0; i < features.length; i++) {
135 //                dbf.setFeature(features[i], false);
136 //                assertFalse(dbf.getFeature(features[i]));
137 //            }
138 //        } catch (ParserConfigurationException e) {
139 //            fail("Unexpected ParserConfigurationException " + e.getMessage());
140 //        }
141 //
142 //        try {
143 //            for (int i = 0; i < features.length; i++) {
144 //                dbf.setFeature(null, false);
145 //                fail("NullPointerException expected");
146 //            }
147 //        } catch (NullPointerException e) {
148 //            // expected
149 //        } catch (ParserConfigurationException e) {
150 //            fail("Unexpected ParserConfigurationException" + e.getMessage());
151 //        }
152 //
153 //        String[] badFeatures = {"bad1", "bad2", ""};
154 //        try {
155 //            for (int i = 0; i < badFeatures.length; i++) {
156 //                dbf.getFeature(badFeatures[i]);
157 //                fail("ParserConfigurationException expected");
158 //            }
159 //        } catch (ParserConfigurationException e) {
160 //            // expected
161 //        }
162 //
163 //    }
164 
165     /**
166      * javax.xml.parsers.DocumentBuilderFactory#getSchema().
167      *  TBD getSchemas() IS NOT SUPPORTED
168      */
169 /*    public void test_getSchema() {
170         assertNull(dbf.getSchema());
171         SchemaFactory sf =
172             SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
173         try {
174             Schema schema = sf.newSchema();
175             dbf.setSchema(schema);
176             assertNotNull(dbf.getSchema());
177         } catch (SAXException sax) {
178             fail("Unexpected exception " + sax.toString());
179         }
180     }
181     */
182 
183     /**
184      * javax.xml.parsers.DocumentBuilderFactory#isCoalescing().
185      */
test_isCoalescing()186     public void test_isCoalescing() {
187         dbf.setCoalescing(true);
188         assertTrue(dbf.isCoalescing());
189 
190         dbf.setCoalescing(false);
191         assertFalse(dbf.isCoalescing());
192     }
193 
194     /**
195      * javax.xml.parsers.DocumentBuilderFactory#isExpandEntityReferences().
196      */
test_isExpandEntityReferences()197     public void test_isExpandEntityReferences() {
198         dbf.setExpandEntityReferences(true);
199         assertTrue(dbf.isExpandEntityReferences());
200 
201         dbf.setExpandEntityReferences(false);
202         assertFalse(dbf.isExpandEntityReferences());
203     }
204 
205     /**
206      * javax.xml.parsers.DocumentBuilderFactory#isIgnoringComments().
207      */
test_isIgnoringComments()208     public void test_isIgnoringComments() {
209         dbf.setIgnoringComments(true);
210         assertTrue(dbf.isIgnoringComments());
211 
212         dbf.setIgnoringComments(false);
213         assertFalse(dbf.isIgnoringComments());
214     }
215 
216     /**
217      * javax.xml.parsers.DocumentBuilderFactory#isIgnoringElementContentWhitespace().
218      */
test_isIgnoringElementContentWhitespace()219     public void test_isIgnoringElementContentWhitespace() {
220         dbf.setIgnoringElementContentWhitespace(true);
221         assertTrue(dbf.isIgnoringElementContentWhitespace());
222 
223         dbf.setIgnoringElementContentWhitespace(false);
224         assertFalse(dbf.isIgnoringElementContentWhitespace());
225     }
226 
227     /**
228      * javax.xml.parsers.DocumentBuilderFactory#isNamespaceAware().
229      */
test_isNamespaceAware()230     public void test_isNamespaceAware() {
231         dbf.setNamespaceAware(true);
232         assertTrue(dbf.isNamespaceAware());
233 
234         dbf.setNamespaceAware(false);
235         assertFalse(dbf.isNamespaceAware());
236     }
237 
test_setIsValidating()238     public void test_setIsValidating() {
239         dbf.setValidating(true);
240         assertTrue(dbf.isValidating());
241 
242         dbf.setValidating(false);
243         assertFalse(dbf.isValidating());
244     }
245 
test_isSetXIncludeAware()246     public void test_isSetXIncludeAware() {
247         dbf.setXIncludeAware(true);
248         assertTrue(dbf.isXIncludeAware());
249 
250         dbf.setXIncludeAware(false);
251         assertFalse(dbf.isXIncludeAware());
252     }
253 
254     /**
255      * javax.xml.parsers.DocumentBuilderFactory#newInstance().
256      */
test_newInstance()257     public void test_newInstance() {
258         String className = null;
259         try {
260             // case 1: Try to obtain a new instance of factory by default.
261             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
262             assertNotNull(dbf);
263 
264             // case 2: Try to create a new instance of factory using
265             // property DATATYPEFACTORY_PROPERTY
266             className = System.getProperty("javax.xml.parsers.DocumentBuilderFactory");
267             System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
268                     "org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl");
269 
270             dbf = DocumentBuilderFactory.newInstance();
271             assertNotNull(dbf);
272             assertTrue(dbf instanceof org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl);
273 
274             // case 3: Try to create a new instance of factory using Property
275             String keyValuePair = "javax.xml.parsers.DocumentBuilderFactory"
276                     + "=" + "org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl";
277             ByteArrayInputStream bis = new ByteArrayInputStream(keyValuePair
278                     .getBytes());
279             Properties prop = System.getProperties();
280             prop.load(bis);
281             dbf = DocumentBuilderFactory.newInstance();
282             assertNotNull(dbf);
283             assertTrue(dbf instanceof org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl);
284 
285             // case 4: Check FactoryConfiguration error
286             System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "");
287             try {
288                 DocumentBuilderFactory.newInstance();
289             } catch (FactoryConfigurationError fce) {
290                 // expected
291             }
292 
293         } catch (Exception e) {
294             fail("Unexpected exception " + e.toString());
295         } finally {
296             // Set default value of Datatype factory,
297             // because of this test modifies it.
298             if (className == null) {
299                 System.clearProperty("javax.xml.parsers.DocumentBuilderFactory");
300             } else {
301                 System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
302                     className);
303             }
304         }
305     }
306 
test_newDocumentBuilder()307     public void test_newDocumentBuilder() {
308         // Ordinary case
309         try {
310             DocumentBuilder db = dbf.newDocumentBuilder();
311             assertTrue(db instanceof DocumentBuilder);
312             db.parse(getClass().getResourceAsStream("/simple.xml"));
313         } catch(Exception e) {
314             throw new RuntimeException("Unexpected exception", e);
315         }
316 
317         // Exception case
318         dbf.setValidating(true);
319         try {
320             DocumentBuilder db = dbf.newDocumentBuilder();
321         } catch(ParserConfigurationException e) {
322             // Expected, since Android doesn't have a validating parser.
323         }
324     }
325 
326     /**
327      * javax.xml.parsers.DocumentBuilderFactory#setAttribute(java.lang.String,
328      *     java.lang.Object).
329      */
330 //    public void test_setAttributeLjava_lang_StringLjava_lang_Object() {
331 //        String[] attributes = {
332 //                "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
333 //                "http://java.sun.com/xml/jaxp/properties/schemaSource" };
334 //        Object[] values = { "http://www.w3.org/2001/XMLSchema", "source" };
335 //
336 //        try {
337 //            for (int i = 0; i < attributes.length; i++) {
338 //                dbf.setAttribute(attributes[i], values[i]);
339 //                assertEquals(values[i], dbf.getAttribute(attributes[i]));
340 //            }
341 //        } catch (IllegalArgumentException e) {
342 //            fail("Unexpected IllegalArgumentException" + e.getMessage());
343 //        } catch (Exception e) {
344 //            fail("Unexpected exception" + e.getMessage());
345 //        }
346 //
347 //        String[] badAttributes = {"bad1", "bad2", ""};
348 //        try {
349 //            for (int i = 0; i < badAttributes.length; i++) {
350 //                dbf.setAttribute(badAttributes[i], "");
351 //                fail("IllegalArgumentException expected");
352 //            }
353 //        } catch (IllegalArgumentException iae) {
354 //            // expected
355 //        }
356 //
357 //        try {
358 //            for (int i = 0; i < attributes.length; i++) {
359 //                dbf.setAttribute(null, null);
360 //                fail("NullPointerException expected");
361 //            }
362 //        } catch (NullPointerException e) {
363 //            // expected
364 //        }
365 //    }
366 
367     /**
368      * javax.xml.parsers.DocumentBuilderFactory#setCoalescing(boolean).
369      */
test_setCoalescingZ()370     public void test_setCoalescingZ() {
371         dbf.setCoalescing(true);
372         assertTrue(dbf.isCoalescing());
373 
374         textElements.clear();
375         cdataElements.clear();
376         Exception parseException = null;
377         DocumentBuilder parser = null;
378 
379         try {
380             parser = dbf.newDocumentBuilder();
381             ValidationErrorHandler errorHandler = new ValidationErrorHandler();
382             parser.setErrorHandler(errorHandler);
383 
384             Document document = parser.parse(getClass().getResourceAsStream(
385                     "/recipt.xml"));
386 
387             parseException = errorHandler.getFirstException();
388 
389             goThroughDocument((Node) document, "");
390             assertTrue(textElements
391                     .contains("BeefParmesan<title>withGarlicAngelHairPasta</title>"));
392         } catch (Exception ex) {
393             parseException = ex;
394         }
395         parser.setErrorHandler(null);
396 
397         if (parseException != null) {
398             fail("Unexpected exception " + parseException.getMessage());
399         }
400 
401         dbf.setCoalescing(false);
402         assertFalse(dbf.isCoalescing());
403 
404         textElements.clear();
405         cdataElements.clear();
406 
407         try {
408             parser = dbf.newDocumentBuilder();
409             ValidationErrorHandler errorHandler = new ValidationErrorHandler();
410             parser.setErrorHandler(errorHandler);
411 
412             Document document = parser.parse(getClass().getResourceAsStream(
413                     "/recipt.xml"));
414 
415             parseException = errorHandler.getFirstException();
416 
417             goThroughDocument((Node) document, "");
418 
419             assertFalse(textElements
420                     .contains("BeefParmesan<title>withGarlicAngelHairPasta</title>"));
421 
422         } catch (Exception ex) {
423             parseException = ex;
424         }
425         parser.setErrorHandler(null);
426 
427         if (parseException != null) {
428             fail("Unexpected exception " + parseException.getMessage());
429         }
430     }
431 
432     /**
433      * javax.xml.parsers.DocumentBuilderFactory#setExpandEntityReferences(boolean).
434      */
test_setExpandEntityReferencesZ()435     public void test_setExpandEntityReferencesZ() {
436         dbf.setExpandEntityReferences(true);
437         assertTrue(dbf.isExpandEntityReferences());
438 
439         Exception parseException = null;
440         DocumentBuilder parser = null;
441 
442         try {
443             parser = dbf.newDocumentBuilder();
444             ValidationErrorHandler errorHandler = new ValidationErrorHandler();
445             parser.setErrorHandler(errorHandler);
446 
447             Document document = parser.parse(getClass().getResourceAsStream(
448                     "/recipt.xml"));
449 
450             parseException = errorHandler.getFirstException();
451 
452             assertNotNull(document);
453 
454         } catch (Exception ex) {
455             parseException = ex;
456         }
457         parser.setErrorHandler(null);
458 
459         if (parseException != null) {
460             fail("Unexpected exception " + parseException.getMessage());
461         }
462 
463         dbf.setExpandEntityReferences(false);
464         assertFalse(dbf.isExpandEntityReferences());
465         try {
466             parser = dbf.newDocumentBuilder();
467             ValidationErrorHandler errorHandler = new ValidationErrorHandler();
468             parser.setErrorHandler(errorHandler);
469 
470             Document document = parser.parse(getClass().getResourceAsStream(
471                     "/recipt.xml"));
472 
473             parseException = errorHandler.getFirstException();
474 
475             assertNotNull(document);
476 
477         } catch (Exception ex) {
478             parseException = ex;
479         }
480         parser.setErrorHandler(null);
481 
482         if (parseException != null) {
483             fail("Unexpected exception " + parseException.getMessage());
484         }
485     }
486 
487     /**
488      * javax.xml.parsers.DocumentBuilderFactory#setFeature(java.lang.String).
489      */
test_getSetFeatureLjava_lang_String()490     public void test_getSetFeatureLjava_lang_String() {
491         String[] features = { "http://xml.org/sax/features/namespaces",
492                 "http://xml.org/sax/features/validation" };
493         try {
494             for (int i = 0; i < features.length; i++) {
495                 dbf.setFeature(features[i], true);
496                 assertTrue(dbf.getFeature(features[i]));
497             }
498         } catch (ParserConfigurationException e) {
499             fail("Unexpected ParserConfigurationException" + e.getMessage());
500         }
501 
502         try {
503             for (int i = 0; i < features.length; i++) {
504                 dbf.setFeature(features[i], false);
505                 assertFalse(dbf.getFeature(features[i]));
506             }
507         } catch (ParserConfigurationException e) {
508             fail("Unexpected ParserConfigurationException" + e.getMessage());
509         }
510 
511         try {
512             for (int i = 0; i < features.length; i++) {
513                 dbf.setFeature(null, false);
514                 fail("NullPointerException expected");
515             }
516         } catch (NullPointerException e) {
517             // expected
518         } catch (ParserConfigurationException e) {
519             fail("Unexpected ParserConfigurationException" + e.getMessage());
520         }
521 
522         String[] badFeatures = { "bad1", "bad2", "" };
523         try {
524             for (int i = 0; i < badFeatures.length; i++) {
525                 dbf.setFeature(badFeatures[i], false);
526                 fail("ParserConfigurationException expected");
527             }
528         } catch (ParserConfigurationException e) {
529             // expected
530         }
531     }
532 
533     /**
534      * javax.xml.parsers.DocumentBuilderFactory#setIgnoringComments(boolean).
535      */
test_setIgnoringCommentsZ()536     public void test_setIgnoringCommentsZ() {
537         commentElements.clear();
538 
539         dbf.setIgnoringComments(true);
540         assertTrue(dbf.isIgnoringComments());
541 
542         try {
543             DocumentBuilder parser = dbf.newDocumentBuilder();
544 
545             Document document = parser.parse(getClass().getResourceAsStream(
546                     "/recipt.xml"));
547 
548             goThroughDocument((Node) document, "");
549             assertFalse(commentElements.contains("comment1"));
550             assertFalse(commentElements.contains("comment2"));
551 
552         } catch (IOException e) {
553             fail("Unexpected IOException " + e.getMessage());
554         } catch (ParserConfigurationException e) {
555             fail("Unexpected ParserConfigurationException " + e.getMessage());
556         } catch (SAXException e) {
557             fail("Unexpected SAXException " + e.getMessage());
558         }
559 
560         commentElements.clear();
561 
562         dbf.setIgnoringComments(false);
563         assertFalse(dbf.isIgnoringComments());
564 
565         try {
566             DocumentBuilder parser = dbf.newDocumentBuilder();
567 
568             Document document = parser.parse(getClass().getResourceAsStream(
569                     "/recipt.xml"));
570 
571             goThroughDocument((Node) document, "");
572             assertTrue(commentElements.contains("comment1"));
573             assertTrue(commentElements.contains("comment2"));
574 
575         } catch (IOException e) {
576             fail("Unexpected IOException " + e.getMessage());
577         } catch (ParserConfigurationException e) {
578             fail("Unexpected ParserConfigurationException " + e.getMessage());
579         } catch (SAXException e) {
580             fail("Unexpected SAXException " + e.getMessage());
581         }
582     }
583 
584     /**
585      * javax.xml.parsers.DocumentBuilderFactory#setIgnoringElementContentWhitespace(boolean).
586      */
test_setIgnoringElementContentWhitespaceZ()587     public void test_setIgnoringElementContentWhitespaceZ() {
588         dbf.setIgnoringElementContentWhitespace(true);
589         assertTrue(dbf.isIgnoringElementContentWhitespace());
590 
591         try {
592             DocumentBuilder parser = dbf.newDocumentBuilder();
593 
594             Document document = parser.parse(getClass().getResourceAsStream(
595                     "/recipt.xml"));
596 
597             assertNotNull(document);
598 
599         } catch (IOException e) {
600             fail("Unexpected IOException " + e.getMessage());
601         } catch (ParserConfigurationException e) {
602             fail("Unexpected ParserConfigurationException " + e.getMessage());
603         } catch (SAXException e) {
604             fail("Unexpected SAXException " + e.getMessage());
605         }
606 
607         dbf.setIgnoringElementContentWhitespace(false);
608         assertFalse(dbf.isIgnoringElementContentWhitespace());
609 
610         try {
611             DocumentBuilder parser = dbf.newDocumentBuilder();
612 
613             Document document = parser.parse(getClass().getResourceAsStream(
614                     "/recipt.xml"));
615 
616             assertNotNull(document);
617 
618         } catch (IOException e) {
619             fail("Unexpected IOException " + e.getMessage());
620         } catch (ParserConfigurationException e) {
621             fail("Unexpected ParserConfigurationException " + e.getMessage());
622         } catch (SAXException e) {
623             fail("Unexpected SAXException " + e.getMessage());
624         }
625     }
626 
627     /**
628      * javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean).
629      */
test_setNamespaceAwareZ()630     public void test_setNamespaceAwareZ() {
631         dbf.setNamespaceAware(true);
632         assertTrue(dbf.isNamespaceAware());
633 
634         try {
635             DocumentBuilder parser = dbf.newDocumentBuilder();
636 
637             Document document = parser.parse(getClass().getResourceAsStream(
638                     "/recipt.xml"));
639 
640             assertNotNull(document);
641 
642         } catch (IOException e) {
643             fail("Unexpected IOException " + e.getMessage());
644         } catch (ParserConfigurationException e) {
645             fail("Unexpected ParserConfigurationException " + e.getMessage());
646         } catch (SAXException e) {
647             fail("Unexpected SAXException " + e.getMessage());
648         }
649 
650         dbf.setNamespaceAware(false);
651         assertFalse(dbf.isNamespaceAware());
652 
653         try {
654             DocumentBuilder parser = dbf.newDocumentBuilder();
655 
656             Document document = parser.parse(getClass().getResourceAsStream(
657                     "/recipt.xml"));
658 
659             assertNotNull(document);
660 
661         } catch (IOException e) {
662             fail("Unexpected IOException " + e.getMessage());
663         } catch (ParserConfigurationException e) {
664             fail("Unexpected ParserConfigurationException " + e.getMessage());
665         } catch (SAXException e) {
666             fail("Unexpected SAXException " + e.getMessage());
667         }
668     }
669 
test_getSetAttribute()670     public void test_getSetAttribute() {
671         // Android SAX implementation doesn't support attributes, so
672         // we can only make sure the expected exception is thrown.
673         try {
674             dbf.setAttribute("foo", new Object());
675             fail("IllegalArgumentException expected");
676         } catch (IllegalArgumentException e) {
677             // Expected
678         }
679 
680         try {
681             dbf.getAttribute("foo");
682             fail("IllegalArgumentException expected");
683         } catch (IllegalArgumentException e) {
684             // Expected
685         }
686     }
687 
688     /**
689      * javax.xml.parsers.DocumentBuilderFactory#setSchema(javax.xml.validation.Schema).
690      */
691  /*   public void test_setSchemaLjavax_xml_validation_Schema() {
692         SchemaFactory sf =
693             SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
694         try {
695             Schema schema = sf.newSchema();
696             dbf.setSchema(schema);
697             assertNotNull(dbf.getSchema());
698         } catch (SAXException sax) {
699             fail("Unexpected exception " + sax.toString());
700         }
701     }
702 */
703     /**
704      * javax.xml.parsers.DocumentBuilderFactory#setValidating(boolean).
705      */
706 //    public void test_setValidatingZ() {
707 //        Exception parseException = null;
708 //        DocumentBuilder parser = null;
709 //        Document document = null;
710 //
711 //        ValidationErrorHandler errorHandler = new ValidationErrorHandler();
712 //
713 //        dbf.setValidating(false);
714 //        assertFalse(dbf.isValidating());
715 //
716 //        // case 1: Validation is not set. Correct xml-file
717 //        try {
718 //
719 //            parser = dbf.newDocumentBuilder();
720 //            parser.setErrorHandler(errorHandler);
721 //
722 //            document = parser.parse(getClass().getResourceAsStream(
723 //                    "/recipt.xml"));
724 //
725 //            parseException = errorHandler.getFirstException();
726 //
727 //            assertNotNull(document);
728 //
729 //            document = parser.parse(getClass().getResourceAsStream(
730 //                    "/reciptWrong.xml"));
731 //
732 //            parseException = errorHandler.getFirstException();
733 //
734 //            assertNotNull(document);
735 //
736 //        } catch (Exception ex) {
737 //            parseException = ex;
738 //        }
739 //        parser.setErrorHandler(null);
740 //
741 //        if (parseException != null) {
742 //            fail("Unexpected exception " + parseException.getMessage());
743 //        }
744 //
745 //        // case 2: Validation is not set. Wrong xml-file
746 //        try {
747 //
748 //            parser = dbf.newDocumentBuilder();
749 //            parser.setErrorHandler(errorHandler);
750 //
751 //            document = parser.parse(getClass().getResourceAsStream(
752 //                    "/reciptWrong.xml"));
753 //            parseException = errorHandler.getFirstException();
754 //
755 //            assertNotNull(document);
756 //
757 //        } catch (Exception ex) {
758 //            parseException = ex;
759 //        }
760 //        parser.setErrorHandler(null);
761 //
762 //        if (parseException != null) {
763 //            fail("Unexpected exception " + parseException.getMessage());
764 //        }
765 //
766 //        // case 3: Validation is set. Correct xml-file
767 //        dbf.setValidating(true);
768 //        assertTrue(dbf.isValidating());
769 //
770 //        try {
771 //
772 //            parser = dbf.newDocumentBuilder();
773 //            parser.setErrorHandler(errorHandler);
774 //
775 //            document = parser.parse(getClass().getResourceAsStream(
776 //                    "/recipt.xml"));
777 //            parseException = errorHandler.getFirstException();
778 //
779 //            assertNotNull(document);
780 //
781 //        } catch (Exception ex) {
782 //            parseException = ex;
783 //        }
784 //        parser.setErrorHandler(null);
785 //
786 //        if (parseException != null) {
787 //            fail("Unexpected exception " + parseException.getMessage());
788 //        }
789 //
790 //        // case 4: Validation is set. Wrong xml-file
791 //        try {
792 //
793 //            parser = dbf.newDocumentBuilder();
794 //            parser.setErrorHandler(errorHandler);
795 //
796 //            document = parser.parse(getClass().getResourceAsStream(
797 //                    "/reciptWrong.xml"));
798 //            parseException = errorHandler.getFirstException();
799 //
800 //            assertNotNull(document);
801 //
802 //        } catch (Exception ex) {
803 //            parseException = ex;
804 //        }
805 //        parser.setErrorHandler(null);
806 //
807 //        if (parseException == null) {
808 //            fail("Unexpected exception " + parseException.getMessage());
809 //        } else {
810 //            assertTrue(parseException
811 //                    .getMessage()
812 //                    .contains(
813 //                            "The content of element type \"collection\" must match \"(description,recipe+)\""));
814 //        }
815 //
816 //    }
817 
818     /**
819      * javax.xml.parsers.DocumentBuilderFactory#setXIncludeAware().
820      */
821 //    public void test_setXIncludeAware() {
822 //        dbf.setXIncludeAware(true);
823 //        assertTrue(dbf.isXIncludeAware());
824 //
825 //        try {
826 //            DocumentBuilder parser = dbf.newDocumentBuilder();
827 //
828 //            Document document = parser.parse(getClass().getResourceAsStream(
829 //                    "/recipt.xml"));
830 //
831 //            assertNotNull(document);
832 //
833 //        } catch (IOException e) {
834 //            fail("Unexpected IOException " + e.getMessage());
835 //        } catch (ParserConfigurationException e) {
836 //            fail("Unexpected ParserConfigurationException " + e.getMessage());
837 //        } catch (SAXException e) {
838 //            fail("Unexpected SAXException " + e.getMessage());
839 //        }
840 //
841 //        dbf.setXIncludeAware(false);
842 //        assertFalse(dbf.isXIncludeAware());
843 //
844 //        try {
845 //            DocumentBuilder parser = dbf.newDocumentBuilder();
846 //
847 //            Document document = parser.parse(getClass().getResourceAsStream(
848 //                    "/recipt.xml"));
849 //
850 //            assertNotNull(document);
851 //
852 //        } catch (IOException e) {
853 //            fail("Unexpected IOException " + e.getMessage());
854 //        } catch (ParserConfigurationException e) {
855 //            fail("Unexpected ParserConfigurationException " + e.getMessage());
856 //        } catch (SAXException e) {
857 //            fail("Unexpected SAXException " + e.getMessage());
858 //        }
859 //    }
860 
goThroughDocument(Node node, String indent)861     private void goThroughDocument(Node node, String indent) {
862         String value = node.getNodeValue();
863 
864         if (value != null) {
865             value = value.replaceAll(" ", "");
866             value = value.replaceAll("\n", "");
867         }
868 
869         switch (node.getNodeType()) {
870         case Node.CDATA_SECTION_NODE:
871             cdataElements.add(value);
872             // System.out.println(indent + "CDATA_SECTION_NODE " + value);
873             break;
874         case Node.COMMENT_NODE:
875             commentElements.add(value);
876             // System.out.println(indent + "COMMENT_NODE " + value);
877             break;
878         case Node.DOCUMENT_FRAGMENT_NODE:
879             // System.out.println(indent + "DOCUMENT_FRAGMENT_NODE " + value);
880             break;
881         case Node.DOCUMENT_NODE:
882             // System.out.println(indent + "DOCUMENT_NODE " + value);
883             break;
884         case Node.DOCUMENT_TYPE_NODE:
885             // System.out.println(indent + "DOCUMENT_TYPE_NODE " + value);
886             break;
887         case Node.ELEMENT_NODE:
888             // System.out.println(indent + "ELEMENT_NODE " + value);
889             break;
890         case Node.ENTITY_NODE:
891             // System.out.println(indent + "ENTITY_NODE " + value);
892             break;
893         case Node.ENTITY_REFERENCE_NODE:
894             // System.out.println(indent + "ENTITY_REFERENCE_NODE " + value);
895             break;
896         case Node.NOTATION_NODE:
897             // System.out.println(indent + "NOTATION_NODE " + value);
898             break;
899         case Node.PROCESSING_INSTRUCTION_NODE:
900             // System.out.println(indent + "PROCESSING_INSTRUCTION_NODE " +
901             // value);
902             break;
903         case Node.TEXT_NODE:
904             textElements.add(value);
905             // System.out.println(indent + "TEXT_NODE " + value);
906             break;
907         default:
908             // System.out.println(indent + "Unknown node " + value);
909             break;
910         }
911         NodeList list = node.getChildNodes();
912         for (int i = 0; i < list.getLength(); i++)
913             goThroughDocument(list.item(i), indent + "   ");
914     }
915 
916     private class ValidationErrorHandler implements ErrorHandler {
917         private SAXException parseException;
918 
919         private int errorCount;
920 
921         private int warningCount;
922 
ValidationErrorHandler()923         public ValidationErrorHandler() {
924             parseException = null;
925             errorCount = 0;
926             warningCount = 0;
927         }
928 
error(SAXParseException ex)929         public void error(SAXParseException ex) {
930             errorCount++;
931             if (parseException == null) {
932                 parseException = ex;
933             }
934         }
935 
warning(SAXParseException ex)936         public void warning(SAXParseException ex) {
937             warningCount++;
938         }
939 
fatalError(SAXParseException ex)940         public void fatalError(SAXParseException ex) {
941             if (parseException == null) {
942                 parseException = ex;
943             }
944         }
945 
getFirstException()946         public SAXException getFirstException() {
947             return parseException;
948         }
949     }
950 
951     private class DocumentBuilderFactoryChild extends DocumentBuilderFactory {
DocumentBuilderFactoryChild()952         public DocumentBuilderFactoryChild() {
953             super();
954         }
955 
getAttribute(String name)956         public Object getAttribute(String name) {
957             return null;
958         }
959 
getFeature(String name)960         public boolean getFeature(String name) {
961             return false;
962         }
963 
newDocumentBuilder()964         public DocumentBuilder newDocumentBuilder() {
965             return null;
966         }
967 
setAttribute(String name, Object value)968         public void setAttribute(String name, Object value) {
969         }
970 
setFeature(String name, boolean value)971         public void setFeature(String name, boolean value) {
972         }
973 
974     }
975 }
976