bodewig 2004/04/19 23:53:30 Modified: . Tag: ANT_16_BRANCH WHATSNEW src/main/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH Java.java src/main/org/apache/tools/ant/taskdefs/optional/junit Tag: ANT_16_BRANCH JUnitTask.java src/main/org/apache/tools/ant/types Tag: ANT_16_BRANCH Assertions.java CommandlineJava.java src/testcases/org/apache/tools/ant/types Tag: ANT_16_BRANCH CommandlineJavaTest.java Log: merge Revision Changes Path No revision No revision 1.503.2.72 +2 -0 ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/ant/WHATSNEW,v retrieving revision 1.503.2.71 retrieving revision 1.503.2.72 diff -u -r1.503.2.71 -r1.503.2.72 --- WHATSNEW 16 Apr 2004 09:35:59 -0000 1.503.2.71 +++ WHATSNEW 20 Apr 2004 06:53:29 -0000 1.503.2.72 @@ -49,6 +49,8 @@ * <zip> and friends would delete the original file when trying to update a read-only archive. Bugzilla Report 28419. +* <junit> and <assertions> are working together. Bugzilla report 27218 + Other changes: -------------- No revision No revision 1.77.2.9 +2 -2 ant/src/main/org/apache/tools/ant/taskdefs/Java.java Index: Java.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v retrieving revision 1.77.2.8 retrieving revision 1.77.2.9 diff -u -r1.77.2.8 -r1.77.2.9 --- Java.java 15 Mar 2004 11:29:13 -0000 1.77.2.8 +++ Java.java 20 Apr 2004 06:53:29 -0000 1.77.2.9 @@ -388,7 +388,7 @@ */ public void setFailonerror(boolean fail) { failOnError = fail; - incompatibleWithSpawn = true; + incompatibleWithSpawn |= fail; } /** @@ -545,7 +545,7 @@ */ public void setTimeout(Long value) { timeout = value; - incompatibleWithSpawn = true; + incompatibleWithSpawn |= timeout != null; } /** No revision No revision 1.83.2.11 +76 -19 ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java Index: JUnitTask.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v retrieving revision 1.83.2.10 retrieving revision 1.83.2.11 diff -u -r1.83.2.10 -r1.83.2.11 --- JUnitTask.java 16 Apr 2004 07:05:32 -0000 1.83.2.10 +++ JUnitTask.java 20 Apr 2004 06:53:29 -0000 1.83.2.11 @@ -277,7 +277,7 @@ } /** - * Set the bahvior when [EMAIL PROTECTED] #setFork fork} fork has been enabled. + * Set the behavior when [EMAIL PROTECTED] #setFork fork} fork has been enabled. * * <p>Possible values are "once", "perTest" and "perBatch". If * set to "once", only a single Java VM will be forked for all @@ -595,6 +595,12 @@ * @since Ant 1.6 */ public void setTempdir(File tmpDir) { + if (tmpDir!=null) { + if (!tmpDir.exists() || !tmpDir.isDirectory()) { + throw new BuildException(tmpDir.toString() + +" is not a valid temp directory"); + } + } this.tmpDir = tmpDir; } @@ -686,11 +692,7 @@ JUnitTest test = null; // Create a temporary file to pass the test cases to run to // the runner (one test case per line) - File casesFile = - FileUtils.newFileUtils().createTempFile("junittestcases", - ".properties", - getProject().getBaseDir()); - casesFile.deleteOnExit(); + File casesFile = createTempPropertiesFile("junittestcases"); PrintWriter writer = null; try { writer = @@ -821,12 +823,8 @@ } } - // Create a temporary file to pass the Ant properties to the - // forked test - File propsFile = - FileUtils.newFileUtils().createTempFile("junit", ".properties", - tmpDir != null ? tmpDir : getProject().getBaseDir()); - propsFile.deleteOnExit(); + + File propsFile = createTempPropertiesFile("junit"); cmd.createArgument().setValue("propsfile=" + propsFile.getAbsolutePath()); Hashtable p = getProject().getProperties(); @@ -885,6 +883,22 @@ return retVal; } + /** + * Create a temporary file to pass the properties to a new process. + * Will auto-delete on (graceful) exit. + * The file will be in the project basedir unless tmpDir declares + * something else. + * @param prefix + * @return + */ + private File createTempPropertiesFile(String prefix) { + File propsFile = + FileUtils.newFileUtils().createTempFile(prefix, ".properties", + tmpDir != null ? tmpDir : getProject().getBaseDir()); + propsFile.deleteOnExit(); + return propsFile; + } + /** * Pass output sent to System.out to the TestRunner so it can @@ -1067,7 +1081,7 @@ if (timeout == null) { return null; } - return new ExecuteWatchdog(timeout.intValue()); + return new ExecuteWatchdog((long) timeout.intValue()); } /** @@ -1216,6 +1230,10 @@ OutputStream out) { formatter.setOutput(out); formatter.startTestSuite(test); + + //the trick to integrating test output to the formatter, is to + //create a special test class that asserts a timout occurred, + //and tell the formatter that it raised. Test t = new Test() { public int countTestCases() { return 1; } public void run(TestResult r) { @@ -1268,6 +1286,7 @@ } /** + * Forked test support * @since Ant 1.6.2 */ private final class ForkedTestConfiguration { @@ -1276,6 +1295,15 @@ private boolean haltOnFailure; private String errorProperty; private String failureProperty; + + /** + * constructor for forked test configuration + * @param filterTrace + * @param haltOnError + * @param haltOnFailure + * @param errorProperty + * @param failureProperty + */ ForkedTestConfiguration(boolean filterTrace, boolean haltOnError, boolean haltOnFailure, String errorProperty, String failureProperty) { @@ -1286,6 +1314,23 @@ this.failureProperty = failureProperty; } + /** + * configure from a test; sets member variables to attributes of the test + * @param test + */ + ForkedTestConfiguration(JUnitTest test) { + this(test.getFiltertrace(), + test.getHaltonerror(), + test.getHaltonfailure(), + test.getErrorProperty(), + test.getFailureProperty()); + } + + /** + * equality test checks all the member variables + * @param other + * @return true if everything is equal + */ public boolean equals(Object other) { if (other == null || other.getClass() != ForkedTestConfiguration.class) { @@ -1305,6 +1350,11 @@ && failureProperty.equals(o.failureProperty))); } + /** + * hashcode is based only on the boolean members, and returns a value + * in the range 0-7. + * @return + */ public int hashCode() { return (filterTrace ? 1 : 0) + (haltOnError ? 2 : 0) @@ -1313,11 +1363,22 @@ } /** + * These are the different forking options * @since 1.6.2 */ public static final class ForkStyle extends EnumeratedAttribute { + + /** + * fork once only + */ public static final String ONCE = "once"; + /** + * fork once per test class + */ public static final String PER_TEST = "perTest"; + /** + * fork once per batch of tests + */ public static final String PER_BATCH = "perBatch"; public ForkStyle() { @@ -1352,11 +1413,7 @@ execute(test); } else { ForkedTestConfiguration c = - new ForkedTestConfiguration(test.getFiltertrace(), - test.getHaltonerror(), - test.getHaltonfailure(), - test.getErrorProperty(), - test.getFailureProperty()); + new ForkedTestConfiguration(test); List l = (List) testConfigurations.get(c); if (l == null) { l = new ArrayList(); No revision No revision 1.6.2.7 +32 -4 ant/src/main/org/apache/tools/ant/types/Assertions.java Index: Assertions.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/Assertions.java,v retrieving revision 1.6.2.6 retrieving revision 1.6.2.7 diff -u -r1.6.2.6 -r1.6.2.7 --- Assertions.java 14 Apr 2004 15:42:41 -0000 1.6.2.6 +++ Assertions.java 20 Apr 2004 06:53:29 -0000 1.6.2.7 @@ -20,15 +20,16 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; -import java.util.List; import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; /** * The assertion datatype. This type describes * assertion settings for the <java> task and others. * One can set the system assertions, and enable/disable those in - * packages & classes. + * packages and classes. * Assertions can only be enabled or disabled when forking Java. * * Example: set system assertions and all org.apache packages except @@ -64,7 +65,7 @@ * </pre> * @since Ant 1.6 */ -public class Assertions extends DataType { +public class Assertions extends DataType implements Cloneable { /** * enable/disable sys assertions; null means undefined @@ -203,6 +204,33 @@ } /** + * add the assertions to a list in a format suitable + * for adding to a command line + * @param commandList + */ + public void applyAssertions(final ListIterator commandIterator) { + getProject().log("Applying assertions", Project.MSG_DEBUG); + Assertions clause = getFinalReference(); + //do the system assertions + if (Boolean.TRUE.equals(clause.enableSystemAssertions)) { + getProject().log("Enabling system assertions", Project.MSG_DEBUG); + commandIterator.add("-enablesystemassertions"); + } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) { + getProject().log("disabling system assertions", Project.MSG_DEBUG); + commandIterator.add("-disablesystemassertions"); + } + + //now any inner assertions + Iterator it = clause.assertionList.iterator(); + while (it.hasNext()) { + BaseAssertion assertion = (BaseAssertion) it.next(); + String arg = assertion.toCommand(); + getProject().log("adding assertion "+arg, Project.MSG_DEBUG); + commandIterator.add(arg); + } + } + + /** * helper method to add a string JVM argument to a command * @param command * @param arg @@ -220,7 +248,7 @@ * @return a cli * @throws CloneNotSupportedException */ - protected Object clone() throws CloneNotSupportedException { + public Object clone() throws CloneNotSupportedException { Assertions that = (Assertions) super.clone(); that.assertionList = (ArrayList) assertionList.clone(); return that; 1.47.2.6 +7 -5 ant/src/main/org/apache/tools/ant/types/CommandlineJava.java Index: CommandlineJava.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/CommandlineJava.java,v retrieving revision 1.47.2.5 retrieving revision 1.47.2.6 diff -u -r1.47.2.5 -r1.47.2.6 --- CommandlineJava.java 14 Apr 2004 15:42:41 -0000 1.47.2.5 +++ CommandlineJava.java 20 Apr 2004 06:53:29 -0000 1.47.2.6 @@ -361,6 +361,7 @@ if (haveBootclasspath(true)) { listIterator.add("-Xbootclasspath:" + bootclasspath.toString()); } + //main classpath if (haveClasspath()) { listIterator.add("-classpath"); @@ -370,7 +371,7 @@ //now any assertions are added if (getAssertions() != null) { - getAssertions().applyAssertions(this); + getAssertions().applyAssertions(listIterator); } // JDK usage command line says that -jar must be the first option, as there is @@ -429,7 +430,7 @@ * Get the VM command parameters, including memory settings * @return the VM command parameters */ - private Commandline getActualVMCommand() { + protected Commandline getActualVMCommand() { Commandline actualVMCommand = (Commandline) vmCommand.clone(); if (maxMemory != null) { if (vmVersion.startsWith("1.1")) { @@ -449,7 +450,8 @@ * @deprecated please dont use this -it effectively creates the entire command. */ public int size() { - int size = getActualVMCommand().size() + javaCommand.size() + sysProperties.size(); + int size = getActualVMCommand().size() + javaCommand.size() + + sysProperties.size(); // classpath is "-classpath <classpath>" -> 2 args if (haveClasspath()) { size += 2; @@ -564,7 +566,7 @@ * * @since Ant 1.6 */ - private boolean haveClasspath() { + protected boolean haveClasspath() { Path fullClasspath = classpath != null ? classpath.concatSystemClasspath("ignore") : null; return fullClasspath != null @@ -581,7 +583,7 @@ * * @since Ant 1.6 */ - private boolean haveBootclasspath(boolean log) { + protected boolean haveBootclasspath(boolean log) { if (bootclasspath != null && bootclasspath.toString().trim().length() > 0) { No revision No revision 1.17.2.6 +42 -0 ant/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java Index: CommandlineJavaTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java,v retrieving revision 1.17.2.5 retrieving revision 1.17.2.6 diff -u -r1.17.2.5 -r1.17.2.6 --- CommandlineJavaTest.java 9 Mar 2004 17:02:07 -0000 1.17.2.5 +++ CommandlineJavaTest.java 20 Apr 2004 06:53:30 -0000 1.17.2.6 @@ -18,6 +18,7 @@ package org.apache.tools.ant.types; import org.apache.tools.ant.Project; +import org.apache.tools.ant.util.JavaEnvUtils; import junit.framework.TestCase; import junit.framework.AssertionFailedError; @@ -128,6 +129,47 @@ } assertNull(System.getProperty("key")); assertNull(System.getProperty("key2")); + } + + public void testAssertions() { + if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2) + || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) { + return; + } + + CommandlineJava c = new CommandlineJava(); + c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest"); + c.setClassname("junit.textui.TestRunner"); + c.createVmArgument().setValue("-Djava.compiler=NONE"); + Assertions a = new Assertions(); + a.setProject(project); + Assertions.EnabledAssertion ea = new Assertions.EnabledAssertion(); + ea.setClass("junit.textui.TestRunner"); + a.addEnable(ea); + c.setAssertions(a); + + String[] expected = new String[] { + null, + "-Djava.compiler=NONE", + "-ea:junit.textui.TestRunner", + "junit.textui.TestRunner", + "org.apache.tools.ant.CommandlineJavaTest", + }; + + // only the second iteration would pass because of PR 27218 + for (int i = 0; i < 3; i++) { + String[] s = c.getCommandline(); + assertEquals(expected.length, s.length); + for (int j = 1; j < expected.length; j++) { + assertEquals(expected[j], s[j]); + } + } + CommandlineJava c2 = (CommandlineJava) c.clone(); + String[] s = c2.getCommandline(); + assertEquals(expected.length, s.length); + for (int j = 1; j < expected.length; j++) { + assertEquals(expected[j], s[j]); + } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]