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:
042: package org.netbeans.tax.dom;
043:
044: import java.io.ByteArrayOutputStream;
045: import java.io.File;
046: import java.io.InputStreamReader;
047: import java.io.OutputStream;
048: import java.lang.reflect.Constructor;
049: import java.net.URL;
050: import junit.framework.*;
051: import org.netbeans.junit.*;
052: import org.netbeans.modules.xml.tax.parser.ParserLoader;
053: import org.w3c.dom.*;
054: import org.xml.sax.*;
055: import org.netbeans.tax.*;
056: import org.netbeans.tax.io.*;
057: import org.openide.xml.XMLUtil;
058: import org.apache.tools.ant.AntClassLoader;
059:
060: /**
061: * Sorry, complicated test setup. It requires OpenIDE and Ant libs.
062: * There is also hardcoded path to isolated jars.
063: *
064: * @author Petr Kuzel
065: */
066: public class WrapperTest extends NbTestCase {
067:
068: /**
069: * Can anybody resolve it dynamically at runtime?
070: */
071: private static String AUTOLOAD_PREFIX = System
072: .getProperty("netbeans.test.xml.autoloadLibrariesPath",
073: "/jungle/prj/netbeans/40/nb_all/xml/netbeans/modules/autoload/ext/");
074:
075: public WrapperTest(java.lang.String testName) {
076: super (testName);
077: }
078:
079: public static void main(java.lang.String[] args) {
080: junit.textui.TestRunner.run(suite());
081: }
082:
083: public static Test suite() {
084: TestSuite suite = new NbTestSuite(WrapperTest.class);
085:
086: return suite;
087: }
088:
089: /** Test of wrap method, of class org.netbeans.tax.dom.Wrapper. */
090: public void testWrap() throws Exception {
091: System.out.println("testWrap");
092:
093: URL prototype = getClass().getResource("data/Prototype.xml");
094: InputSource in = new InputSource(prototype.toExternalForm());
095: ByteArrayOutputStream out;
096:
097: // prepare golden serialized XML
098:
099: Document goldenDocument = XMLUtil.parse(in, false, false, null,
100: null);
101: out = new ByteArrayOutputStream(2000);
102: XMLUtil.write(goldenDocument, out, "UTF-8");
103: String golden = out.toString("UTF-8");
104:
105: // prepare the same for tax
106: in = new InputSource(prototype.toExternalForm());
107: in.setCharacterStream(new InputStreamReader(prototype
108: .openStream(), "UTF8"));
109: //ClassLoader loader = ParserLoader.getInstance();
110: AntClassLoader loader = new AntClassLoader(getClass()
111: .getClassLoader(), true);
112: String path = AUTOLOAD_PREFIX + "xerces2.jar";
113: if (new File(path).exists() == false) {
114: throw new IllegalStateException("Xerces file not found! "
115: + path);
116: }
117: ;
118: loader.addPathElement(path);
119:
120: String taxpath = AUTOLOAD_PREFIX + "tax.jar";
121: if (new File(taxpath).exists() == false) {
122: throw new IllegalStateException("TAX file not found! "
123: + taxpath);
124: }
125: ;
126: loader.addPathElement(taxpath);
127:
128: loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder");
129: loader
130: .forceLoadClass("org.netbeans.tax.io.XNIBuilder$XMLBuilder");
131: loader
132: .forceLoadClass("org.netbeans.tax.io.XNIBuilder$DTDStopException");
133: loader
134: .forceLoadClass("org.netbeans.tax.io.XNIBuilder$DTDEntityResolver");
135: loader.forceLoadClass("org.netbeans.tax.io.XNIBuilder$1");
136: loader.addLoaderPackageRoot("org.apache.xerces");
137:
138: Class builderClass = loader
139: .loadClass("org.netbeans.tax.io.XNIBuilder");
140: Constructor builderConstructor = builderClass
141: .getConstructor(new Class[] { Class.class,
142: InputSource.class, EntityResolver.class,
143: TreeStreamBuilderErrorHandler.class });
144: TreeBuilder builder = (TreeBuilder) builderConstructor
145: .newInstance(new Object[] { TreeDocument.class, in,
146: null, new TreeStreamBuilderErrorHandler() {
147: public void message(int type,
148: SAXParseException e) {
149: e.printStackTrace();
150: }
151: } });
152: TreeDocumentRoot taxDocument = builder.buildDocument();
153: Document wrappedDocument = Wrapper.wrap(taxDocument);
154:
155: out = new ByteArrayOutputStream(2000);
156: XMLUtil.write(wrappedDocument, out, "UTF-8");
157: String serializedWrapped = out.toString("UTF-8");
158:
159: if (golden.equals(serializedWrapped) == false) {
160: System.out.println("Golden:\n" + golden);
161: System.out.println("====\nWrapped TAX:\n"
162: + serializedWrapped);
163: String serializedTax = Convertors.treeToString(taxDocument);
164: System.out
165: .println("====\nSerilized TAX:\n" + serializedTax);
166: System.out.println("====");
167: assertTrue("Serialized documents are different!", false);
168: }
169:
170: }
171:
172: }
|