KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > ant > JBlanketReportTask


1 package csdl.jblanket.ant;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.apache.tools.ant.BuildException;
7
8 /**
9  * Implements the JBlanket Apache Ant task definition that creates reports from the raw intermediate
10  * results collected from the JUnit test cases. This Ant task definition calls the JBlanketReport
11  * class to create the HTML pages that reflect the coverage of a system. The format of the HTML
12  * pages are similar to those of JUnitReport in Ant. All files are stored in the directory
13  * specified by the jblanket.dir system property. If it is not set, the default directory is
14  * <user_home>/<user_account>/jblanket.
15  * <p>
16  * <b>Required</b> nested element for the report task:
17  * <ul>
18  * <p>
19  * None.
20  * </ul>
21  * <p>
22  * <b>Optional</b> nested element for the report task:
23  * <ul>
24  * <p>
25  * 'sysproperty' - describes additional system properties not set in the environment. If
26  * jblanket.dir is not set as an environment variable, include this nested element
27  * to set it.
28  * <i>For example</i>: see <a HREF="http://jakarta.apache.org/ant/manual/index.html">Ant</a>
29  * </ul>
30  * <p>
31  * <b>Required</b> attributes for the report task:
32  * <ul>
33  * <p>
34  * None.
35  * </ul>
36  * <p>
37  * <b>Optional</b> attributes for the report task:
38  * <ul>
39  * <p>
40  * 'enable' - describes if a report should be created. Valid values include "true", "on", "yes"
41  * to create a report or "false", "off", or "no" to not create a report.<br>
42  * <i>For example</i>: enable="true"
43  * <p>
44  * 'verbose' - describes if additional output should be sent to standard out. Valid values include
45  * "true", "on", "yes" for additional output or "false", "off", or "no" for no
46  * additional output.<br>
47  * <i>For example</i>: verbose="false"
48  * <p>
49  * 'reportformat' - format of the HTML report, either 'frames' or 'noframes'<br>
50  * <i>For example</i>: reportformat="frames"
51  * <p>
52  * 'toDir' - directory where all HTML files should be sent<br>
53  * <i>For example</i>: toDir="jblanket/html"
54  * <p>
55  * 'excludeonelinemethods' - describes if methods with one line of code were excluded from coverage.
56  * Values include "true", "on", "yes" when one-line methods were excluded
57  * or "false", "off", or "no" when one-line methods were included.<br>
58  * <i>For example</i>: excludeonelinemethods="false"
59  * <p>
60  * 'excludeconstructors' - describes if constructors were excluded from coverage. Values include
61  * "true", "on", "yes" when constructors were excluded or "false", "off",
62  * or "no" when constructors were included.<br>
63  * <i>For example</i>: excludeconstructors="false"
64  * <p>
65  * 'excludeindividualmethods' - describes if individual methods were excluded from coverage.
66  * Values include "true", "on", "yes" when individual methods were
67  * excluded or "false", "off", or "no" when individual methods were
68  * not excluded.<br>
69  * <i>For example</i>: excludeindividualmethods="false"
70  * <p>
71  * 'totalfile' - name of XML file containing all methods included in the coverage measurement<br>
72  * <i>For example</i>: totalfile="totalMethods.xml"
73  * <p>
74  * 'testedfile' - name of XML file to contain all tested methods<br>
75  * <i>For example</i>: testedfile="testedMethods.xml"
76  * <p>
77  * 'untestedfile' - name of XML file to contain all untested methods<br>
78  * <i>For example</i>: untestedfile="untestedMethods.xml"
79  * <p>
80  * 'onelinefile' - name of XML file containing all one-line methods<br>
81  * <i>For example</i>: onelinefile="oneLineMethods.xml"
82  * <p>
83  * 'constructorfile' - name of XML file containing all cosntructors<br>
84  * <i>For example</i>: constructorfile="constructorsMethods.xml"
85  * <p>
86  * 'excludedindividualfile' - name of XML file containing all individually excluded methods<br>
87  * <i>For example</i>: excludedindividualfile="excludedIndividualMethods.xml"
88  * </ul>
89  * <p>
90  * If any of the optional attributes are not specified, their default values specified in the
91  * examples are used. For example, to exclude methods with one line of source code from
92  * the coverage measurement, specifiy the 'excludeonelinemethods' attribute with a 'true' value.
93  * When not specified, one-line methods will be included in the coverage report.
94  * <p>
95  * Use this class to execute JBlanketReport with Ant. One example of the 'jblanketreport'
96  * Ant target is:
97  * <pre>
98  * &lt;taskdef name="jblanketreport" classname="csdl.jblanket.ant.JBlanketReportTask"/&gt;
99  * &lt;jblanketreport excludeonelinemethods="true"
100  * totalfile="myTotalMethods.xml"
101  * testedfile="myTestedMethods.xml"
102  * reportformat="frames"
103  * verbose="true"/&gt;
104  * </pre>
105  * From the example, one-line methods were excluded and constructors were included. No methods
106  * were individually excluded. The total methods are stored in 'myTotalMethods.xml', tested
107  * methods will be stored in 'myTestedMethods.xml'. Untested methods will be stored in its default
108  * file and excluded one-line methods are stored in its default file. The HTML report will use
109  * frames. Additional output will be sent to standard out.
110  * <p>
111  * Another more basic example is:
112  * <pre>
113  * &lt;jblanketreport verbose="true"/&gt;
114  * </pre>
115  * where all files are referred to by their default values and one-line methods and constructors
116  * are included in the coverage measurement. Additional output will not be sent to standard out.
117  * The HTML report format will use frames.
118  *
119  * @author Joy M. Agustin
120  * @version $Id: JBlanketReportTask.java,v 1.1 2004/11/07 00:32:33 timshadel Exp $id
121  */

