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;
|
---|
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 | /**
|
---|
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 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 | // this is a sample from running Ant include data for java.* only
|
---|
57 | 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 | // there is only java.util
|
---|
73 | NodeList 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 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 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 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 | }
|
---|