KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > xml > parsers > SAXEntityParserTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.xml.parsers;
20
21 import java.io.*;
22 import java.net.URL JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.junit.*;
25 import org.xml.sax.*;
26 import org.xml.sax.helpers.*;
27 import org.xml.sax.ext.*;
28
29 /**
30  * Tests SAXEntityParser as DTDParser.
31  * Tests wrapping logic for relative references.
32  *
33  * @author Petr Kuzel
34  */

35 public class SAXEntityParserTest extends NbTestCase {
36
37     public SAXEntityParserTest(java.lang.String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static void main(java.lang.String JavaDoc[] args) {
42         junit.textui.TestRunner.run(suite());
43     }
44     
45     public static Test suite() {
46         TestSuite suite = new NbTestSuite(SAXEntityParserTest.class);
47         
48         return suite;
49     }
50     
51     /** Test of parse method, of class org.netbeans.api.xml.parsers.SAXEntityParser. */
52     public void testParse() throws Exception JavaDoc {
53         System.out.println("testParse");
54         
55         // DTD parser test
56

57         InputSource input = new InputSource(new StringReader("<!ELEMENT x ANY>"));
58         input.setSystemId("StringReader");
59                 
60         XMLReader peer = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
61         TestDeclHandler dtdHandler = new TestDeclHandler();
62         peer.setProperty("http://xml.org/sax/properties/declaration-handler", dtdHandler);
63         SAXEntityParser parser = new SAXEntityParser(peer, false);
64         parser.parse(input);
65
66         // Add your test code below by replacing the default call to fail.
67
assertTrue("DTD entity parser did not detected 'x' decl!", dtdHandler.pass);
68
69         // Reentrance test
70

71         boolean exceptionThrown = false;
72         try {
73             parser.parse(new InputSource(new StringReader("")));
74         }
75         catch (IllegalStateException JavaDoc ex) {
76             exceptionThrown = true;
77         }
78         finally {
79             assertTrue("Parser may not be reused!", exceptionThrown);
80         }
81         
82         relativeReferenceTest();
83     }
84     
85     /**
86      * Wrapping used to broke relative references.
87      */

88     private void relativeReferenceTest() throws Exception JavaDoc {
89
90         final boolean pass[] = {false};
91
92         try {
93             URL JavaDoc url = getClass().getResource("data/RelativeTest.dtd");
94             System.out.println("URL:" + url);
95             InputSource input = new InputSource(url.toExternalForm());
96             XMLReader peer = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
97             peer.setDTDHandler(new DefaultHandler() {
98                 public void notationDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) {
99                     if ("notation".equals(name)) pass[0] = true;
100                 }
101             });
102             SAXEntityParser parser = new SAXEntityParser(peer, false);
103             parser.parse(input);
104         }
105         finally {
106             assertTrue("External entity not reached!", pass[0]);
107         }
108     }
109     
110     
111     class TestDeclHandler implements DeclHandler {
112         
113         boolean pass;
114         
115         public void attributeDecl(String JavaDoc str, String JavaDoc str1, String JavaDoc str2, String JavaDoc str3, String JavaDoc str4) throws org.xml.sax.SAXException JavaDoc {
116         }
117         
118         public void elementDecl(String JavaDoc str, String JavaDoc str1) throws org.xml.sax.SAXException JavaDoc {
119             if ("x".equals(str)) pass = true;
120         }
121         
122         public void externalEntityDecl(String JavaDoc str, String JavaDoc str1, String JavaDoc str2) throws org.xml.sax.SAXException JavaDoc {
123         }
124         
125         public void internalEntityDecl(String JavaDoc str, String JavaDoc str1) throws org.xml.sax.SAXException JavaDoc {
126         }
127         
128     }
129     
130 }
131
Popular Tags