122 public class JBlanketReportTask extends JBlanketTask {
123
124   /** Format of final HTML report */
125   private String JavaDoc reportFormat;
126   /** Output directory of final HTML report */
127   private String JavaDoc toDirString;
128   
129   /** Name of file for tested methods */
130   protected String JavaDoc testedFile;
131   /** Name of file for untested methods */
132   protected String JavaDoc untestedFile;
133
134   /**
135    * Constructs a new JBlanketReportTask object.
136    */

137   public JBlanketReportTask() {
138     super();
139     
140     this.reportFormat = null;
141     
142     this.testedFile = null;
143     this.untestedFile = null;
144   }
145
146   /**
147    * Sets the format of the final HTML report containing coverage results.
148    *
149    * @param format the report format, either <code>frames</code> or <code>noframes</code>.
150    */

151   public void setReportformat(String JavaDoc format) {
152     this.reportFormat = format;
153   }
154
155   /**
156    * Sets the output directory of the final HTML report containing coverage results.
157    *
158    * @param toDirString directory the report format, either <code>frames</code> or
159    * <code>noframes</code>.
160    */

161   public void setToDir(String JavaDoc toDirString) {
162     this.toDirString = toDirString;
163   }
164
165   /**
166    * Sets the name of the XML file for the tested methods.
167    *
168    * @param testedFile the test file name.
169    */

170   public void setTestfile(String JavaDoc testedFile) {
171     this.testedFile = testedFile;
172   }
173
174   /**
175    * Sets the name of the XML file for the untested methods.
176    *
177    * @param untestedFile the untested file name.
178    */

179   public void setUntestfile(String JavaDoc untestedFile) {
180     this.untestedFile = untestedFile;
181   }
182
183   /**
184    * Executes the report Ant taskdef.
185    * */

186   public void execute() {
187     
188     Date JavaDoc startTime = new Date JavaDoc();
189
190     // Return without creating report if jblanketreport is disabled.
191
if (!super.enable) {
192       
193       if (super.verbose) {
194         System.out.println("jblanketreport disabled; no reports created.");
195       }
196       
197       return;
198     }
199
200     // Format JBlanketReport arguments.
201
ArrayList JavaDoc args = new ArrayList JavaDoc();
202     
203     // add verbose
204
args.add("-verbose");
205     args.add(new Boolean JavaDoc(super.verbose));
206
207     // add format of report if user specified
208
if (this.reportFormat != null) {
209       args.add("-reportFormat");
210       args.add(this.reportFormat);
211     }
212
213     // add outpur dir of report if user specified
214
if (this.toDirString != null) {
215       args.add("-toDir");
216       args.add(this.toDirString);
217     }
218
219     // add oneLineFile if user specified
220
if (super.excludeOneLineMethods) {
221       args.add("-excludeOneLineMethods");
222       args.add(new Boolean JavaDoc(super.excludeOneLineMethods));
223       if (super.oneLineFile != null) {
224         args.add("-oneLineFile");
225         args.add(super.oneLineFile);
226       }
227     }
228
229     // add contructorFile if user specified
230
if (super.excludeConstructors) {
231       args.add("-excludeConstructors");
232       args.add(new Boolean JavaDoc(super.excludeConstructors));
233       if (super.constructorFile != null) {
234         args.add("-constructorFile");
235         args.add(super.constructorFile);
236       }
237     }
238
239     // add excludeIndividualFile if user specified
240
if (super.excludeIndividualMethods) {
241       args.add("-excludeIndividualMethods");
242       args.add(new Boolean JavaDoc(super.excludeIndividualMethods));
243       if (super.excludedIndividualFile != null) {
244         args.add("-excludeIndividualFile");
245         args.add(super.excludedIndividualFile);
246       }
247     }
248
249     // add totalFile if user specified
250
if (super.totalFile != null) {
251       args.add("-totalFile");
252       args.add(super.totalFile);
253     }
254
255     // add testedFile if user specified
256
if (this.testedFile != null) {
257       args.add("-testedFile");
258       args.add(this.testedFile);
259     }
260
261     // add untestedFile if user specified
262
if (this.untestedFile != null) {
263       args.add("-untestedFile");
264       args.add(this.untestedFile);
265     }
266     
267     // execute JBlanketReport.main on arguments.
268
// TODO write out Exceptions to a Log, and print an error message to the screen
269
try {
270       csdl.jblanket.report.JBlanketReport.main(args);
271     }
272     catch (Exception JavaDoc e) {
273       e.printStackTrace();
274       throw new BuildException("Error in JBlanket report.");
275     }
276     
277     Date JavaDoc endTime = new Date JavaDoc();
278     long elapsedTime = (endTime.getTime() - startTime.getTime()) / 1000;
279     System.out.println("JBlanket report task completed " + "(" + elapsedTime + " secs.)");
280   }
281 }
Popular Tags