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.

TaskdefsTest.java 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000 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.taskdefs;
  55. import junit.framework.*;
  56. import org.apache.tools.ant.*;
  57. import java.io.*;
  58. /**
  59. * @author Nico Seessle <nico@seessle.de>
  60. */
  61. public abstract class TaskdefsTest extends TestCase {
  62. protected Project project;
  63. private StringBuffer logBuffer;
  64. private StringBuffer fullLogBuffer;
  65. private StringBuffer outBuffer;
  66. private StringBuffer errBuffer;
  67. private BuildException buildException;
  68. public TaskdefsTest(String name) {
  69. super(name);
  70. }
  71. protected String getLog() {
  72. return logBuffer.toString();
  73. }
  74. protected String getFullLog() {
  75. return fullLogBuffer.toString();
  76. }
  77. protected void expectBuildException(String taskname, String cause) {
  78. expectSpecificBuildException(taskname, cause, null);
  79. }
  80. protected void expectOutput(String taskname, String output) {
  81. executeTarget(taskname);
  82. String realOutput = getOutput();
  83. assertEquals(output, realOutput);
  84. }
  85. protected void expectOutputAndError(String taskname, String output, String error) {
  86. executeTarget(taskname);
  87. String realOutput = getOutput();
  88. assertEquals(output, realOutput);
  89. String realError = getError();
  90. assertEquals(error, realError);
  91. }
  92. protected void expectLog(String taskname, String log) {
  93. executeTarget(taskname);
  94. String realLog = getLog();
  95. assertEquals(log, realLog);
  96. }
  97. protected String getOutput() {
  98. return cleanBuffer(outBuffer);
  99. }
  100. protected String getError() {
  101. return cleanBuffer(errBuffer);
  102. }
  103. private String cleanBuffer(StringBuffer buffer) {
  104. StringBuffer cleanedBuffer = new StringBuffer();
  105. boolean cr = false;
  106. for (int i = 0; i < buffer.length(); i++) {
  107. char ch = buffer.charAt(i);
  108. if (ch == '\r') {
  109. cr = true;
  110. continue;
  111. }
  112. if (!cr) {
  113. cleanedBuffer.append(ch);
  114. } else {
  115. if (ch == '\n') {
  116. cleanedBuffer.append(ch);
  117. } else {
  118. cleanedBuffer.append('\r').append(ch);
  119. }
  120. }
  121. }
  122. return cleanedBuffer.toString();
  123. }
  124. protected void configureProject(String filename) {
  125. logBuffer = new StringBuffer();
  126. fullLogBuffer = new StringBuffer();
  127. project = new Project();
  128. project.init();
  129. project.setUserProperty( "ant.file" , new File(filename).getAbsolutePath() );
  130. project.addBuildListener(new AntTestListener());
  131. ProjectHelper.configureProject(project, new File(filename));
  132. }
  133. protected void executeTarget(String targetName) {
  134. PrintStream sysOut = System.out;
  135. PrintStream sysErr = System.err;
  136. try {
  137. sysOut.flush();
  138. sysErr.flush();
  139. outBuffer = new StringBuffer();
  140. PrintStream out = new PrintStream(new AntOutputStream());
  141. System.setOut(out);
  142. errBuffer = new StringBuffer();
  143. PrintStream err = new PrintStream(new AntOutputStream());
  144. System.setErr(err);
  145. logBuffer = new StringBuffer();
  146. fullLogBuffer = new StringBuffer();
  147. buildException = null;
  148. project.executeTarget(targetName);
  149. } finally {
  150. System.setOut(sysOut);
  151. System.setErr(sysErr);
  152. }
  153. }
  154. protected File getProjectDir() {
  155. return project.getBaseDir();
  156. }
  157. protected void expectSpecificBuildException(String taskname, String cause, String msg) {
  158. try {
  159. executeTarget(taskname);
  160. } catch (org.apache.tools.ant.BuildException ex) {
  161. if ((null != msg) && (ex.getMessage() != msg)) {
  162. fail("Should throw BuildException because '" + cause + "' with message '" + msg + "' (received message '" + ex.getMessage() + "' instead)");
  163. }
  164. return;
  165. }
  166. fail("Should throw BuildException because: " + cause);
  167. }
  168. private class AntOutputStream extends java.io.OutputStream {
  169. public void write(int b) {
  170. outBuffer.append((char)b);
  171. }
  172. }
  173. private class AntTestListener implements BuildListener {
  174. /**
  175. * Fired before any targets are started.
  176. */
  177. public void buildStarted(BuildEvent event) {
  178. }
  179. /**
  180. * Fired after the last target has finished. This event
  181. * will still be thrown if an error occured during the build.
  182. *
  183. * @see BuildEvent#getException()
  184. */
  185. public void buildFinished(BuildEvent event) {
  186. }
  187. /**
  188. * Fired when a target is started.
  189. *
  190. * @see BuildEvent#getTarget()
  191. */
  192. public void targetStarted(BuildEvent event) {
  193. //System.out.println("targetStarted " + event.getTarget().getName());
  194. }
  195. /**
  196. * Fired when a target has finished. This event will
  197. * still be thrown if an error occured during the build.
  198. *
  199. * @see BuildEvent#getException()
  200. */
  201. public void targetFinished(BuildEvent event) {
  202. //System.out.println("targetFinished " + event.getTarget().getName());
  203. }
  204. /**
  205. * Fired when a task is started.
  206. *
  207. * @see BuildEvent#getTask()
  208. */
  209. public void taskStarted(BuildEvent event) {
  210. //System.out.println("taskStarted " + event.getTask().getTaskName());
  211. }
  212. /**
  213. * Fired when a task has finished. This event will still
  214. * be throw if an error occured during the build.
  215. *
  216. * @see BuildEvent#getException()
  217. */
  218. public void taskFinished(BuildEvent event) {
  219. //System.out.println("taskFinished " + event.getTask().getTaskName());
  220. }
  221. /**
  222. * Fired whenever a message is logged.
  223. *
  224. * @see BuildEvent#getMessage()
  225. * @see BuildEvent#getPriority()
  226. */
  227. public void messageLogged(BuildEvent event) {
  228. if (event.getPriority() == Project.MSG_INFO ||
  229. event.getPriority() == Project.MSG_WARN ||
  230. event.getPriority() == Project.MSG_ERR)
  231. {
  232. logBuffer.append(event.getMessage());
  233. }
  234. fullLogBuffer.append(event.getMessage());
  235. }
  236. }
  237. }