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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import junit.framework.*;
  19. import java.io.*;
  20. import org.apache.tools.ant.*;
  21. import org.apache.tools.ant.util.FileUtils;
  22. import org.apache.tools.ant.util.TeeOutputStream;
  23. /**
  24. * stress out java task
  25. * */
  26. public class JavaTest extends BuildFileTest {
  27. private static final int TIME_TO_WAIT = 1;
  28. // wait 1 second extra to allow for java to start ...
  29. // this time was OK on a Win NT machine and on nagoya
  30. private static final int SECURITY_MARGIN = 2000;
  31. private boolean runFatalTests=false;
  32. public JavaTest(String name) {
  33. super(name);
  34. }
  35. /**
  36. * configure the project.
  37. * if the property junit.run.fatal.tests is set we run
  38. * the fatal tests
  39. */
  40. public void setUp() {
  41. configureProject("src/etc/testcases/taskdefs/java.xml");
  42. //final String propname="tests-classpath.value";
  43. //String testClasspath=System.getProperty(propname);
  44. //System.out.println("Test cp="+testClasspath);
  45. String propname="tests-classpath.value";
  46. String runFatal=System.getProperty("junit.run.fatal.tests");
  47. if(runFatal!=null)
  48. runFatalTests=true;
  49. }
  50. public void tearDown() {
  51. // remove log file from testSpawn
  52. project.executeTarget("cleanup");
  53. }
  54. public void testNoJarNoClassname(){
  55. expectBuildExceptionContaining("testNoJarNoClassname",
  56. "parameter validation",
  57. "Classname must not be null.");
  58. }
  59. public void testJarNoFork() {
  60. expectBuildExceptionContaining("testJarNoFork",
  61. "parameter validation",
  62. "Cannot execute a jar in non-forked mode. "
  63. + "Please set fork='true'. ");
  64. }
  65. public void testJarAndClassName() {
  66. expectBuildException("testJarAndClassName",
  67. "Should not be able to set both classname AND jar");
  68. }
  69. public void testClassnameAndJar() {
  70. expectBuildException("testClassnameAndJar",
  71. "Should not be able to set both classname AND jar");
  72. }
  73. public void testRun() {
  74. executeTarget("testRun");
  75. }
  76. /** this test fails but we ignore the return value;
  77. * we verify that failure only matters when failonerror is set
  78. */
  79. public void testRunFail() {
  80. if(runFatalTests) {
  81. executeTarget("testRunFail");
  82. }
  83. }
  84. public void testRunFailFoe() {
  85. if(runFatalTests) {
  86. expectBuildExceptionContaining("testRunFailFoe",
  87. "java failures being propagated",
  88. "Java returned:");
  89. }
  90. }
  91. public void testRunFailFoeFork() {
  92. expectBuildExceptionContaining("testRunFailFoeFork",
  93. "java failures being propagated",
  94. "Java returned:");
  95. }
  96. public void testExcepting() {
  97. expectLogContaining("testExcepting",
  98. "Exception raised inside called program");
  99. }
  100. public void testExceptingFork() {
  101. expectLogContaining("testExceptingFork",
  102. "Java Result:");
  103. }
  104. public void testExceptingFoe() {
  105. expectBuildExceptionContaining("testExceptingFoe",
  106. "passes exception through",
  107. "Exception raised inside called program");
  108. }
  109. public void testExceptingFoeFork() {
  110. expectBuildExceptionContaining("testExceptingFoeFork",
  111. "exceptions turned into error codes",
  112. "Java returned:");
  113. }
  114. public void testResultPropertyZero() {
  115. executeTarget("testResultPropertyZero");
  116. assertEquals("0",project.getProperty("exitcode"));
  117. }
  118. public void testResultPropertyNonZero() {
  119. executeTarget("testResultPropertyNonZero");
  120. assertEquals("2",project.getProperty("exitcode"));
  121. }
  122. public void testResultPropertyZeroNoFork() {
  123. executeTarget("testResultPropertyZeroNoFork");
  124. assertEquals("0",project.getProperty("exitcode"));
  125. }
  126. public void testResultPropertyNonZeroNoFork() {
  127. executeTarget("testResultPropertyNonZeroNoFork");
  128. assertEquals("-1",project.getProperty("exitcode"));
  129. }
  130. public void testRunFailWithFailOnError() {
  131. expectBuildExceptionContaining("testRunFailWithFailOnError",
  132. "non zero return code",
  133. "Java returned:");
  134. }
  135. public void testRunSuccessWithFailOnError() {
  136. executeTarget("testRunSuccessWithFailOnError");
  137. }
  138. public void testSpawn() {
  139. FileUtils fileutils = FileUtils.newFileUtils();
  140. File logFile = fileutils.createTempFile("spawn","log", project.getBaseDir());
  141. // this is guaranteed by FileUtils#createTempFile
  142. assertTrue("log file not existing", !logFile.exists());
  143. project.setProperty("logFile", logFile.getAbsolutePath());
  144. project.setProperty("timeToWait", Long.toString(TIME_TO_WAIT));
  145. project.executeTarget("testSpawn");
  146. try {
  147. Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN);
  148. } catch (Exception ex) {
  149. System.out.println("my sleep was interrupted");
  150. }
  151. // let's be nice with the next generation of developers
  152. if (!logFile.exists()) {
  153. System.out.println("suggestion: increase the constant"
  154. + " SECURITY_MARGIN to give more time for java to start.");
  155. }
  156. assertTrue("log file exists", logFile.exists());
  157. }
  158. public void testRedirect1() {
  159. executeTarget("redirect1");
  160. }
  161. public void testRedirect2() {
  162. executeTarget("redirect2");
  163. }
  164. public void testRedirect3() {
  165. executeTarget("redirect3");
  166. }
  167. public void testRedirector1() {
  168. executeTarget("redirector1");
  169. }
  170. public void testRedirector2() {
  171. executeTarget("redirector2");
  172. }
  173. /**
  174. * entry point class with no dependencies other
  175. * than normal JRE runtime
  176. */
  177. public static class EntryPoint {
  178. /**
  179. * this entry point is used by the java.xml tests to
  180. * generate failure strings to handle
  181. * argv[0] = exit code (optional)
  182. * argv[1] = string to print to System.out (optional)
  183. * argv[1] = string to print to System.err (optional)
  184. */
  185. public static void main(String[] argv) {
  186. int exitCode=0;
  187. if(argv.length>0) {
  188. try {
  189. exitCode=Integer.parseInt(argv[0]);
  190. } catch(NumberFormatException nfe) {
  191. exitCode=-1;
  192. }
  193. }
  194. if(argv.length>1) {
  195. System.out.println(argv[1]);
  196. }
  197. if(argv.length>2) {
  198. System.err.println(argv[2]);
  199. }
  200. if(exitCode!=0) {
  201. System.exit(exitCode);
  202. }
  203. }
  204. }
  205. /**
  206. * entry point class with no dependencies other
  207. * than normal JRE runtime
  208. */
  209. public static class ExceptingEntryPoint {
  210. /**
  211. * throw a run time exception which does not need
  212. * to be in the signature of the entry point
  213. */
  214. public static void main(String[] argv) {
  215. throw new NullPointerException("Exception raised inside called program");
  216. }
  217. }
  218. /**
  219. * test class for spawn
  220. */
  221. public static class SpawnEntryPoint {
  222. public static void main(String [] argv) {
  223. int sleepTime = 10;
  224. String logFile = "spawn.log";
  225. if (argv.length >= 1) {
  226. sleepTime = Integer.parseInt(argv[0]);
  227. }
  228. if (argv.length >= 2)
  229. {
  230. logFile = argv[1];
  231. }
  232. OutputStreamWriter out = null;
  233. try {
  234. Thread.sleep(sleepTime * 1000);
  235. } catch (InterruptedException ex) {
  236. System.out.println("my sleep was interrupted");
  237. }
  238. try {
  239. File dest = new File(logFile);
  240. FileOutputStream fos = new FileOutputStream(dest);
  241. out = new OutputStreamWriter(fos);
  242. out.write("bye bye\n");
  243. } catch (Exception ex) {}
  244. finally {
  245. try {out.close();} catch (IOException ioe) {}}
  246. }
  247. }
  248. /**
  249. * entry point class to pipe System.in to the specified stream:
  250. * "out", "err", or "both". If none specified, swallow the input.
  251. */
  252. public static class PipeEntryPoint {
  253. /**
  254. * pipe input to specified output
  255. */
  256. public static void main(String[] args) {
  257. OutputStream os = null;
  258. if (args.length > 0) {
  259. if ("out".equalsIgnoreCase(args[0])) {
  260. os = System.out;
  261. } else if ("err".equalsIgnoreCase(args[0])) {
  262. os = System.err;
  263. } else if ("both".equalsIgnoreCase(args[0])) {
  264. os = new TeeOutputStream(System.out, System.err);
  265. }
  266. }
  267. if (os != null) {
  268. Thread t = new Thread(new StreamPumper(System.in, os, true));
  269. t.start();
  270. try {
  271. t.join();
  272. } catch (InterruptedException eyeEx) {
  273. }
  274. }
  275. }
  276. }
  277. }