1 17 package org.apache.tools.ant.taskdefs.optional.sitraka; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.OutputStream ; 22 import java.net.URL ; 23 import javax.xml.transform.OutputKeys ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.stream.StreamResult ; 28 29 import junit.framework.TestCase; 30 import org.apache.tools.ant.types.Path; 31 import org.apache.tools.ant.util.FileUtils; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 36 39 public class XMLReportTest extends TestCase { 40 41 private static FileUtils fileUtils = FileUtils.newFileUtils(); 42 43 public XMLReportTest(String s) { 44 super(s); 45 } 46 47 protected File getFile(String name) throws FileNotFoundException { 48 URL url = getClass().getResource(name); 49 if (url == null) { 50 throw new FileNotFoundException ("Unable to load '" + name + "' from classpath"); 51 } 52 return new File (fileUtils.fromURI(url.toString())); 53 } 54 55 public void testCreateDocument() throws Exception { 56 File reportFile = getFile("/taskdefs/optional/sitraka/covreport-test.xml"); 58 XMLReport report = new XMLReport(reportFile); 59 ReportFilters filters = new ReportFilters(); 60 ReportFilters.Include incl = new ReportFilters.Include(); 61 incl.setClass("java.util.Vector"); 62 incl.setMethod("set*"); 63 filters.addInclude(incl); 64 report.setReportFilters(filters); 65 Path p = new Path(null); 66 p.addJavaRuntime(); 67 Document doc = report.createDocument(p.list()); 68 69 Node snapshot = doc.getDocumentElement(); 70 assertEquals("snapshot", snapshot.getNodeName()); 71 72 NodeList packages = doc.getElementsByTagName("package"); 74 assertEquals(1, packages.getLength()); 75 assertEquals("java.util", packages.item(0).getAttributes().getNamedItem("name").getNodeValue()); 76 77 NodeList classes = doc.getElementsByTagName("class"); 79 assertEquals(1, classes.getLength()); 80 assertEquals("Vector", classes.item(0).getAttributes().getNamedItem("name").getNodeValue()); 81 82 NodeList methods = doc.getElementsByTagName("method"); 87 assertEquals(3, methods.getLength()); 88 89 } 91 92 96 protected void dump(Document doc, OutputStream out) throws Exception { 97 TransformerFactory tfactory = TransformerFactory.newInstance(); 98 Transformer transformer = tfactory.newTransformer(); 99 transformer.setOutputProperty(OutputKeys.INDENT, "no"); 100 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 101 transformer.transform(new DOMSource (doc), new StreamResult (out)); 102 } 103 } 104 | Popular Tags |