| 1 23 24 package org.dbforms.util; 25 26 import com.bitmechanic.maxq.IScriptAdapter; 27 import com.bitmechanic.maxq.Utils; 28 import com.bitmechanic.maxq.generator.AbstractCodeGenerator; 29 import com.bitmechanic.maxq.generator.GeneratorFactory; 30 31 import java.io.File ; 32 33 34 35 45 public class HttpTestCaseGenerator extends AbstractCodeGenerator { 46 private String baseClasspath; 47 48 53 public HttpTestCaseGenerator(IScriptAdapter adapter) { 54 super(adapter); 55 56 File basePath = new File (GeneratorFactory.generatorProperties.getProperty("generator.java.sourcepath", 57 "")); 58 this.baseClasspath = basePath.getAbsolutePath(); 59 System.out.println(this.baseClasspath); 60 } 61 62 67 public String [] getValidFileExtensions() { 68 return new String [] { 69 ".java" 70 }; 71 } 72 73 74 80 public void doAddParameterToList(String name, 81 String value) { 82 super.getScriptAdapter() 83 .append(createStatement("list.add(" + "new KeyValuePair(\"" + name 84 + "\", \"" + value + "\")" + ")")); 85 } 86 87 88 93 public void doAssertResponse(String respCode) { 94 } 95 96 97 106 public void doCallUrl(String url, 107 String method, 108 String data, 109 String contentLength, 110 String list) { 111 super.getScriptAdapter() 112 .append(createStatement(method + "(\"" + url + "\"" + data 113 + contentLength + list + ")")); 114 } 115 116 117 120 public void doCreateList() { 121 super.getScriptAdapter() 122 .append(createStatement("list = new ArrayList()")); 123 } 124 125 126 131 public void doLoad(String script) { 132 super.loadScriptAdapter(script); 133 134 String testName = parseTestName(); 135 System.out.println("testName = " + testName); 136 setTestName(testName); 137 setDefaultTestName(testName); 138 139 String testPath = parseTestPath(); 140 System.out.println("TestPath = " + testPath); 141 setTestPath(parseTestPath()); 142 } 143 144 145 148 public void doResponseForFile() { 149 } 150 151 152 157 public void doResponseForStdOut(String url) { 158 } 159 160 161 167 public void doSave(String path, 168 String fileName) { 169 if (path == null) { 170 throw new IllegalArgumentException ("path cannot be null"); 171 } 172 173 if (fileName == null) { 174 throw new IllegalArgumentException ("file name cannot be null"); 175 } 176 177 String name = fileName; 178 179 if (fileName.indexOf(".") > -1) { 180 name = fileName.substring(0, fileName.indexOf(".")); 181 } 182 183 setTestName(name); 184 setTestPath(path); 185 186 updateTestName(); 187 updateTestPath(); 188 } 189 190 191 196 public void doSetData(String data) { 197 super.getScriptAdapter() 198 .append(createStatement("data = '" + data + "'")); 199 } 200 201 202 205 public void doStartRecording() { 206 if (!scriptContainsTestDeclaration()) { 207 getScriptAdapter() 208 .insert(getTestDeclarationText(), 0); 209 } 210 } 211 212 213 216 public void doStopRecording() { 217 StringBuffer result = new StringBuffer (); 218 result.append(" }"); result.append(EOL + EOL); 220 result.append("/****************/" + EOL + EOL); 221 result.append("}"); 222 super.getScriptAdapter() 223 .append(result.toString()); 224 } 225 226 227 232 public void doTestUrlMessage(String url) { 233 } 234 235 236 241 public void doTidyCode(String url) { 242 } 243 244 245 250 public String parseTestName() { 251 String testName = ""; 252 253 if (getScript() != null) { 254 int startpos = getScript() 255 .indexOf("public class "); 256 257 if (startpos > -1) { 258 startpos += 13; 259 260 int end = getScript() 261 .indexOf(" extends", startpos); 262 testName = getScript() 263 .substring(startpos, end); 264 } 265 } 266 267 return testName; 268 } 269 270 271 276 protected String getTestDeclarationText() { 277 StringBuffer result = new StringBuffer (); 278 result.append("// package" + EOL); 279 result.append("// This class was generated by MaxQ (maxq.tigris.org)" 280 + EOL); 281 result.append(getPackageDeclaration()); 282 result.append("// imports" + EOL); 283 result.append("import java.util.List" + END_STATEMENT); 284 result.append("import java.util.ArrayList" + END_STATEMENT); 285 result.append("import org.dbforms.util.KeyValuePair" + END_STATEMENT); 286 result.append("import org.dbforms.util.HttpTestCase" + END_STATEMENT); 287 288 result.append(EOL + "// definition of test class" + EOL); 289 result.append("public class " + getTestName() + " extends HttpTestCase {" 290 + EOL); 291 292 result.append(" // Test method generated from the MaxQ Java generator" 293 + EOL); 294 result.append(" public " + getTestName() + "(String name) {" + EOL); 295 result.append(" super(name)" + END_STATEMENT); 296 result.append(" }" + EOL); 297 298 result.append(" public void test" + getTestName() 299 + "() throws Exception {" + EOL); 300 result.append(" List list" + END_STATEMENT); 301 302 return result.toString(); 303 } 304 305 306 313 protected String createStatement(String text) { 314 return Utils.lpad(8, text + END_STATEMENT); 315 } 316 317 318 private String getPackage() { 319 String path = ""; 320 321 System.out.println("baseclasspath = " + this.baseClasspath); 322 System.out.println("testpath = " + getTestPath()); 323 324 if ((getTestPath() != null) && (baseClasspath != null)) { 325 if (getTestPath() 326 .toLowerCase() 327 .startsWith(this.baseClasspath.toLowerCase()) 328 && (getTestPath() 329 .length() != baseClasspath.length())) { 330 path = getTestPath() 331 .substring(this.baseClasspath.length() + 1); 332 } 333 } 334 335 return path.replace('\\', '.'); 336 } 337 338 339 private String getPackageDeclaration() { 340 String pkgDeclaration = ""; 341 342 if (!getPackage() 343 .equals("")) { 344 pkgDeclaration = "package " + getPackage() + END_STATEMENT; 345 } 346 347 return pkgDeclaration; 348 } 349 350 351 private int getPackageNameEndPos() { 352 int pos = -1; 353 354 if (getScript() != null) { 355 pos = getScript() 356 .indexOf(";", getPackageNameStartPos()); 357 } 358 359 return pos; 360 } 361 362 363 private int getPackageNameStartPos() { 364 int pos = -1; 365 366 if (getScript() != null) { 367 pos = getScript() 368 .indexOf("package "); 369 370 if (pos > -1) { 371 pos += 7; 372 } 373 } 374 375 return pos; 376 } 377 378 379 private String packageToPath(String s) { 380 return getTestPath() + "\\" + s.replace('.', '\\'); 381 } 382 383 384 private String parseTestPath() { 385 String testPath = null; 386 int pos = getPackageNameStartPos(); 387 388 if (pos > -1) { 389 int end = getPackageNameEndPos(); 390 testPath = packageToPath(getScript().substring(pos, end).trim()); 391 } 392 393 return testPath; 394 } 395 396 397 private boolean scriptContainsTestDeclaration() { 398 int pos = getScript() 399 .indexOf("// This class was generated by MaxQ (maxq.tigris.org)"); 400 401 return (pos > -1); 402 } 403 404 405 private void updateClassDefinition() { 406 int startpos = -1; 407 408 if (getScript() != null) { 409 startpos = getScript() 410 .indexOf("public class " + getDefaultTestName()); 411 412 if (startpos > -1) { 413 startpos += 13; 414 415 int end = -1; 416 417 if (getScript() != null) { 418 end = getScript() 419 .indexOf(" extends"); 420 } 421 422 getScriptAdapter() 423 .replace(getTestName(), startpos, end); 424 } 425 } 426 } 427 428 429 private void updateConstructor() { 430 int startpos = -1; 431 432 if (getScript() != null) { 433 startpos = getScript() 434 .indexOf("public " + getDefaultTestName() 435 + "(String name) {"); 436 437 if (startpos > -1) { 438 startpos += 7; 439 getScriptAdapter() 440 .replace(getTestName(), startpos, 441 startpos + getDefaultTestName().length()); 442 } 443 } 444 } 445 446 447 private void updateTestMethod() { 448 int startpos = -1; 449 450 if (getScript() != null) { 451 startpos = getScript() 452 .indexOf("public void test" + getDefaultTestName() 453 + "() throws Exception {"); 454 455 if (startpos > -1) { 456 startpos += 16; 457 getScriptAdapter() 458 .replace(getTestName(), startpos, 459 startpos + getDefaultTestName().length()); 460 } 461 } 462 } 463 464 465 private void updateTestName() { 466 updateClassDefinition(); 468 469 updateConstructor(); 471 472 updateTestMethod(); 474 } 475 476 477 private void updateTestPath() { 478 String pkg = getPackage(); 479 System.out.println("package = " + pkg); 480 481 if (getScript() != null) { 482 int startpos = getScript() 483 .indexOf("package "); 484 485 if (startpos > -1) { 486 startpos += 8; 487 488 int end = getScript() 489 .indexOf(";", startpos); 490 getScriptAdapter() 491 .replace(getPackage(), startpos, end); 492 } else { 493 if (!pkg.equals("")) { 494 getScriptAdapter() 495 .insert("package " + pkg + ";" + EOL + EOL, 0); 496 } 497 } 498 } 499 } 500 } 501 | Popular Tags |