KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > programmerplanet > ant > taskdefs > jmeter > JMeterTask


1 package org.programmerplanet.ant.taskdefs.jmeter;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileReader JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.DirectoryScanner;
12 import org.apache.tools.ant.Project;
13 import org.apache.tools.ant.Task;
14 import org.apache.tools.ant.taskdefs.Execute;
15 import org.apache.tools.ant.taskdefs.LogStreamHandler;
16 import org.apache.tools.ant.types.CommandlineJava;
17 import org.apache.tools.ant.types.FileSet;
18
19 /**
20  * Runs one or more JMeter test plans sequentially.
21  *
22  * @author <a HREF="mailto:jfifield@programmerplanet.org">Joseph Fifield</a>
23  */

24 public class JMeterTask extends Task {
25
26     /**
27      * The JMeter installation directory.
28      */

29     private File JavaDoc jmeterHome;
30
31     /**
32      * The property file to use.
33      */

34     private File JavaDoc jmeterProperties;
35
36     /**
37      * The test plan to execute.
38      */

39     private File JavaDoc testPlan;
40
41     /**
42      * The file to log results to.
43      */

44     private File JavaDoc resultLog;
45
46     /**
47      * A collection of FileSets specifying test plans to execute.
48      */

49     private ArrayList JavaDoc testPlans = new ArrayList JavaDoc();
50
51     /**
52      * Whether or not to run the remote servers as specified in the properties file.
53      * Default: false.
54      */

55     private boolean runRemote = false;
56
57     /**
58      * The proxy server hostname or ip address.
59      */

60     private String JavaDoc proxyHost;
61
62     /**
63      * The proxy server port.
64      */

65     private String JavaDoc proxyPort;
66
67     /**
68      * The username for the proxy server.
69      */

70     private String JavaDoc proxyUser;
71
72     /**
73      * The password for the proxy server.
74      */

75     private String JavaDoc proxyPass;
76
77     /**
78      * The main JMeter jar.
79      */

80     private File JavaDoc jmeterJar;
81
82     /**
83      * Array of arguments to be passed to the JVM that will run JMeter.
84      */

85     private ArrayList JavaDoc jvmArgs = new ArrayList JavaDoc();
86
87     /**
88      * Array of properties dynamically passed to JMeter
89      */

90     private ArrayList JavaDoc jmProperties = new ArrayList JavaDoc();
91
92     /**
93      * Indicate if build to be forcefully failed upon testcase failure.
94      */

95     private boolean forceBuildFailure;
96
97     /**
98      * @see org.apache.tools.ant.Task#execute()
99      */

100     public void execute() throws BuildException {
101         jmeterJar = new File JavaDoc(jmeterHome.getAbsolutePath() + File.separator + "bin" + File.separator + "ApacheJMeter.jar");
102
103         validate();
104
105         log("Using JMeter Home: " + jmeterHome.getAbsolutePath(), Project.MSG_VERBOSE);
106         log("Using JMeter Jar: " + jmeterJar.getAbsolutePath(), Project.MSG_VERBOSE);
107
108         // execute the single test plan if specified
109
if (testPlan != null) {
110             executeTestPlan(testPlan);
111         }
112
113         // execute each of the test plans specified in each of the "testplans" FileSets
114
Iterator JavaDoc testPlanIter = testPlans.iterator();
115         while (testPlanIter.hasNext()) {
116             FileSet fileSet = (FileSet)testPlanIter.next();
117             DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
118             File JavaDoc baseDir = scanner.getBasedir();
119             String JavaDoc[] files = scanner.getIncludedFiles();
120
121             for (int i = 0; i < files.length; i++) {
122                 String JavaDoc testPlanFile = baseDir + File.separator + files[i];
123                 executeTestPlan(new File JavaDoc(testPlanFile));
124             }
125         }
126
127         //check if forceful build failure is requested.
128
if (getForceBuildFailure()) {
129             checkForFailures();
130         }
131     }
132
133     /**
134      * Validate the results.
135      */

136     private void checkForFailures() throws BuildException {
137         BufferedReader JavaDoc LINE;
138         String JavaDoc linedata = "";
139         try {
140             LINE = new BufferedReader JavaDoc(new FileReader JavaDoc(getResultLog()));
141             //look for any success="false"
142
while ((linedata = LINE.readLine()) != null) {
143                 //throw Error if there are failures.
144
if (linedata.toLowerCase().indexOf("success=\"false\"") > 0) {
145                     throw new BuildException("Testcase failure, please check the report!!.", location);
146                 }
147             }
148         } catch (IOException JavaDoc e) {
149             throw new BuildException("JMeter execution failed.", e, location);
150         }
151     }
152
153     /**
154      * Validate the task attributes.
155      */

156     private void validate() throws BuildException {
157         if (jmeterHome == null || !jmeterHome.isDirectory()) {
158             throw new BuildException("You must set jmeterhome to your JMeter install directory.", location);
159         }
160
161         if (!(jmeterJar.exists() && jmeterJar.isFile())) {
162             throw new BuildException("jmeterhome does not appear to contain a valid JMeter installation.", location);
163         }
164
165         if (resultLog == null) {
166             throw new BuildException("You must set resultLog.", location);
167         }
168     }
169
170     /**
171      * Execute a JMeter test plan.
172      */

173     private void executeTestPlan(File JavaDoc testPlanFile) {
174         log("Executing test plan: " + testPlanFile, Project.MSG_INFO);
175
176         CommandlineJava cmd = new CommandlineJava();
177
178         cmd.setJar(jmeterJar.getAbsolutePath());
179
180         // Set the JVM args
181
Iterator JavaDoc jvmArgIterator = jvmArgs.iterator();
182         while (jvmArgIterator.hasNext()) {
183             Arg jvmArg = (Arg)jvmArgIterator.next();
184             cmd.createVmArgument().setValue(jvmArg.getValue());
185         }
186
187         // non-gui mode
188
cmd.createArgument().setValue("-n");
189         // the properties file
190
if (jmeterProperties != null) {
191             cmd.createArgument().setValue("-p");
192             cmd.createArgument().setValue(jmeterProperties.getAbsolutePath());
193         }
194         // the test plan file
195
cmd.createArgument().setValue("-t");
196         cmd.createArgument().setValue(testPlanFile.getAbsolutePath());
197         // the result log file
198
cmd.createArgument().setValue("-l");
199         cmd.createArgument().setValue(resultLog.getAbsolutePath());
200         //run remote servers?
201
if (runRemote) {
202             cmd.createArgument().setValue("-r");
203         }
204
205         // the proxy host
206
if ((proxyHost != null) && (proxyHost.length() > 0)) {
207             cmd.createArgument().setValue("-H");
208             cmd.createArgument().setValue(proxyHost);
209         }
210         // the proxy port
211
if ((proxyPort != null) && (proxyPort.length() > 0)) {
212             cmd.createArgument().setValue("-P");
213             cmd.createArgument().setValue(proxyPort);
214         }
215         // the proxy user
216
if ((proxyUser != null) && (proxyUser.length() > 0)) {
217             cmd.createArgument().setValue("-u");
218             cmd.createArgument().setValue(proxyUser);
219         }
220         // the proxy password
221
if ((proxyPass != null) && (proxyPass.length() > 0)) {
222             cmd.createArgument().setValue("-a");
223             cmd.createArgument().setValue(proxyPass);
224         }
225
226         // the JMeter runtime properties
227
Iterator JavaDoc jmPropertyIterator = jmProperties.iterator();
228         while (jmPropertyIterator.hasNext()) {
229             Property jmProperty = (Property)jmPropertyIterator.next();
230             if (jmProperty.isValid()) {
231                 cmd.createArgument().setValue("-J" + jmProperty.toString());
232             }
233         }
234
235         Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
236         execute.setCommandline(cmd.getCommandline());
237         execute.setAntRun(getProject());
238
239         execute.setWorkingDirectory(new File JavaDoc(jmeterHome.getAbsolutePath() + File.separator + "bin"));
240         log(cmd.describeCommand(), Project.MSG_VERBOSE);
241
242         try {
243             execute.execute();
244         } catch (IOException JavaDoc e) {
245             throw new BuildException("JMeter execution failed.", e, location);
246         }
247     }
248
249     public void setJmeterHome(File JavaDoc jmeterHome) {
250         this.jmeterHome = jmeterHome;
251     }
252
253     public File JavaDoc getJmeterHome() {
254         return jmeterHome;
255     }
256
257     public void setJmeterProperties(File JavaDoc jmeterProperties) {
258         this.jmeterProperties = jmeterProperties;
259     }
260
261     public File JavaDoc getJmeterProperties() {
262         return jmeterProperties;
263     }
264
265     public void setTestPlan(File JavaDoc testPlan) {
266         this.testPlan = testPlan;
267     }
268
269     public File JavaDoc getTestPlan() {
270         return testPlan;
271     }
272
273     public void setResultLog(File JavaDoc resultLog) {
274         this.resultLog = resultLog;
275     }
276
277     public File JavaDoc getResultLog() {
278         return resultLog;
279     }
280
281     public void addTestPlans(FileSet set) {
282         testPlans.add(set);
283     }
284
285     public void addJvmarg(Arg arg) {
286         jvmArgs.add(arg);
287     }
288
289     public void setRunRemote(boolean runRemote) {
290         this.runRemote = runRemote;
291     }
292
293     public boolean getRunRemote() {
294         return runRemote;
295     }
296
297     public void setProxyHost(String JavaDoc proxyHost) {
298         this.proxyHost = proxyHost;
299     }
300
301     public String JavaDoc getProxyHost() {
302         return proxyHost;
303     }
304
305     public void setProxyPort(String JavaDoc proxyPort) {
306         this.proxyPort = proxyPort;
307     }
308
309     public String JavaDoc getProxyPort() {
310         return proxyPort;
311     }
312
313     public void setProxyUser(String JavaDoc proxyUser) {
314         this.proxyUser = proxyUser;
315     }
316
317     public String JavaDoc getProxyUser() {
318         return proxyUser;
319     }
320
321     public void setProxyPass(String JavaDoc proxyPass) {
322         this.proxyPass = proxyPass;
323     }
324
325     public String JavaDoc getProxyPass() {
326         return proxyPass;
327     }
328
329     public void addProperty(Property property) {
330         jmProperties.add(property);
331     }
332
333     public void setForceBuildFailure(boolean forceBuildFailure) {
334         this.forceBuildFailure = forceBuildFailure;
335     }
336
337     public boolean getForceBuildFailure() {
338         return forceBuildFailure;
339     }
340
341 }
342
Popular Tags