KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sitraka > XMLReportTest


1 /*
2  * Copyright 2001,2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.tools.ant.taskdefs.optional.sitraka;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import javax.xml.transform.OutputKeys JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 /**
37  * Ensure that reference classpath feature is working fine...
38  */

39 public class XMLReportTest extends TestCase {
40     /** helper for some File/URL connversions */
41     private static FileUtils fileUtils = FileUtils.newFileUtils();
42
43     public XMLReportTest(String JavaDoc s) {
44         super(s);
45     }
46
47     protected File JavaDoc getFile(String JavaDoc name) throws FileNotFoundException JavaDoc {
48         URL JavaDoc url = getClass().getResource(name);
49         if (url == null) {
50             throw new FileNotFoundException JavaDoc("Unable to load '" + name + "' from classpath");
51         }
52         return new File JavaDoc(fileUtils.fromURI(url.toString()));
53     }
54
55     public void testCreateDocument() throws Exception JavaDoc {
56         // this is a sample from running Ant include data for java.* only
57
File JavaDoc 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 JavaDoc doc = report.createDocument(p.list());
68
69         Node JavaDoc snapshot = doc.getDocumentElement();
70         assertEquals("snapshot", snapshot.getNodeName());
71
72         // there is only java.util
73
NodeList JavaDoc packages = doc.getElementsByTagName("package");
74         assertEquals(1, packages.getLength());
75         assertEquals("java.util", packages.item(0).getAttributes().getNamedItem("name").getNodeValue());
76
77         // there is only Vector
78
NodeList JavaDoc classes = doc.getElementsByTagName("class");
79         assertEquals(1, classes.getLength());
80         assertEquals("Vector", classes.item(0).getAttributes().getNamedItem("name").getNodeValue());
81
82         // there are 3 set* methods
83
// set(int, Object)
84
// setSize(int)
85
// setElementAt(Object, int)
86
NodeList JavaDoc methods = doc.getElementsByTagName("method");
87         assertEquals(3, methods.getLength());
88
89         //dump(doc, System.out);
90
}
91
92     /**
93      * might be useful to spit out the document
94      * it's a nightmare to navigate in a DOM structure in a debugger.
95      */

96     protected void dump(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc {
97         TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
98         Transformer JavaDoc transformer = tfactory.newTransformer();
99         transformer.setOutputProperty(OutputKeys.INDENT, "no");
100         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
101         transformer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(out));
102     }
103 }
104
Popular Tags