KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ExecTaskTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import org.apache.tools.ant.*;
21 import org.apache.tools.ant.util.FileUtils;
22
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.GregorianCalendar JavaDoc;
28
29 import junit.framework.ComparisonFailure;
30
31 /**
32  * Unit test for the <exec> task.
33  */

34 public class ExecTaskTest extends BuildFileTest {
35     private static final String JavaDoc BUILD_PATH = "src/etc/testcases/taskdefs/exec/";
36     private static final String JavaDoc BUILD_FILE = BUILD_PATH + "exec.xml";
37     private final int TIME_TO_WAIT = 1;
38     /** maximum time allowed for the build in milliseconds */
39     private final int MAX_BUILD_TIME = 4000;
40     private final int SECURITY_MARGIN = 2000; // wait 2 second extras
41
// the test failed with 100 ms of margin on cvs.apache.org on August 1st, 2003
42
private File JavaDoc logFile;
43     private MonitoredBuild myBuild = null;
44     volatile private boolean buildFinished = false;
45     public ExecTaskTest(String JavaDoc name) {
46         super(name);
47     }
48
49     public void setUp() {
50         configureProject(BUILD_FILE);
51     }
52
53     public void tearDown() {
54         executeTarget("cleanup");
55         if (logFile != null && logFile.exists()) {
56             logFile.delete();
57         }
58     }
59
60     public void testNoRedirect() {
61         executeTarget("no-redirect");
62         if (getProject().getProperty("test.can.run") == null) {
63             return;
64         }
65         assertEquals("unexpected log content",
66             getProject().getProperty("ant.file") + " out"
67             + getProject().getProperty("ant.file") + " err", getLog());
68     }
69
70     public void testRedirect1() throws IOException JavaDoc {
71         executeTarget("redirect1");
72         if (getProject().getProperty("test.can.run") == null) {
73             return;
74         }
75         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n"
76             + getProject().getProperty("ant.file") + " err\n";
77
78         assertEquals("unexpected output",
79             expectedOut, getFileString("redirect.out"));
80     }
81
82     public void testRedirect2() throws IOException JavaDoc {
83         executeTarget("redirect2");
84         if (getProject().getProperty("test.can.run") == null) {
85             return;
86         }
87
88         assertEquals("unexpected output",
89             getProject().getProperty("ant.file") + " out\n",
90             getFileString("redirect.out"));
91         assertEquals("unexpected error output",
92             getProject().getProperty("ant.file") + " err\n",
93             getFileString("redirect.err"));
94     }
95
96     public void testRedirect3() throws IOException JavaDoc {
97         executeTarget("redirect3");
98         if (getProject().getProperty("test.can.run") == null) {
99             return;
100         }
101         assertEquals("unexpected log content",
102             getProject().getProperty("ant.file") + " err", getLog());
103         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n";
104
105         assertEquals("unexpected output",
106             expectedOut, getFileString("redirect.out"));
107         assertPropertyEquals("redirect.out", expectedOut.trim());
108     }
109
110     public void testRedirect4() throws IOException JavaDoc {
111         executeTarget("redirect4");
112         if (getProject().getProperty("test.can.run") == null) {
113             return;
114         }
115         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n";
116         String JavaDoc expectedErr = getProject().getProperty("ant.file") + " err\n";
117
118         assertEquals("unexpected output",
119             expectedOut, getFileString("redirect.out"));
120         assertPropertyEquals("redirect.out", expectedOut.trim());
121         assertEquals("unexpected error output",
122             expectedErr, getFileString("redirect.err"));
123         assertPropertyEquals("redirect.err", expectedErr.trim());
124     }
125
126     public void testRedirect5() throws IOException JavaDoc {
127         testRedirect5or6("redirect5");
128     }
129
130     public void testRedirect6() throws IOException JavaDoc {
131         testRedirect5or6("redirect6");
132     }
133
134     public void testRedirect5or6(String JavaDoc target) throws IOException JavaDoc {
135         executeTarget(target);
136         if (getProject().getProperty("wc.can.run") == null) {
137             return;
138         }
139
140         assertEquals("unexpected output", "3", getFileString("redirect.out").trim());
141         assertEquals("property redirect.out", "3",
142             getProject().getProperty("redirect.out").trim());
143         assertNull("unexpected error output", getFileString("redirect.err"));
144         assertPropertyEquals("redirect.err", "");
145     }
146
147     public void testRedirect7() throws IOException JavaDoc {
148         executeTarget("redirect7");
149         if (getProject().getProperty("wc.can.run") == null) {
150             return;
151         }
152
153         assertEquals("unexpected output", "3", getFileString("redirect.out").trim());
154         assertEquals("property redirect.out", "3",
155             getProject().getProperty("redirect.out").trim());
156         assertNull("unexpected error output", getFileString("redirect.err"));
157     }
158
159     public void testRedirector1() {
160         executeTarget("init");
161         if (getProject().getProperty("test.can.run") == null) {
162             return;
163         }
164         expectBuildException("redirector1", "cannot have > 1 nested <redirector>s");
165     }
166
167     public void testRedirector2() throws IOException JavaDoc {
168         executeTarget("redirector2");
169         if (getProject().getProperty("test.can.run") == null) {
170             return;
171         }
172
173         assertEquals("unexpected output",
174             getProject().getProperty("ant.file") + " out\n"
175             + getProject().getProperty("ant.file") + " err\n",
176             getFileString("redirector.out"));
177     }
178
179     public void testRedirector3() throws IOException JavaDoc {
180         executeTarget("redirector3");
181         if (getProject().getProperty("test.can.run") == null) {
182             return;
183         }
184
185         assertEquals("unexpected output",
186             getProject().getProperty("ant.file") + " out\n",
187             getFileString("redirector.out"));
188         assertEquals("unexpected error output",
189             getProject().getProperty("ant.file") + " err\n",
190             getFileString("redirector.err"));
191     }
192
193     public void testRedirector4() throws IOException JavaDoc {
194         executeTarget("redirector4");
195         if (getProject().getProperty("test.can.run") == null) {
196             return;
197         }
198         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n";
199
200         assertEquals("unexpected log content",
201             getProject().getProperty("ant.file") + " err", getLog());
202         assertEquals("unexpected output", expectedOut,
203             getFileString("redirector.out"));
204         assertPropertyEquals("redirector.out", expectedOut.trim());
205     }
206
207     public void testRedirector5() throws IOException JavaDoc {
208         testRedirector5or6("redirector5");
209     }
210
211     public void testRedirector6() throws IOException JavaDoc {
212         testRedirector5or6("redirector6");
213     }
214
215     private void testRedirector5or6(String JavaDoc target) throws IOException JavaDoc {
216         executeTarget(target);
217         if (getProject().getProperty("test.can.run") == null) {
218             return;
219         }
220         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n";
221         String JavaDoc expectedErr = getProject().getProperty("ant.file") + " err\n";
222
223         assertEquals("unexpected output", expectedOut,
224             getFileString("redirector.out"));
225         assertPropertyEquals("redirector.out", expectedOut.trim());
226         assertEquals("unexpected error output", expectedErr,
227             getFileString("redirector.err"));
228         assertPropertyEquals("redirector.err", expectedErr.trim());
229     }
230
231     public void testRedirector7() throws IOException JavaDoc {
232         executeTarget("redirector7");
233         if (getProject().getProperty("test.can.run") == null) {
234             return;
235         }
236         String JavaDoc expectedOut = getProject().getProperty("ant.file") + " out\n";
237         String JavaDoc expectedErr = getProject().getProperty("ant.file") + " ERROR!!!\n";
238
239         assertEquals("unexpected output", expectedOut,
240             getFileString("redirector.out"));
241         assertPropertyEquals("redirector.out", expectedOut.trim());
242         assertEquals("unexpected error output", expectedErr,
243             getFileString("redirector.err"));
244         assertPropertyEquals("redirector.err", expectedErr.trim());
245     }
246
247     public void testRedirector8() throws IOException JavaDoc {
248         executeTarget("redirector8");
249         if (getProject().getProperty("wc.can.run") == null) {
250             return;
251         }
252
253         assertEquals("unexpected output", "3", getFileString("redirector.out").trim());
254         assertEquals("property redirector.out", "3",
255             getProject().getProperty("redirector.out").trim());
256         assertNull("unexpected error output", getFileString("redirector.err"));
257         assertPropertyEquals("redirector.err", "");
258     }
259
260     public void testRedirector9() throws IOException JavaDoc {
261         testRedirector9Thru12("redirector9");
262     }
263
264     public void testRedirector10() throws IOException JavaDoc {
265         testRedirector9Thru12("redirector10");
266     }
267
268     public void testRedirector11() throws IOException JavaDoc {
269         testRedirector9Thru12("redirector11");
270     }
271
272     public void testRedirector12() throws IOException JavaDoc {
273         testRedirector9Thru12("redirector12");
274     }
275
276     private void testRedirector9Thru12(String JavaDoc target) throws IOException JavaDoc {
277         executeTarget(target);
278         if (getProject().getProperty("cat.can.run") == null) {
279             return;
280         }
281         String JavaDoc expectedOut = "blah after blah";
282
283         assertEquals("unexpected output",
284             expectedOut, getFileString("redirector.out").trim());
285         assertPropertyEquals("redirector.out", expectedOut.trim());
286         assertNull("unexpected error output", getFileString("redirector.err"));
287         assertPropertyEquals("redirector.err", "");
288     }
289
290     public void testRedirector13() {
291         executeTarget("redirector13");
292         if (getProject().getProperty("test.can.run") == null) {
293             return;
294         }
295         String JavaDoc antfile = getProject().getProperty("ant.file");
296         try {
297             //no point in setting a message
298
assertEquals(antfile + " OUTPUT???" + antfile + " ERROR!!!", getLog());
299         } catch (ComparisonFailure cf) {
300             assertEquals("unexpected log content",
301                 antfile + " ERROR!!!" + antfile + " OUTPUT???", getLog());
302         }
303     }
304
305     public void testRedirector14() {
306         executeTarget("redirector14");
307         if (getProject().getProperty("cat.can.run") == null) {
308             return;
309         }
310         assertEquals("unexpected log output", "blah after blah", getLog());
311     }
312
313     public void testRedirector15() throws IOException JavaDoc {
314         executeTarget("redirector15");
315         if (getProject().getProperty("cat.can.run") == null) {
316             return;
317         }
318         assertTrue("error with transcoding",
319             FileUtils.newFileUtils().contentEquals(
320             getProject().resolveFile("expected/utf-8"),
321             getProject().resolveFile("redirector.out")));
322     }
323
324     public void testRedirector16() {
325         executeTarget("redirector16");
326     }
327
328     public void testRedirector17() {
329         executeTarget("redirector17");
330     }
331
332     public void testRedirector18() {
333         if (getProject().getProperty("test.can.run") == null) {
334             return;
335         }
336         expectLog("redirector18", getProject().getProperty("ant.file")
337             + " out" + getProject().getProperty("ant.file") + " err");
338     }
339
340     public void testspawn() {
341         project.executeTarget("init");
342         if (project.getProperty("test.can.run") == null) {
343             return;
344         }
345         myBuild = new MonitoredBuild(new File JavaDoc(BUILD_FILE), "spawn");
346         FileUtils fileutils = FileUtils.newFileUtils();
347         logFile = fileutils.createTempFile("spawn","log", project.getBaseDir());
348         // this is guaranteed by FileUtils#createTempFile
349
assertTrue("log file not existing", !logFile.exists());
350         // make the spawned process run 4 seconds
351
myBuild.setTimeToWait(TIME_TO_WAIT);
352         myBuild.setLogFile(logFile.getAbsolutePath());
353         myBuild.addBuildListener(new MonitoredBuildListener());
354         myBuild.start();
355         GregorianCalendar JavaDoc startwait = new GregorianCalendar JavaDoc();
356         // this loop runs parallel to the build
357
while (!buildFinished) {
358             try {
359                 Thread.sleep(10);
360             } catch (InterruptedException JavaDoc e) {
361                 System.out.println("my sleep was interrupted");
362             }
363             GregorianCalendar JavaDoc now = new GregorianCalendar JavaDoc();
364             // security
365
if (now.getTime().getTime() - startwait.getTime().getTime() > MAX_BUILD_TIME) {
366                 System.out.println("aborting wait, too long " + (now.getTime().getTime() - startwait.getTime().getTime()) + "milliseconds");
367                 break;
368             }
369         }
370         // now wait until the spawned process is finished
371
try {
372             Thread.sleep((TIME_TO_WAIT) * 1000 + SECURITY_MARGIN);
373         } catch (InterruptedException JavaDoc e) {
374             System.out.println("my sleep was interrupted");
375         }
376         // time of the build in milli seconds
377
long elapsed = myBuild.getTimeElapsed();
378         assertTrue("we waited more than the process lasted", TIME_TO_WAIT * 1000
379                 + SECURITY_MARGIN > elapsed);
380         logFile = new File JavaDoc(logFile.getAbsolutePath());
381         assertTrue("log file found after spawn", logFile.exists());
382     }
383
384     private static class MonitoredBuild implements Runnable JavaDoc {
385         private Thread JavaDoc worker;
386         private File JavaDoc myBuildFile = null;
387         private String JavaDoc target = null;
388         private Project project = null;
389         private int timeToWait = 0;
390         private String JavaDoc logFile = null;
391         private GregorianCalendar JavaDoc timeStarted = null;
392         private GregorianCalendar JavaDoc timeFinished = null;
393
394         public void setLogFile(String JavaDoc logFile) {
395             this.logFile = logFile;
396             project.setProperty("logFile", logFile);
397         }
398
399         public void setTimeToWait(int timeToWait) {
400             this.timeToWait = timeToWait;
401             project.setProperty("timeToWait", Long.toString(timeToWait));
402         }
403
404         public void addBuildListener(BuildListener bl) {
405             project.addBuildListener(bl);
406         }
407         public MonitoredBuild(File JavaDoc buildFile, String JavaDoc target) {
408             myBuildFile = buildFile;
409             this.target = target;
410             project=new Project();
411             project = new Project();
412             project.init();
413             project.setUserProperty( "ant.file" , myBuildFile.getAbsolutePath() );
414             ProjectHelper.configureProject(project, myBuildFile);
415         }
416         /**
417          *
418          * @return time in millis of the build
419          */

420         public long getTimeElapsed() {
421             return timeFinished.getTime().getTime() - timeStarted.getTime().getTime();
422         }
423         public void start() {
424             worker = new Thread JavaDoc(this, myBuildFile.toString() + "/" + target);
425             worker.start();
426         }
427         public void run() {
428             startProject();
429         }
430         private void startProject() {
431             timeStarted = new GregorianCalendar JavaDoc();
432             project.executeTarget(target);
433             timeFinished = new GregorianCalendar JavaDoc();
434         }
435     }
436     private class MonitoredBuildListener implements BuildListener {
437         public void buildStarted(BuildEvent event) {
438         }
439
440         public void buildFinished(BuildEvent event) {
441         }
442
443         public void targetStarted(BuildEvent event) {
444         }
445
446         public void targetFinished(BuildEvent event) {
447             if (event.getTarget().getName().equals("spawn")) {
448                 buildFinished = true;
449             }
450         }
451
452         public void taskStarted(BuildEvent event) {
453         }
454
455         public void taskFinished(BuildEvent event) {
456         }
457
458         public void messageLogged(BuildEvent event) {
459         }
460     }
461
462     //borrowed from TokenFilterTest
463
private String JavaDoc getFileString(String JavaDoc filename) throws IOException JavaDoc {
464         String JavaDoc result = null;
465         FileReader JavaDoc reader = null;
466         try {
467             reader = new FileReader JavaDoc(getProject().resolveFile(filename));
468             result = FileUtils.newFileUtils().readFully(reader);
469         } catch (IOException JavaDoc eyeOhEx) {
470         } finally {
471             if (reader != null) {
472                 try {
473                     reader.close();
474                 } catch (Throwable JavaDoc ignore) {
475                 }
476             }
477         }
478         return result;
479     }
480
481 }
482
Popular Tags