KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > dom > WrapperTest


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
20 package org.netbeans.tax.dom;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.net.URL JavaDoc;
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 /**
39  * Sorry, complicated test setup. It requires OpenIDE and Ant libs.
40  * There is also hardcoded path to isolated jars.
41  *
42  * @author Petr Kuzel
43  */

44 public class WrapperTest extends NbTestCase {
45
46     /**
47      * Can anybody resolve it dynamically at runtime?
48      */

49     private static String JavaDoc 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 JavaDoc testName) {
55         super(testName);
56     }
57     
58     public static void main(java.lang.String JavaDoc[] 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     /** Test of wrap method, of class org.netbeans.tax.dom.Wrapper. */
69     public void testWrap() throws Exception JavaDoc {
70         System.out.println("testWrap");
71
72         URL JavaDoc prototype = getClass().getResource("data/Prototype.xml");
73         InputSource in = new InputSource(prototype.toExternalForm());
74         ByteArrayOutputStream JavaDoc out;
75         
76         // prepare golden serialized XML
77

78         Document goldenDocument = XMLUtil.parse(in, false, false, null, null);
79         out = new ByteArrayOutputStream JavaDoc(2000);
80         XMLUtil.write(goldenDocument, out, "UTF-8");
81         String JavaDoc golden = out.toString("UTF-8");
82         
83         // prepare the same for tax
84
in = new InputSource(prototype.toExternalForm());
85         in.setCharacterStream(new InputStreamReader JavaDoc(prototype.openStream(), "UTF8"));
86         //ClassLoader loader = ParserLoader.getInstance();
87
AntClassLoader loader = new AntClassLoader(getClass().getClassLoader(), true);
88         String JavaDoc path = AUTOLOAD_PREFIX + "xerces2.jar";
89         if (new File JavaDoc(path).exists() == false) {
90             throw new IllegalStateException JavaDoc("Xerces file not found! " + path);
91         };
92         loader.addPathElement(path);
93         
94         String JavaDoc taxpath = AUTOLOAD_PREFIX + "tax.jar";
95         if (new File JavaDoc(taxpath).exists() == false) {
96             throw new IllegalStateException JavaDoc("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 JavaDoc builderClass = loader.loadClass("org.netbeans.tax.io.XNIBuilder");
109         Constructor JavaDoc builderConstructor = builderClass.getConstructor(new Class JavaDoc[] {
110             Class JavaDoc.class,
111             InputSource.class,
112             EntityResolver.class,
113             TreeStreamBuilderErrorHandler.class
114         });
115         TreeBuilder builder = (TreeBuilder) builderConstructor.newInstance(new Object JavaDoc[] {
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 JavaDoc(2000);
129         XMLUtil.write(wrappedDocument, out, "UTF-8");
130         String JavaDoc 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 JavaDoc 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