KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > utils > TransformUtilTest


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 package org.netbeans.modules.xsl.utils;
20
21 import java.io.*;
22 import java.net.*;
23 import java.beans.PropertyVetoException JavaDoc;
24
25 import junit.framework.*;
26 import org.netbeans.junit.*;
27
28 import org.xml.sax.*;
29 import javax.xml.parsers.*;
30 import javax.xml.transform.*;
31 import javax.xml.transform.sax.*;
32 import javax.xml.transform.stream.*;
33
34 import org.openide.filesystems.*;
35 import org.openide.loaders.*;
36
37 import org.netbeans.api.xml.cookies.*;
38
39 /**
40  *
41  * @author Libor Kramolis
42  */

43 public class TransformUtilTest extends NbTestCase {
44
45     public TransformUtilTest(java.lang.String JavaDoc testName) {
46         super(testName);
47     }
48     
49     public static void main(java.lang.String JavaDoc[] args) {
50         junit.textui.TestRunner.run(suite());
51     }
52     
53     public static Test suite() {
54         TestSuite suite = new NbTestSuite(TransformUtilTest.class);
55         return suite;
56     }
57     
58     
59     public void testIsXSLTransformation () throws Exception JavaDoc {
60         System.out.println("testIsXSLTransformation");
61   
62         assertTrue (".xml document must NOT pass!", false==TransformUtil.isXSLTransformation (getDataObject ("doc.xml")));
63         assertTrue (".xsl document MUST pass!", TransformUtil.isXSLTransformation (getDataObject ("doc2xhtml.xsl")));
64     }
65     
66     public void testGetURLName () throws Exception JavaDoc {
67         System.out.println("testGetURLName");
68         
69         FileObject docXML = getFileObject("doc.xml");
70         String JavaDoc docXMLName = TransformUtil.getURLName(docXML);
71         System.out.println(" docXML: " + docXML + " => '" + docXMLName + "'");
72         assertTrue ("URL should not contain nbsf://!",-1==docXMLName.indexOf("nbfs"));
73     }
74
75     public void testCreateURL () throws Exception JavaDoc {
76         System.out.println("testCreateURL");
77         
78         URL dataURL = getClass().getResource("data/");
79         URL docXMLURL = getClass().getResource("data/doc.xml");
80         URL docDTDURL = getClass().getResource("data/doc.dtd");
81         
82         assertTrue ("Both URLs must be same!", docXMLURL.sameFile (TransformUtil.createURL (dataURL, "doc.xml")));
83         assertTrue ("Both URLs must be same!", docXMLURL.sameFile (TransformUtil.createURL (docDTDURL, "doc.xml")));
84         assertTrue ("Both URLs must be same!", docXMLURL.sameFile (TransformUtil.createURL (docDTDURL, "../data/doc.xml")));
85         assertTrue ("Both URLs must NOT be same!", false==docXMLURL.sameFile (TransformUtil.createURL (docDTDURL, "data/doc.xml")));
86         assertTrue ("Both URLs must be same!", false==docXMLURL.sameFile (TransformUtil.createURL (docDTDURL, docDTDURL.toExternalForm())));
87     }
88
89     public void testGetAssociatedStylesheet () throws Exception JavaDoc {
90         System.out.println("testGetAssociatedStylesheet");
91         
92         URL docXMLURL = getClass().getResource("data/doc.xml");
93         URL invalidDocXMLURL = getClass().getResource("data/InvalidDocument.xml");
94         
95         //
96
assertTrue ("doc.xml does NOT have associated stylesheet", null==TransformUtil.getAssociatedStylesheet(docXMLURL));
97         
98         // FAILS probably because bug in org.apache.xml.utils.URI =>
99
// "org.apache.xml.utils.URI$MalformedURIException: Path contains invalid character: [" if it is nbfs: URL!
100
//assertTrue ("InvalidDocument.xml DOES have associated stylesheet", null!=TransformUtil.getAssociatedStylesheet(invalidDocXMLURL));
101

102         // Same URL converted to file: format.
103
FileObject FO = URLMapper.findFileObjects (invalidDocXMLURL)[0];
104         URL url = URLMapper.findURL(FO, URLMapper.EXTERNAL);
105         assertTrue ("InvalidDocument.xml DOES have associated stylesheet", null!=TransformUtil.getAssociatedStylesheet (url));
106     }
107     
108     public void testGuessOutputExt () throws Exception JavaDoc {
109         System.out.println("testGuessOutputExt");
110         
111         URL doc2htmlURL = getClass().getResource("data/doc2html.xsl");
112         URL doc2textURL = getClass().getResource("data/doc2text.xsl");
113         URL doc2xhtmlURL = getClass().getResource("data/doc2xhtml.xsl");
114         
115         assertTrue ("doc2html.xsl produces HTML output!", "html".equals (TransformUtil.guessOutputExt (getSource (doc2htmlURL))));
116         assertTrue ("doc2text.xsl produces TXT output!", "txt".equals (TransformUtil.guessOutputExt (getSource (doc2textURL))));
117         assertTrue ("doc2xhtml.xsl produces XML output!", "xml".equals (TransformUtil.guessOutputExt (getSource (doc2xhtmlURL))));
118     }
119     
120     public void testTransform () throws Exception JavaDoc {
121         System.out.println("testTransform");
122         
123         assertTrue ("Correct XML and correct XSLT must pass!", transform ("data/doc.xml", "data/doc2xhtml.xsl"));
124         assertTrue ("Incorrect XML and correct XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/doc2xhtml.xsl"));
125         assertTrue ("Correct XML and incorrect XSLT must not pass!", false==transform ("data/doc.xml", "data/InvalidDocument.xml"));
126         assertTrue ("Incrrect XML and incorrect XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/InvalidDocument.xml"));
127     }
128
129     private boolean transform (String JavaDoc xml, String JavaDoc xslt) {
130         URL xmlURL = getClass().getResource(xml);
131         URL xsltURL = getClass().getResource(xslt);
132         Source xmlSource = new SAXSource (new InputSource (xmlURL.toExternalForm()));
133         Source xsltSource = new SAXSource (new InputSource (xsltURL.toExternalForm()));
134         Result outputResult = new StreamResult (new StringWriter());
135         
136         Observer observer = new Observer(); // not yet used
137
boolean exceptionThrown = false;
138         try {
139             TransformUtil.transform (xmlSource, null, xsltSource, outputResult, observer);
140         } catch (TransformerException exc) {
141             System.err.println("!!! " + exc);
142             exceptionThrown = true;
143         }
144         
145         System.out.println(xml + " & " + xslt + " => " + ( exceptionThrown ? "WRONG" : "OK" ));
146         return exceptionThrown==false;
147     }
148         
149     //
150
// utils
151
//
152

