1 19 package org.netbeans.spi.xml.cookies; 20 21 import java.io.*; 22 import java.net.*; 23 24 import junit.framework.*; 25 import org.netbeans.junit.*; 26 27 import org.xml.sax.*; 28 import javax.xml.parsers.*; 29 import javax.xml.transform.*; 30 import javax.xml.transform.sax.*; 31 import javax.xml.transform.stream.*; 32 33 import org.netbeans.api.xml.cookies.*; 34 import org.netbeans.spi.xml.cookies.*; 35 36 40 public class TransformableSupportTest extends NbTestCase { 41 42 public TransformableSupportTest(java.lang.String testName) { 43 super(testName); 44 } 45 46 public static void main(java.lang.String [] args) { 47 junit.textui.TestRunner.run(suite()); 48 } 49 50 public static Test suite() { 51 TestSuite suite = new NbTestSuite(TransformableSupportTest.class); 52 return suite; 53 } 54 55 56 public void testTransform () { 57 assertTrue ("Correct XML and correct XSLT must pass!", transform ("data/doc.xml", "data/doc2xhtml.xsl")); 58 assertTrue ("Incorrect XML and correct XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/doc2xhtml.xsl")); 59 assertTrue ("Correct XML and incorrect XSLT must not pass!", false==transform ("data/doc.xml", "data/InvalidDocument.xml")); 60 assertTrue ("Incrrect XML and incorrect XSLT must not pass!", false==transform ("data/InvalidDocument.xml", "data/InvalidDocument.xml")); 61 } 62 63 private boolean transform (String xml, String xslt) { 64 URL xmlURL = getClass().getResource(xml); 65 URL xsltURL = getClass().getResource(xslt); 66 Source xmlSource = new SAXSource (new InputSource (xmlURL.toExternalForm())); 67 Source xsltSource = new SAXSource (new InputSource (xsltURL.toExternalForm())); 68 Result outputResult = new StreamResult (new StringWriter()); 69 70 TransformableSupport support = new TransformableSupport (xmlSource); 71 Observer observer = new Observer(); boolean exceptionThrown = false; 73 try { 74 support.transform (xsltSource, outputResult, null); 75 } catch (TransformerException exc) { 76 System.err.println("!!! " + exc); 77 exceptionThrown = true; 78 } 79 80 System.out.println(xml + " & " + xslt + " => " + ( exceptionThrown ? "WRONG" : "OK" )); 81 return exceptionThrown==false; 82 } 83 84 85 89 private static class Observer implements CookieObserver { 90 private int receives; 91 private int warnings; 92 93 public void receive(CookieMessage msg) { 94 receives++; 95 if (msg.getLevel() >= msg.WARNING_LEVEL) { 96 warnings++; 97 } 98 } 99 public int getWarnings() { 100 return warnings; 101 } 102 103 } 104 105 } 106 | Popular Tags |