001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026:
027: import org.apache.tools.ant.BuildFileTest;
028: import org.apache.tools.ant.util.FileUtils;
029: import org.apache.tools.ant.util.TeeOutputStream;
030:
031: /**
032: * stress out java task
033: * */
034: public class JavaTest extends BuildFileTest {
035:
036: private static final int TIME_TO_WAIT = 1;
037: // wait 1 second extra to allow for java to start ...
038: // this time was OK on a Win NT machine and on nagoya
039: private static final int SECURITY_MARGIN = 2000;
040:
041: /** Utilities used for file operations */
042: private static final FileUtils FILE_UTILS = FileUtils
043: .getFileUtils();
044:
045: private boolean runFatalTests = false;
046:
047: public JavaTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * configure the project.
053: * if the property junit.run.fatal.tests is set we run
054: * the fatal tests
055: */
056: public void setUp() {
057: configureProject("src/etc/testcases/taskdefs/java.xml");
058:
059: //final String propname="tests-classpath.value";
060: //String testClasspath=System.getProperty(propname);
061: //System.out.println("Test cp="+testClasspath);
062: String propname = "tests-classpath.value";
063: String runFatal = System.getProperty("junit.run.fatal.tests");
064: if (runFatal != null)
065: runFatalTests = true;
066: }
067:
068: public void tearDown() {
069: // remove log file from testSpawn
070: project.executeTarget("cleanup");
071: }
072:
073: public void testNoJarNoClassname() {
074: expectBuildExceptionContaining("testNoJarNoClassname",
075: "parameter validation", "Classname must not be null.");
076: }
077:
078: public void testJarNoFork() {
079: expectBuildExceptionContaining("testJarNoFork",
080: "parameter validation",
081: "Cannot execute a jar in non-forked mode. "
082: + "Please set fork='true'. ");
083: }
084:
085: public void testJarAndClassName() {
086: expectBuildException("testJarAndClassName",
087: "Should not be able to set both classname AND jar");
088: }
089:
090: public void testClassnameAndJar() {
091: expectBuildException("testClassnameAndJar",
092: "Should not be able to set both classname AND jar");
093: }
094:
095: public void testRun() {
096: executeTarget("testRun");
097: }
098:
099: /** this test fails but we ignore the return value;
100: * we verify that failure only matters when failonerror is set
101: */
102: public void testRunFail() {
103: if (runFatalTests) {
104: executeTarget("testRunFail");
105: }
106: }
107:
108: public void testRunFailFoe() {
109: if (runFatalTests) {
110: expectBuildExceptionContaining("testRunFailFoe",
111: "java failures being propagated", "Java returned:");
112: }
113: }
114:
115: public void testRunFailFoeFork() {
116: expectBuildExceptionContaining("testRunFailFoeFork",
117: "java failures being propagated", "Java returned:");
118: }
119:
120: public void testExcepting() {
121: expectLogContaining("testExcepting",
122: "Exception raised inside called program");
123: }
124:
125: public void testExceptingFork() {
126: expectLogContaining("testExceptingFork", "Java Result:");
127: }
128:
129: public void testExceptingFoe() {
130: expectBuildExceptionContaining("testExceptingFoe",
131: "passes exception through",
132: "Exception raised inside called program");
133: }
134:
135: public void testExceptingFoeFork() {
136: expectBuildExceptionContaining("testExceptingFoeFork",
137: "exceptions turned into error codes", "Java returned:");
138: }
139:
140: public void testResultPropertyZero() {
141: executeTarget("testResultPropertyZero");
142: assertEquals("0", project.getProperty("exitcode"));
143: }
144:
145: public void testResultPropertyNonZero() {
146: executeTarget("testResultPropertyNonZero");
147: assertEquals("2", project.getProperty("exitcode"));
148: }
149:
150: public void testResultPropertyZeroNoFork() {
151: executeTarget("testResultPropertyZeroNoFork");
152: assertEquals("0", project.getProperty("exitcode"));
153: }
154:
155: public void testResultPropertyNonZeroNoFork() {
156: executeTarget("testResultPropertyNonZeroNoFork");
157: assertEquals("-1", project.getProperty("exitcode"));
158: }
159:
160: public void testRunFailWithFailOnError() {
161: expectBuildExceptionContaining("testRunFailWithFailOnError",
162: "non zero return code", "Java returned:");
163: }
164:
165: public void testRunSuccessWithFailOnError() {
166: executeTarget("testRunSuccessWithFailOnError");
167: }
168:
169: public void testSpawn() {
170: File logFile = FILE_UTILS.createTempFile("spawn", "log",
171: 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
185: .println("suggestion: increase the constant"
186: + " SECURITY_MARGIN to give more time for java to start.");
187: }
188: assertTrue("log file exists", logFile.exists());
189: }
190:
191: public void testRedirect1() {
192: executeTarget("redirect1");
193: }
194:
195: public void testRedirect2() {
196: executeTarget("redirect2");
197: }
198:
199: public void testRedirect3() {
200: executeTarget("redirect3");
201: }
202:
203: public void testRedirector1() {
204: executeTarget("redirector1");
205: }
206:
207: public void testRedirector2() {
208: executeTarget("redirector2");
209: }
210:
211: /**
212: * entry point class with no dependencies other
213: * than normal JRE runtime
214: */
215: public static class EntryPoint {
216:
217: /**
218: * this entry point is used by the java.xml tests to
219: * generate failure strings to handle
220: * argv[0] = exit code (optional)
221: * argv[1] = string to print to System.out (optional)
222: * argv[1] = string to print to System.err (optional)
223: */
224: public static void main(String[] argv) {
225: int exitCode = 0;
226: if (argv.length > 0) {
227: try {
228: exitCode = Integer.parseInt(argv[0]);
229: } catch (NumberFormatException nfe) {
230: exitCode = -1;
231: }
232: }
233: if (argv.length > 1) {
234: System.out.println(argv[1]);
235: }
236: if (argv.length > 2) {
237: System.err.println(argv[2]);
238: }
239: if (exitCode != 0) {
240: System.exit(exitCode);
241: }
242: }
243: }
244:
245: /**
246: * entry point class with no dependencies other
247: * than normal JRE runtime
248: */
249: public static class ExceptingEntryPoint {
250:
251: /**
252: * throw a run time exception which does not need
253: * to be in the signature of the entry point
254: */
255: public static void main(String[] argv) {
256: throw new NullPointerException(
257: "Exception raised inside called program");
258: }
259: }
260:
261: /**
262: * test class for spawn
263: */
264: public static class SpawnEntryPoint {
265: public static void main(String[] argv) {
266: int sleepTime = 10;
267: String logFile = "spawn.log";
268: if (argv.length >= 1) {
269: sleepTime = Integer.parseInt(argv[0]);
270: }
271: if (argv.length >= 2) {
272: logFile = argv[1];
273: }
274: OutputStreamWriter out = null;
275: try {
276: Thread.sleep(sleepTime * 1000);
277: } catch (InterruptedException ex) {
278: System.out.println("my sleep was interrupted");
279: }
280:
281: try {
282: File dest = new File(logFile);
283: FileOutputStream fos = new FileOutputStream(dest);
284: out = new OutputStreamWriter(fos);
285: out.write("bye bye\n");
286: } catch (Exception ex) {
287: } finally {
288: try {
289: out.close();
290: } catch (IOException ioe) {
291: }
292: }
293:
294: }
295: }
296:
297: /**
298: * entry point class to pipe System.in to the specified stream:
299: * "out", "err", or "both". If none specified, swallow the input.
300: */
301: public static class PipeEntryPoint {
302:
303: /**
304: * pipe input to specified output
305: */
306: public static void main(String[] args) {
307: OutputStream os = null;
308: if (args.length > 0) {
309: if ("out".equalsIgnoreCase(args[0])) {
310: os = System.out;
311: } else if ("err".equalsIgnoreCase(args[0])) {
312: os = System.err;
313: } else if ("both".equalsIgnoreCase(args[0])) {
314: os = new TeeOutputStream(System.out, System.err);
315: }
316: }
317: if (os != null) {
318: Thread t = new Thread(new StreamPumper(System.in, os,
319: true));
320: t.start();
321: try {
322: t.join();
323: } catch (InterruptedException eyeEx) {
324: }
325: }
326: }
327: }
328: }
|