FileDocCategorySizeDatePackage
ExecuteJavaTest.javaAPI DocApache Ant 1.704901Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

ExecuteJavaTest

public class ExecuteJavaTest extends TestCase
Simple testcase for the ExecuteJava class - mostly stolen from ExecuteWatchdogTest.

Fields Summary
private static final int
TIME_OUT
private static final int
CLOCK_ERROR
private static final int
TIME_OUT_TEST
private ExecuteJava
ej
private org.apache.tools.ant.Project
project
private org.apache.tools.ant.types.Path
cp
Constructors Summary
public ExecuteJavaTest(String name)


       
        super(name);
    
Methods Summary
private org.apache.tools.ant.types.CommandlinegetCommandline(int timetorun)

        Commandline cmd = new Commandline();
        cmd.setExecutable(TimeProcess.class.getName());
        cmd.createArgument().setValue(String.valueOf(timetorun));
        return cmd;
    
private static java.lang.StringgetTestClassPath()
Dangerous method to obtain the classpath for the test. This is severely tighted to the build.xml properties.

        String classpath = System.getProperty("build.tests");
        if (classpath == null) {
            System.err.println("WARNING: 'build.tests' property is not available !");
            classpath = System.getProperty("java.class.path");
        }

        return classpath;
    
protected voidsetUp()

        ej = new ExecuteJava();
        ej.setTimeout(new Long(TIME_OUT));
        project = new Project();
        project.setBasedir(".");
        project.setProperty(MagicNames.ANT_HOME, System.getProperty(MagicNames.ANT_HOME));
        cp = new Path(project, getTestClassPath());
        ej.setClasspath(cp);
    
public voidtestNoTimeOut()

        Commandline cmd = getCommandline(TIME_OUT/2);
        ej.setJavaCommand(cmd);
        ej.execute(project);
        assertTrue("process should not have been killed", !ej.killedProcess());
    
public voidtestNoTimeOutForked()

        Commandline cmd = getCommandline(TIME_OUT/2);
        ej.setJavaCommand(cmd);
        ej.fork(cp);
        assertTrue("process should not have been killed", !ej.killedProcess());
    
public voidtestTimeOut()

        Commandline cmd = getCommandline(TIME_OUT*2);
        ej.setJavaCommand(cmd);
        long now = System.currentTimeMillis();
        ej.execute(project);
        long elapsed = System.currentTimeMillis() - now;
        assertTrue("process should have been killed", ej.killedProcess());

        assertTrue("elapse time of "+elapsed
                   +" ms is less than timeout value of "+TIME_OUT_TEST+" ms",
                   elapsed >= TIME_OUT_TEST);
        assertTrue("elapse time of "+elapsed
                   +" ms is greater than run value of "+(TIME_OUT*2)+" ms",
                   elapsed < TIME_OUT*2);
    
public voidtestTimeOutForked()

        //process doesn't die properly under this combination,
        //thus test fails.  No workaround?
        if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
            && Os.isFamily("dos")) {
            return;
        }
        Commandline cmd = getCommandline(TIME_OUT*2);
        ej.setJavaCommand(cmd);
        long now = System.currentTimeMillis();
        ej.fork(cp);
        long elapsed = System.currentTimeMillis() - now;
        assertTrue("process should have been killed", ej.killedProcess());

        assertTrue("elapse time of "+elapsed
                   +" ms is less than timeout value of "+TIME_OUT_TEST+" ms",
                   elapsed >= TIME_OUT_TEST);
        assertTrue("elapse time of "+elapsed
                   +" ms is greater than run value of "+(TIME_OUT*2)+" ms",
                   elapsed < TIME_OUT*2);