001: /* TaskControl.java */
002:
003: package org.quilt.frontend.ant;
004:
005: import java.io.File;
006: import java.io.OutputStream;
007: import java.util.Vector;
008:
009: import org.apache.tools.ant.BuildException;
010: import org.apache.tools.ant.Project;
011: import org.apache.tools.ant.Task;
012: import org.apache.tools.ant.taskdefs.ExecuteWatchdog;
013: import org.apache.tools.ant.taskdefs.LogOutputStream;
014: import org.apache.tools.ant.types.Commandline;
015: import org.apache.tools.ant.types.CommandlineJava;
016: import org.apache.tools.ant.types.Environment;
017: import org.apache.tools.ant.types.Path;
018:
019: import org.quilt.cl.QuiltClassLoader;
020: import org.quilt.framework.QuiltTest;
021: import org.quilt.reports.FmtSelector;
022: import org.quilt.runner.Runner;
023:
024: /////////////////////////////////////////////////////////////////////
025: // NEEDS A LOT OF ATTENTION
026: /////////////////////////////////////////////////////////////////////
027:
028: /**
029: * Run control parameters from the Ant build.xml file.
030: */
031: public class TaskControl {
032:
033: // set by constructor or created at construction ////////////////
034: /** Command line, clone of which is used to fork tests. */
035: private CommandlineJava commandline = new CommandlineJava();
036: /** Run-time environment for command line. */
037: private Environment env = new Environment();
038:
039: /** The running Task. */
040: private QuiltTask task;
041:
042: // values assigned by Ant using add/create/set //////////////////
043: private File dir = null;
044: private Vector formatters = new Vector();
045: private boolean includeAntRuntime = true;
046: private boolean mockExec = false;
047: private boolean newEnvironment = false;
048: private boolean showOutput = false;
049: private boolean summary = false; // printsummary
050: private Vector tests = new Vector();
051: private Long timeout = null;
052:
053: // ancillary variables //////////////////////////////////////////
054: private Path antRuntimeClasses = null;
055: private Runner runner = null;
056: private String summaryValue = "";
057:
058: // Quilt-specific, not available from Ant ///////////////////////
059: protected QuiltClassLoader loader = null;
060:
061: /////////////////////////////////////////////////////////////////
062: /** No-arg constructor for clone() */
063: private TaskControl() {
064: }
065:
066: /**
067: * Constructor.
068: * @param t The QuiltTask being executed.
069: */
070: public TaskControl(QuiltTask t) {
071: task = t;
072: }
073:
074: // //////////////////////////////////////////////////////////////
075: // ADD/CREATE/SET METHODS. Please keep in
076: // alphabetical order by the name of the variable being set (so
077: // addXYZ sorts after createABC).
078: // //////////////////////////////////////////////////////////////
079: public void setAntRuntimeClasses(Path p) {
080: antRuntimeClasses = p;
081: }
082:
083: public Path createClasspath() {
084: return commandline.createClasspath(task.getProject())
085: .createPath();
086: }
087:
088: public void setCommandline(CommandlineJava cmd) {
089: commandline = cmd;
090: }
091:
092: public void setDir(File dir) {
093: this .dir = dir;
094: }
095:
096: public void addEnv(Environment.Variable var) {
097: env.addVariable(var);
098: }
099:
100: public void addFormatter(FmtSelector fs) {
101: formatters.addElement(fs);
102: }
103:
104: public void setFormatters(Vector v) {
105: formatters = v;
106: }
107:
108: public void setIncludeAntRuntime(boolean b) {
109: includeAntRuntime = b;
110: }
111:
112: public void setJvm(String value) {
113: commandline.setVm(value);
114: }
115:
116: public Commandline.Argument createJvmarg() {
117: return commandline.createVmArgument();
118: }
119:
120: // not for use from Ant
121: public void setLoader(QuiltClassLoader qcl) {
122: loader = qcl;
123: }
124:
125: public void setMaxmemory(String max) {
126: commandline.setMaxmemory(max);
127: }
128:
129: public void setMockExec(boolean b) {
130: mockExec = b;
131: }
132:
133: public void setNewEnvironment(boolean newenv) {
134: newEnvironment = newenv;
135: }
136:
137: public void setPrintsummary(SummaryAttribute value) {
138: summaryValue = value.getValue();
139: summary = value.asBoolean();
140: }
141:
142: public void setRunner(Runner r) {
143: runner = r;
144: }
145:
146: public void setShowOutput(boolean b) {
147: showOutput = b;
148: }
149:
150: public void setSummary(boolean b) {
151: summary = b;
152: }
153:
154: public void setSummaryValue(String s) {
155: summaryValue = s;
156: }
157:
158: public void addSysproperty(Environment.Variable sysp) {
159: commandline.addSysproperty(sysp);
160: }
161:
162: public void addTest(QuiltTest t) {
163: tests.addElement(t);
164: }
165:
166: public void setTests(Vector v) {
167: tests = v;
168: }
169:
170: public void setTask(QuiltTask t) {
171: task = t;
172: }
173:
174: public void setTimeout(Long value) {
175: timeout = value;
176: }
177:
178: // GET METHODS //////////////////////////////////////////////////
179: public CommandlineJava getCommandline() {
180: return commandline;
181: }
182:
183: public File getDir() {
184: return dir;
185: }
186:
187: public Environment getEnv() {
188: return env;
189: }
190:
191: public Vector getFormatters() {
192: return formatters;
193: }
194:
195: public boolean getIncludeAntRuntime() {
196: return includeAntRuntime;
197: }
198:
199: public QuiltClassLoader getLoader() {
200: return loader;
201: }
202:
203: public boolean getMockExec() {
204: return mockExec;
205: }
206:
207: public boolean getNewEnvironment() {
208: return newEnvironment;
209: }
210:
211: public boolean getSummary() {
212: return summary;
213: }
214:
215: public Task getTask() {
216: return task;
217: }
218:
219: public Vector getTests() {
220: return tests;
221: }
222:
223: public Long getTimeout() {
224: return timeout;
225: }
226:
227: public Path getAntRuntimeClasses() {
228: return antRuntimeClasses;
229: }
230:
231: public Runner getRunner() {
232: return runner;
233: }
234:
235: public String getSummaryValue() {
236: return summaryValue;
237: }
238:
239: // I/O HANDLING /////////////////////////////////////////////////
240: void handleOutput(String line) {
241: if (runner != null) {
242: runner.handleOutput(line);
243: if (showOutput) {
244: task.handleTheOutput(line);
245: }
246: } else {
247: task.handleTheOutput(line);
248: }
249: }
250:
251: void handleFlush(String line) {
252: if (runner != null) {
253: runner.handleFlush(line);
254: if (showOutput) {
255: task.handleTheFlush(line);
256: }
257: } else {
258: task.handleTheFlush(line);
259: }
260: }
261:
262: void handleErrorOutput(String line) {
263: if (runner != null) {
264: runner.handleErrorOutput(line);
265: if (showOutput) {
266: task.handleTheErrorOutput(line);
267: }
268: } else {
269: task.handleTheErrorOutput(line);
270: }
271: }
272:
273: void handleErrorFlush(String line) {
274: if (runner != null) {
275: runner.handleErrorFlush(line);
276: if (showOutput) {
277: task.handleTheErrorFlush(line);
278: }
279: } else {
280: task.handleTheErrorFlush(line);
281: }
282: }
283:
284: OutputStream getDefaultOutput() {
285: return new LogOutputStream(task, Project.MSG_INFO);
286: }
287:
288: FmtSelector[] mergeSelectors(QuiltTest test) {
289: Vector fsVector = (Vector) getFormatters().clone();
290: test.addFormattersTo(fsVector);
291: FmtSelector[] fsArray = new FmtSelector[fsVector.size()];
292: fsVector.copyInto(fsArray);
293: return fsArray;
294: }
295:
296: File getOutput(FmtSelector fs, QuiltTest test) {
297: if (fs.getUseFile()) {
298: String filename = test.getOutfile() + fs.getExtension();
299: File destFile = new File(test.getTodir(), filename);
300: String absFilename = destFile.getAbsolutePath();
301: return task.getProject().resolveFile(absFilename);
302: }
303: return null;
304: }
305:
306: ExecuteWatchdog createWatchdog() throws BuildException {
307: if (timeout == null) {
308: return null;
309: }
310: return new ExecuteWatchdog(timeout.longValue());
311: }
312:
313: // EQUALS() /////////////////////////////////////////////////////
314: public boolean equals(TaskControl tc2) {
315: if (this == tc2)
316: return true;
317: else
318: return formatters.equals(tc2.getFormatters())
319: && tests.equals(tc2.getTests()) &&
320:
321: task == tc2.getTask()
322: && commandline == tc2.getCommandline()
323: && dir == tc2.getDir()
324: && includeAntRuntime == tc2.getIncludeAntRuntime()
325: && loader == tc2.getLoader()
326: && mockExec == tc2.getMockExec()
327: && newEnvironment == tc2.getNewEnvironment()
328: && summary == tc2.getSummary()
329: && timeout == tc2.getTimeout()
330: && antRuntimeClasses == tc2.getAntRuntimeClasses()
331: && runner == tc2.getRunner()
332: && summaryValue.equals(tc2.getSummaryValue());
333: }
334:
335: // CLONE() //////////////////////////////////////////////////////
336: public Object clone() {
337: TaskControl tc2 = new TaskControl();
338: tc2.setTask(task);
339: tc2.setCommandline(commandline);
340:
341: // the Vectors
342: tc2.setFormatters((Vector) formatters.clone());
343: tc2.setTests((Vector) tests.clone());
344:
345: tc2.setDir(dir);
346: tc2.setIncludeAntRuntime(includeAntRuntime);
347: tc2.setLoader(loader);
348: tc2.setMockExec(mockExec);
349: tc2.setNewEnvironment(newEnvironment);
350: tc2.setSummary(summary);
351: tc2.setTimeout(timeout);
352: tc2.setAntRuntimeClasses(antRuntimeClasses);
353: tc2.setRunner(runner);
354: tc2.setSummaryValue(summaryValue);
355: return tc2;
356: }
357:
358: // TOSTRING () //////////////////////////////////////////////////
359: public String toString() {
360: // the Vectors
361: String f = "";
362: for (int i = 0; i < formatters.size(); i++)
363: f += formatters.elementAt(i) + " ";
364:
365: String s = " task: " + task.getTaskName()
366: + "\n antRuntimeClasses: " + antRuntimeClasses
367: + "\n commandline: "
368: + "\n as string: "
369: + commandline
370: + "\n classname: "
371: + commandline.getClassname()
372: + "\n classpath: "
373: + commandline.getClasspath()
374: + "\n dir: "
375: + dir
376: + "\n includeAntRuntime: "
377: + includeAntRuntime
378: // loader ignored
379: + "\n mockExec: " + mockExec
380: + "\n newEnvironment: " + newEnvironment
381: + "\n runner: " + runner
382: + "\n summary: " + summary
383: + "\n summaryValue: " + summaryValue
384: + "\n timeout: " + timeout
385: + "\n formatters: " + f
386: + "\n ---------------------------------------------\n";
387: return s;
388: }
389: }
|