001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.tax.io;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.InputStreamReader;
045: import java.io.PrintStream;
046: import java.lang.reflect.Constructor;
047: import java.util.Iterator;
048: import junit.textui.TestRunner;
049: import org.netbeans.modules.xml.tax.parser.ParserLoader;
050: import org.netbeans.tax.TreeDTD;
051: import org.netbeans.tax.TreeDocument;
052: import org.netbeans.tax.TreeDocumentRoot;
053: import org.netbeans.tax.TreeDocumentType;
054: import org.netbeans.tax.TreeNode;
055: import org.netbeans.tax.TreeObjectList;
056: import org.netbeans.tax.TreeParameterEntityReference;
057: import org.netbeans.tax.TreeParentNode;
058: import org.netbeans.tests.xml.XTest;
059: import org.openide.xml.EntityCatalog;
060: import org.xml.sax.EntityResolver;
061: import org.xml.sax.InputSource;
062: import org.xml.sax.SAXParseException;
063:
064: /**
065: *
066: * @author ms113234
067: *
068: */
069: public class TreeBuilderTest extends XTest {
070:
071: /** Creates new CoreSettingsTest */
072: public TreeBuilderTest(String testName) {
073: super (testName);
074: }
075:
076: public void testAttlistWithRefs() throws Exception {
077: parseDocument("data/attlist-with-refs.dtd", TreeDTD.class);
078: }
079:
080: public void testAttlistWithRefsInstance() throws Exception {
081: parseDocument("data/attlist-with-refs-instance.xml",
082: TreeDocument.class);
083: }
084:
085: public void testLevele1() throws Exception {
086: parseDocument("data/dir/level1.dtd", TreeDTD.class);
087: }
088:
089: public void testDistributed() throws Exception {
090: parseDocument("data/distributed.xml", TreeDocument.class);
091: }
092:
093: public void xtestPeRefInIcludeSection() throws Exception { // issue #18096
094: parseDocument("data/pe-ref-in-include-section.dtd",
095: TreeDTD.class);
096: }
097:
098: public void testIncludeIgnoreSection() throws Exception {
099: parseDocument("data/include-ignore-section.dtd", TreeDTD.class);
100: }
101:
102: // public void testTwoColonsInElementName() throws Exception { //issue #22197
103: // parseDocument("data/two-colons-in-element-name.xml", TreeDocument.class);
104: // }
105:
106: /**
107: * Parses XML or DTD document ant writes it into golden file.
108: * @param name document's name
109: * @param clazz document's class
110: */
111: private void parseDocument(String name, Class clazz) {
112: ByteArrayOutputStream errOut = new ByteArrayOutputStream();
113: final PrintStream errStream = new PrintStream(errOut);
114:
115: try {
116: // ClassLoader myl = ParserLoader.getInstance();
117: ClassLoader myl = getClass().getClassLoader();
118: InputSource in = new InputSource(new InputStreamReader(this
119: .getClass().getResourceAsStream(name)));
120: in.setSystemId(getClass().getResource(name)
121: .toExternalForm());
122:
123: TreeStreamBuilderErrorHandler errHandler = new TreeStreamBuilderErrorHandler() {
124: public void message(int type, SAXParseException ex) {
125: ex
126: .printStackTrace(new PrintStream(errStream,
127: true));
128: }
129: };
130:
131: Class klass = myl
132: .loadClass("org.netbeans.tax.io.XNIBuilder");
133: Constructor cons = klass.getConstructor(new Class[] {
134: Class.class, InputSource.class,
135: EntityResolver.class,
136: TreeStreamBuilderErrorHandler.class });
137: TreeBuilder builder = (TreeBuilder) cons
138: .newInstance(new Object[] { clazz, in,
139: EntityCatalog.getDefault(), errHandler });
140: TreeDocumentRoot document = (TreeDocumentRoot) builder
141: .buildDocument();
142:
143: assertEquals("", errOut.toString());
144: ref(docToString((TreeParentNode) document));
145: } catch (Exception ex) {
146: ex.printStackTrace(errStream);
147: fail("\nParse document " + name + " failed.\n"
148: + errOut.toString());
149: }
150: compareReferenceFiles();
151: }
152:
153: /**
154: * Converts document to string.
155: */
156: private String docToString(TreeParentNode parent) throws Exception {
157: String str = "";
158:
159: if (TreeDocument.class.isInstance(parent)) {
160: // insert external DTD
161: Iterator it = parent.getChildNodes(TreeDocumentType.class)
162: .iterator();
163: while (it.hasNext()) {
164: TreeDocumentType docType = (TreeDocumentType) it.next();
165:
166: str += listToString(docType.getExternalDTD());
167: str += "\n<!--- End of External DTD --->\n\n";
168: }
169: }
170: TestUtil tu = TestUtil.THIS;
171: str += TestUtil.THIS.nodeToString(parent);
172: return str;
173: }
174:
175: private String listToString(TreeObjectList list) throws Exception {
176: Iterator it = list.iterator();
177: String str = "";
178:
179: while (it.hasNext()) {
180: TreeNode node = (TreeNode) it.next();
181: str += TestUtil.THIS.nodeToString(node) + "\n";
182:
183: if (TreeParameterEntityReference.class.isInstance(node)) {
184: TreeParameterEntityReference ref = (TreeParameterEntityReference) node;
185:
186: str += "\n<!--- Parameter Entity Reference: "
187: + ref.getName() + " --->\n\n";
188: str += listToString(ref.getChildNodes());
189: }
190: }
191: return str;
192: }
193:
194: /**
195: * Performs this testsuite.
196: * @param args the command line arguments
197: */
198: public static void main(String args[]) {
199: TestRunner.run(TreeBuilderTest.class);
200: }
201: }
|