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

BuildFileTest.java 19KB

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