001: package net.firstpartners.nounit.ant;
002:
003: import net.firstpartners.nounit.ui.common.CommandPackage;
004: import net.firstpartners.nounit.ui.common.IProcessor;
005: import net.firstpartners.nounit.ui.common.Processor;
006: import net.firstpartners.nounit.ui.common.XMLProcessor;
007: import net.firstpartners.nounit.utility.NoUnitException;
008:
009: import org.apache.log4j.BasicConfigurator;
010: import org.apache.log4j.PropertyConfigurator;
011: import org.apache.log4j.Logger;
012:
013: import org.apache.tools.ant.BuildException;
014: import org.apache.tools.ant.Task;
015:
016: import java.io.File;
017:
018: /**
019: * NoUnitTask
020: *
021: * @see ant-run-nounit-report.xml for example of using task
022: * @since Mar 24, 2003
023: * @author aglover
024: * @version 0.7
025: *
026: */
027: public class NoUnitTask extends Task {
028:
029: private String startDirectory;
030: private String outputDirectory;
031: private String reportClass;
032: private String userMessage;
033: private String reportName;
034: private String outputFile;
035: private String xmlFileName;
036: private String log4jConfig;
037:
038: //handle to logger
039: static Logger log = Logger.getLogger(NoUnitTask.class);
040:
041: /**
042: * Constructor for NoUnitTask.
043: */
044: public NoUnitTask() {
045: super ();
046: }
047:
048: /**
049: * Called by ant as part of initialisation process
050: */
051: public void init() {
052:
053: //Call any other initialisation
054: super .init();
055:
056: // Make sure Log4j is initialised property
057: //Set up a simple configuration that logs on the console.
058: if (this .log4jConfig == null) {
059: //Use Default Configuator
060: BasicConfigurator.configure();
061: System.out.println("using Default log4j file"); //ironic , but ensures logging
062:
063: } else {
064: //Set configuration using supplied config file
065: PropertyConfigurator.configure(log4jConfig);
066: File logFile = new File(log4jConfig);
067: System.out.println("Using Log4j config file:"
068: + logFile.getAbsolutePath()); //ironic , but ensures logging
069: }
070:
071: }
072:
073: /**
074: * Method runs no unit against supplied attributes
075: * @throws BuildException
076: */
077: public void execute() throws BuildException {
078:
079: try {
080: //first validate
081: this .validateAttributes();
082:
083: //Get a command package - ie whatever way the parameters were passed in
084: // as a single easy to handle package
085: CommandPackage pckage = this .getCommandPackage();
086:
087: //Processor - what actually does the NoUnit work
088: IProcessor mainProcessor = this .getProcessor();
089:
090: //Do the NoUnit Stuff
091: CommandPackage results = mainProcessor.transform(pckage);
092:
093: //show the result if any
094: super .handleOutput(results
095: .getString(CommandPackage.USER_MESSAGE));
096:
097: } catch (NoUnitException ex) {
098:
099: //handle NoUnitExceptions
100: logExceptionTrace(ex);
101: throw new BuildException(
102: "NoUnitException occurred in Ant Task", ex);
103:
104: } catch (Exception ex) {
105:
106: //handle all other exceptions
107: logExceptionTrace(ex);
108: throw new BuildException("Exception occurred in Ant Task",
109: ex);
110: }
111: }
112:
113: /**
114: * Utility method to log exception stack traces
115: * @param exception exceptionToPrint
116: */
117: private void logExceptionTrace(Exception exceptionToPrint) {
118:
119: //Get the Stack Elements
120: StackTraceElement[] stackElements = exceptionToPrint
121: .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)
141: && this .isPropertyValid(this .reportName)) {
142: processr = new Processor();
143: this .log("using the default processor");
144: } else {
145: processr = new XMLProcessor();
146: this .log("using the XML Only processor");
147: }
148: return processr;
149: }
150:
151: /**
152: * Get a Command (which actually runs NoUnit) initialized
153: * with the various parameters
154: * @return CommandPackage
155: */
156: private CommandPackage getCommandPackage() {
157:
158: CommandPackage pckage = new CommandPackage();
159: //start dir and output dir are required
160: pckage.addValue(CommandPackage.START_DIR, this .startDirectory);
161: pckage
162: .addValue(CommandPackage.OUTPUT_DIR,
163: this .outputDirectory);
164:
165: if (this .isPropertyValid(this .reportClass)) {
166: pckage.addValue(CommandPackage.REPORT_CLASS,
167: this .reportClass);
168: }
169: if (this .isPropertyValid(this .reportName)) {
170: pckage.addValue("report_name", this .reportName);
171: }
172: if (this .isPropertyValid(this .outputFile)) {
173: pckage.addValue("output_file", this .outputFile);
174: }
175: if (this .isPropertyValid(this .xmlFileName)) {
176: pckage.addValue(CommandPackage.XML_OUTPUT_NAME,
177: this .xmlFileName);
178: }
179:
180: return pckage;
181: }
182:
183: /**
184: *
185: * @param property
186: * @return boolean
187: */
188: private boolean isPropertyValid(String property) {
189: return (property != null && !property.equals(""));
190: }
191:
192: /**
193: * quick and dirty data validation- there probably is a cleaner way
194: * to do this....
195: * @throws BuildException
196: */
197: public void validateAttributes() throws BuildException {
198: if ((this .startDirectory == null)
199: || (this .startDirectory.equals(""))
200: || (this .outputDirectory == null)
201: || (this .outputDirectory.equals(""))) {
202: throw new BuildException("Missing required attribute!");
203: }
204: }
205:
206: /**
207: * Sets the outputFile - accessor method
208: * @param outputFile The outputFile to set
209: */
210: public void setOutputFile(String outputFile) {
211: this .outputFile = outputFile;
212: }
213:
214: /**
215: * Sets the reportClass - accessor method
216: * @param reportClass The reportClass to set
217: */
218: public void setReportClass(String reportClass) {
219: this .reportClass = reportClass;
220: }
221:
222: /**
223: * Sets the reportName - accessor method
224: * @param reportName The reportName to set
225: */
226: public void setReportName(String reportName) {
227: this .reportName = reportName;
228: }
229:
230: /**
231: * Sets the startDirectory - accessor method
232: * @param startDirectory The startDirectory to set
233: */
234: public void setStartDirectory(String startDirectory) {
235: this .startDirectory = startDirectory;
236: this .log("set start directory to:" + startDirectory);
237: }
238:
239: /**
240: * Sets the userMessage- accessor method
241: * @param userMessage The userMessage to set
242: */
243: public void setUserMessage(String userMessage) {
244: this .userMessage = userMessage;
245: }
246:
247: /**
248: * Sets the outputDirectory- accessor method
249: * @param outputDirectory The outputDirectory to set
250: */
251: public void setOutputDirectory(String outputDirectory) {
252: this .outputDirectory = outputDirectory;
253: log.debug("Setting output directory to:" + outputDirectory);
254: }
255:
256: /**
257: * Sets the XML File Name (interim file) - accessor method
258: * @param string
259: */
260: public void setXmlFileName(String string) {
261: xmlFileName = string;
262: log.debug("Setting xml file name to:" + string);
263: }
264:
265: /**
266: * Sets the XML File Name (interim file) - accessor method
267: * @param string
268: */
269: public void setLog4jConfig(String string) {
270: log4jConfig = string;
271: log.debug("Setting log4j config to:" + string);
272: }
273:
274: }
|