1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.Project; 21 import org.apache.tools.ant.types.Path; 22 import org.apache.tools.ant.types.Commandline; 23 import org.apache.tools.ant.util.JavaEnvUtils; 24 25 import java.io.File ; 26 27 import junit.framework.TestCase; 28 29 34 public class ExecuteJavaTest extends TestCase { 35 36 private final static int TIME_OUT = 5000; 37 38 private final static int CLOCK_ERROR=200; 39 private final static int TIME_OUT_TEST=TIME_OUT-CLOCK_ERROR; 40 41 private ExecuteJava ej; 42 private Project project; 43 44 public ExecuteJavaTest(String name) { 45 super(name); 46 } 47 48 protected void setUp(){ 49 ej = new ExecuteJava(); 50 ej.setTimeout(new Long (TIME_OUT)); 51 project = new Project(); 52 project.setBasedir("."); 53 ej.setClasspath(new Path(project, getTestClassPath())); 54 } 55 56 private Commandline getCommandline(int timetorun) throws Exception { 57 Commandline cmd = new Commandline(); 58 cmd.setExecutable(TimeProcess.class.getName()); 59 cmd.createArgument().setValue(String.valueOf(timetorun)); 60 return cmd; 61 } 62 63 public void testNoTimeOut() throws Exception { 64 Commandline cmd = getCommandline(TIME_OUT/2); 65 ej.setJavaCommand(cmd); 66 ej.execute(project); 67 assertTrue("process should not have been killed", !ej.killedProcess()); 68 } 69 70 public void testTimeOut() throws Exception { 72 Commandline cmd = getCommandline(TIME_OUT*2); 73 ej.setJavaCommand(cmd); 74 long now = System.currentTimeMillis(); 75 ej.execute(project); 76 long elapsed = System.currentTimeMillis() - now; 77 assertTrue("process should have been killed", ej.killedProcess()); 78 79 assertTrue("elapse time of "+elapsed 80 +" ms is less than timeout value of "+TIME_OUT_TEST+" ms", 81 elapsed >= TIME_OUT_TEST); 82 assertTrue("elapse time of "+elapsed 83 +" ms is greater than run value of "+(TIME_OUT*2)+" ms", 84 elapsed < TIME_OUT*2); 85 } 86 87 88 92 private static String getTestClassPath(){ 93 String classpath = System.getProperty("build.tests"); 94 if (classpath == null) { 95 System.err.println("WARNING: 'build.tests' property is not available !"); 96 classpath = System.getProperty("java.class.path"); 97 } 98 99 if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) { 101 classpath += File.pathSeparator 102 + System.getProperty("java.home") 103 + File.separator + "lib" 104 + File.separator + "classes.zip"; 105 } 106 107 return classpath; 108 } 109 110 } 111 | Popular Tags |