153     private FileObject getFileObject (String JavaDoc name) throws PropertyVetoException JavaDoc, IOException {
154         URL url = getClass().getResource("data/" + name);
155 /* FileSystem FS = getDataFileSystem();
156         FileObject FO = FS.findResource (name);
157         return FO;*/

158         
159         FileObject[] fos = URLMapper.findFileObjects (url);
160         return fos[0];
161     }
162     
163     private DataObject getDataObject (String JavaDoc name) throws PropertyVetoException JavaDoc, IOException, DataObjectNotFoundException {
164         FileObject FO = getFileObject (name);
165         DataObject DO = DataObject.find (FO);
166         
167         return DO;
168     }
169    
170 /* private FileSystem getDataFileSystem () throws PropertyVetoException, IOException {
171         URL dataURL = getClass().getResource("data");
172         String dataSysName = dataURL.toExternalForm();
173         Repository repository = Repository.getDefault();
174         FileSystem dataFS = repository.findFileSystem (dataSysName);
175         
176         if ( dataFS == null ) {
177             LocalFileSystem locFS = new LocalFileSystem();
178             locFS.setRootDirectory (new File (dataSysName));
179             dataFS = locFS;
180         }
181         
182         return dataFS;
183     }*/

184     
185     private Source getSource (URL url) throws ParserConfigurationException, SAXException {
186         XMLReader reader = TransformUtil.newXMLReader();
187         reader.setEntityResolver (TransformUtil.getEntityResolver());
188         Source source = new SAXSource (reader, new InputSource (url.toExternalForm()));
189         return source;
190     }
191
192     //
193
// class Observer
194
//
195

196     private static class Observer implements CookieObserver {
197         private int receives;
198         private int warnings;
199         
200         public void receive(CookieMessage msg) {
201             receives++;
202             if (msg.getLevel() >= msg.WARNING_LEVEL) {
203                 warnings++;
204             }
205         }
206         public int getWarnings() {
207             return warnings;
208         }
209     } // class Observer
210

211 }
212
Popular Tags