View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/HttpTestCaseGenerator.java,v 1.4 2004/10/17 07:02:30 hkollmann Exp $
3    * $Revision: 1.4 $
4    * $Date: 2004/10/17 07:02:30 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
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  /***
36   * Generate a java JUnit test case. Take a look at the CodeGenerator Interface
37   * to see how to specify which one you want. The important information is that
38   * this class generates Java code.
39   *
40   * @author Robert Dawson
41   * @author Wayne Hearn
42   *
43   * @see IScriptGenerator
44   */
45  public class HttpTestCaseGenerator extends AbstractCodeGenerator {
46     private String baseClasspath;
47  
48     /***
49      * Creates a new HttpTestCaseGenerator object.
50      *
51      * @param adapter DOCUMENT ME!
52      */
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     /***
63      * DOCUMENT ME!
64      *
65      * @return DOCUMENT ME!
66      */
67     public String[] getValidFileExtensions() {
68        return new String[] {
69                  ".java"
70               };
71     }
72  
73  
74     /***
75      * DOCUMENT ME!
76      *
77      * @param name DOCUMENT ME!
78      * @param value DOCUMENT ME!
79      */
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     /***
89      * DOCUMENT ME!
90      *
91      * @param respCode DOCUMENT ME!
92      */
93     public void doAssertResponse(String respCode) {
94     }
95  
96  
97     /***
98      * DOCUMENT ME!
99      *
100     * @param url DOCUMENT ME!
101     * @param method DOCUMENT ME!
102     * @param data DOCUMENT ME!
103     * @param contentLength DOCUMENT ME!
104     * @param list DOCUMENT ME!
105     */
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    /***
118     * DOCUMENT ME!
119     */
120    public void doCreateList() {
121       super.getScriptAdapter()
122            .append(createStatement("list = new ArrayList()"));
123    }
124 
125 
126    /***
127     * DOCUMENT ME!
128     *
129     * @param script DOCUMENT ME!
130     */
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    /***
146     * DOCUMENT ME!
147     */
148    public void doResponseForFile() {
149    }
150 
151 
152    /***
153     * DOCUMENT ME!
154     *
155     * @param url DOCUMENT ME!
156     */
157    public void doResponseForStdOut(String url) {
158    }
159 
160 
161    /***
162     * DOCUMENT ME!
163     *
164     * @param path DOCUMENT ME!
165     * @param fileName DOCUMENT ME!
166     */
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    /***
192     * DOCUMENT ME!
193     *
194     * @param data DOCUMENT ME!
195     */
196    public void doSetData(String data) {
197       super.getScriptAdapter()
198            .append(createStatement("data = '" + data + "'"));
199    }
200 
201 
202    /***
203     * DOCUMENT ME!
204     */
205    public void doStartRecording() {
206       if (!scriptContainsTestDeclaration()) {
207          getScriptAdapter()
208             .insert(getTestDeclarationText(), 0);
209       }
210    }
211 
212 
213    /***
214     * DOCUMENT ME!
215     */
216    public void doStopRecording() {
217       StringBuffer result = new StringBuffer();
218       result.append("    }"); //close the method.
219       result.append(EOL + EOL);
220       result.append("/****************/" + EOL + EOL);
221       result.append("}");
222       super.getScriptAdapter()
223            .append(result.toString());
224    }
225 
226 
227    /***
228     * DOCUMENT ME!
229     *
230     * @param url DOCUMENT ME!
231     */
232    public void doTestUrlMessage(String url) {
233    }
234 
235 
236    /***
237     * DOCUMENT ME!
238     *
239     * @param url DOCUMENT ME!
240     */
241    public void doTidyCode(String url) {
242    }
243 
244 
245    /***
246     * DOCUMENT ME!
247     *
248     * @return DOCUMENT ME!
249     */
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    /***
272     * DOCUMENT ME!
273     *
274     * @return DOCUMENT ME!
275     */
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    /***
307     * Returns the supplied string left padded
308     *
309     * @param text
310     *
311     * @return
312     */
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    privateng> 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       //Update Class definition
467       updateClassDefinition();
468 
469       //update constructor
470       updateConstructor();
471 
472       //Update default test method
473       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 }