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 18 kB

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