001: package csdl.jblanket.ant;
002:
003: import java.util.ArrayList;
004: import java.util.Date;
005:
006: import org.apache.tools.ant.BuildException;
007:
008: /**
009: * Implements the JBlanket Apache Ant task definition that creates reports from the raw intermediate
010: * results collected from the JUnit test cases. This Ant task definition calls the JBlanketReport
011: * class to create the HTML pages that reflect the coverage of a system. The format of the HTML
012: * pages are similar to those of JUnitReport in Ant. All files are stored in the directory
013: * specified by the jblanket.dir system property. If it is not set, the default directory is
014: * <user_home>/<user_account>/jblanket.
015: * <p>
016: * <b>Required</b> nested element for the report task:
017: * <ul>
018: * <p>
019: * None.
020: * </ul>
021: * <p>
022: * <b>Optional</b> nested element for the report task:
023: * <ul>
024: * <p>
025: * 'sysproperty' - describes additional system properties not set in the environment. If
026: * jblanket.dir is not set as an environment variable, include this nested element
027: * to set it.
028: * <i>For example</i>: see <a href="http://jakarta.apache.org/ant/manual/index.html">Ant</a>
029: * </ul>
030: * <p>
031: * <b>Required</b> attributes for the report task:
032: * <ul>
033: * <p>
034: * None.
035: * </ul>
036: * <p>
037: * <b>Optional</b> attributes for the report task:
038: * <ul>
039: * <p>
040: * 'enable' - describes if a report should be created. Valid values include "true", "on", "yes"
041: * to create a report or "false", "off", or "no" to not create a report.<br>
042: * <i>For example</i>: enable="true"
043: * <p>
044: * 'verbose' - describes if additional output should be sent to standard out. Valid values include
045: * "true", "on", "yes" for additional output or "false", "off", or "no" for no
046: * additional output.<br>
047: * <i>For example</i>: verbose="false"
048: * <p>
049: * 'reportformat' - format of the HTML report, either 'frames' or 'noframes'<br>
050: * <i>For example</i>: reportformat="frames"
051: * <p>
052: * 'toDir' - directory where all HTML files should be sent<br>
053: * <i>For example</i>: toDir="jblanket/html"
054: * <p>
055: * 'excludeonelinemethods' - describes if methods with one line of code were excluded from coverage.
056: * Values include "true", "on", "yes" when one-line methods were excluded
057: * or "false", "off", or "no" when one-line methods were included.<br>
058: * <i>For example</i>: excludeonelinemethods="false"
059: * <p>
060: * 'excludeconstructors' - describes if constructors were excluded from coverage. Values include
061: * "true", "on", "yes" when constructors were excluded or "false", "off",
062: * or "no" when constructors were included.<br>
063: * <i>For example</i>: excludeconstructors="false"
064: * <p>
065: * 'excludeindividualmethods' - describes if individual methods were excluded from coverage.
066: * Values include "true", "on", "yes" when individual methods were
067: * excluded or "false", "off", or "no" when individual methods were
068: * not excluded.<br>
069: * <i>For example</i>: excludeindividualmethods="false"
070: * <p>
071: * 'totalfile' - name of XML file containing all methods included in the coverage measurement<br>
072: * <i>For example</i>: totalfile="totalMethods.xml"
073: * <p>
074: * 'testedfile' - name of XML file to contain all tested methods<br>
075: * <i>For example</i>: testedfile="testedMethods.xml"
076: * <p>
077: * 'untestedfile' - name of XML file to contain all untested methods<br>
078: * <i>For example</i>: untestedfile="untestedMethods.xml"
079: * <p>
080: * 'onelinefile' - name of XML file containing all one-line methods<br>
081: * <i>For example</i>: onelinefile="oneLineMethods.xml"
082: * <p>
083: * 'constructorfile' - name of XML file containing all cosntructors<br>
084: * <i>For example</i>: constructorfile="constructorsMethods.xml"
085: * <p>
086: * 'excludedindividualfile' - name of XML file containing all individually excluded methods<br>
087: * <i>For example</i>: excludedindividualfile="excludedIndividualMethods.xml"
088: * </ul>
089: * <p>
090: * If any of the optional attributes are not specified, their default values specified in the
091: * examples are used. For example, to exclude methods with one line of source code from
092: * the coverage measurement, specifiy the 'excludeonelinemethods' attribute with a 'true' value.
093: * When not specified, one-line methods will be included in the coverage report.
094: * <p>
095: * Use this class to execute JBlanketReport with Ant. One example of the 'jblanketreport'
096: * Ant target is:
097: * <pre>
098: * <taskdef name="jblanketreport" classname="csdl.jblanket.ant.JBlanketReportTask"/>
099: * <jblanketreport excludeonelinemethods="true"
100: * totalfile="myTotalMethods.xml"
101: * testedfile="myTestedMethods.xml"
102: * reportformat="frames"
103: * verbose="true"/>
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: * <jblanketreport verbose="true"/>
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 reportFormat;
126: /** Output directory of final HTML report */
127: private String toDirString;
128:
129: /** Name of file for tested methods */
130: protected String testedFile;
131: /** Name of file for untested methods */
132: protected String 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 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 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 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 untestedFile) {
180: this .untestedFile = untestedFile;
181: }
182:
183: /**
184: * Executes the report Ant taskdef.
185: * */
186: public void execute() {
187:
188: Date startTime = new Date();
189:
190: // Return without creating report if jblanketreport is disabled.
191: if (!super .enable) {
192:
193: if (super .verbose) {
194: System.out
195: .println("jblanketreport disabled; no reports created.");
196: }
197:
198: return;
199: }
200:
201: // Format JBlanketReport arguments.
202: ArrayList args = new ArrayList();
203:
204: // add verbose
205: args.add("-verbose");
206: args.add(new Boolean(super .verbose));
207:
208: // add format of report if user specified
209: if (this .reportFormat != null) {
210: args.add("-reportFormat");
211: args.add(this .reportFormat);
212: }
213:
214: // add outpur dir of report if user specified
215: if (this .toDirString != null) {
216: args.add("-toDir");
217: args.add(this .toDirString);
218: }
219:
220: // add oneLineFile if user specified
221: if (super .excludeOneLineMethods) {
222: args.add("-excludeOneLineMethods");
223: args.add(new Boolean(super .excludeOneLineMethods));
224: if (super .oneLineFile != null) {
225: args.add("-oneLineFile");
226: args.add(super .oneLineFile);
227: }
228: }
229:
230: // add contructorFile if user specified
231: if (super .excludeConstructors) {
232: args.add("-excludeConstructors");
233: args.add(new Boolean(super .excludeConstructors));
234: if (super .constructorFile != null) {
235: args.add("-constructorFile");
236: args.add(super .constructorFile);
237: }
238: }
239:
240: // add excludeIndividualFile if user specified
241: if (super .excludeIndividualMethods) {
242: args.add("-excludeIndividualMethods");
243: args.add(new Boolean(super .excludeIndividualMethods));
244: if (super .excludedIndividualFile != null) {
245: args.add("-excludeIndividualFile");
246: args.add(super .excludedIndividualFile);
247: }
248: }
249:
250: // add totalFile if user specified
251: if (super .totalFile != null) {
252: args.add("-totalFile");
253: args.add(super .totalFile);
254: }
255:
256: // add testedFile if user specified
257: if (this .testedFile != null) {
258: args.add("-testedFile");
259: args.add(this .testedFile);
260: }
261:
262: // add untestedFile if user specified
263: if (this .untestedFile != null) {
264: args.add("-untestedFile");
265: args.add(this .untestedFile);
266: }
267:
268: // execute JBlanketReport.main on arguments.
269: // TODO write out Exceptions to a Log, and print an error message to the screen
270: try {
271: csdl.jblanket.report.JBlanketReport.main(args);
272: } catch (Exception e) {
273: e.printStackTrace();
274: throw new BuildException("Error in JBlanket report.");
275: }
276:
277: Date endTime = new Date();
278: long elapsedTime = (endTime.getTime() - startTime.getTime()) / 1000;
279: System.out.println("JBlanket report task completed " + "("
280: + elapsedTime + " secs.)");
281: }
282: }
|