001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.*;
022: import org.apache.tools.ant.util.FileUtils;
023:
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.IOException;
027: import java.util.GregorianCalendar;
028:
029: import junit.framework.ComparisonFailure;
030:
031: /**
032: * Unit test for the <exec> task.
033: */
034: public class ExecTaskTest extends BuildFileTest {
035: private static final String BUILD_PATH = "src/etc/testcases/taskdefs/exec/";
036: private static final String BUILD_FILE = BUILD_PATH + "exec.xml";
037: private static final int TIME_TO_WAIT = 1;
038: /** maximum time allowed for the build in milliseconds */
039: private static final int MAX_BUILD_TIME = 4000;
040: private static final int SECURITY_MARGIN = 2000; // wait 2 second extras
041: // the test failed with 100 ms of margin on cvs.apache.org on August 1st, 2003
042:
043: /** Utilities used for file operations */
044: private static final FileUtils FILE_UTILS = FileUtils
045: .getFileUtils();
046:
047: private File logFile;
048: private MonitoredBuild myBuild = null;
049: volatile private boolean buildFinished = false;
050:
051: public ExecTaskTest(String name) {
052: super (name);
053: }
054:
055: public void setUp() {
056: configureProject(BUILD_FILE);
057: }
058:
059: public void tearDown() {
060: if (logFile != null && logFile.exists()) {
061: getProject().setProperty("logFile",
062: logFile.getAbsolutePath());
063: }
064: executeTarget("cleanup");
065: }
066:
067: public void testNoRedirect() {
068: executeTarget("no-redirect");
069: if (getProject().getProperty("test.can.run") == null) {
070: return;
071: }
072: assertEquals("unexpected log content", getProject()
073: .getProperty("ant.file")
074: + " out"
075: + getProject().getProperty("ant.file")
076: + " err", getLog());
077: }
078:
079: public void testRedirect1() throws IOException {
080: executeTarget("redirect1");
081: if (getProject().getProperty("test.can.run") == null) {
082: return;
083: }
084: String expectedOut = getProject().getProperty("ant.file")
085: + " out\n" + getProject().getProperty("ant.file")
086: + " err\n";
087:
088: assertEquals("unexpected output", expectedOut,
089: getFileString("redirect.out"));
090: }
091:
092: public void testRedirect2() throws IOException {
093: executeTarget("redirect2");
094: if (getProject().getProperty("test.can.run") == null) {
095: return;
096: }
097:
098: assertEquals("unexpected output", getProject().getProperty(
099: "ant.file")
100: + " out\n", getFileString("redirect.out"));
101: assertEquals("unexpected error output", getProject()
102: .getProperty("ant.file")
103: + " err\n", getFileString("redirect.err"));
104: }
105:
106: public void testRedirect3() throws IOException {
107: executeTarget("redirect3");
108: if (getProject().getProperty("test.can.run") == null) {
109: return;
110: }
111: assertEquals("unexpected log content", getProject()
112: .getProperty("ant.file")
113: + " err", getLog());
114: String expectedOut = getProject().getProperty("ant.file")
115: + " out\n";
116:
117: assertEquals("unexpected output", expectedOut,
118: getFileString("redirect.out"));
119: assertPropertyEquals("redirect.out", expectedOut.trim());
120: }
121:
122: public void testRedirect4() throws IOException {
123: executeTarget("redirect4");
124: if (getProject().getProperty("test.can.run") == null) {
125: return;
126: }
127: String expectedOut = getProject().getProperty("ant.file")
128: + " out\n";
129: String expectedErr = getProject().getProperty("ant.file")
130: + " err\n";
131:
132: assertEquals("unexpected output", expectedOut,
133: getFileString("redirect.out"));
134: assertPropertyEquals("redirect.out", expectedOut.trim());
135: assertEquals("unexpected error output", expectedErr,
136: getFileString("redirect.err"));
137: assertPropertyEquals("redirect.err", expectedErr.trim());
138: }
139:
140: public void testRedirect5() throws IOException {
141: testRedirect5or6("redirect5");
142: }
143:
144: public void testRedirect6() throws IOException {
145: testRedirect5or6("redirect6");
146: }
147:
148: public void testRedirect5or6(String target) throws IOException {
149: executeTarget(target);
150: if (getProject().getProperty("wc.can.run") == null) {
151: return;
152: }
153:
154: assertEquals("unexpected output", "3", getFileString(
155: "redirect.out").trim());
156: assertEquals("property redirect.out", "3", getProject()
157: .getProperty("redirect.out").trim());
158: assertNull("unexpected error output",
159: getFileString("redirect.err"));
160: assertPropertyEquals("redirect.err", "");
161: }
162:
163: public void testRedirect7() throws IOException {
164: executeTarget("redirect7");
165: if (getProject().getProperty("wc.can.run") == null) {
166: return;
167: }
168:
169: assertEquals("unexpected output", "3", getFileString(
170: "redirect.out").trim());
171: assertEquals("property redirect.out", "3", getProject()
172: .getProperty("redirect.out").trim());
173: assertNull("unexpected error output",
174: getFileString("redirect.err"));
175: }
176:
177: public void testRedirector1() {
178: executeTarget("init");
179: if (getProject().getProperty("test.can.run") == null) {
180: return;
181: }
182: expectBuildException("redirector1",
183: "cannot have > 1 nested <redirector>s");
184: }
185:
186: public void testRedirector2() throws IOException {
187: executeTarget("redirector2");
188: if (getProject().getProperty("test.can.run") == null) {
189: return;
190: }
191:
192: assertEquals("unexpected output", getProject().getProperty(
193: "ant.file")
194: + " out\n"
195: + getProject().getProperty("ant.file")
196: + " err\n", getFileString("redirector.out"));
197: }
198:
199: public void testRedirector3() throws IOException {
200: executeTarget("redirector3");
201: if (getProject().getProperty("test.can.run") == null) {
202: return;
203: }
204:
205: assertEquals("unexpected output", getProject().getProperty(
206: "ant.file")
207: + " out\n", getFileString("redirector.out"));
208: assertEquals("unexpected error output", getProject()
209: .getProperty("ant.file")
210: + " err\n", getFileString("redirector.err"));
211: }
212:
213: public void testRedirector4() throws IOException {
214: executeTarget("redirector4");
215: if (getProject().getProperty("test.can.run") == null) {
216: return;
217: }
218: String expectedOut = getProject().getProperty("ant.file")
219: + " out\n";
220:
221: assertEquals("unexpected log content", getProject()
222: .getProperty("ant.file")
223: + " err", getLog());
224: assertEquals("unexpected output", expectedOut,
225: getFileString("redirector.out"));
226: assertPropertyEquals("redirector.out", expectedOut.trim());
227: }
228:
229: public void testRedirector5() throws IOException {
230: testRedirector5or6("redirector5");
231: }
232:
233: public void testRedirector6() throws IOException {
234: testRedirector5or6("redirector6");
235: }
236:
237: private void testRedirector5or6(String target) throws IOException {
238: executeTarget(target);
239: if (getProject().getProperty("test.can.run") == null) {
240: return;
241: }
242: String expectedOut = getProject().getProperty("ant.file")
243: + " out\n";
244: String expectedErr = getProject().getProperty("ant.file")
245: + " err\n";
246:
247: assertEquals("unexpected output", expectedOut,
248: getFileString("redirector.out"));
249: assertPropertyEquals("redirector.out", expectedOut.trim());
250: assertEquals("unexpected error output", expectedErr,
251: getFileString("redirector.err"));
252: assertPropertyEquals("redirector.err", expectedErr.trim());
253: }
254:
255: public void testRedirector7() throws IOException {
256: executeTarget("redirector7");
257: if (getProject().getProperty("test.can.run") == null) {
258: return;
259: }
260: String expectedOut = getProject().getProperty("ant.file")
261: + " out\n";
262: String expectedErr = getProject().getProperty("ant.file")
263: + " ERROR!!!\n";
264:
265: assertEquals("unexpected output", expectedOut,
266: getFileString("redirector.out"));
267: assertPropertyEquals("redirector.out", expectedOut.trim());
268: assertEquals("unexpected error output", expectedErr,
269: getFileString("redirector.err"));
270: assertPropertyEquals("redirector.err", expectedErr.trim());
271: }
272:
273: public void testRedirector8() throws IOException {
274: executeTarget("redirector8");
275: if (getProject().getProperty("wc.can.run") == null) {
276: return;
277: }
278:
279: assertEquals("unexpected output", "3", getFileString(
280: "redirector.out").trim());
281: assertEquals("property redirector.out", "3", getProject()
282: .getProperty("redirector.out").trim());
283: assertNull("unexpected error output",
284: getFileString("redirector.err"));
285: assertPropertyEquals("redirector.err", "");
286: }
287:
288: public void testRedirector9() throws IOException {
289: testRedirector9Thru12("redirector9");
290: }
291:
292: public void testRedirector10() throws IOException {
293: testRedirector9Thru12("redirector10");
294: }
295:
296: public void testRedirector11() throws IOException {
297: testRedirector9Thru12("redirector11");
298: }
299:
300: public void testRedirector12() throws IOException {
301: testRedirector9Thru12("redirector12");
302: }
303:
304: private void testRedirector9Thru12(String target)
305: throws IOException {
306: executeTarget(target);
307: if (getProject().getProperty("cat.can.run") == null) {
308: return;
309: }
310: String expectedOut = "blah after blah";
311:
312: assertEquals("unexpected output", expectedOut, getFileString(
313: "redirector.out").trim());
314: assertPropertyEquals("redirector.out", expectedOut.trim());
315: assertNull("unexpected error output",
316: getFileString("redirector.err"));
317: assertPropertyEquals("redirector.err", "");
318: }
319:
320: public void testRedirector13() {
321: executeTarget("redirector13");
322: if (getProject().getProperty("test.can.run") == null) {
323: return;
324: }
325: String antfile = getProject().getProperty("ant.file");
326: try {
327: //no point in setting a message
328: assertEquals(
329: antfile + " OUTPUT???" + antfile + " ERROR!!!",
330: getLog());
331: } catch (ComparisonFailure cf) {
332: assertEquals("unexpected log content", antfile
333: + " ERROR!!!" + antfile + " OUTPUT???", getLog());
334: }
335: }
336:
337: public void testRedirector14() {
338: executeTarget("redirector14");
339: if (getProject().getProperty("cat.can.run") == null) {
340: return;
341: }
342: assertEquals("unexpected log output", "blah after blah",
343: getLog());
344: }
345:
346: public void testRedirector15() throws IOException {
347: executeTarget("redirector15");
348: if (getProject().getProperty("cat.can.run") == null) {
349: return;
350: }
351: assertTrue("error with transcoding", FILE_UTILS.contentEquals(
352: getProject().resolveFile("expected/utf-8"),
353: getProject().resolveFile("redirector.out")));
354: }
355:
356: public void testRedirector16() {
357: executeTarget("redirector16");
358: }
359:
360: public void testRedirector17() {
361: executeTarget("redirector17");
362: }
363:
364: public void testRedirector18() {
365: if (getProject().getProperty("test.can.run") == null) {
366: return;
367: }
368: expectLog("redirector18", getProject().getProperty("ant.file")
369: + " out" + getProject().getProperty("ant.file")
370: + " err");
371: }
372:
373: public void testspawn() {
374: project.executeTarget("init");
375: if (project.getProperty("test.can.run") == null) {
376: return;
377: }
378: myBuild = new MonitoredBuild(new File(System
379: .getProperty("root"), BUILD_FILE), "spawn");
380: logFile = FILE_UTILS.createTempFile("spawn", "log", project
381: .getBaseDir());
382: // this is guaranteed by FileUtils#createTempFile
383: assertTrue("log file not existing", !logFile.exists());
384: // make the spawned process run 4 seconds
385: myBuild.setTimeToWait(TIME_TO_WAIT);
386: myBuild.setLogFile(logFile.getAbsolutePath());
387: myBuild.addBuildListener(new MonitoredBuildListener());
388: myBuild.start();
389: GregorianCalendar startwait = new GregorianCalendar();
390: // this loop runs parallel to the build
391: while (!buildFinished) {
392: try {
393: Thread.sleep(10);
394: } catch (InterruptedException e) {
395: System.out.println("my sleep was interrupted");
396: }
397: GregorianCalendar now = new GregorianCalendar();
398: // security
399: if (now.getTime().getTime() - startwait.getTime().getTime() > MAX_BUILD_TIME) {
400: System.out.println("aborting wait, too long "
401: + (now.getTime().getTime() - startwait
402: .getTime().getTime()) + "milliseconds");
403: break;
404: }
405: }
406: // now wait until the spawned process is finished
407: try {
408: Thread.sleep((TIME_TO_WAIT) * 1000 + SECURITY_MARGIN);
409: } catch (InterruptedException e) {
410: System.out.println("my sleep was interrupted");
411: }
412: // time of the build in milli seconds
413: long elapsed = myBuild.getTimeElapsed();
414: assertTrue("we waited more than the process lasted",
415: TIME_TO_WAIT * 1000 + SECURITY_MARGIN > elapsed);
416: logFile = new File(logFile.getAbsolutePath());
417: assertTrue("log file found after spawn", logFile.exists());
418: }
419:
420: public void testExecUnknownOS() {
421: executeTarget("testExecUnknownOS");
422: }
423:
424: public void testExecOSFamily() {
425: executeTarget("testExecOSFamily");
426: }
427:
428: public void testExecInconsistentSettings() {
429: executeTarget("testExecInconsistentSettings");
430: }
431:
432: private static class MonitoredBuild implements Runnable {
433: private Thread worker;
434: private File myBuildFile = null;
435: private String target = null;
436: private Project project = null;
437: private int timeToWait = 0;
438: private String logFile = null;
439: private GregorianCalendar timeStarted = null;
440: private GregorianCalendar timeFinished = null;
441:
442: public void setLogFile(String logFile) {
443: this .logFile = logFile;
444: project.setProperty("logFile", logFile);
445: }
446:
447: public void setTimeToWait(int timeToWait) {
448: this .timeToWait = timeToWait;
449: project
450: .setProperty("timeToWait", Long
451: .toString(timeToWait));
452: }
453:
454: public void addBuildListener(BuildListener bl) {
455: project.addBuildListener(bl);
456: }
457:
458: public MonitoredBuild(File buildFile, String target) {
459: myBuildFile = buildFile;
460: this .target = target;
461: project = new Project();
462: project = new Project();
463: project.init();
464: project.setUserProperty("ant.file", myBuildFile
465: .getAbsolutePath());
466: ProjectHelper.configureProject(project, myBuildFile);
467: }
468:
469: /**
470: *
471: * @return time in millis of the build
472: */
473: public long getTimeElapsed() {
474: return timeFinished.getTime().getTime()
475: - timeStarted.getTime().getTime();
476: }
477:
478: public void start() {
479: worker = new Thread(this , myBuildFile.toString() + "/"
480: + target);
481: worker.start();
482: }
483:
484: public void run() {
485: startProject();
486: }
487:
488: private void startProject() {
489: timeStarted = new GregorianCalendar();
490: project.executeTarget(target);
491: timeFinished = new GregorianCalendar();
492: }
493: }
494:
495: private class MonitoredBuildListener implements BuildListener {
496: public void buildStarted(BuildEvent event) {
497: }
498:
499: public void buildFinished(BuildEvent event) {
500: }
501:
502: public void targetStarted(BuildEvent event) {
503: }
504:
505: public void targetFinished(BuildEvent event) {
506: if (event.getTarget().getName().equals("spawn")) {
507: buildFinished = true;
508: }
509: }
510:
511: public void taskStarted(BuildEvent event) {
512: }
513:
514: public void taskFinished(BuildEvent event) {
515: }
516:
517: public void messageLogged(BuildEvent event) {
518: }
519: }
520:
521: //borrowed from TokenFilterTest
522: private String getFileString(String filename) throws IOException {
523: String result = null;
524: FileReader reader = null;
525: try {
526: reader = new FileReader(getProject().resolveFile(filename));
527: result = FileUtils.readFully(reader);
528: } catch (IOException eyeOhEx) {
529: } finally {
530: FileUtils.close(reader);
531: }
532: return result;
533: }
534:
535: }
|