1 19 package org.netbeans.modules.xsl.utils; 20 21 import java.io.*; 22 import java.net.*; 23 import java.beans.PropertyVetoException ; 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 43 public class TransformUtilTest extends NbTestCase { 44 45 public TransformUtilTest(java.lang.String testName) { 46 super(testName); 47 } 48 49 public static void main(java.lang.String [] 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 { 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 { 67 System.out.println("testGetURLName"); 68 69 FileObject docXML = getFileObject("doc.xml"); 70 String 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 { 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 { 90 System.out.println("testGetAssociatedStylesheet"); 91 92 URL docXMLURL = getClass().getResource("data/doc.xml"); 93 URL invalidDocXMLURL = getClass().getResource("data/InvalidDocument.xml"); 94 95 assertTrue ("doc.xml does NOT have associated stylesheet", null==TransformUtil.getAssociatedStylesheet(docXMLURL)); 97 98 102 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 { 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 { 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 xml, String 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(); 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 153 private FileObject getFileObject (String name) throws PropertyVetoException , IOException { 154 URL url = getClass().getResource("data/" + name); 155 158 159 FileObject[] fos = URLMapper.findFileObjects (url); 160 return fos[0]; 161 } 162 163 private DataObject getDataObject (String name) throws PropertyVetoException , IOException, DataObjectNotFoundException { 164 FileObject FO = getFileObject (name); 165 DataObject DO = DataObject.find (FO); 166 167 return DO; 168 } 169 170 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 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 } 211 } 212 | Popular Tags |