source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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