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.

JavaTest.java 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.taskdefs;
  55. import junit.framework.*;
  56. import java.io.*;
  57. import org.apache.tools.ant.*;
  58. import org.apache.tools.ant.util.FileUtils;
  59. /**
  60. * stress out java task
  61. * @author steve loughran
  62. * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
  63. * @author <a href="mailto:donal@savvion.com">Donal Quinlan</a>
  64. * @author <a href="mailto:martijn@kruithof.xs4all.nl">Martijn Kruithof</a>
  65. * */
  66. public class JavaTest extends BuildFileTest {
  67. private static final int TIME_TO_WAIT = 4;
  68. // wait 1 second extra to allow for java to start ...
  69. // this time was OK on a Win NT machine and on nagoya
  70. private static final int SECURITY_MARGIN = 1000;
  71. private boolean runFatalTests=false;
  72. public JavaTest(String name) {
  73. super(name);
  74. }
  75. /**
  76. * configure the project.
  77. * if the property junit.run.fatal.tests is set we run
  78. * the fatal tests
  79. */
  80. public void setUp() {
  81. configureProject("src/etc/testcases/taskdefs/java.xml");
  82. //final String propname="tests-classpath.value";
  83. //String testClasspath=System.getProperty(propname);
  84. //System.out.println("Test cp="+testClasspath);
  85. String propname="tests-classpath.value";
  86. String runFatal=System.getProperty("junit.run.fatal.tests");
  87. if(runFatal!=null)
  88. runFatalTests=true;
  89. }
  90. public void tearDown() {
  91. // remove log file from testSpawn
  92. project.executeTarget("cleanup");
  93. }
  94. public void testNoJarNoClassname(){
  95. expectBuildExceptionContaining("testNoJarNoClassname",
  96. "parameter validation",
  97. "Classname must not be null.");
  98. }
  99. public void testJarNoFork() {
  100. expectBuildExceptionContaining("testJarNoFork",
  101. "parameter validation",
  102. "Cannot execute a jar in non-forked mode. "
  103. + "Please set fork='true'. ");
  104. }
  105. public void testJarAndClassName() {
  106. expectBuildException("testJarAndClassName",
  107. "Should not be able to set both classname AND jar");
  108. }
  109. public void testClassnameAndJar() {
  110. expectBuildException("testClassnameAndJar",
  111. "Should not be able to set both classname AND jar");
  112. }
  113. public void testRun() {
  114. executeTarget("testRun");
  115. }
  116. /** this test fails but we ignore the return value;
  117. * we verify that failure only matters when failonerror is set
  118. */
  119. public void testRunFail() {
  120. if(runFatalTests) {
  121. executeTarget("testRunFail");
  122. }
  123. }
  124. public void testRunFailFoe() {
  125. if(runFatalTests) {
  126. expectBuildExceptionContaining("testRunFailFoe",
  127. "java failures being propagated",
  128. "Java returned:");
  129. }
  130. }
  131. public void testRunFailFoeFork() {
  132. expectBuildExceptionContaining("testRunFailFoeFork",
  133. "java failures being propagated",
  134. "Java returned:");
  135. }
  136. public void testExcepting() {
  137. expectLogContaining("testExcepting",
  138. "Exception raised inside called program");
  139. }
  140. public void testExceptingFork() {
  141. expectLogContaining("testExceptingFork",
  142. "Java Result:");
  143. }
  144. public void testExceptingFoe() {
  145. expectBuildExceptionContaining("testExceptingFoe",
  146. "passes exception through",
  147. "Exception raised inside called program");
  148. }
  149. public void testExceptingFoeFork() {
  150. expectBuildExceptionContaining("testExceptingFoeFork",
  151. "exceptions turned into error codes",
  152. "Java returned:");
  153. }
  154. public void testResultPropertyZero() {
  155. executeTarget("testResultPropertyZero");
  156. assertEquals("0",project.getProperty("exitcode"));
  157. }
  158. public void testResultPropertyNonZero() {
  159. executeTarget("testResultPropertyNonZero");
  160. assertEquals("2",project.getProperty("exitcode"));
  161. }
  162. public void testResultPropertyZeroNoFork() {
  163. executeTarget("testResultPropertyZeroNoFork");
  164. assertEquals("0",project.getProperty("exitcode"));
  165. }
  166. public void testResultPropertyNonZeroNoFork() {
  167. executeTarget("testResultPropertyNonZeroNoFork");
  168. assertEquals("-1",project.getProperty("exitcode"));
  169. }
  170. public void testRunFailWithFailOnError() {
  171. expectBuildExceptionContaining("testRunFailWithFailOnError",
  172. "non zero return code",
  173. "Java returned:");
  174. }
  175. public void testRunSuccessWithFailOnError() {
  176. executeTarget("testRunSuccessWithFailOnError");
  177. }
  178. public void testSpawn() {
  179. FileUtils fileutils = FileUtils.newFileUtils();
  180. File logFile = fileutils.createTempFile("spawn","log", project.getBaseDir());
  181. // this is guaranteed by FileUtils#createTempFile
  182. assertTrue("log file not existing", !logFile.exists());
  183. project.setProperty("logFile", logFile.getAbsolutePath());
  184. project.setProperty("timeToWait", Long.toString(TIME_TO_WAIT));
  185. project.executeTarget("testSpawn");
  186. try {
  187. Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN);
  188. } catch (Exception ex) {
  189. System.out.println("my sleep was interrupted");
  190. }
  191. // let's be nice with the next generation of developers
  192. if (!logFile.exists()) {
  193. System.out.println("suggestion: increase the constant"
  194. + " SECURITY_MARGIN to give more time for java to start.");
  195. }
  196. assertTrue("log file exists", logFile.exists());
  197. }
  198. /**
  199. * entry point class with no dependencies other
  200. * than normal JRE runtime
  201. */
  202. public static class EntryPoint {
  203. /**
  204. * this entry point is used by the java.xml tests to
  205. * generate failure strings to handle
  206. * argv[0] = exit code (optional)
  207. * argv[1] = string to print to System.out (optional)
  208. * argv[1] = string to print to System.err (optional)
  209. */
  210. public static void main(String[] argv) {
  211. int exitCode=0;
  212. if(argv.length>0) {
  213. try {
  214. exitCode=Integer.parseInt(argv[0]);
  215. } catch(NumberFormatException nfe) {
  216. exitCode=-1;
  217. }
  218. }
  219. if(argv.length>1) {
  220. System.out.println(argv[1]);
  221. }
  222. if(argv.length>2) {
  223. System.err.println(argv[2]);
  224. }
  225. if(exitCode!=0) {
  226. System.exit(exitCode);
  227. }
  228. }
  229. }
  230. /**
  231. * entry point class with no dependencies other
  232. * than normal JRE runtime
  233. */
  234. public static class ExceptingEntryPoint {
  235. /**
  236. * throw a run time exception which does not need
  237. * to be in the signature of the entry point
  238. */
  239. public static void main(String[] argv) {
  240. throw new NullPointerException("Exception raised inside called program");
  241. }
  242. }
  243. /**
  244. * test class for spawn
  245. */
  246. public static class SpawnEntryPoint {
  247. public static void main(String [] argv) {
  248. int sleepTime = 10;
  249. String logFile = "spawn.log";
  250. if (argv.length >= 1) {
  251. sleepTime = Integer.parseInt(argv[0]);
  252. }
  253. if (argv.length >= 2)
  254. {
  255. logFile = argv[1];
  256. }
  257. OutputStreamWriter out = null;
  258. try {
  259. Thread.sleep(sleepTime * 1000);
  260. } catch (InterruptedException ex) {
  261. System.out.println("my sleep was interrupted");
  262. }
  263. try {
  264. File dest = new File(logFile);
  265. FileOutputStream fos = new FileOutputStream(dest);
  266. out = new OutputStreamWriter(fos);
  267. out.write("bye bye\n");
  268. } catch (Exception ex) {}
  269. finally {
  270. try {out.close();} catch (IOException ioe) {}}
  271. }
  272. }
  273. }