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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "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.TestCase;
  56. import org.apache.tools.ant.Project;
  57. import java.io.File;
  58. import java.io.PrintStream;
  59. import java.net.URL;
  60. /**
  61. * A BuildFileTest is a TestCase which executes targets from an Ant buildfile
  62. * for testing.
  63. *
  64. * This class provides a number of utility methods for particular build file
  65. * tests which extend this class.
  66. *
  67. * @author Nico Seessle <nico@seessle.de>
  68. * @author Conor MacNeill
  69. */
  70. public abstract class BuildFileTest extends TestCase {
  71. protected Project project;
  72. private StringBuffer logBuffer;
  73. private StringBuffer fullLogBuffer;
  74. private StringBuffer outBuffer;
  75. private StringBuffer errBuffer;
  76. private BuildException buildException;
  77. /**
  78. * Constructor for the BuildFileTest object
  79. *
  80. *@param name string to pass up to TestCase constructor
  81. */
  82. public BuildFileTest(String name) {
  83. super(name);
  84. }
  85. /**
  86. * run a target, expect for any build exception
  87. *
  88. *@param target target to run
  89. *@param cause information string to reader of report
  90. */
  91. protected void expectBuildException(String target, String cause) {
  92. expectSpecificBuildException(target, cause, null);
  93. }
  94. /**
  95. * Assert that only the given message has been logged with a
  96. * priority &gt;= INFO when running the given target.
  97. */
  98. protected void expectLog(String target, String log) {
  99. executeTarget(target);
  100. String realLog = getLog();
  101. assertEquals(log, realLog);
  102. }
  103. /**
  104. * Assert that the given message has been logged with a priority
  105. * &gt;= INFO when running the given target.
  106. */
  107. protected void expectLogContaining(String target, String log) {
  108. executeTarget(target);
  109. String realLog = getLog();
  110. assertTrue("expecting log to contain \""+log+"\" log was \""
  111. + realLog + "\"",
  112. realLog.indexOf(log) >= 0);
  113. }
  114. /**
  115. * Gets the log the BuildFileTest object.
  116. * only valid if configureProject() has
  117. * been called.
  118. * @pre logBuffer!=null
  119. * @return The log value
  120. */
  121. protected String getLog() {
  122. return logBuffer.toString();
  123. }
  124. /**
  125. * Assert that the given message has been logged with a priority
  126. * &gt;= DEBUG when running the given target.
  127. */
  128. protected void expectDebuglog(String target, String log) {
  129. executeTarget(target);
  130. String realLog = getFullLog();
  131. assertEquals(log, realLog);
  132. }
  133. /**
  134. * Gets the log the BuildFileTest object.
  135. * only valid if configureProject() has
  136. * been called.
  137. * @pre fullLogBuffer!=null
  138. * @return The log value
  139. */
  140. protected String getFullLog() {
  141. return fullLogBuffer.toString();
  142. }
  143. /**
  144. * execute the target, verify output matches expectations
  145. *
  146. *@param target target to execute
  147. *@param output output to look for
  148. */
  149. protected void expectOutput(String target, String output) {
  150. executeTarget(target);
  151. String realOutput = getOutput();
  152. assertEquals(output, realOutput.trim());
  153. }
  154. /**
  155. * execute the target, verify output matches expectations
  156. * and that we got the named error at the end
  157. *@param target target to execute
  158. *@param output output to look for
  159. *@param error Description of Parameter
  160. */
  161. protected void expectOutputAndError(String target, String output, String error) {
  162. executeTarget(target);
  163. String realOutput = getOutput();
  164. assertEquals(output, realOutput);
  165. String realError = getError();
  166. assertEquals(error, realError);
  167. }
  168. protected String getOutput() {
  169. return cleanBuffer(outBuffer);
  170. }
  171. protected String getError() {
  172. return cleanBuffer(errBuffer);
  173. }
  174. protected BuildException getBuildException() {
  175. return buildException;
  176. }
  177. private String cleanBuffer(StringBuffer buffer) {
  178. StringBuffer cleanedBuffer = new StringBuffer();
  179. boolean cr = false;
  180. for (int i = 0; i < buffer.length(); i++) {
  181. char ch = buffer.charAt(i);
  182. if (ch == '\r') {
  183. cr = true;
  184. continue;
  185. }
  186. if (!cr) {
  187. cleanedBuffer.append(ch);
  188. } else {
  189. if (ch == '\n') {
  190. cleanedBuffer.append(ch);
  191. } else {
  192. cleanedBuffer.append('\r').append(ch);
  193. }
  194. }
  195. }
  196. return cleanedBuffer.toString();
  197. }
  198. /**
  199. * set up to run the named project
  200. *
  201. * @param filename name of project file to run
  202. */
  203. protected void configureProject(String filename) throws BuildException {
  204. configureProject(filename, Project.MSG_DEBUG);
  205. }
  206. /**
  207. * set up to run the named project
  208. *
  209. * @param filename name of project file to run
  210. */
  211. protected void configureProject(String filename, int logLevel)
  212. throws BuildException {
  213. logBuffer = new StringBuffer();
  214. fullLogBuffer = new StringBuffer();
  215. project = new Project();
  216. project.init();
  217. project.setUserProperty( "ant.file" , new File(filename).getAbsolutePath() );
  218. project.addBuildListener(new AntTestListener(logLevel));
  219. ProjectHelper.configureProject(project, new File(filename));
  220. }
  221. /**
  222. * execute a target we have set up
  223. * @pre configureProject has been called
  224. * @param targetName target to run
  225. */
  226. protected void executeTarget(String targetName) {
  227. PrintStream sysOut = System.out;
  228. PrintStream sysErr = System.err;
  229. try {
  230. sysOut.flush();
  231. sysErr.flush();
  232. outBuffer = new StringBuffer();
  233. PrintStream out = new PrintStream(new AntOutputStream(outBuffer));
  234. System.setOut(out);
  235. errBuffer = new StringBuffer();
  236. PrintStream err = new PrintStream(new AntOutputStream(errBuffer));
  237. System.setErr(err);
  238. logBuffer = new StringBuffer();
  239. fullLogBuffer = new StringBuffer();
  240. buildException = null;
  241. project.executeTarget(targetName);
  242. } finally {
  243. System.setOut(sysOut);
  244. System.setErr(sysErr);
  245. }
  246. }
  247. /**
  248. * Get the project which has been configured for a test.
  249. *
  250. * @return the Project instance for this test.
  251. */
  252. protected Project getProject() {
  253. return project;
  254. }
  255. /**
  256. * get the directory of the project
  257. * @return the base dir of the project
  258. */
  259. protected File getProjectDir() {
  260. return project.getBaseDir();
  261. }
  262. /**
  263. * run a target, wait for a build exception
  264. *
  265. *@param target target to run
  266. *@param cause information string to reader of report
  267. *@param msg the message value of the build exception we are waiting for
  268. set to null for any build exception to be valid
  269. */
  270. protected void expectSpecificBuildException(String target, String cause, String msg) {
  271. try {
  272. executeTarget(target);
  273. } catch (org.apache.tools.ant.BuildException ex) {
  274. buildException = ex;
  275. if ((null != msg) && (!ex.getMessage().equals(msg))) {
  276. fail("Should throw BuildException because '" + cause
  277. + "' with message '" + msg
  278. + "' (actual message '" + ex.getMessage() + "' instead)");
  279. }
  280. return;
  281. }
  282. fail("Should throw BuildException because: " + cause);
  283. }
  284. /**
  285. * run a target, expect an exception string
  286. * containing the substring we look for (case sensitive match)
  287. *
  288. *@param target target to run
  289. *@param cause information string to reader of report
  290. *@param contains substring of the build exception to look for
  291. */
  292. protected void expectBuildExceptionContaining(String target, String cause, String contains) {
  293. try {
  294. executeTarget(target);
  295. } catch (org.apache.tools.ant.BuildException ex) {
  296. buildException = ex;
  297. if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) {
  298. fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
  299. }
  300. return;
  301. }
  302. fail("Should throw BuildException because: " + cause);
  303. }
  304. /**
  305. * call a target, verify property is as expected
  306. *
  307. * @param target build file target
  308. * @param property property name
  309. * @param value expected value
  310. */
  311. protected void expectPropertySet(String target, String property, String value) {
  312. executeTarget(target);
  313. assertPropertyEquals(property, value);
  314. }
  315. /**
  316. * assert that a property equals a value; comparison is case sensitive.
  317. * @param property property name
  318. * @param value expected value
  319. */
  320. protected void assertPropertyEquals(String property, String value) {
  321. String result = project.getProperty(property);
  322. assertEquals("property " + property,value,result);
  323. }
  324. /**
  325. * assert that a property equals &quot;true&quot;
  326. * @param property property name
  327. */
  328. protected void assertPropertySet(String property) {
  329. assertPropertyEquals(property, "true");
  330. }
  331. /**
  332. * assert that a property is null
  333. * @param property property name
  334. */
  335. protected void assertPropertyUnset(String property) {
  336. assertPropertyEquals(property, null);
  337. }
  338. /**
  339. * call a target, verify named property is "true".
  340. *
  341. * @param target build file target
  342. * @param property property name
  343. */
  344. protected void expectPropertySet(String target, String property) {
  345. expectPropertySet(target, property, "true");
  346. }
  347. /**
  348. * call a target, verify property is null
  349. * @param target build file target
  350. * @param property property name
  351. */
  352. protected void expectPropertyUnset(String target, String property) {
  353. expectPropertySet(target, property, null);
  354. }
  355. /**
  356. * Retrieve a resource from the caller classloader to avoid
  357. * assuming a vm working directory. The resource path must be
  358. * relative to the package name or absolute from the root path.
  359. * @param resource the resource to retrieve its url.
  360. * @throws AssertionFailureException if resource is not found.
  361. */
  362. protected URL getResource(String resource){
  363. URL url = getClass().getResource(resource);
  364. assertNotNull("Could not find resource :" + resource, url);
  365. return url;
  366. }
  367. /**
  368. * an output stream which saves stuff to our buffer.
  369. */
  370. private static class AntOutputStream extends java.io.OutputStream {
  371. private StringBuffer buffer;
  372. public AntOutputStream( StringBuffer buffer ) {
  373. this.buffer = buffer;
  374. }
  375. public void write(int b) {
  376. buffer.append((char)b);
  377. }
  378. }
  379. /**
  380. * our own personal build listener
  381. */
  382. private class AntTestListener implements BuildListener {
  383. private int logLevel;
  384. /**
  385. * Constructs a test listener which will ignore log events
  386. * above the given level
  387. */
  388. public AntTestListener(int logLevel) {
  389. this.logLevel = logLevel;
  390. }
  391. /**
  392. * Fired before any targets are started.
  393. */
  394. public void buildStarted(BuildEvent event) {
  395. }
  396. /**
  397. * Fired after the last target has finished. This event
  398. * will still be thrown if an error occured during the build.
  399. *
  400. * @see BuildEvent#getException()
  401. */
  402. public void buildFinished(BuildEvent event) {
  403. }
  404. /**
  405. * Fired when a target is started.
  406. *
  407. * @see BuildEvent#getTarget()
  408. */
  409. public void targetStarted(BuildEvent event) {
  410. //System.out.println("targetStarted " + event.getTarget().getName());
  411. }
  412. /**
  413. * Fired when a target has finished. This event will
  414. * still be thrown if an error occured during the build.
  415. *
  416. * @see BuildEvent#getException()
  417. */
  418. public void targetFinished(BuildEvent event) {
  419. //System.out.println("targetFinished " + event.getTarget().getName());
  420. }
  421. /**
  422. * Fired when a task is started.
  423. *
  424. * @see BuildEvent#getTask()
  425. */
  426. public void taskStarted(BuildEvent event) {
  427. //System.out.println("taskStarted " + event.getTask().getTaskName());
  428. }
  429. /**
  430. * Fired when a task has finished. This event will still
  431. * be throw if an error occured during the build.
  432. *
  433. * @see BuildEvent#getException()
  434. */
  435. public void taskFinished(BuildEvent event) {
  436. //System.out.println("taskFinished " + event.getTask().getTaskName());
  437. }
  438. /**
  439. * Fired whenever a message is logged.
  440. *
  441. * @see BuildEvent#getMessage()
  442. * @see BuildEvent#getPriority()
  443. */
  444. public void messageLogged(BuildEvent event) {
  445. if (event.getPriority() > logLevel) {
  446. // ignore event
  447. return;
  448. }
  449. if (event.getPriority() == Project.MSG_INFO ||
  450. event.getPriority() == Project.MSG_WARN ||
  451. event.getPriority() == Project.MSG_ERR) {
  452. logBuffer.append(event.getMessage());
  453. }
  454. fullLogBuffer.append(event.getMessage());
  455. }
  456. }
  457. }