You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExecTaskTest.java 17 kB

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