1 package org.apache.tools.ant.taskdefs.optional; 2 3 19 20 import junit.framework.TestCase; 21 import org.apache.tools.ant.taskdefs.XSLTLiaison; 22 import org.w3c.dom.Document ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import java.io.File ; 27 import java.io.FileNotFoundException ; 28 import java.net.URL ; 29 30 36 public abstract class AbstractXSLTLiaisonTest extends TestCase { 37 38 protected XSLTLiaison liaison; 39 40 protected AbstractXSLTLiaisonTest(String name){ 41 super(name); 42 } 43 44 protected void setUp() throws Exception { 45 liaison = createLiaison(); 46 } 47 48 protected abstract XSLTLiaison createLiaison() throws Exception ; 50 51 52 protected File getFile(String name) throws FileNotFoundException { 53 URL url = getClass().getResource(name); 54 if (url == null){ 55 throw new FileNotFoundException ("Unable to load '" + name + "' from classpath"); 56 } 57 return new File (url.getFile()); 58 } 59 60 61 public void testTransform() throws Exception { 62 File xsl = getFile("/taskdefs/optional/xsltliaison-in.xsl"); 63 liaison.setStylesheet(xsl); 64 liaison.addParam("param", "value"); 65 File in = getFile("/taskdefs/optional/xsltliaison-in.xml"); 66 File out = new File ("xsltliaison.tmp"); 67 try { 68 liaison.transform(in, out); 69 } finally { 70 out.delete(); 71 } 72 } 73 74 public void testEncoding() throws Exception { 75 File xsl = getFile("/taskdefs/optional/xsltliaison-encoding-in.xsl"); 76 liaison.setStylesheet(xsl); 77 File in = getFile("/taskdefs/optional/xsltliaison-encoding-in.xml"); 78 File out = new File ("xsltliaison-encoding.tmp"); 79 try { 80 liaison.transform(in, out); 81 Document doc = parseXML(out); 82 assertEquals("root",doc.getDocumentElement().getNodeName()); 83 assertEquals("message",doc.getDocumentElement().getFirstChild().getNodeName()); 84 assertEquals("\u00E9\u00E0\u00E8\u00EF\u00F9",doc.getDocumentElement().getFirstChild().getFirstChild().getNodeValue()); 85 } finally { 86 out.delete(); 87 } 88 } 89 90 public Document parseXML(File file) throws Exception { 91 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); 92 DocumentBuilder dbuilder = dbfactory.newDocumentBuilder(); 93 return dbuilder.parse(file); 94 } 95 } 96 | Popular Tags |