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.

BuildFileTest.java 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.io.PrintStream;
  22. import java.net.URL;
  23. import junit.framework.TestCase;
  24. import org.apache.tools.ant.util.ProcessUtil;
  25. /**
  26. * A BuildFileTest is a TestCase which executes targets from an Ant buildfile
  27. * for testing.
  28. *
  29. * This class provides a number of utility methods for particular build file
  30. * tests which extend this class.
  31. *
  32. * @deprecated as of 1.9.4. Use BuildFileRule, Assert, AntAssert and JUnit4 annotations to drive tests instead
  33. * @see org.apache.tools.ant.BuildFileRule
  34. */
  35. @Deprecated
  36. public abstract class BuildFileTest extends TestCase {
  37. protected Project project;
  38. private StringBuffer logBuffer;
  39. private StringBuffer fullLogBuffer;
  40. private StringBuffer outBuffer;
  41. private StringBuffer errBuffer;
  42. private BuildException buildException;
  43. /**
  44. * Default constructor for the BuildFileTest object.
  45. */
  46. public BuildFileTest() {
  47. super();
  48. }
  49. /**
  50. * Constructor for the BuildFileTest object.
  51. *
  52. * @param name string to pass up to TestCase constructor
  53. */
  54. public BuildFileTest(String name) {
  55. super(name);
  56. }
  57. /**
  58. * Automatically calls the target called "tearDown"
  59. * from the build file tested if it exits.
  60. *
  61. * This allows to use Ant tasks directly in the build file
  62. * to clean up after each test. Note that no "setUp" target
  63. * is automatically called, since it's trivial to have a
  64. * test target depend on it.
  65. */
  66. protected void tearDown() {
  67. if (project == null) {
  68. /*
  69. * Maybe the BuildFileTest was subclassed and there is
  70. * no initialized project. So we could avoid getting a
  71. * NPE.
  72. * If there is an initialized project getTargets() does
  73. * not return null as it is initialized by an empty
  74. * HashSet.
  75. */
  76. return;
  77. }
  78. final String tearDown = "tearDown";
  79. if (project.getTargets().containsKey(tearDown)) {
  80. project.executeTarget(tearDown);
  81. }
  82. }
  83. /**
  84. * run a target, expect for any build exception
  85. *
  86. * @param target target to run
  87. * @param cause information string to reader of report
  88. */
  89. public void expectBuildException(String target, String cause) {
  90. expectSpecificBuildException(target, cause, null);
  91. }
  92. /**
  93. * Assert that only the given message has been logged with a
  94. * priority <= INFO when running the given target.
  95. *
  96. * @param target String
  97. * @param log String
  98. */
  99. public void expectLog(String target, String log) {
  100. executeTarget(target);
  101. String realLog = getLog();
  102. assertEquals(log, realLog);
  103. }
  104. /**
  105. * Assert that the given substring is in the log messages.
  106. *
  107. * @param substring String
  108. */
  109. public void assertLogContaining(String substring) {
  110. String realLog = getLog();
  111. assertTrue("expecting log to contain \"" + substring + "\" log was \""
  112. + realLog + "\"",
  113. realLog.contains(substring));
  114. }
  115. /**
  116. * Assert that the given substring is not in the log messages.
  117. *
  118. * @param substring String
  119. */
  120. public void assertLogNotContaining(String substring) {
  121. String realLog = getLog();
  122. assertFalse("didn't expect log to contain \"" + substring + "\" log was \""
  123. + realLog + "\"",
  124. realLog.contains(substring));
  125. }
  126. /**
  127. * Assert that the given substring is in the output messages.
  128. *
  129. * @param substring String
  130. * @since Ant1.7
  131. */
  132. public void assertOutputContaining(String substring) {
  133. assertOutputContaining(null, substring);
  134. }
  135. /**
  136. * Assert that the given substring is in the output messages.
  137. *
  138. * @param message Print this message if the test fails. Defaults to
  139. * a meaningful text if <tt>null</tt> is passed.
  140. * @param substring String
  141. * @since Ant1.7
  142. */
  143. public void assertOutputContaining(String message, String substring) {
  144. String realOutput = getOutput();
  145. String realMessage = (message != null)
  146. ? message
  147. : "expecting output to contain \"" + substring + "\" output was \"" + realOutput + "\"";
  148. assertTrue(realMessage, realOutput.contains(substring));
  149. }
  150. /**
  151. * Assert that the given substring is not in the output messages.
  152. *
  153. * @param message Print this message if the test fails. Defaults to
  154. * a meaningful text if <tt>null</tt> is passed.
  155. * @param substring String
  156. * @since Ant1.7
  157. */
  158. public void assertOutputNotContaining(String message, String substring) {
  159. String realOutput = getOutput();
  160. String realMessage = (message != null)
  161. ? message
  162. : "expecting output to not contain \"" + substring + "\" output was \"" + realOutput + "\"";
  163. assertFalse(realMessage, realOutput.contains(substring));
  164. }
  165. /**
  166. * Assert that the given message has been logged with a priority &lt;= INFO when running the
  167. * given target.
  168. *
  169. * @param target String
  170. * @param log String
  171. */
  172. public void expectLogContaining(String target, String log) {
  173. executeTarget(target);
  174. assertLogContaining(log);
  175. }
  176. /**
  177. * Assert that the given message has not been logged with a
  178. * priority &lt;= INFO when running the given target.
  179. *
  180. * @param target String
  181. * @param log String
  182. */
  183. public void expectLogNotContaining(String target, String log) {
  184. executeTarget(target);
  185. assertLogNotContaining(log);
  186. }
  187. /**
  188. * Gets the log the BuildFileTest object.
  189. * Only valid if configureProject() has been called.
  190. *
  191. * @pre logBuffer!=null
  192. * @return The log value
  193. */
  194. public String getLog() {
  195. return logBuffer.toString();
  196. }
  197. /**
  198. * Assert that the given message has been logged with a priority
  199. * &gt;= VERBOSE when running the given target.
  200. *
  201. * @param target String
  202. * @param log String
  203. */
  204. public void expectDebuglog(String target, String log) {
  205. executeTarget(target);
  206. String realLog = getFullLog();
  207. assertEquals(log, realLog);
  208. }
  209. /**
  210. * Assert that the given substring is in the log messages.
  211. *
  212. * @param substring String
  213. */
  214. public void assertDebuglogContaining(String substring) {
  215. String realLog = getFullLog();
  216. assertTrue("expecting debug log to contain \"" + substring
  217. + "\" log was \""
  218. + realLog + "\"",
  219. realLog.contains(substring));
  220. }
  221. /**
  222. * Gets the log the BuildFileTest object.
  223. *
  224. * Only valid if configureProject() has been called.
  225. *
  226. * @pre fullLogBuffer!=null
  227. * @return The log value
  228. */
  229. public String getFullLog() {
  230. return fullLogBuffer.toString();
  231. }
  232. /**
  233. * execute the target, verify output matches expectations
  234. *
  235. * @param target target to execute
  236. * @param output output to look for
  237. */
  238. public void expectOutput(String target, String output) {
  239. executeTarget(target);
  240. String realOutput = getOutput();
  241. assertEquals(output, realOutput.trim());
  242. }
  243. /**
  244. * Executes the target, verify output matches expectations
  245. * and that we got the named error at the end
  246. *
  247. * @param target target to execute
  248. * @param output output to look for
  249. * @param error Description of Parameter
  250. */
  251. public void expectOutputAndError(String target, String output, String error) {
  252. executeTarget(target);
  253. String realOutput = getOutput();
  254. assertEquals(output, realOutput);
  255. String realError = getError();
  256. assertEquals(error, realError);
  257. }
  258. public String getOutput() {
  259. return cleanBuffer(outBuffer);
  260. }
  261. public String getError() {
  262. return cleanBuffer(errBuffer);
  263. }
  264. public BuildException getBuildException() {
  265. return buildException;
  266. }
  267. private String cleanBuffer(StringBuffer buffer) {
  268. StringBuilder cleanedBuffer = new StringBuilder();
  269. for (int i = 0; i < buffer.length(); i++) {
  270. char ch = buffer.charAt(i);
  271. if (ch != '\r') {
  272. cleanedBuffer.append(ch);
  273. }
  274. }
  275. return cleanedBuffer.toString();
  276. }
  277. /**
  278. * Sets up to run the named project
  279. *
  280. * @param filename name of project file to run
  281. */
  282. public void configureProject(String filename) throws BuildException {
  283. configureProject(filename, Project.MSG_DEBUG);
  284. }
  285. /**
  286. * Sets up to run the named project
  287. *
  288. * @param filename name of project file to run
  289. * @param logLevel int
  290. */
  291. public void configureProject(String filename, int logLevel)
  292. throws BuildException {
  293. logBuffer = new StringBuffer();
  294. fullLogBuffer = new StringBuffer();
  295. project = new Project();
  296. project.init();
  297. File antFile = new File(System.getProperty("root"), filename);
  298. project.setUserProperty("ant.file", antFile.getAbsolutePath());
  299. // set two new properties to allow to build unique names when running multithreaded tests
  300. project.setProperty("ant.processid", ProcessUtil.getProcessId("<Process>"));
  301. project.setProperty("ant.threadname", Thread.currentThread().getName());
  302. project.addBuildListener(new AntTestListener(logLevel));
  303. ProjectHelper.configureProject(project, antFile);
  304. }
  305. /**
  306. * Executes a target we have set up
  307. *
  308. * @pre configureProject has been called
  309. * @param targetName target to run
  310. */
  311. public void executeTarget(String targetName) {
  312. PrintStream sysOut = System.out;
  313. PrintStream sysErr = System.err;
  314. try {
  315. sysOut.flush();
  316. sysErr.flush();
  317. outBuffer = new StringBuffer();
  318. PrintStream out = new PrintStream(new AntOutputStream(outBuffer));
  319. System.setOut(out);
  320. errBuffer = new StringBuffer();
  321. PrintStream err = new PrintStream(new AntOutputStream(errBuffer));
  322. System.setErr(err);
  323. logBuffer = new StringBuffer();
  324. fullLogBuffer = new StringBuffer();
  325. buildException = null;
  326. project.executeTarget(targetName);
  327. } finally {
  328. System.setOut(sysOut);
  329. System.setErr(sysErr);
  330. }
  331. }
  332. /**
  333. * Get the project which has been configured for a test.
  334. *
  335. * @return the Project instance for this test.
  336. */
  337. public Project getProject() {
  338. return project;
  339. }
  340. /**
  341. * Gets the directory of the project.
  342. *
  343. * @return the base dir of the project
  344. */
  345. public File getProjectDir() {
  346. return project.getBaseDir();
  347. }
  348. /**
  349. * get location of temporary directory pointed to by property "output"
  350. * @return location of temporary directory pointed to by property "output"
  351. * @since Ant 1.9.4
  352. */
  353. public File getOutputDir() {
  354. return new File(project.getProperty("output"));
  355. }
  356. /**
  357. * Runs a target, wait for a build exception.
  358. *
  359. * @param target target to run
  360. * @param cause information string to reader of report
  361. * @param msg the message value of the build exception we are waiting
  362. * for set to null for any build exception to be valid
  363. */
  364. public void expectSpecificBuildException(String target, String cause, String msg) {
  365. try {
  366. executeTarget(target);
  367. } catch (BuildException ex) {
  368. buildException = ex;
  369. if ((null != msg) && (!ex.getMessage().equals(msg))) {
  370. fail("Should throw BuildException because '" + cause
  371. + "' with message '" + msg
  372. + "' (actual message '" + ex.getMessage() + "' instead)");
  373. }
  374. return;
  375. }
  376. fail("Should throw BuildException because: " + cause);
  377. }
  378. /**
  379. * run a target, expect an exception string
  380. * containing the substring we look for (case sensitive match)
  381. *
  382. * @param target target to run
  383. * @param cause information string to reader of report
  384. * @param contains substring of the build exception to look for
  385. */
  386. public void expectBuildExceptionContaining(String target, String cause, String contains) {
  387. try {
  388. executeTarget(target);
  389. } catch (org.apache.tools.ant.BuildException ex) {
  390. buildException = ex;
  391. if ((null != contains) && (!ex.getMessage().contains(contains))) {
  392. fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
  393. }
  394. return;
  395. }
  396. fail("Should throw BuildException because: " + cause);
  397. }
  398. /**
  399. * call a target, verify property is as expected
  400. *
  401. * @param target build file target
  402. * @param property property name
  403. * @param value expected value
  404. */
  405. public void expectPropertySet(String target, String property, String value) {
  406. executeTarget(target);
  407. assertPropertyEquals(property, value);
  408. }
  409. /**
  410. * assert that a property equals a value; comparison is case sensitive.
  411. *
  412. * @param property property name
  413. * @param value expected value
  414. */
  415. public void assertPropertyEquals(String property, String value) {
  416. String result = project.getProperty(property);
  417. assertEquals("property " + property, value, result);
  418. }
  419. /**
  420. * assert that a property equals "true".
  421. *
  422. * @param property property name
  423. */
  424. public void assertPropertySet(String property) {
  425. assertPropertyEquals(property, "true");
  426. }
  427. /**
  428. * assert that a property is null.
  429. *
  430. * @param property property name
  431. */
  432. public void assertPropertyUnset(String property) {
  433. String result = project.getProperty(property);
  434. if (result != null) {
  435. fail("Expected property " + property
  436. + " to be unset, but it is set to the value: " + result);
  437. }
  438. }
  439. /**
  440. * call a target, verify named property is "true".
  441. *
  442. * @param target build file target
  443. * @param property property name
  444. */
  445. public void expectPropertySet(String target, String property) {
  446. expectPropertySet(target, property, "true");
  447. }
  448. /**
  449. * Call a target, verify property is null.
  450. *
  451. * @param target build file target
  452. * @param property property name
  453. */
  454. public void expectPropertyUnset(String target, String property) {
  455. expectPropertySet(target, property, null);
  456. }
  457. /**
  458. * Retrieve a resource from the caller classloader to avoid
  459. * assuming a vm working directory. The resource path must be
  460. * relative to the package name or absolute from the root path.
  461. *
  462. * @param resource the resource to retrieve its url.
  463. * @return URL ditto
  464. */
  465. public URL getResource(String resource) {
  466. URL url = getClass().getResource(resource);
  467. assertNotNull("Could not find resource :" + resource, url);
  468. return url;
  469. }
  470. /**
  471. * an output stream which saves stuff to our buffer.
  472. */
  473. protected static class AntOutputStream extends OutputStream {
  474. private StringBuffer buffer;
  475. public AntOutputStream(StringBuffer buffer) {
  476. this.buffer = buffer;
  477. }
  478. public void write(int b) {
  479. buffer.append((char) b);
  480. }
  481. }
  482. /**
  483. * Our own personal build listener.
  484. */
  485. private class AntTestListener implements BuildListener {
  486. private int logLevel;
  487. /**
  488. * Constructs a test listener which will ignore log events
  489. * above the given level.
  490. */
  491. public AntTestListener(int logLevel) {
  492. this.logLevel = logLevel;
  493. }
  494. /**
  495. * Fired before any targets are started.
  496. */
  497. public void buildStarted(BuildEvent event) {
  498. }
  499. /**
  500. * Fired after the last target has finished. This event
  501. * will still be thrown if an error occurred during the build.
  502. *
  503. * @see BuildEvent#getException()
  504. */
  505. public void buildFinished(BuildEvent event) {
  506. }
  507. /**
  508. * Fired when a target is started.
  509. *
  510. * @see BuildEvent#getTarget()
  511. */
  512. public void targetStarted(BuildEvent event) {
  513. //System.out.println("targetStarted " + event.getTarget().getName());
  514. }
  515. /**
  516. * Fired when a target has finished. This event will
  517. * still be thrown if an error occurred during the build.
  518. *
  519. * @see BuildEvent#getException()
  520. */
  521. public void targetFinished(BuildEvent event) {
  522. //System.out.println("targetFinished " + event.getTarget().getName());
  523. }
  524. /**
  525. * Fired when a task is started.
  526. *
  527. * @see BuildEvent#getTask()
  528. */
  529. public void taskStarted(BuildEvent event) {
  530. //System.out.println("taskStarted " + event.getTask().getTaskName());
  531. }
  532. /**
  533. * Fired when a task has finished. This event will still
  534. * be throw if an error occurred during the build.
  535. *
  536. * @see BuildEvent#getException()
  537. */
  538. public void taskFinished(BuildEvent event) {
  539. //System.out.println("taskFinished " + event.getTask().getTaskName());
  540. }
  541. /**
  542. * Fired whenever a message is logged.
  543. *
  544. * @see BuildEvent#getMessage()
  545. * @see BuildEvent#getPriority()
  546. */
  547. public void messageLogged(BuildEvent event) {
  548. if (event.getPriority() > logLevel) {
  549. // ignore event
  550. return;
  551. }
  552. if (event.getPriority() == Project.MSG_INFO
  553. || event.getPriority() == Project.MSG_WARN
  554. || event.getPriority() == Project.MSG_ERR) {
  555. logBuffer.append(event.getMessage());
  556. }
  557. fullLogBuffer.append(event.getMessage());
  558. }
  559. }
  560. }