KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > ant > NoUnitTask


1 package net.firstpartners.nounit.ant;
2
3 import net.firstpartners.nounit.ui.common.CommandPackage;
4 import net.firstpartners.nounit.ui.common.IProcessor;
5 import net.firstpartners.nounit.ui.common.Processor;
6 import net.firstpartners.nounit.ui.common.XMLProcessor;
7 import net.firstpartners.nounit.utility.NoUnitException;
8
9 import org.apache.log4j.BasicConfigurator;
10 import org.apache.log4j.PropertyConfigurator;
11 import org.apache.log4j.Logger;
12
13 import org.apache.tools.ant.BuildException;
14 import org.apache.tools.ant.Task;
15
16 import java.io.File JavaDoc;
17
18 /**
19  * NoUnitTask
20  *
21  * @see ant-run-nounit-report.xml for example of using task
22  * @since Mar 24, 2003
23  * @author aglover
24  * @version 0.7
25  *
26  */

27 public class NoUnitTask extends Task {
28     
29     private String JavaDoc startDirectory;
30     private String JavaDoc outputDirectory;
31     private String JavaDoc reportClass;
32     private String JavaDoc userMessage;
33     private String JavaDoc reportName;
34     private String JavaDoc outputFile;
35     private String JavaDoc xmlFileName;
36     private String JavaDoc log4jConfig;
37     
38     //handle to logger
39
static Logger log = Logger.getLogger(NoUnitTask.class);
40     
41     /**
42      * Constructor for NoUnitTask.
43      */

44     public NoUnitTask() {
45         super();
46     }
47     
48     /**
49      * Called by ant as part of initialisation process
50      */

51     public void init() {
52         
53         //Call any other initialisation
54
super.init();
55
56         // Make sure Log4j is initialised property
57
//Set up a simple configuration that logs on the console.
58
if(this.log4jConfig==null){
59               //Use Default Configuator
60
BasicConfigurator.configure();
61               System.out.println("using Default log4j file"); //ironic , but ensures logging
62

63           } else {
64               //Set configuration using supplied config file
65
PropertyConfigurator.configure(log4jConfig);
66               File JavaDoc logFile=new File JavaDoc(log4jConfig);
67               System.out.println("Using Log4j config file:"+logFile.getAbsolutePath()); //ironic , but ensures logging
68
}
69         
70         
71     }
72     
73     
74     /**
75      * Method runs no unit against supplied attributes
76      * @throws BuildException
77      */

78     public void execute() throws BuildException{
79       
80   
81       try{
82            //first validate
83
this.validateAttributes();
84
85            
86            //Get a command package - ie whatever way the parameters were passed in
87
// as a single easy to handle package
88
CommandPackage pckage = this.getCommandPackage();
89             
90            //Processor - what actually does the NoUnit work
91
IProcessor mainProcessor = this.getProcessor();
92            
93            //Do the NoUnit Stuff
94
CommandPackage results = mainProcessor.transform(pckage);
95            
96            //show the result if any
97
super.handleOutput(results.getString(CommandPackage.USER_MESSAGE));
98
99            
100           }catch(NoUnitException ex){
101             
102             //handle NoUnitExceptions
103
logExceptionTrace(ex);
104             throw new BuildException("NoUnitException occurred in Ant Task", ex);
105           
106           }catch(Exception JavaDoc ex){
107             
108             //handle all other exceptions
109
logExceptionTrace(ex);
110             throw new BuildException("Exception occurred in Ant Task", ex);
111         }
112     }
113     
114     /**
115      * Utility method to log exception stack traces
116      * @param exception exceptionToPrint
117      */

118     private void logExceptionTrace(Exception JavaDoc exceptionToPrint){
119         
120         //Get the Stack Elements
121
StackTraceElement JavaDoc[] stackElements = exceptionToPrint.getStackTrace();
122         
123         //Now send them for Ant to display
124
for (int a=0;a<stackElements.length;a++){
125             
126             super.handleErrorOutput(stackElements[a].toString());
127         }
128         
129     }
130     
131     /**
132      *
133      * @return IProcessor
134      */

135     private IProcessor getProcessor(){
136         IProcessor processr = null;
137         
138         //if report name and class are null, then we'll assume
139
//they want just the xml file processor
140
if( this.isPropertyValid(this.reportClass) && this.isPropertyValid(this.reportName) ){
141             processr = new Processor();
142             this.log("using the default processor");
143         }else{
144             processr = new XMLProcessor();
145             this.log("using the XML Only processor");
146         }
147         return processr;
148     }
149     /**
150      * Get a Command (which actually runs NoUnit) initialized
151      * with the various parameters
152      * @return CommandPackage
153      */

154     private CommandPackage getCommandPackage(){
155         
156         CommandPackage pckage = new CommandPackage();
157         //start dir and output dir are required
158
pckage.addValue(CommandPackage.START_DIR, this.startDirectory);
159         pckage.addValue(CommandPackage.OUTPUT_DIR, this.outputDirectory);
160         
161         if(this.isPropertyValid(this.reportClass)){
162             pckage.addValue(CommandPackage.REPORT_CLASS, this.reportClass);
163         }
164         if(this.isPropertyValid(this.reportName)){
165             pckage.addValue("report_name", this.reportName);
166         }
167         if(this.isPropertyValid(this.outputFile)){
168             pckage.addValue("output_file", this.outputFile);
169         }
170         if(this.isPropertyValid(this.xmlFileName)){
171             pckage.addValue(CommandPackage.XML_OUTPUT_NAME, this.xmlFileName);
172         }
173                            
174         return pckage;
175     }
176     /**
177      *
178      * @param property
179      * @return boolean
180      */

181     private boolean isPropertyValid(String JavaDoc property){
182         return (property != null && !property.equals(""));
183     }
184     
185     /**
186      * quick and dirty data validation- there probably is a cleaner way
187      * to do this....
188      * @throws BuildException
189      */

190     public void validateAttributes() throws BuildException{
191         if((this.startDirectory == null) || (this.startDirectory.equals("")) ||
192            (this.outputDirectory == null) || (this.outputDirectory.equals(""))) {
193                 throw new BuildException("Missing required attribute!");
194          }
195     }
196     /**
197      * Sets the outputFile - accessor method
198      * @param outputFile The outputFile to set
199      */

200     public void setOutputFile(String JavaDoc outputFile) {
201         this.outputFile = outputFile;
202     }
203
204     /**
205      * Sets the reportClass - accessor method
206      * @param reportClass The reportClass to set
207      */

208     public void setReportClass(String JavaDoc reportClass) {
209         this.reportClass = reportClass;
210     }
211
212     /**
213      * Sets the reportName - accessor method
214      * @param reportName The reportName to set
215      */

216     public void setReportName(String JavaDoc reportName) {
217         this.reportName = reportName;
218     }
219
220     /**
221      * Sets the startDirectory - accessor method
222      * @param startDirectory The startDirectory to set
223      */

224     public void setStartDirectory(String JavaDoc startDirectory) {
225         this.startDirectory = startDirectory;
226         this.log("set start directory to:"+startDirectory);
227     }
228
229     /**
230      * Sets the userMessage- accessor method
231      * @param userMessage The userMessage to set
232      */

233     public void setUserMessage(String JavaDoc userMessage) {
234         this.userMessage = userMessage;
235     }
236
237     /**
238      * Sets the outputDirectory- accessor method
239      * @param outputDirectory The outputDirectory to set
240      */

241     public void setOutputDirectory(String JavaDoc outputDirectory) {
242         this.outputDirectory = outputDirectory;
243         log.debug("Setting output directory to:"+outputDirectory);
244     }
245
246     /**
247      * Sets the XML File Name (interim file) - accessor method
248      * @param string
249      */

250     public void setXmlFileName(String JavaDoc string) {
251         xmlFileName = string;
252         log.debug("Setting xml file name to:"+string);
253     }
254     
255     /**
256      * Sets the XML File Name (interim file) - accessor method
257      * @param string
258      */

259     public void setLog4jConfig(String JavaDoc string) {
260         log4jConfig = string;
261         log.debug("Setting log4j config to:"+string);
262     }
263
264 }
265
Popular Tags