1 19 20 package org.netbeans.tax.dom; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.lang.reflect.Constructor ; 27 import java.net.URL ; 28 import junit.framework.*; 29 import org.netbeans.junit.*; 30 import org.netbeans.modules.xml.tax.parser.ParserLoader; 31 import org.w3c.dom.*; 32 import org.xml.sax.*; 33 import org.netbeans.tax.*; 34 import org.netbeans.tax.io.*; 35 import org.openide.xml.XMLUtil; 36 import org.apache.tools.ant.AntClassLoader; 37 38 44 public class WrapperTest extends NbTestCase { 45 46 49 private static String AUTOLOAD_PREFIX = 50 System.getProperty("netbeans.test.xml.autoloadLibrariesPath", 51 "/jungle/prj/netbeans/40/nb_all/xml/netbeans/modules/autoload/ext/" 52 ); 53 54 public WrapperTest(java.lang.String testName) { 55 super(testName); 56 } 57 58 public static void main(java.lang.String [] args) { 59 junit.textui.TestRunner.run(suite()); 60 } 61 62 public static Test suite() { 63 TestSuite suite = new NbTestSuite(WrapperTest.class); 64 65 return suite; 66 } 67 68 69 public void testWrap() throws Exception { 70 System.out.println("testWrap"); 71 72 URL prototype = getClass().getResource("data/Prototype.xml"); 73 InputSource in = new InputSource(prototype.toExternalForm()); 74 ByteArrayOutputStream out; 75 76 78 Document goldenDocument = XMLUtil.parse(in, false, false, null, null); 79 out = new ByteArrayOutputStream (2000); 80 XMLUtil.write(goldenDocument, out, "UTF-8"); 81 String golden = out.toString("UTF-8"); 82 83 in = new InputSource(prototype.toExternalForm()); 85 in.setCharacterStream(new InputStreamReader (prototype.openStream(), "UTF8")); 86 AntClassLoader loader = new AntClassLoader(getClass().getClassLoader(), true); 88 String path = AUTOLOAD_PREFIX + "xerces2.jar"; 89 if (new File (path).exists() == false) { 90 throw new IllegalStateException ("Xerces file not found! " + path); 91 }; 92 loader.addPathElement(path); 93 94 String taxpath = AUTOLOAD_PREFIX + "tax.jar"; 95 if (new File (taxpath).exists() == false) { 96 throw new IllegalStateException ("TAX file not found! " + taxpath); 97 }; 98 loader.addPathElement(taxpath); 99 100 loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder"); 101 loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder$XMLBuilder"); 102 loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder$DTDStopException"); 103 loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder$DTDEntityResolver"); 104 loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder$1"); 105 loader.addLoaderPackageRoot("org.apache.xerces"); 106 107 108 Class builderClass = loader.loadClass("org.netbeans.tax.io.XNIBuilder"); 109 Constructor builderConstructor = builderClass.getConstructor(new Class [] { 110 Class .class, 111 InputSource.class, 112 EntityResolver.class, 113 TreeStreamBuilderErrorHandler.class 114 }); 115 TreeBuilder builder = (TreeBuilder) builderConstructor.newInstance(new Object [] { 116 TreeDocument.class, 117 in, 118 null, 119 new TreeStreamBuilderErrorHandler() { 120 public void message(int type, SAXParseException e) { 121 e.printStackTrace(); 122 } 123 } 124 }); 125 TreeDocumentRoot taxDocument = builder.buildDocument(); 126 Document wrappedDocument = Wrapper.wrap(taxDocument); 127 128 out = new ByteArrayOutputStream (2000); 129 XMLUtil.write(wrappedDocument, out, "UTF-8"); 130 String serializedWrapped = out.toString("UTF-8"); 131 132 if (golden.equals(serializedWrapped) == false) { 133 System.out.println("Golden:\n" + golden); 134 System.out.println("====\nWrapped TAX:\n" + serializedWrapped); 135 String serializedTax = Convertors.treeToString(taxDocument); 136 System.out.println("====\nSerilized TAX:\n" + serializedTax); 137 System.out.println("===="); 138 assertTrue("Serialized documents are different!", false); 139 } 140 141 } 142 143 144 } 145 | Popular Tags |