1 package org.programmerplanet.ant.taskdefs.jmeter; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 import java.io.IOException ; 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 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 24 public class JMeterTask extends Task { 25 26 29 private File jmeterHome; 30 31 34 private File jmeterProperties; 35 36 39 private File testPlan; 40 41 44 private File resultLog; 45 46 49 private ArrayList testPlans = new ArrayList (); 50 51 55 private boolean runRemote = false; 56 57 60 private String proxyHost; 61 62 65 private String proxyPort; 66 67 70 private String proxyUser; 71 72 75 private String proxyPass; 76 77 80 private File jmeterJar; 81 82 85 private ArrayList jvmArgs = new ArrayList (); 86 87 90 private ArrayList jmProperties = new ArrayList (); 91 92 95 private boolean forceBuildFailure; 96 97 100 public void execute() throws BuildException { 101 jmeterJar = new File (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 if (testPlan != null) { 110 executeTestPlan(testPlan); 111 } 112 113 Iterator testPlanIter = testPlans.iterator(); 115 while (testPlanIter.hasNext()) { 116 FileSet fileSet = (FileSet)testPlanIter.next(); 117 DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject()); 118 File baseDir = scanner.getBasedir(); 119 String [] files = scanner.getIncludedFiles(); 120 121 for (int i = 0; i < files.length; i++) { 122 String testPlanFile = baseDir + File.separator + files[i]; 123 executeTestPlan(new File (testPlanFile)); 124 } 125 } 126 127 if (getForceBuildFailure()) { 129 checkForFailures(); 130 } 131 } 132 133 136 private void checkForFailures() throws BuildException { 137 BufferedReader LINE; 138 String linedata = ""; 139 try { 140 LINE = new BufferedReader (new FileReader (getResultLog())); 141 while ((linedata = LINE.readLine()) != null) { 143 if (linedata.toLowerCase().indexOf("success=\"false\"") > 0) { 145 throw new BuildException("Testcase failure, please check the report!!.", location); 146 } 147 } 148 } catch (IOException e) { 149 throw new BuildException("JMeter execution failed.", e, location); 150 } 151 } 152 153 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 173 private void executeTestPlan(File testPlanFile) { 174 log("Executing test plan: " + testPlanFile, Project.MSG_INFO); 175 176 CommandlineJava cmd = new CommandlineJava(); 177 178 cmd.setJar(jmeterJar.getAbsolutePath()); 179 180 Iterator jvmArgIterator = jvmArgs.iterator(); 182 while (jvmArgIterator.hasNext()) { 183 Arg jvmArg = (Arg)jvmArgIterator.next(); 184 cmd.createVmArgument().setValue(jvmArg.getValue()); 185 } 186 187 cmd.createArgument().setValue("-n"); 189 if (jmeterProperties != null) { 191 cmd.createArgument().setValue("-p"); 192 cmd.createArgument().setValue(jmeterProperties.getAbsolutePath()); 193 } 194 cmd.createArgument().setValue("-t"); 196 cmd.createArgument().setValue(testPlanFile.getAbsolutePath()); 197 cmd.createArgument().setValue("-l"); 199 cmd.createArgument().setValue(resultLog.getAbsolutePath()); 200 if (runRemote) { 202 cmd.createArgument().setValue("-r"); 203 } 204 205 if ((proxyHost != null) && (proxyHost.length() > 0)) { 207 cmd.createArgument().setValue("-H"); 208 cmd.createArgument().setValue(proxyHost); 209 } 210 if ((proxyPort != null) && (proxyPort.length() > 0)) { 212 cmd.createArgument().setValue("-P"); 213 cmd.createArgument().setValue(proxyPort); 214 } 215 if ((proxyUser != null) && (proxyUser.length() > 0)) { 217 cmd.createArgument().setValue("-u"); 218 cmd.createArgument().setValue(proxyUser); 219 } 220 if ((proxyPass != null) && (proxyPass.length() > 0)) { 222 cmd.createArgument().setValue("-a"); 223 cmd.createArgument().setValue(proxyPass); 224 } 225 226 Iterator 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 (jmeterHome.getAbsolutePath() + File.separator + "bin")); 240 log(cmd.describeCommand(), Project.MSG_VERBOSE); 241 242 try { 243 execute.execute(); 244 } catch (IOException e) { 245 throw new BuildException("JMeter execution failed.", e, location); 246 } 247 } 248 249 public void setJmeterHome(File jmeterHome) { 250 this.jmeterHome = jmeterHome; 251 } 252 253 public File getJmeterHome() { 254 return jmeterHome; 255 } 256 257 public void setJmeterProperties(File jmeterProperties) { 258 this.jmeterProperties = jmeterProperties; 259 } 260 261 public File getJmeterProperties() { 262 return jmeterProperties; 263 } 264 265 public void setTestPlan(File testPlan) { 266 this.testPlan = testPlan; 267 } 268 269 public File getTestPlan() { 270 return testPlan; 271 } 272 273 public void setResultLog(File resultLog) { 274 this.resultLog = resultLog; 275 } 276 277 public File 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 proxyHost) { 298 this.proxyHost = proxyHost; 299 } 300 301 public String getProxyHost() { 302 return proxyHost; 303 } 304 305 public void setProxyPort(String proxyPort) { 306 this.proxyPort = proxyPort; 307 } 308 309 public String getProxyPort() { 310 return proxyPort; 311 } 312 313 public void setProxyUser(String proxyUser) { 314 this.proxyUser = proxyUser; 315 } 316 317 public String getProxyUser() { 318 return proxyUser; 319 } 320 321 public void setProxyPass(String proxyPass) { 322 this.proxyPass = proxyPass; 323 } 324 325 public String 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 |