KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > io > TreeBuilderTest


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.tax.io;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import junit.textui.TestRunner;
27 import org.netbeans.modules.xml.tax.parser.ParserLoader;
28 import org.netbeans.tax.TreeDTD;
29 import org.netbeans.tax.TreeDocument;
30 import org.netbeans.tax.TreeDocumentRoot;
31 import org.netbeans.tax.TreeDocumentType;
32 import org.netbeans.tax.TreeNode;
33 import org.netbeans.tax.TreeObjectList;
34 import org.netbeans.tax.TreeParameterEntityReference;
35 import org.netbeans.tax.TreeParentNode;
36 import org.netbeans.tests.xml.XTest;
37 import org.openide.xml.EntityCatalog;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXParseException JavaDoc;
41
42 /**
43  *
44  * @author ms113234
45  *
46  */

47 public class TreeBuilderTest extends XTest {
48     
49     /** Creates new CoreSettingsTest */
50     public TreeBuilderTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     public void testAttlistWithRefs() throws Exception JavaDoc {
55         parseDocument("data/attlist-with-refs.dtd", TreeDTD.class);
56     }
57     
58     public void testAttlistWithRefsInstance() throws Exception JavaDoc {
59         parseDocument("data/attlist-with-refs-instance.xml", TreeDocument.class);
60     }
61     
62     public void testLevele1() throws Exception JavaDoc {
63         parseDocument("data/dir/level1.dtd", TreeDTD.class);
64     }
65     
66     public void testDistributed() throws Exception JavaDoc {
67         parseDocument("data/distributed.xml", TreeDocument.class);
68     }
69     
70     public void xtestPeRefInIcludeSection() throws Exception JavaDoc { // issue #18096
71
parseDocument("data/pe-ref-in-include-section.dtd", TreeDTD.class);
72     }
73     
74     public void testIncludeIgnoreSection() throws Exception JavaDoc {
75         parseDocument("data/include-ignore-section.dtd", TreeDTD.class);
76     }
77     
78 // public void testTwoColonsInElementName() throws Exception { //issue #22197
79
// parseDocument("data/two-colons-in-element-name.xml", TreeDocument.class);
80
// }
81

82     /**
83      * Parses XML or DTD document ant writes it into golden file.
84      * @param name document's name
85      * @param clazz document's class
86      */

87     private void parseDocument(String JavaDoc name, Class JavaDoc clazz) {
88         ByteArrayOutputStream JavaDoc errOut = new ByteArrayOutputStream JavaDoc();
89         final PrintStream JavaDoc errStream = new PrintStream JavaDoc(errOut);
90         
91         try {
92             // ClassLoader myl = ParserLoader.getInstance();
93
ClassLoader JavaDoc myl = getClass().getClassLoader();
94             InputSource JavaDoc in = new InputSource JavaDoc(new InputStreamReader JavaDoc(this.getClass().getResourceAsStream(name)));
95             in.setSystemId(getClass().getResource(name).toExternalForm());
96             
97             TreeStreamBuilderErrorHandler errHandler = new TreeStreamBuilderErrorHandler() {
98                 public void message(int type, SAXParseException JavaDoc ex) {
99                     ex.printStackTrace(new PrintStream JavaDoc(errStream, true));
100                 }
101             };
102             
103             Class JavaDoc klass = myl.loadClass("org.netbeans.tax.io.XNIBuilder");
104             Constructor JavaDoc cons = klass.getConstructor(new Class JavaDoc[] {Class JavaDoc.class, InputSource JavaDoc.class, EntityResolver JavaDoc.class, TreeStreamBuilderErrorHandler.class});
105             TreeBuilder builder = (TreeBuilder) cons.newInstance(new Object JavaDoc[] {clazz, in, EntityCatalog.getDefault(), errHandler});
106             TreeDocumentRoot document = (TreeDocumentRoot) builder.buildDocument();
107             
108             assertEquals("", errOut.toString());
109             ref(docToString((TreeParentNode) document));
110         } catch (Exception JavaDoc ex) {
111             ex.printStackTrace(errStream);
112             fail("\nParse document " + name +" failed.\n" + errOut.toString());
113         }
114         compareReferenceFiles();
115     }
116     
117     /**
118      * Converts document to string.
119      */

120     private String JavaDoc docToString(TreeParentNode parent) throws Exception JavaDoc {
121         String JavaDoc str ="";
122         
123         if (TreeDocument.class.isInstance(parent)) {
124             // insert external DTD
125
Iterator JavaDoc it = parent.getChildNodes(TreeDocumentType.class).iterator();
126             while (it.hasNext()) {
127                 TreeDocumentType docType = (TreeDocumentType) it.next();
128                 
129                 str += listToString(docType.getExternalDTD());
130                 str += "\n<!--- End of External DTD --->\n\n";
131             }
132         }
133         TestUtil tu = TestUtil.THIS;
134         str += TestUtil.THIS.nodeToString(parent);
135         return str;
136     }
137     
138     private String JavaDoc listToString(TreeObjectList list) throws Exception JavaDoc {
139         Iterator JavaDoc it = list.iterator();
140         String JavaDoc str = "";
141         
142         while (it.hasNext()) {
143             TreeNode node = (TreeNode) it.next();
144             str += TestUtil.THIS.nodeToString(node) + "\n";
145             
146             if (TreeParameterEntityReference.class.isInstance(node)) {
147                 TreeParameterEntityReference ref = (TreeParameterEntityReference) node;
148                 
149                 str += "\n<!--- Parameter Entity Reference: " + ref.getName() + " --->\n\n";
150                 str += listToString(ref.getChildNodes());
151             }
152         }
153         return str;
154     }
155     
156     /**
157      * Performs this testsuite.
158      * @param args the command line arguments
159      */

160     public static void main(String JavaDoc args[]) {
161         TestRunner.run(TreeBuilderTest.class);
162     }
163 }
164
Popular Tags