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.modules.xsl.utils;
042:
043: import java.io.*;
044: import java.net.*;
045: import java.beans.PropertyVetoException;
046:
047: import junit.framework.*;
048: import org.netbeans.junit.*;
049:
050: import org.xml.sax.*;
051: import javax.xml.parsers.*;
052: import javax.xml.transform.*;
053: import javax.xml.transform.sax.*;
054: import javax.xml.transform.stream.*;
055:
056: import org.openide.filesystems.*;
057: import org.openide.loaders.*;
058:
059: import org.netbeans.api.xml.cookies.*;
060:
061: /**
062: *
063: * @author Libor Kramolis
064: */
065: public class TransformUtilTest extends NbTestCase {
066:
067: public TransformUtilTest(java.lang.String testName) {
068: super (testName);
069: }
070:
071: public static void main(java.lang.String[] args) {
072: junit.textui.TestRunner.run(suite());
073: }
074:
075: public static Test suite() {
076: TestSuite suite = new NbTestSuite(TransformUtilTest.class);
077: return suite;
078: }
079:
080: public void testIsXSLTransformation() throws Exception {
081: System.out.println("testIsXSLTransformation");
082:
083: assertTrue(".xml document must NOT pass!",
084: false == TransformUtil
085: .isXSLTransformation(getDataObject("doc.xml")));
086: assertTrue(".xsl document MUST pass!", TransformUtil
087: .isXSLTransformation(getDataObject("doc2xhtml.xsl")));
088: }
089:
090: public void testGetURLName() throws Exception {
091: System.out.println("testGetURLName");
092:
093: FileObject docXML = getFileObject("doc.xml");
094: String docXMLName = TransformUtil.getURLName(docXML);
095: System.out.println(" docXML: " + docXML + " => '"
096: + docXMLName + "'");
097: assertTrue("URL should not contain nbsf://!", -1 == docXMLName
098: .indexOf("nbfs"));
099: }
100:
101: public void testCreateURL() throws Exception {
102: System.out.println("testCreateURL");
103:
104: URL dataURL = getClass().getResource("data/");
105: URL docXMLURL = getClass().getResource("data/doc.xml");
106: URL docDTDURL = getClass().getResource("data/doc.dtd");
107:
108: assertTrue("Both URLs must be same!", docXMLURL
109: .sameFile(TransformUtil.createURL(dataURL, "doc.xml")));
110: assertTrue("Both URLs must be same!",
111: docXMLURL.sameFile(TransformUtil.createURL(docDTDURL,
112: "doc.xml")));
113: assertTrue("Both URLs must be same!", docXMLURL
114: .sameFile(TransformUtil.createURL(docDTDURL,
115: "../data/doc.xml")));
116: assertTrue("Both URLs must NOT be same!", false == docXMLURL
117: .sameFile(TransformUtil.createURL(docDTDURL,
118: "data/doc.xml")));
119: assertTrue("Both URLs must be same!", false == docXMLURL
120: .sameFile(TransformUtil.createURL(docDTDURL, docDTDURL
121: .toExternalForm())));
122: }
123:
124: public void testGetAssociatedStylesheet() throws Exception {
125: System.out.println("testGetAssociatedStylesheet");
126:
127: URL docXMLURL = getClass().getResource("data/doc.xml");
128: URL invalidDocXMLURL = getClass().getResource(
129: "data/InvalidDocument.xml");
130:
131: //
132: assertTrue("doc.xml does NOT have associated stylesheet",
133: null == TransformUtil
134: .getAssociatedStylesheet(docXMLURL));
135:
136: // FAILS probably because bug in org.apache.xml.utils.URI =>
137: // "org.apache.xml.utils.URI$MalformedURIException: Path contains invalid character: [" if it is nbfs: URL!
138: //assertTrue ("InvalidDocument.xml DOES have associated stylesheet", null!=TransformUtil.getAssociatedStylesheet(invalidDocXMLURL));
139:
140: // Same URL converted to file: format.
141: FileObject FO = URLMapper.findFileObjects(invalidDocXMLURL)[0];
142: URL url = URLMapper.findURL(FO, URLMapper.EXTERNAL);
143: assertTrue(
144: "InvalidDocument.xml DOES have associated stylesheet",
145: null != TransformUtil.getAssociatedStylesheet(url));
146: }
147:
148: public void testGuessOutputExt() throws Exception {
149: System.out.println("testGuessOutputExt");
150:
151: URL doc2htmlURL = getClass().getResource("data/doc2html.xsl");
152: URL doc2textURL = getClass().getResource("data/doc2text.xsl");
153: URL doc2xhtmlURL = getClass().getResource("data/doc2xhtml.xsl");
154:
155: assertTrue("doc2html.xsl produces HTML output!", "html"
156: .equals(TransformUtil
157: .guessOutputExt(getSource(doc2htmlURL))));
158: assertTrue("doc2text.xsl produces TXT output!", "txt"
159: .equals(TransformUtil
160: .guessOutputExt(getSource(doc2textURL))));
161: assertTrue("doc2xhtml.xsl produces XML output!", "xml"
162: .equals(TransformUtil
163: .guessOutputExt(getSource(doc2xhtmlURL))));
164: }
165:
166: public void testTransform() throws Exception {
167: System.out.println("testTransform");
168:
169: assertTrue("Correct XML and correct XSLT must pass!",
170: transform("data/doc.xml", "data/doc2xhtml.xsl"));
171: assertTrue("Incorrect XML and correct XSLT must not pass!",
172: false == transform("data/InvalidDocument.xml",
173: "data/doc2xhtml.xsl"));
174: assertTrue("Correct XML and incorrect XSLT must not pass!",
175: false == transform("data/doc.xml",
176: "data/InvalidDocument.xml"));
177: assertTrue("Incrrect XML and incorrect XSLT must not pass!",
178: false == transform("data/InvalidDocument.xml",
179: "data/InvalidDocument.xml"));
180: }
181:
182: private boolean transform(String xml, String xslt) {
183: URL xmlURL = getClass().getResource(xml);
184: URL xsltURL = getClass().getResource(xslt);
185: Source xmlSource = new SAXSource(new InputSource(xmlURL
186: .toExternalForm()));
187: Source xsltSource = new SAXSource(new InputSource(xsltURL
188: .toExternalForm()));
189: Result outputResult = new StreamResult(new StringWriter());
190:
191: Observer observer = new Observer(); // not yet used
192: boolean exceptionThrown = false;
193: try {
194: TransformUtil.transform(xmlSource, null, xsltSource,
195: outputResult, observer);
196: } catch (TransformerException exc) {
197: System.err.println("!!! " + exc);
198: exceptionThrown = true;
199: }
200:
201: System.out.println(xml + " & " + xslt + " => "
202: + (exceptionThrown ? "WRONG" : "OK"));
203: return exceptionThrown == false;
204: }
205:
206: //
207: // utils
208: //
209:
210: private FileObject getFileObject(String name)
211: throws PropertyVetoException, IOException {
212: URL url = getClass().getResource("data/" + name);
213: /* FileSystem FS = getDataFileSystem();
214: FileObject FO = FS.findResource (name);
215: return FO;*/
216:
217: FileObject[] fos = URLMapper.findFileObjects(url);
218: return fos[0];
219: }
220:
221: private DataObject getDataObject(String name)
222: throws PropertyVetoException, IOException,
223: DataObjectNotFoundException {
224: FileObject FO = getFileObject(name);
225: DataObject DO = DataObject.find(FO);
226:
227: return DO;
228: }
229:
230: /* private FileSystem getDataFileSystem () throws PropertyVetoException, IOException {
231: URL dataURL = getClass().getResource("data");
232: String dataSysName = dataURL.toExternalForm();
233: Repository repository = Repository.getDefault();
234: FileSystem dataFS = repository.findFileSystem (dataSysName);
235:
236: if ( dataFS == null ) {
237: LocalFileSystem locFS = new LocalFileSystem();
238: locFS.setRootDirectory (new File (dataSysName));
239: dataFS = locFS;
240: }
241:
242: return dataFS;
243: }*/
244:
245: private Source getSource(URL url)
246: throws ParserConfigurationException, SAXException {
247: XMLReader reader = TransformUtil.newXMLReader();
248: reader.setEntityResolver(TransformUtil.getEntityResolver());
249: Source source = new SAXSource(reader, new InputSource(url
250: .toExternalForm()));
251: return source;
252: }
253:
254: //
255: // class Observer
256: //
257:
258: private static class Observer implements CookieObserver {
259: private int receives;
260: private int warnings;
261:
262: public void receive(CookieMessage msg) {
263: receives++;
264: if (msg.getLevel() >= msg.WARNING_LEVEL) {
265: warnings++;
266: }
267: }
268:
269: public int getWarnings() {
270: return warnings;
271: }
272: } // class Observer
273:
274: }
|