bodewig 02/04/08 08:40:33
Modified: src/etc/testcases/taskdefs java.xml
src/main/org/apache/tools/ant/taskdefs ExecuteJava.java
Java.java
src/main/org/apache/tools/ant/taskdefs/optional
XMLValidateTask.java
src/testcases/org/apache/tools/ant/taskdefs JavaTest.java
Log:
Make <java> handle exceptions gracefully if failonerror="true" in
non-forked mode as well.
PR: 6353
Revision Changes Path
1.3 +7 -0 jakarta-ant/src/etc/testcases/taskdefs/java.xml
Index: java.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/src/etc/testcases/taskdefs/java.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- java.xml 11 Feb 2002 02:46:44 -0000 1.2
+++ java.xml 8 Apr 2002 15:40:33 -0000 1.3
@@ -67,6 +67,13 @@
</java>
</target>
+ <target name="testExceptingFork">
+ <java classname="${app2}"
+ classpath="${tests-classpath.value}"
+ fork="true">
+ </java>
+ </target>
+
<target name="testExceptingFoe">
<java classname="${app2}"
classpath="${tests-classpath.value}"
1.23 +7 -3
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java
Index: ExecuteJava.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecuteJava.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- ExecuteJava.java 7 Apr 2002 14:35:22 -0000 1.22
+++ ExecuteJava.java 8 Apr 2002 15:40:33 -0000 1.23
@@ -134,6 +134,10 @@
AntClassLoader.initializeClass(target);
}
main = target.getMethod("main", param);
+ if (main == null) {
+ throw new BuildException("Could not find main() method in "
+ + classname);
+ }
if (timeout == null) {
run();
@@ -169,10 +173,10 @@
throw caught;
}
- } catch (NullPointerException e) {
- throw new BuildException("Could not find main() method in " +
classname);
} catch (ClassNotFoundException e) {
- throw new BuildException("Could not find " + classname + ". Make
sure you have it in your classpath");
+ throw new BuildException("Could not find " + classname + "."
+ + " Make sure you have it in your"
+ + " classpath");
} catch (SecurityException e) {
throw e;
} catch (Throwable e) {
1.38 +37 -14
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java
Index: Java.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- Java.java 5 Apr 2002 12:56:34 -0000 1.37
+++ Java.java 8 Apr 2002 15:40:33 -0000 1.38
@@ -98,7 +98,8 @@
*/
public void execute() throws BuildException {
int err = -1;
- if ((err = executeJava()) != 0) {
+
+ if ((err = executeJava()) != 0) {
if (failOnError) {
throw new BuildException("Java returned: "+err, location);
} else {
@@ -110,43 +111,65 @@
/**
* Do the execution and return a return code.
*
- * @return the return code from the execute java class if it was
executed in
- * a separate VM (fork = "yes").
+ * @return the return code from the execute java class if it was
+ * executed in a separate VM (fork = "yes").
*/
public int executeJava() throws BuildException {
String classname = cmdl.getClassname();
if (classname == null && cmdl.getJar() == null) {
throw new BuildException("Classname must not be null.");
}
+
if (!fork && cmdl.getJar() != null){
- throw new BuildException("Cannot execute a jar in non-forked
mode. Please set fork='true'. ");
+ throw new BuildException("Cannot execute a jar in non-forked
mode."
+ + " Please set fork='true'. ");
}
if (fork) {
log("Forking " + cmdl.toString(), Project.MSG_VERBOSE);
-
- return run(cmdl.getCommandline());
} else {
if (cmdl.getVmCommand().size() > 1) {
- log("JVM args ignored when same JVM is used.",
Project.MSG_WARN);
+ log("JVM args ignored when same JVM is used.",
+ Project.MSG_WARN);
}
if (dir != null) {
- log("Working directory ignored when same JVM is used.",
Project.MSG_WARN);
+ log("Working directory ignored when same JVM is used.",
+ Project.MSG_WARN);
}
if (newEnvironment || null != env.getVariables()) {
- log("Changes to environment variables are ignored when same
JVM is used.",
- Project.MSG_WARN);
+ log("Changes to environment variables are ignored when same "
+ + "JVM is used.", Project.MSG_WARN);
}
log("Running in same VM " + cmdl.getJavaCommand().toString(),
Project.MSG_VERBOSE);
- try {
- run(cmdl);
+ }
+
+ try {
+ if (fork) {
+ return run(cmdl.getCommandline());
+ } else {
+ try {
+ run(cmdl);
+ return 0;
+ } catch (ExitException ex) {
+ return ex.getStatus();
+ }
+ }
+ } catch (BuildException e) {
+ if (failOnError) {
+ throw e;
+ } else {
+ log(e.getMessage(), Project.MSG_ERR);
return 0;
}
- catch (ExitException ex) {
- return ex.getStatus();
+ } catch (Throwable t) {
+ if (failOnError) {
+ throw new BuildException(t);
+ } else {
+ log(t.getMessage(), Project.MSG_ERR);
+ return 0;
}
}
}
1.12 +1 -1
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
Index: XMLValidateTask.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- XMLValidateTask.java 8 Apr 2002 15:28:58 -0000 1.11
+++ XMLValidateTask.java 8 Apr 2002 15:40:33 -0000 1.12
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
1.6 +10 -4
jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java
Index: JavaTest.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/JavaTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- JavaTest.java 14 Feb 2002 09:30:30 -0000 1.5
+++ JavaTest.java 8 Apr 2002 15:40:33 -0000 1.6
@@ -144,13 +144,19 @@
}
public void testExcepting() {
- executeTarget("testExcepting");
+ expectLogContaining("testExcepting",
+ "Exception raised inside called program");
+ }
+
+ public void testExceptingFork() {
+ expectLogContaining("testExceptingFork",
+ "Java Result:");
}
public void testExceptingFoe() {
- //if(runFatalTests) {
- executeTarget("testExceptingFoe");
- //}
+ expectBuildExceptionContaining("testExceptingFoe",
+ "passes exception through",
+ "Exception raised inside called program");
}
public void testExceptingFoeFork() {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>