1 19 package org.netbeans.tax.io; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.PrintStream ; 24 import java.lang.reflect.Constructor ; 25 import java.util.Iterator ; 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 ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXParseException ; 41 42 47 public class TreeBuilderTest extends XTest { 48 49 50 public TreeBuilderTest(String testName) { 51 super(testName); 52 } 53 54 public void testAttlistWithRefs() throws Exception { 55 parseDocument("data/attlist-with-refs.dtd", TreeDTD.class); 56 } 57 58 public void testAttlistWithRefsInstance() throws Exception { 59 parseDocument("data/attlist-with-refs-instance.xml", TreeDocument.class); 60 } 61 62 public void testLevele1() throws Exception { 63 parseDocument("data/dir/level1.dtd", TreeDTD.class); 64 } 65 66 public void testDistributed() throws Exception { 67 parseDocument("data/distributed.xml", TreeDocument.class); 68 } 69 70 public void xtestPeRefInIcludeSection() throws Exception { parseDocument("data/pe-ref-in-include-section.dtd", TreeDTD.class); 72 } 73 74 public void testIncludeIgnoreSection() throws Exception { 75 parseDocument("data/include-ignore-section.dtd", TreeDTD.class); 76 } 77 78 82 87 private void parseDocument(String name, Class clazz) { 88 ByteArrayOutputStream errOut = new ByteArrayOutputStream (); 89 final PrintStream errStream = new PrintStream (errOut); 90 91 try { 92 ClassLoader myl = getClass().getClassLoader(); 94 InputSource in = new InputSource (new InputStreamReader (this.getClass().getResourceAsStream(name))); 95 in.setSystemId(getClass().getResource(name).toExternalForm()); 96 97 TreeStreamBuilderErrorHandler errHandler = new TreeStreamBuilderErrorHandler() { 98 public void message(int type, SAXParseException ex) { 99 ex.printStackTrace(new PrintStream (errStream, true)); 100 } 101 }; 102 103 Class klass = myl.loadClass("org.netbeans.tax.io.XNIBuilder"); 104 Constructor cons = klass.getConstructor(new Class [] {Class .class, InputSource .class, EntityResolver .class, TreeStreamBuilderErrorHandler.class}); 105 TreeBuilder builder = (TreeBuilder) cons.newInstance(new Object [] {clazz, in, EntityCatalog.getDefault(), errHandler}); 106 TreeDocumentRoot document = (TreeDocumentRoot) builder.buildDocument(); 107 108 assertEquals("", errOut.toString()); 109 ref(docToString((TreeParentNode) document)); 110 } catch (Exception ex) { 111 ex.printStackTrace(errStream); 112 fail("\nParse document " + name +" failed.\n" + errOut.toString()); 113 } 114 compareReferenceFiles(); 115 } 116 117 120 private String docToString(TreeParentNode parent) throws Exception { 121 String str =""; 122 123 if (TreeDocument.class.isInstance(parent)) { 124 Iterator 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 listToString(TreeObjectList list) throws Exception { 139 Iterator it = list.iterator(); 140 String 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 160 public static void main(String args[]) { 161 TestRunner.run(TreeBuilderTest.class); 162 } 163 } 164 | Popular Tags |