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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import junit.framework.*;
  56. import org.apache.tools.ant.*;
  57. import java.io.*;
  58. import java.net.URL;
  59. /**
  60. * A BuildFileTest is a TestCase which executes targets from an Ant buildfile
  61. * for testing.
  62. *
  63. * This class provides a number of utility methods for particular build file
  64. * tests which extend this class.
  65. *
  66. * @author Nico Seessle <nico@seessle.de>
  67. * @author Conor MacNeill
  68. */
  69. public abstract class BuildFileTest extends TestCase {
  70. protected Project project;
  71. private StringBuffer logBuffer;
  72. private StringBuffer fullLogBuffer;
  73. private StringBuffer outBuffer;
  74. private StringBuffer errBuffer;
  75. private BuildException buildException;
  76. /**
  77. * Constructor for the BuildFileTest object
  78. *
  79. *@param name string to pass up to TestCase constructor
  80. */
  81. public BuildFileTest(String name) {
  82. super(name);
  83. }
  84. /**
  85. * run a target, expect for any build exception
  86. *
  87. *@param target target to run
  88. *@param cause information string to reader of report
  89. */
  90. protected void expectBuildException(String target, String cause) {
  91. expectSpecificBuildException(target, cause, null);
  92. }
  93. /**
  94. * Assert that only the given message has been logged with a
  95. * priority &gt;= INFO when running the given target.
  96. */
  97. protected void expectLog(String target, String log) {
  98. executeTarget(target);
  99. String realLog = getLog();
  100. assertEquals(log, realLog);
  101. }
  102. /**
  103. * Assert that the given message has been logged with a priority
  104. * &gt;= INFO when running the given target.
  105. */
  106. protected void expectLogContaining(String target, String log) {
  107. executeTarget(target);
  108. String realLog = getLog();
  109. assertTrue("expecting log to contain \""+log+"\" log was \""
  110. + realLog + "\"",
  111. realLog.indexOf(log) >= 0);
  112. }
  113. /**
  114. * Gets the log the BuildFileTest object.
  115. * only valid if configureProject() has
  116. * been called.
  117. * @pre logBuffer!=null
  118. * @return The log value
  119. */
  120. protected String getLog() {
  121. return logBuffer.toString();
  122. }
  123. /**
  124. * Assert that the given message has been logged with a priority
  125. * &gt;= DEBUG when running the given target.
  126. */
  127. protected void expectDebuglog(String target, String log) {
  128. executeTarget(target);
  129. String realLog = getFullLog();
  130. assertEquals(log, realLog);
  131. }
  132. /**
  133. * Gets the log the BuildFileTest object.
  134. * only valid if configureProject() has
  135. * been called.
  136. * @pre fullLogBuffer!=null
  137. * @return The log value
  138. */
  139. protected String getFullLog() {
  140. return fullLogBuffer.toString();
  141. }
  142. /**
  143. * execute the target, verify output matches expectations
  144. *
  145. *@param target target to execute
  146. *@param output output to look for
  147. */
  148. protected void expectOutput(String target, String output) {
  149. executeTarget(target);
  150. String realOutput = getOutput();
  151. assertEquals(output, realOutput);
  152. }
  153. /**
  154. * execute the target, verify output matches expectations
  155. * and that we got the named error at the end
  156. *@param target target to execute
  157. *@param output output to look for
  158. *@param error Description of Parameter
  159. */
  160. protected void expectOutputAndError(String target, String output, String error) {
  161. executeTarget(target);
  162. String realOutput = getOutput();
  163. assertEquals(output, realOutput);
  164. String realError = getError();
  165. assertEquals(error, realError);
  166. }
  167. protected String getOutput() {
  168. return cleanBuffer(outBuffer);
  169. }
  170. protected String getError() {
  171. return cleanBuffer(errBuffer);
  172. }
  173. protected BuildException getBuildException() {
  174. return buildException;
  175. }
  176. private String cleanBuffer(StringBuffer buffer) {
  177. StringBuffer cleanedBuffer = new StringBuffer();
  178. boolean cr = false;
  179. for (int i = 0; i < buffer.length(); i++) {
  180. char ch = buffer.charAt(i);
  181. if (ch == '\r') {
  182. cr = true;
  183. continue;
  184. }
  185. if (!cr) {
  186. cleanedBuffer.append(ch);
  187. } else {
  188. if (ch == '\n') {
  189. cleanedBuffer.append(ch);
  190. } else {
  191. cleanedBuffer.append('\r').append(ch);
  192. }
  193. }
  194. }
  195. return cleanedBuffer.toString();
  196. }
  197. /**
  198. * set up to run the named project
  199. *
  200. * @param filename name of project file to run
  201. */
  202. protected void configureProject(String filename) throws BuildException {
  203. configureProject(filename, Project.MSG_DEBUG);
  204. }
  205. /**
  206. * set up to run the named project
  207. *
  208. * @param filename name of project file to run
  209. */
  210. protected void configureProject(String filename, int logLevel)
  211. throws BuildException {
  212. logBuffer = new StringBuffer();
  213. fullLogBuffer = new StringBuffer();
  214. project = new Project();
  215. project.init();
  216. project.setUserProperty( "ant.file" , new File(filename).getAbsolutePath() );
  217. project.addBuildListener(new AntTestListener(logLevel));
  218. ProjectHelper.configureProject(project, new File(filename));
  219. }
  220. /**
  221. * execute a target we have set up
  222. * @pre configureProject has been called
  223. * @param targetName target to run
  224. */
  225. protected void executeTarget(String targetName) {
  226. PrintStream sysOut = System.out;
  227. PrintStream sysErr = System.err;
  228. try {
  229. sysOut.flush();
  230. sysErr.flush();
  231. outBuffer = new StringBuffer();
  232. PrintStream out = new PrintStream(new AntOutputStream());
  233. System.setOut(out);
  234. errBuffer = new StringBuffer();
  235. PrintStream err = new PrintStream(new AntOutputStream());
  236. System.setErr(err);
  237. logBuffer = new StringBuffer();
  238. fullLogBuffer = new StringBuffer();
  239. buildException = null;
  240. project.executeTarget(targetName);
  241. } finally {
  242. System.setOut(sysOut);
  243. System.setErr(sysErr);
  244. }
  245. }
  246. /**
  247. * Get the project which has been configured for a test.
  248. *
  249. * @return the Project instance for this test.
  250. */
  251. protected Project getProject() {
  252. return project;
  253. }
  254. /**
  255. * get the directory of the project
  256. * @return the base dir of the project
  257. */
  258. protected File getProjectDir() {
  259. return project.getBaseDir();
  260. }
  261. /**
  262. * run a target, wait for a build exception
  263. *
  264. *@param target target to run
  265. *@param cause information string to reader of report
  266. *@param msg the message value of the build exception we are waiting for
  267. set to null for any build exception to be valid
  268. */
  269. protected void expectSpecificBuildException(String target, String cause, String msg) {
  270. try {
  271. executeTarget(target);
  272. } catch (org.apache.tools.ant.BuildException ex) {
  273. buildException = ex;
  274. if ((null != msg) && (!ex.getMessage().equals(msg))) {
  275. fail("Should throw BuildException because '" + cause
  276. + "' with message '" + msg
  277. + "' (actual message '" + ex.getMessage() + "' instead)");
  278. }
  279. return;
  280. }
  281. fail("Should throw BuildException because: " + cause);
  282. }
  283. /**
  284. * run a target, expect an exception string
  285. * containing the substring we look for (case sensitive match)
  286. *
  287. *@param target target to run
  288. *@param cause information string to reader of report
  289. *@param contains substring of the build exception to look for
  290. */
  291. protected void expectBuildExceptionContaining(String target, String cause, String contains) {
  292. try {
  293. executeTarget(target);
  294. } catch (org.apache.tools.ant.BuildException ex) {
  295. buildException = ex;
  296. if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) {
  297. fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
  298. }
  299. return;
  300. }
  301. fail("Should throw BuildException because: " + cause);
  302. }
  303. /**
  304. * call a target, verify property is as expected
  305. *
  306. * @param target build file target
  307. * @param property property name
  308. * @param value expected value
  309. */
  310. protected void expectPropertySet(String target, String property, String value) {
  311. executeTarget(target);
  312. assertPropertyEquals(property, value);
  313. }
  314. /**
  315. * assert that a property equals a value; comparison is case sensitive.
  316. * @param property property name
  317. * @param value expected value
  318. */
  319. protected void assertPropertyEquals(String property, String value) {
  320. String result = project.getProperty(property);
  321. assertEquals("property " + property,value,result);
  322. }
  323. /**
  324. * assert that a property equals &quot;true&quot;
  325. * @param property property name
  326. */
  327. protected void assertPropertySet(String property) {
  328. assertPropertyEquals(property, "true");
  329. }
  330. /**
  331. * assert that a property is null
  332. * @param property property name
  333. */
  334. protected void assertPropertyUnset(String property) {
  335. assertPropertyEquals(property, null);
  336. }
  337. /**
  338. * call a target, verify named property is "true".
  339. *
  340. * @param target build file target
  341. * @param property property name
  342. */
  343. protected void expectPropertySet(String target, String property) {
  344. expectPropertySet(target, property, "true");
  345. }
  346. /**
  347. * call a target, verify property is null
  348. * @param target build file target
  349. * @param property property name
  350. */
  351. protected void expectPropertyUnset(String target, String property) {
  352. expectPropertySet(target, property, null);
  353. }
  354. /**
  355. * Retrieve a resource from the caller classloader to avoid
  356. * assuming a vm working directory. The resource path must be
  357. * relative to the package name or absolute from the root path.
  358. * @param resource the resource to retrieve its url.
  359. * @throws AssertionFailureException if resource is not found.
  360. */
  361. protected URL getResource(String resource){
  362. URL url = getClass().getResource(resource);
  363. assertNotNull("Could not find resource :" + resource, url);
  364. return url;
  365. }
  366. /**
  367. * an output stream which saves stuff to our buffer.
  368. */
  369. private class AntOutputStream extends java.io.OutputStream {
  370. public void write(int b) {
  371. outBuffer.append((char)b);
  372. }
  373. }
  374. /**
  375. * our own personal build listener
  376. */
  377. private class AntTestListener implements BuildListener {
  378. private int logLevel;
  379. /**
  380. * Constructs a test listener which will ignore log events
  381. * above the given level
  382. */
  383. public AntTestListener(int logLevel) {
  384. this.logLevel = logLevel;
  385. }
  386. /**
  387. * Fired before any targets are started.
  388. */
  389. public void buildStarted(BuildEvent event) {
  390. }
  391. /**
  392. * Fired after the last target has finished. This event
  393. * will still be thrown if an error occured during the build.
  394. *
  395. * @see BuildEvent#getException()
  396. */
  397. public void buildFinished(BuildEvent event) {
  398. }
  399. /**
  400. * Fired when a target is started.
  401. *
  402. * @see BuildEvent#getTarget()
  403. */
  404. public void targetStarted(BuildEvent event) {
  405. //System.out.println("targetStarted " + event.getTarget().getName());
  406. }
  407. /**
  408. * Fired when a target has finished. This event will
  409. * still be thrown if an error occured during the build.
  410. *
  411. * @see BuildEvent#getException()
  412. */
  413. public void targetFinished(BuildEvent event) {
  414. //System.out.println("targetFinished " + event.getTarget().getName());
  415. }
  416. /**
  417. * Fired when a task is started.
  418. *
  419. * @see BuildEvent#getTask()
  420. */
  421. public void taskStarted(BuildEvent event) {
  422. //System.out.println("taskStarted " + event.getTask().getTaskName());
  423. }
  424. /**
  425. * Fired when a task has finished. This event will still
  426. * be throw if an error occured during the build.
  427. *
  428. * @see BuildEvent#getException()
  429. */
  430. public void taskFinished(BuildEvent event) {
  431. //System.out.println("taskFinished " + event.getTask().getTaskName());
  432. }
  433. /**
  434. * Fired whenever a message is logged.
  435. *
  436. * @see BuildEvent#getMessage()
  437. * @see BuildEvent#getPriority()
  438. */
  439. public void messageLogged(BuildEvent event) {
  440. if (event.getPriority() > logLevel) {
  441. // ignore event
  442. return;
  443. }
  444. if (event.getPriority() == Project.MSG_INFO ||
  445. event.getPriority() == Project.MSG_WARN ||
  446. event.getPriority() == Project.MSG_ERR) {
  447. logBuffer.append(event.getMessage());
  448. }
  449. fullLogBuffer.append(event.getMessage());
  450. }
  451. }
  452. }