KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ExecuteJavaTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

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 JavaDoc;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Simple testcase for the ExecuteJava class - mostly stolen from
31  * ExecuteWatchdogTest.
32  *
33  */

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 JavaDoc name) {
45         super(name);
46     }
47
48     protected void setUp(){
49         ej = new ExecuteJava();
50         ej.setTimeout(new Long JavaDoc(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 JavaDoc {
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 JavaDoc {
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     // test that the watchdog ends the process
71
public void testTimeOut() throws Exception JavaDoc {
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     /**
89      * Dangerous method to obtain the classpath for the test. This is
90      * severely tighted to the build.xml properties.
91      */

92     private static String JavaDoc getTestClassPath(){
93         String JavaDoc 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         // JDK 1.1 needs classes.zip in -classpath argument
100
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