1   // ComTest - Comments for testing
2   package comtest;
3   import java.util.Calendar;
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.PrintWriter;
7   import java.io.FileWriter;
8   
9   import static comtest.StringUtilities.*;
10  
11  /**
12   * A class for making testing easy.
13   * Reads the files given in parameters and generates JUNIT tests
14   * from comments.<p />
15   * 
16   *   The idea is to simplify writing unit tests.  And to get
17   *   unit test as examples to comments and JavaDoc.  
18   *   That's where the name ComTest comes (Comments for Testing)
19   *       
20   * <pre>
21   * Example: Just simple initialization section:
22   * 
23   * Counter cnt = new Counter(3);                            // IS
24   *              cnt.getCount() === 0;  cnt.getSum() === 0;  // IS
25   * cnt.add(1);  cnt.getCount() === 1;  cnt.getSum() === 1;  // IS
26   * cnt.add(2);  cnt.getCount() === 2;  cnt.getSum() === 3;  // IS
27   * 
28   * Syntax:
29   *   The ComTest comment must be inside block comments 
30   *   and start with
31   *     (at)example
32   *     [pre name="testModuleName"]  // change [] to HTML <> 
33   *   and ends with 
34   *     [/pre]
35   *     
36   *   Before handling the ComTest line, comments after //
37   *   is removed.
38   *     
39   *   The test itself contains following sections
40   *    0) The test case name is taken from pre-tag.
41   *    
42   *       Or if there is only name="test" then name is
43   *       found from the next starting class or method
44   *       and the the test case names comes like
45   *         testAdd34
46   *       where 34 is the line of the add-methods heading.     
47   *   
48   *    1) Initialization section.                           (IS)
49   *       This is Java code until first template section
50   *       
51   *    2) Template section                                  (TS)
52   *       This is Java-code where at least one $ exists
53   *       on the line outside of quotes. 
54   *       Template code is output from
55   *       each table value line by replacing template
56   *       values by table values.  After first template
57   *       line all lines are kept as template line until
58   *       the table starts.  Template "variables" are
59   *       just text replacements.
60   *       If the line is form: "$a" == "$b";
61   *       it is not template line, but
62   *       it can be forced template line be extra $ at the
63   *       end of it, so: "$a" == "$b"; $ 
64   *       
65   *    3) Normal code                                       (NC)
66   *       This is normal Java code after first table ends.
67   *       Normal code is output immediately when it is hit.
68   *       Normal code ends to first template line.
69   *       
70   *    4) Table heading                                     (TH)
71   *       This is the first line after template lines 
72   *       where there is at least one column separator 
73   *         " | "
74   *       or | as a first or last char in the line 
75   *       (after removing * and trim)  
76   *       There could be more columns
77   *       than there is templates variables.  All template
78   *       variable does not need to begin with $ (actually none), 
79   *       but there is a big risk to change some unwanted 
80   *       on that case.
81   *       
82   *    5) Table values.                                     (TV)
83   *       These are lines with at least one column separator
84   *         " | "
85   *       or | as a first or last char in the line 
86   *       (after removing * and trim)  
87   *       If the cell value is ignore value
88   *         "---"
89   *       then all template sentences with that column header
90   *       will be ignored.  
91   *       
92   *       Table values are substituted to corresponding template
93   *       variables for each table line.  If there is sentences
94   *       with $ after substitution then those sentences are 
95   *       removed.
96   *       
97   *    6) Table separator (line)                            (LI)
98   *         "-----"  (at least 5 "-" -signs)
99   *       Table separator lines are comments. Actually it 
100  *       can be anywhere in ComTest.  The whole line is 
101  *       treated as a comment even ----- is in 
102  *       the end of line
103  *       
104  *    7) Initialization separator                           (IZ)
105  *         "====="  (at least 5 "=" -signs)
106  *       After initialization separator the next table
107  *       line or normal section causes start of new test case 
108  *       and that starts with initialization section.
109  *       The test cases are numbered by adding _n after
110  *       original test case name.
111  *       
112  *    8) If Initialization separator is before first       (IZE)
113  *       table heading, the whole table will be handled
114  *       so that every line comes a new test case with
115  *       initialization section.                  
116  *       
117  *    9) If there is normal lines or template lines
118  *       after table, then that resets previous templates.
119  *       
120  *   10) All sentences with === is divided to leftSide and
121  *       rightSide.  And then generated code
122  *       assertEquals("explanation",rightSide,leftSide);    
123  *       Note that for more complicated assert's one can write
124  *       f.ex assertTrue("Should be less: #LINE#" , a < 3);
125  *       
126  *       Sentences with ~~~ or ~~ is handled as double
127  *       comparison so they produce 
128  *       assertEquals("explanation",rightSide,leftSide,#PRECISION);
129  *       
130  *       If the line has only one === or ~~ sentence
131  *       then the ending ; may left out.
132  *       
133  *       Sentences with =R= or =~ is handled as regular expression
134  *       comparison.    
135  *           
136  *       
137  *   11) To write directly to Java class, introduce the
138  *       test with name="testJAVA".  This turns Raw-mode on
139  *       and on raw mode the lines are not written inside any 
140  *       test case method.  In raw-mode there is no
141  *       initialization section (IS), so on case of re-init
142  *       no code is repeated.  
143  *       To write JUnit annotations line .@Before the
144  *       at sign  must be prefixed by any char to prevent
145  *       JavaDoc going wrong.  The prefix char is removed.
146  *   
147  *   12) Lines that ends with                           
148  *         #THROWS exceptionName
149  *       is surrounded by "try ... fail() .. catch exceptionName e {}"
150  *       so that line fails if the exception is not thrown.
151  *       Note that the #THROWS works only line based.
152  *       But if table line contains #THROWS then the
153  *       template may generate more lines inside try catch.
154  *       
155  *       If #THROWS exeptionName is before any other line after pre-tag, 
156  *       then the test is started by
157  *         public void testN() throws exeptionName {
158  *       
159  *   13) Import is handled by "commands"
160  *         #STATICIMPORT    - creates a line import static package.ClassName.*;
161  *         #DYNAMICIMPORT   - creates a line import package.ClassName.*;
162  *         #CLASSIMPORT     - creates a line import package.ClassName;
163  *         #PACKAGEIMPORT   - creates a line import package.*;
164  *         #NOIMPORT        - clears the automatic importing for JUnit
165  *         #import impSentence; - adds the selected import sentence
166  *       Import commands can be either in ComTest comments or in
167  *       normal comments if there is only spaces before the command.
168  *       If there is no import commands on the file or ComTest.ini files, then
169  *       import package.ClassName; is inserted automatically
170  *       
171  *   14) The result directory and package can be changed be variables
172  *         #DIRECTORY=dir   
173  *         #PACKAGE=packageName
174  *       in normal comments if there is only spaces before.
175  *       If dir is relative, then it is appended to end of files directory.
176  *       If packageName is .subPackageName then it is added to files
177  *       packageName. 
178  *       
179  *   15) Variable sentences can be also in file
180  *       ComTest.ini in same directory than the file to scan.
181  *       In ini file the variables do not need to be in comments.
182  *       Before # there is allowed only spaces.
183  *       // is treated as a comment like in Java-file.  
184  *       The ComTest.ini file is read first and the variables
185  *       in the Java-file may override the values.                
186  *          
187  *   16) The symbols $, | , ---  and other "variables"
188  *       can be changed by variable substitution lines     
189  *          #COLUMNSEPARATOR=" | ";
190  *          #COLUMNCHAR="|";
191  *          #TEMPLATELINEMARKER="$";
192  *          #DELETESENTENCEMARKER="---";
193  *          #INITLINE="=====";
194  *          #EVERYLINEINITS1="=====";
195  *          #EVERYLINEINITS2="=== Every line inits ===";
196  *          #EQUALSMARKER="===";
197  *          #EQUALSMARKER2="=>";
198  *          #ALMOSTMARKER1="~~~";
199  *          #ALMOSTMARKER2="~~";
200  *          #REGEXPMARKER1="=R=";
201  *          #REGEXPMARKER2="=~";
202  *          #TOLERANCE="0.000001";
203  *          #THROWSMARKER="#THROWS";
204  *          #IMPORTMARKER="#import";
205  *          #PACKAGEMARKER="#PACKAGE=";
206  *          #DIRECTORYMARKER="#DIRECTORY=";
207  *       Each substitution must be in own line and
208  *       = must be the next char after variable name.
209  *       Variables are valid after substitution point until
210  *       end of file or new change.  Every file starts with defaults.
211  *       COLUMNCHAR variable is needed only if it is
212  *       the first or last char on the line.  So mostly
213  *       in one column tables.        
214  *       Variable substitution can be in ComTest-comments or
215  *       in normal comments if there is only spaces before the command.
216  *   
217  *     
218  * Example: Initialization section, template section and table 
219  *          with two new initialization                           
220  *   
221  * Counter cnt = new Counter(3);                            // IS 
222  * cnt.add($add);  cnt.getCount() === $count;               // TS  
223  * cnt.getSum() === $sum;                                   // TS
224  * cnt.getMax() === $max; cnt.getMin() === $min;            // TS
225  * 
226  *  ----------------------------------------------          // LI
227  *     $add   |   $count   | $sum   | $max | $min           // TH
228  *  ----------------------------------------------          // LI
229  *     ---    |     0      |  0     |   0  |   0            // TV
230  *      1     |     1      |  1     |   1  |   1            // TV
231  *      2     |     2      |  1+2   |   2  |   1            // TV
232  *      3     |     3      |  1+2+3 |   3  |   1            // TV
233  *      4     |     3      |  6     |   3  |   1            // TV
234  *  ==============================================          // IZ
235  *      5     |     1      |  5     |   5  |   5            // TV 
236  *      2     |     2      |  5+2   |   5  |   2            // TV
237  *      3     |     3      |  5+2+3 |   5  |   2            // TV 
238  *  ==============================================          // IZ
239  *     -1     |     1      |  -1    |  -1  |  -1            // TV
240  *      2     |     2      | -1+2   |   2  |  -1            // TV  
241  *      9     |     3      | -1+2+9 |   9  |  -1            // TV
242  * </pre>                                              
243  * 
244  * Below is a real life example from test with all sections
245  * and with many tables. For code generated
246  * see ComTestTest.testStringAdd()
247  * 
248  * @example
249  * <pre name="testStringAdd">             
250  * // Example of many tables
251  *   Strings list = new Strings();               // IS
252  *   
253  *   list.size() === 0;                          // IS
254  *   list.add("$value"); list.size() === $size;  // TS
255  *   list.get($i) === "$value";                  // TS
256  *   
257  *    $i   |  $value | $size                     // TH
258  *   -------------------------                   // LI
259  *     0   |  one    |  1                        // TV
260  *     1   |  two    |  2                        // TV
261  *     2   |  three  |  3                        // TV
262  *     3   |  ---    |  3                        // TV
263  *
264  *   list.get(3) === "three"; #THROWS IndexOutOfBoundsException // NC
265  *        
266  *   list.add("$value"); list.size() === $size;  // TS2
267  *   list.add($b); // no output no variable $b   // --       
268  *   
269  *   === Every line inits ===
270  *    $i   |  $value | $size                     // TH2
271  *   -------------------------                   // LI
272  *     0   |  one    |  1                        // TV
273  *     1   |  two    |  1                        // TV 
274  *     2   |  three  |  1                        // TV
275  *     
276  * #COLUMNSEPARATOR=" , "; 
277  * #TEMPLATELINEMARKER="%";
278  *   list.add("four"); list.size() === 1;        // NC 
279  *   assertTrue("Strings: #LINE#",list.size()>0);// NC
280  *   list.add("%value"); list.size() === %size;  // TS3
281  *   assertTrue("Strings: #LINE#",list.size()>0);// TS3
282  *   
283  *    %i   ,  %value , %size                     // TH3
284  *   -------------------------                   // LI
285  *     0   ,  one    ,  2                        // TV
286  *     1   ,  two    ,  3                        // TV
287  *     2   ,  three  ,  4                        // TV
288  *     
289  *   if ( %i > 0 ) { list.get(%i) === "%value"; }// TS4
290  *   
291  *    %i   ,  %value                             // TH3
292  *   -----------------                           // LI
293  *     1   ,  one                                // TV
294  *     2   ,  two                                // TV
295  *     3   ,  three                              // TV
296  *     4   ,  four   #THROWS IndexOutOfBoundsException 
297  *    -1   ,  four   
298  *     
299  * #COLUMNSEPARATOR=" | "; 
300  * #TEMPLATELINEMARKER="$";
301  * </pre>
302  *      
303  * @example 
304  * <pre name="testDouble">
305  *  // Test double comparison and tolerance   
306  *  double d1 = 0.1;                             // IS
307  *  d1 ~~ 1.0/10;                                // IS
308  *  #TOLERANCE=0.2;
309  *  d1 ~~ 0;                                     // IS
310  *  </pre>   
311  *
312  * @example 
313  * <pre name="testRegExp">
314  *  // Test regExp comparison
315  *  "cat" =R= "c.*t"   
316  *  "cat" =~  "c.*"   
317  *  </pre>   
318  *
319  * 
320  * @example 
321  * <pre name="testJAVA">
322  * // Test raw mode and introduce an attribute and initializer 
323  * 
324  * private Strings globalList;
325  * 
326  * /.** *./    // This is the way to make comments inside comments 
327  * .@Before public void init() {// Run before ALL tests, not only those below
328  *   globalList = new Strings();
329  *   String st[] = {"one", "two", "three" };
330  *   for (String s:st) globalList.add(s);
331  * }
332  * </pre>
333  *
334  * @example
335  * <pre name="testGlobalList">             
336  *   globalList.size() === 3;
337  *   globalList.add("one");
338  * </pre>               
339  *
340  * @example
341  * <pre name="testGlobalList2">             
342  *   globalList.size() === 3;
343  *   globalList.add("two");
344  * </pre>               
345  *
346  * 
347  * <h2>Tips</h2>
348  * <ul>
349  * <li>More difficult testing directly to JUNit test file.
350  * ComTest prevents all hand made things in test unit.</li>
351  *
352  * <li>Things written to test unit is usable in ComTest</li>
353  *
354  * <li>All JUnit commands are available</li>
355  *
356  * <li>To write attributes or helper methods to the TestUnit
357  *  use name="testJAVA"</li>
358  *
359  * <li>Do not use variable names that includes the other one
360  * like $a and $aa.  If $a=6 and the replace is done, then
361  * $aa may come 6a.</li> 
362  *
363  * <li> To use logical or test: |, do it without spaces around it
364  * like c = a|b; not like c = a | b; Or if you need to write " | "
365  * then change the separator char by substituting to variables, see 12)</li>
366  * 
367  * <li>Write the code inside comments first as normal code
368  * and test the code and then move inside comments.</li>
369  *  
370  * <li>To change the output directory and package write
371  * ComTest.ini to the package file.</li>
372  *    
373  * <li>It is possible to make templates without naming
374  * the template variables (column names) by $.  
375  * Then it is enough to have one $
376  * f.ex at the end of the first line after the ending ;.
377  * The only danger is to have column names that are included
378  * in the Java-sentences for some other meaning.
379  * </li>   
380  * 
381  * <li>If using if sentences with === then you must use
382  * brackets.  So instead of
383  * <pre> 
384  *   if ( a < 5 ) a === 3;
385  * </pre>
386  * use
387  * <pre> 
388  *   if ( a < 5 ) { a === 3; }
389  * </pre>
390  * The else sentence can be used with or without brackets.
391  * There is same problem also for for and while and do sentences.
392  * </li>
393  * 
394  * </ul>
395  * 
396  * @author vesal
397  * @version 29.9.2007
398  *
399 #STATICIMPORT 
400 #import comtest.Strings;
401  */
402 public class ComTest { // NOPMD by vesal on 13.1.2008 15:29
403     // TODO Syntax:  $arg:REPEAT(one,two,three)
404     // TODO Syntax:  $arg:REPEAT(0..7)
405     // TODO Make a safe copy from old file in case of problems
406     // TODO Should the table line be printed as a comment when every line produces a different test
407 
408 
409     /**
410      * Split line to smaller tokens. Line is divided from 
411      * ;, { or }.  The splitter chars are returned as
412      * own one char sentences.  
413      * @param s line to split
414      * @return line that is split to smaller sentences.
415      * 
416      * @example
417      * <pre name="test">
418      *   Strings sns = splitToSentences("a === 5;");
419      *   sns.size() === 2; 
420      *   sns.get(0) === "a === 5"; sns.get(1) === "; "; 
421      *   sns.toString("|") === "a === 5|; "; 
422      *
423      *   sns = splitToSentences("a === 5; b === 3;");
424      *   sns.size() === 4; 
425      *   sns.toString("|") === "a === 5|; | b === 3|; "; 
426      *
427      *   sns = splitToSentences("if ( a < 3 ) { a === 5; } else b === 3;");
428      *   sns.size() === 8; 
429      *   sns.toString("|") === "if ( a < 3 ) |{ | a === 5|; |} |else | b === 3|; "; 
430      *
431      *   sns = splitToSentences("if ( a < 3 ) { a === 5; } else { b === 3; }");
432      *   sns.size() === 10;
433      *   sns.toString("|") === "if ( a < 3 ) |{ | a === 5|; |} |else |{ | b === 3|; | }";
434      *   
435      *   sns = splitToSentences("get(request,new String[]{\"str\",\"id\"},13,true) === 123;"); 
436      *   sns.size() === 2;
437      *   sns.toString("|") === "get(request,new String[]{\"str\",\"id\"},13,true) === 123|; ";
438      * </pre>
439      */
440     public static Strings splitToSentences(String s) { // NOPMD by vesal on 13.1.2008 15:29
441         Strings sentences = new Strings();
442         String sentence,separator; 
443         int p = 0;
444         while ( true ) {
445             int p1 = indexOfNotInQuotes(s, ";", QUOTES, p);
446             int p2 = indexOfNotInQuotes(s, "{", QUOTES, p);
447             int p3 = indexOfNotInQuotes(s, "}", QUOTES, p);
448             int p4 = indexOfNotInQuotes(s, "else", QUOTES, p);
449             int p5 = indexOfNotInQuotes(s, "]", QUOTES, p);
450             int p0 = p1;
451             int plen = 1;
452             if ( 0 <= p5 && p5 < p2 ) { // format new int[]{3,5}; => new int[]{3,5} 
453                 sentence = s.substring(p5+1,p2);
454                 if ( isEmpty(sentence) ) { p2 = -1; p3 = -1; } 
455             }
456             if ( 0 <= p2 && p2 < p0) p0 = p2;
457             if ( 0 <= p3 && p3 < p0) p0 = p3;
458             if ( 0 <= p4 && p4 < p0) { p0 = p4; plen = 4; }
459             if ( p0 < 0 ) { // take the rest of line
460                 sentence = s.substring(p);
461                 if ( !isEmpty(sentence) ) sentences.add(sentence);
462                 break;
463             }
464             sentence = s.substring(p,p0); 
465             if ( !isEmpty(sentence) ) sentences.add(sentence);
466             separator = s.substring(p0,p0+plen);
467             sentences.add(separator + " ");
468             p = p0+plen;
469             
470         }
471         return sentences;
472     }
473 
474     private static final String TEST = "test";
475     
476     private static final String SEPARATORLINE        = "-----";
477     private static final String LINENUMBERMARKER     = "#LINE#";
478     private static final String METHODINDEXMARKER    = "#METHODINDEX#";
479     private static final String STARTOFTEST          = "<pre name=\""+TEST;
480     private static final String COMTESTID            = "Generated by ComTest";
481     private static final String COMTESTIDOLD         = "Genereted by ComTest";
482     private static final String COMTESTBEGIN         = "BEGIN";
483     private static final String COMTESTEND           = "END";
484     private static final String STATICIMPORT         = "#STATICIMPORT";
485     private static final String DYNAMICIMPORT        = "#DYNAMICIMPORT";
486     private static final String CLASSIMPORT          = "#CLASSIMPORT";
487     private static final String PACKAGEIMPORT        = "#PACKAGEIMPORT";
488     private static final String NOIMPORT             = "#NOIMPORT";
489     private static final String COMTESTINI           = "ComTest.ini";
490 
491     // Constants that have corresponding variables in ComTestScanner 
492     private static final String COLUMNSEPARATOR      = " | ";
493     private static final String COLUMNCHAR           = "|";
494     private static final String TEMPLATELINEMARKER   = "$";
495     private static final String DELETESENTENCEMARKER = "---";
496     private static final String INITLINE             = "=====";
497     private static final String EVERYLINEINITS1      = "=====";
498     private static final String EVERYLINEINITS2      = "=== Every line inits ===";
499     private static final String EQUALSMARKER         = "===";
500     private static final String EQUALSMARKER2        = "=>";
501     private static final String ALMOSTMARKER1        = "~~~";
502     private static final String ALMOSTMARKER2        = "~~";
503     private static final String REGEXPMARKER1        = "=R=";
504     private static final String REGEXPMARKER2        = "=~";
505     private static final String TOLERANCE            = "0.000001";
506     private static final String THROWSMARKER         = "#THROWS";
507     private static final String IMPORTMARKER         = "#import";
508     private static final String PACKAGEMARKER        = "#PACKAGE=";
509     private static final String DIRECTORYMARKER      = "#DIRECTORY=";
510     
511 
512     /**
513      * Class to do the file scanning and output for test class
514      * @author vesal
515      */
516     @SuppressWarnings("hiding")
517     public static class ComTestScanner { // NOPMD by vesal on 13.1.2008 15:29
518         private static final String JAVAIMPOPRT = "import ";
519         private final Strings contents;
520         private final Strings imports = new Strings();
521         private Strings outlines = null;
522         private boolean doStaticImport = false;
523         private boolean noAutoImport = false;
524         private String className = null;
525         private String outFileName = null;
526         private String outDirName  = null;
527         private String testClassName = null;
528         private String packageName = "";
529         private String originalPackageName = "";
530         private String testFilePackageName = "";
531         private String directory = null;
532         private String comTestIni = null;
533         private int printedTableLines = 0;
534         
535      
536         private String COLUMNSEPARATOR      = ComTest.COLUMNSEPARATOR;
537         private String COLUMNCHAR           = ComTest.COLUMNCHAR;
538         private String TEMPLATELINEMARKER   = ComTest.TEMPLATELINEMARKER;
539         private String DELETESENTENCEMARKER = ComTest.DELETESENTENCEMARKER;
540         private String INITLINE             = ComTest.INITLINE;
541         private String EVERYLINEINITS1      = ComTest.EVERYLINEINITS1;
542         private String EVERYLINEINITS2      = ComTest.EVERYLINEINITS2;
543         private String EQUALSMARKER         = ComTest.EQUALSMARKER;
544         private String EQUALSMARKER2        = ComTest.EQUALSMARKER2;
545         private String ALMOSTMARKER1        = ComTest.ALMOSTMARKER1;
546         private String ALMOSTMARKER2        = ComTest.ALMOSTMARKER2;
547         private String REGEXPMARKER1        = ComTest.REGEXPMARKER1;
548         private String REGEXPMARKER2        = ComTest.REGEXPMARKER2;
549         private String TOLERANCE            = ComTest.TOLERANCE;
550         private String THROWSMARKER         = ComTest.THROWSMARKER;
551         private String IMPORTMARKER         = ComTest.IMPORTMARKER;
552         private String PACKAGEMARKER        = ComTest.PACKAGEMARKER;
553         private String DIRECTORYMARKER      = ComTest.DIRECTORYMARKER;
554         
555 
556         /**
557          * Constructs the new ComTestScanner from stringlist contents
558          * @param contents where to scan the comment tests
559          */
560         ComTestScanner(Strings contents) { 
561             this.contents = contents;
562             outlines = new Strings();
563         }
564 
565 
566         
567         /**
568          * Checks if s is for setting imports.  If is, then true
569          * is returned and the import is handled.
570          * @param line line to check
571          * @return true if import line, false otherwise 
572          */
573         private boolean handleImports(String line) { // NOPMD by vesal on 13.1.2008 21:05
574             String s = line.trim();
575             
576             if ( indexOfNotInQuotes(s,NOIMPORT) == 0 ) {
577                 imports.clear();
578                 doStaticImport = false;
579                 noAutoImport = true;
580                 return true;
581             }
582             if ( indexOfNotInQuotes(s,STATICIMPORT) == 0 ) {
583                 doStaticImport = true;
584                 noAutoImport = true;
585                 return true;
586             }
587             if ( indexOfNotInQuotes(s,DYNAMICIMPORT) == 0 ) {
588                 if ( originalPackageName.length() == 0) return true; 
589                 imports.add(JAVAIMPOPRT + originalPackageName + "." + className + ".*;");
590                 noAutoImport = true;
591                 return true;
592             }
593             if ( indexOfNotInQuotes(s,CLASSIMPORT) == 0 ) {
594                 if ( originalPackageName.length() == 0) return true; 
595                 imports.add(JAVAIMPOPRT + originalPackageName + "." + className +";");
596                 noAutoImport = true;
597                 return true;
598             }
599             if ( indexOfNotInQuotes(s,PACKAGEIMPORT) == 0 ) {
600                 if ( originalPackageName.length() == 0) return true; 
601                 imports.add(JAVAIMPOPRT + originalPackageName + ".*;");
602                 noAutoImport = true;
603                 return true;
604             }
605             if ( indexOfNotInQuotes(s, IMPORTMARKER) == 0 ) {
606                 String imp = s.substring(IMPORTMARKER.length()+1).trim();
607                 imports.add(JAVAIMPOPRT + imp);
608                 noAutoImport = true;
609                 return true;
610             }
611             
612             return false;
613         }
614 
615         /**
616          * Handles directory and package specific lines
617          * @param line line to handle
618          * @return true if handled
619          */
620         private boolean handleDirAndPackage(String line) {
621             String s = line.trim();
622             if ( indexOfNotInQuotes(s, DIRECTORYMARKER) == 0 ) {
623                 directory = s.substring(DIRECTORYMARKER.length()).trim();
624                 return true;
625             }
626             
627             if ( indexOfNotInQuotes(s, PACKAGEMARKER) == 0 ) {
628                 packageName = s.substring(PACKAGEMARKER.length()).trim();
629                 return true;
630             }
631             return false;
632         }
633         
634         
635 
636         /**
637          * Check if s is a substitution to specified variable
638          * @param s        string to looks for
639          * @param varName  varName to look
640          * @return         new value for variable, null if not variable
641          */
642         private String checkVariable(String s, String varName) {
643             if ( !s.startsWith("#"+varName+"=") ) return null;
644             String val = s.substring(varName.length()+2).trim();
645             val = val.replace("\"", "");
646             val = val.replace("'", "");
647             return val;
648         }
649         
650         /**
651          * Check if d is substitution to any know variable
652          * @param st string to check
653          * @return true if any variable, false otherwise
654          */
655         private boolean checkVariables(String st) { // NOPMD by vesal on 13.1.2008 15:29
656             String val;
657             String s = removeAllAfter(st,";").trim();
658             val = checkVariable(s,"COLUMNSEPARATOR"     ); if ( val != null ) { COLUMNSEPARATOR      = val; return true; }
659             val = checkVariable(s,"TEMPLATELINEMARKER"  ); if ( val != null ) { TEMPLATELINEMARKER   = val; return true; }
660             val = checkVariable(s,"DELETESENTENCEMARKER"); if ( val != null ) { DELETESENTENCEMARKER = val; return true; }
661             val = checkVariable(s,"COLUMNCHAR"          ); if ( val != null ) { COLUMNCHAR           = val; return true; }
662             val = checkVariable(s,"INITLINE"            ); if ( val != null ) { INITLINE             = val; return true; }
663             val = checkVariable(s,"EVERYLINEINITS1"     ); if ( val != null ) { EVERYLINEINITS1      = val; return true; }
664             val = checkVariable(s,"EVERYLINEINITS2"     ); if ( val != null ) { EVERYLINEINITS2      = val; return true; }
665             val = checkVariable(s,"EQUALSMARKER"        ); if ( val != null ) { EQUALSMARKER         = val; return true; }
666             val = checkVariable(s,"EQUALSMARKER2"       ); if ( val != null ) { EQUALSMARKER2        = val; return true; }
667             val = checkVariable(s,"ALMOSTMARKER1"       ); if ( val != null ) { ALMOSTMARKER1        = val; return true; }
668             val = checkVariable(s,"ALMOSTMARKER2"       ); if ( val != null ) { ALMOSTMARKER2        = val; return true; }
669             val = checkVariable(s,"REGEXPMARKER1"       ); if ( val != null ) { REGEXPMARKER1        = val; return true; }
670             val = checkVariable(s,"REGEXPMARKER2"       ); if ( val != null ) { REGEXPMARKER2        = val; return true; }
671             val = checkVariable(s,"TOLERANCE"           ); if ( val != null ) { TOLERANCE            = val; return true;}
672             val = checkVariable(s,"THROWSMARKER"        ); if ( val != null ) { THROWSMARKER         = val; return true; }
673             val = checkVariable(s,"IMPORTMARKER"        ); if ( val != null ) { IMPORTMARKER         = val; return true; }
674             val = checkVariable(s,"PACKAGEMARKER"       ); if ( val != null ) { PACKAGEMARKER        = val; return true; }
675             val = checkVariable(s,"DIRECTORYMARKER"     ); if ( val != null ) { DIRECTORYMARKER      = val; return true; }
676             return false;
677         }
678 
679 
680         /**
681          * Add string s to outlines 
682          * @param s
683          */
684         private void addOut(String s) {
685             outlines.add(s);
686         }
687 
688         /**
689          * Add string s to outlines with new line
690          * @param s
691          */
692         private void addOutln(String s) {
693             outlines.add(s+"\n");
694         }
695         
696         /**
697          * Add strings from list to outlines by separating from new line
698          * @param list strings to add
699          */
700         private void addOut(Strings list) {
701             for (String s:list) addOut(s);
702         }
703 
704         /**
705          * Prints the stringlist by replacing target-strings by replacement strings
706          * @param out     stream to print
707          * @param list    stringlist to print
708          * @param sep     string to separate items 
709          */
710         private void println(PrintWriter out, Strings list, String sep) {
711             for (String s:list) out.print(s+sep);
712         }
713         
714         
715         /**
716          * Prints the stringlist by replacing target-strings by replacement strings
717          * Separator is not printed after last line
718          * @param list    stringlist to print
719          * @param target  string that is replaced 
720          * @param replacement string by what to replace
721          */
722         private void addOut(Strings list, String target, String replacement) {
723             if ( list.size() == 0 ) return;
724             for (int i=0; i<list.size();i++) 
725                 addOut(list.get(i).replace(target, replacement));
726         }
727         
728         /**
729          * Deletes from the list all lines that contains target
730          * outside of quotes and comments
731          * @param list   where to delete
732          * @param target what is condition for delete
733          * @return the list itself
734          */
735         private Strings delete(Strings list, String target, String quotes) {
736            int ir,iw = 0; 
737            for (ir=0; ir<list.size(); ir++) {
738                String s = list.get(ir);
739                int p = indexOfNotInQuotes(s, target, quotes);
740                int pc = indexOfNotInQuotes(s, "//", quotes);
741                // int p = s.indexOf(target);
742                if ( p < 0 || ( 0 <= pc  && pc < p ) ) list.set(iw++, s);
743            }
744            if ( ir != iw ) list.removeRange(iw,ir);
745            return list;
746         }
747         
748         
749         /**
750          * This method is just for testing purposes to return
751          * current outlines.
752          * @return current outlines.
753          */
754         public Strings getOutlines() {
755             return outlines;
756         }
757         
758         /**
759          * This factory method is just for testing purposes
760          * @return a new handler
761          */
762         public CommentCodeHandler createCommentCodeHandler() {
763             return new CommentCodeHandler(); 
764         }
765         
766         /**
767          * Handles to ComTest-comment and returns the position
768          * of last handled line. 
769          * The main method is readCode and all others are just helper
770          * methods to handle the lines. 
771          * Precondition: inside JavaDoc comment
772          * Postcondition: still inside JavaDoc comment 
773          */
774         public class CommentCodeHandler { // NOPMD by vesal on 13.1.2008 15:29
775             private static final String javaShortComment = "  // ";
776             private int lineNr;
777             private String methodName = null;
778             private final Strings template = new Strings();
779             private final Strings initCmds = new Strings();
780             private final Strings methodHeader = new Strings();
781             private final Strings methodFooter = new Strings();
782             private boolean methodHeaderPrinted = false;
783             private int methodIndex = 1;
784             private String sMethodIndex = "";
785             private Strings vars = null;
786             private Strings values = null;
787             private int initLine = 0;
788             private boolean everyLineInits = false; 
789             private boolean tablePrinted = false;
790             private boolean oneTablePrinted = false;
791             private boolean rawMode = false;
792             private String baseIndent = "  ";
793             private String indent = baseIndent;
794             private final Strings startOfLine = new Strings();
795             private final Strings endOfLine = new Strings();
796             private String methodThrows = "";
797             
798 
799             /**
800              * This is just for testing purposes to return the initCmds
801              * @return initCmnds
802              */
803             public Strings getInitCmnds() {
804                 return initCmds;
805             }
806             
807             
808             /**
809              * Try to find name for next method.  If no name is found, then
810              * a sequence number is used. 
811              * This is very unfinished and works only for nice code
812              * where the method name and type is on the same line.
813              * @param contents where to find the name
814              * @param lineNr where to start
815              * @return test method name
816              */
817             public String tryToFindMethodName(Strings contents, int lineNr) { // NOPMD by vesal on 13.1.2008 15:29
818                 String autoNumber = ""+(lineNr+1);
819                 String name;
820                 String type="";
821                 boolean inComment = true; 
822                 for ( int i=lineNr; i<contents.size(); i++ ) {
823                     String s = contents.get(i);
824                     s = removeAllAfter(s, "//").trim();
825                     if ( s.length() == 0 ) continue;
826                     if ( indexOfNotInQuotes(s,"/*") >= 0 ) { inComment = true; continue; }
827                     if ( indexOfNotInQuotes(s,"*/") >= 0 ) { inComment = false; continue; }
828                     if ( inComment ) continue;
829                     s = remove(s,"public");
830                     s = remove(s,"protected");
831                     s = remove(s,"private");
832                     s = remove(s,"static");
833                     s = remove(s,"final");
834                     s = remove(s,"class");
835                     s = removeAllAfter(s,"extends");
836                     s = removeAllAfter(s,"implements");
837                     s = s.replaceAll("@.[^\\s]*\\s*",""); // Remove annotations
838                     s = s.trim();
839                     if ( s.length() == 0 ) continue;
840                     int p = s.indexOf(' ');
841                     if ( p >= 0 ) { type = s.substring(0,p); s = s.substring(p+1); }
842                     int ic;
843                     for ( ic=0; ic<s.length(); ic++)
844                         if ( !Character.isJavaIdentifierPart(s.charAt(ic)) ) break;
845                     name = s.substring(0,ic);
846                     if ( name.length() == 0 ) name = type; 
847                     if ( name.length() == 0 ) return TEST + autoNumber;
848                     name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
849                     return TEST + name + autoNumber;
850                 }
851                 return TEST + autoNumber;
852             }
853 
854             
855             /**
856              * Check if line is THROWS line.  If it is, correct
857              * the start and end lists as needed.
858              * @param s line to look.
859              * @return line without THROWS sentence
860              */
861             private String checkThrows(String s) {
862                 int p = indexOfNotInQuotes(s, THROWSMARKER);
863                 if ( p < 0 ) return s;
864                 indent = baseIndent + "";
865                 String sentence = s.substring(0,p);
866                 String exception = s.substring(p+THROWSMARKER.length()).trim();
867                 startOfLine.add(0,baseIndent + "  try {\n");
868                 endOfLine.add(0,"\n"+indent + "  fail(\"" + className + ": " + (lineNr+1) +" Did not throw " + exception + "\");\n");
869                 endOfLine.add(1,baseIndent + "  } catch(" + exception + " e){ e.getMessage(); }");
870                 return sentence;
871             }
872             
873             /**
874              * Check if line is THROWS line.  If it is, correct
875              * the start and end lists as needed.
876              * @param s line to look.
877              * @return line without THROWS sentence
878              */
879             private String checkMethodThrows(String s) {
880                 int p = indexOfNotInQuotes(s, THROWSMARKER);
881                 if ( p < 0 ) return s;
882                 String sentence = remove(s.substring(0,p),"*").trim();
883                 if ( sentence.length() > 0 ) return s;
884                 String exception = s.substring(p+THROWSMARKER.length()).trim();
885                 methodThrows = "throws " + exception + " ";
886                 return sentence;
887             }
888             
889             
890             /**
891              * Find comments and put possible try catch to start and end
892              * of the the current line's start and end lists.
893              * @param s line to handle
894              * @return the line where comments and THROWS-sentence is removed.
895              */
896             private String saveStartAndEndOfLine(String s) {
897                 startOfLine.clear();
898                 endOfLine.clear();
899                 indent = baseIndent;
900                 int p = indexOfNotInQuotes(s, "//");
901                 if ( p < 0 ) {
902                   endOfLine.add("\n");
903                   return checkThrows(s);
904                 }
905                 endOfLine.add(" " + s.substring(p) + "\n"); 
906                 return checkThrows(s.substring(0,p)); 
907             }
908 
909 
910             /**
911              * Copies method footer to outlines if method header is
912              * already moved.  Nothing is done if method header
913              * is not copied.
914              * @param realLineNr current line number 
915              */
916             private void outFooter(int realLineNr) {
917                 if ( methodHeaderPrinted ) { 
918                     addOut(methodFooter);
919                     if ( realLineNr > 0 ) initLine = realLineNr;
920                 }
921                 methodHeaderPrinted = false;
922             }
923 
924             
925             /**
926              * Moves header lines to outlines.  Possible method
927              * index markers are replaced by method index.
928              */
929             public void outNewHeader() {
930                 outFooter(0);
931                 addOut(methodHeader, METHODINDEXMARKER, sMethodIndex);
932                 methodIndex++; 
933                 sMethodIndex = ""+methodIndex; 
934                 methodHeaderPrinted = true;
935                 if ( initLine == 0 ) initLine = lineNr+1;
936                 addOutln(baseIndent + javaShortComment + className + ": " + initLine);
937                 addOut(initCmds);
938             }
939             
940             /**
941              * Moves list to outlines.  If header is not moved,
942              * then move it now. Possible target strings are replaced
943              * by replacement strings while moving.
944              * @param list what to move
945              * @param target what is replaced
946              * @param replacement by what to replace
947              */
948             private void outWithHeader(Strings list, String target, String replacement) {
949                 if ( !methodHeaderPrinted ) outNewHeader();
950                 addOut(list,target,replacement);
951             }
952             
953             
954             /**
955              * Add sentences to init section.  Possible line number markers
956              * are replaced by current line number.
957              * @param commands sentences to add
958              * @param realLineNr real line number for current line
959              */
960             private void addInit(Strings commands, int realLineNr) {
961                 if ( initLine == 0 ) initLine = realLineNr;
962                 initCmds.add(commands.replace(LINENUMBERMARKER, ""+realLineNr));
963             }
964             
965             
966             /**
967              * Moves commands to outlines.  If header is not moved,
968              * then move it now. Possible line number markers
969              * are replaced by current line number.
970              * @param commands what to move
971              * @param realLineNr real line number for current line
972              */
973             private void outLines(Strings commands, int realLineNr) {
974                 outWithHeader(commands,LINENUMBERMARKER, ""+realLineNr);
975             }
976             
977             
978             /**
979              * Check if s is one of the JUnit annotation.
980              * @param s line to look
981              * @return true if annotation, false otherwise.
982              */
983             private boolean jUnitAnnotate(String s) {
984                 if ( s.startsWith("@Test") )   return true;
985                 if ( s.startsWith("@Before") ) return true;
986                 if ( s.startsWith("@After") )  return true;
987                 return false;
988             }
989             
990             
991             /**
992              * Annotations in comments must be prefixed with something.
993              * This takes the prefixing away. 
994              * @param s where to look annotation
995              * @return s if no annotation, otherwise annotation with
996              *           prefix info removed. 
997              * @example
998              * <pre name="test">
999              *   TestHelpper t = new TestHelpper();
1000             *   t.codeHandler.handleAnnotationInComments("a") === "a";    
1001             *   t.codeHandler.handleAnnotationInComments("* #@Test") === "@Test";    
1002             *   t.codeHandler.handleAnnotationInComments("* #@example") === "* #@example";    
1003             * 
1004             * </pre>    
1005             */
1006            public String handleAnnotationInComments(String s) {
1007                int p = indexOfNotInQuotes(s, "@");
1008                if ( p < 0 ) return s;
1009                String st = s.substring(p);
1010                if ( jUnitAnnotate(st) ) return st; 
1011                return s;
1012            }
1013            
1014            /**
1015             * Deletes \n from the last line of cmds
1016             * @param cmds where to delete
1017             */
1018            private void deleteLastNewLine(Strings cmds) {
1019                if ( cmds.size() <= 0 ) return;
1020                String s = cmds.get(cmds.size()-1);
1021                if ( s.length() == 0 ) return;
1022                if ( s.charAt(s.length()-1) != '\n' ) return;
1023                cmds.set(cmds.size()-1,s.substring(0,s.length()-1)+" "); 
1024            }
1025            
1026            /**
1027             * Check if the line is a table line, so containing either " | "
1028             * or | at the beginning or end.
1029             * @param s line to check
1030             * @return true if was a table line.
1031             * @example
1032             * <pre name="test">
1033             *   TestHelpper t = new TestHelpper();
1034             *   t.codeHandler.isTableLine("") === false;    
1035             *   t.codeHandler.isTableLine("a") === false;    
1036             *   t.codeHandler.isTableLine("a|") === true;    
1037             *   t.codeHandler.isTableLine("|a") === true;    
1038             *   t.codeHandler.isTableLine(" a|b ") === false;    
1039             *   t.codeHandler.isTableLine(" a | b ") === true;    
1040             * </pre>
1041             */
1042            public boolean isTableLine(String s) {
1043                if ( indexOfNotInQuotes(s,COLUMNSEPARATOR) >= 0 ) return true;
1044                if ( s.startsWith(COLUMNCHAR) ) return true;
1045                if ( s.endsWith(COLUMNCHAR) ) return true;
1046                return false;
1047            }
1048
1049            /**
1050             * Handles lines like     "  $s        |  $what |  $result"
1051             * or                     " "aab"      |  "a"   |  "ab"  "
1052             * If there is a $ on the line, it is kept as a header line.
1053             * Otherwise as a normal values line.  From header line
1054             * the variable names a collected.  In normal line the
1055             * variable values a replaced in the corresponding template
1056             * line.  Sentences with unused variable names are removed. 
1057             * @param line line to handle.
1058             */
1059            private void handleTableLine(String line) { // NOPMD by vesal on 13.1.2008 15:29
1060                String s = line;
1061                if ( template.size() == 0 ) return; // No use of table if no template
1062                if ( s.startsWith(COLUMNCHAR) ) s = s.substring(1);
1063                if ( s.endsWith(COLUMNCHAR) ) s = s.substring(1,s.length()-1);
1064                if ( vars == null ) { // || indexOfNotInQuotes(s, TEMPLATELINEMARKER ) >= 0 ) {
1065                    vars = splitBy(s,COLUMNSEPARATOR,COLUMNSEPARATOR.length(),true);
1066                    return;
1067                }
1068                values = splitBy(s,COLUMNSEPARATOR,COLUMNSEPARATOR.length(),true); 
1069                Strings cmds = template.clone();
1070                for (int v=0; v<values.size(); v++ ) {
1071                    if ( v >= vars.size() ) break;
1072                    String value = values.get(v).trim();
1073                    String var = vars.get(v).trim();
1074                    if ( value.equals(DELETESENTENCEMARKER) ) delete(cmds,var,"");
1075                    else cmds.replace(var, value);
1076                }
1077                if ( everyLineInits ) outFooter(lineNr+1);
1078                delete(cmds, "$", QUOTES);  // remove sentences with $ outside ""
1079                deleteLastNewLine(cmds);
1080                cmds.addAll(0, startOfLine);
1081                cmds.add(endOfLine);
1082                outWithHeader(cmds,LINENUMBERMARKER, ""+(lineNr+1));
1083                printedTableLines += cmds.size();
1084
1085                if ( everyLineInits ) 
1086                    outFooter(lineNr+1);
1087                tablePrinted = true;
1088                oneTablePrinted = true;
1089            }
1090
1091            
1092            /**
1093             * Check if there is coming a ; then nothing is done.
1094             * If there is no ; coming, then one ; is added to commands.
1095             * @param sentences where to look ;
1096             * @param i from what index to look ;
1097             * @param commands where to add ; if needed
1098             */
1099            private void checkSemiColon(Strings sentences, int i, Strings commands) {
1100                if ( ( i < sentences.size() - 1 ) &&  
1101                     ( sentences.get(i+1).indexOf(";") >= 0 ) ) return;
1102                commands.add(";");
1103            }
1104            
1105
1106            /**
1107             * Check if line is pseudo comment /.* or  *./
1108             * @return comments handled or s
1109             */
1110            private String isPseudoComment(String line) {
1111                String s = line;
1112                s = s.replaceAll("\\/\\.\\*", "/*"); 
1113                s = s.replaceAll("\\*\\.\\/", "*/");
1114                return s;
1115            }
1116            
1117            /**
1118             * Handles normal lines and table lines from ComTest lines.
1119             * The line is first divided to "sentences" so that
1120             * ; { } are each one "sentence".  Then ===, ~~ is try to
1121             * find and in case of success the corresponding assert is generated.
1122             * If the line has at least on $ sign, all sentences are kept as
1123             * a template sentences.  Also after starting the template, all
1124             * lines are kept as template lines.
1125             * If generated assert-line does not include ; , then it is added.
1126             * @param line line to handle.
1127             * 
1128             * @example
1129             * <pre name="test">
1130             *   TestHelpper t = new TestHelpper();
1131             *     
1132             *   t.codeHandler.handleSentences("int a = 5;");
1133             *   t.initCmnds.get(1) === "int a = 5";
1134             *   
1135             *   t.initCmnds.clear();
1136             *   t.codeHandler.handleSentences("int a = 5; int b = 7;");
1137             *   t.initCmnds.get(0) === "    ";
1138             *   t.initCmnds.get(1) === "int a = 5";
1139             *   t.initCmnds.get(2) === "; ";
1140             *   t.initCmnds.get(3) === "int b = 7";
1141             *   t.initCmnds.get(4) === "; ";
1142             *   
1143             *   t.initCmnds.clear();
1144             *   t.codeHandler.handleSentences("a === 5;");
1145             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 5, a)";
1146             *
1147             *   t.initCmnds.clear();
1148             *   t.codeHandler.handleSentences("get(request,new String[]{\"str\",\"id\"},13,true) === 123;");
1149             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 123, get(request,new String[]{\"str\",\"id\"},13,true))";
1150             *   
1151             *   t.initCmnds.clear();
1152             *   t.codeHandler.handleSentences("\"cat\" =R= \"c.*t\";"); // "cat" =R= "c.*t";
1153             *   t.initCmnds.get(1) === "{ String l=\"cat\",r=\"c.*t\"; if ( !l.matches(r) ) fail(\"From: test line: 1\" + \" does not match: [\"+ l + \"] != [\" + r + \"]\");}";
1154             *   
1155             *   t.initCmnds.clear();
1156             *   t.codeHandler.handleSentences("a ~~~ 6.0;");  
1157             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 6.0, a, 0.000001)";
1158             *   
1159             *   t.initCmnds.clear();
1160             *   t.codeHandler.handleSentences("\"a$1\""); /// Not a template line  
1161             *   t.initCmnds.get(1) === "\"a$1\"";
1162             *   
1163             *   t.template.size() === 0;
1164             * </pre>
1165             * 
1166             * 
1167             * @example
1168             * <pre name="test">
1169             *   String s="$a"; s === "$b"; $
1170             *     $a | $b
1171             *    ---------
1172             *      1 | 1    
1173             *   
1174             *  $a; "a".matches("^a$") === false;  // does not generate code
1175             * </pre>
1176             * 
1177             * @example
1178             * <pre name="test">
1179             *   String s=null; s === null; 
1180             *   "a".matches("^a$") === true;  // generate code
1181             * </pre>
1182             * 
1183             * @example
1184             * <pre name="test">
1185             * String s="$a"; s === "$b"; $; 
1186             *     $a | $b
1187             *    ---------
1188             *      $ | $    
1189             *   "a".matches("^a$") === true;  // this generates
1190             * </pre>
1191             */
1192            public void handleSentences(String line) { // NOPMD by vesal on 13.1.2008 15:29
1193                int p;
1194                String eq,tol;
1195                String s = line;
1196                boolean isTemplateLine = indexOfNotInQuotes(s,TEMPLATELINEMARKER) >= 0;
1197                
1198                boolean isRegExp = false;
1199                
1200                Strings commands = new Strings();
1201                commands.add(indent + "  ");
1202                Strings sentences = splitToSentences(s);
1203                if ( tablePrinted ) { template.clear(); tablePrinted = false; everyLineInits = false; vars = null; } // NOPMD by vesal on 13.1.2008 15:30
1204                for ( int i=0; i<sentences.size(); i++ ) {
1205                    String st = sentences.get(i);
1206                    isRegExp = false;
1207                    
1208                    if ( st.length() == 0 ) continue;
1209                    if ( rawMode ) s = removeFromBegining(st," ",1); else s = removeFromBegining(st, " ");
1210                    if ( s.length() == 0 ) continue;
1211                    
1212                    p = indexOfNotInQuotes(s,ALMOSTMARKER1);  eq = ALMOSTMARKER1; tol = ", " + TOLERANCE;  /// ~~~
1213                    if ( p < 0 ) { p = indexOfNotInQuotes(s,ALMOSTMARKER2); eq = ALMOSTMARKER2; }          /// ~~
1214                    if ( p < 0 ) { p = indexOfNotInQuotes(s,EQUALSMARKER);  eq = EQUALSMARKER; tol = ""; } /// ===
1215                    if ( p < 0 ) { p = indexOfNotInQuotes(s,EQUALSMARKER2); eq = EQUALSMARKER2; }          /// =>
1216                    if ( p < 0 ) { p = indexOfNotInQuotes(s,REGEXPMARKER1); eq = REGEXPMARKER1; isRegExp = true; } /// =R=
1217                    if ( p < 0 ) { p = indexOfNotInQuotes(s,REGEXPMARKER2); eq = REGEXPMARKER2; isRegExp = true; } /// =~
1218                    if ( p >= 0 ) {
1219                        String leftSide = s.substring(0,p).trim();
1220                        String rightSide = s.substring(p+eq.length()).trim();
1221                        String message = "\"From: " + className + " line: "+ LINENUMBERMARKER + "\"";
1222                        if ( isRegExp ) {
1223                          commands.add("{ String l="+leftSide+",r="+rightSide+"; if ( !l.matches(r) ) fail(" + message + " + \" does not match: [\"+ l + \"] != [\" + r + \"]\");}");  
1224                        } else { 
1225                          commands.add("assertEquals(" + message + ", "+rightSide + ", " + leftSide + tol + ")");
1226                          checkSemiColon(sentences,i,commands);
1227                        }  
1228                        continue;
1229                    }
1230                    
1231                    commands.add(s);
1232                }
1233                commands.addAll(0,startOfLine);
1234                commands.add(endOfLine);
1235                if ( isTemplateLine || template.size() > 0 ) template.add(commands);
1236                else if ( oneTablePrinted || rawMode )
1237                    outLines(commands,lineNr+1);
1238                else
1239                    addInit(commands,lineNr+1);
1240            }
1241            
1242            
1243            /**
1244             * Creates a header and footer for new ComTest test method.
1245             * If the name is for raw-code, then no method header is done.
1246             * If there is no name for the test method, then next method is
1247             * searched and that method name is used as a test method name. 
1248             * @param s line to start the test.
1249             */
1250            private void createHeaderAndFooter(String s) {
1251                int p = s.indexOf(STARTOFTEST); 
1252                if ( p < 0 ) return;
1253                if ( s.indexOf("JAVA") > 0 ) { // start Raw mode
1254                    methodName = "";
1255                    methodHeader.add("\n");
1256                    methodHeader.add(javaShortComment + COMTESTID + " " + COMTESTBEGIN + "");
1257                    methodFooter.add(javaShortComment + COMTESTID + " " + COMTESTEND + "\n");
1258                    rawMode = true;
1259                    initLine = lineNr+1;
1260                    baseIndent = "";
1261                    return;
1262                }
1263                methodName = TEST + s.substring(p+STARTOFTEST.length());
1264                methodName = removeAllAfter(methodName, "\"");
1265                if ( methodName.equals(TEST)) methodName = tryToFindMethodName(contents,lineNr);
1266                methodHeader.add("\n");
1267                methodHeader.add("\n");
1268                methodHeader.add(javaShortComment + COMTESTID+ " " + COMTESTBEGIN + "\n");
1269                if ( methodThrows.length() == 0 ) // generate simple comment
1270                  methodHeader.add("  /** " +methodName + METHODINDEXMARKER +" */\n");
1271                else { // generate better comments for @throws
1272                    methodHeader.add("  /** \n");
1273                    methodHeader.add("   * " + methodName + METHODINDEXMARKER +" \n");
1274                    String exception = remove(methodThrows, "throws");
1275                    Strings excptions = splitBy(exception, ",");
1276                    for (String exp:excptions) 
1277                      methodHeader.add("   * @throws " + exp.trim() +" \n");
1278                    methodHeader.add("   */\n");
1279                    
1280                }
1281                methodHeader.add("  @Test\n");
1282                methodHeader.add("  public void "+methodName + METHODINDEXMARKER +"() " + methodThrows + "{");
1283                //methodHeader.add("  @Test // " + COMTESTID+ " " + COMTESTBEGIN + "\n");
1284                //methodHeader.add("  public void "+methodName + METHODINDEXMARKER +"() {");
1285                methodFooter.add("  } // " + COMTESTID + " " + COMTESTEND + "\n");
1286                initLine = lineNr+1;
1287            }
1288            
1289
1290            /**
1291             * Reads the code from contents starting from line start
1292             * and returns the line last handled.  
1293             * The ComTest block ends to next @-line or
1294             * to end of pre-tag.
1295             * @param start first line index to handle
1296             * @return last line index handled
1297             */
1298            public int readCode(int start) { // NOPMD by vesal on 13.1.2008 15:30
1299                methodThrows = "";
1300                printedTableLines = 0;
1301                try {
1302                    for ( lineNr = start; lineNr < contents.size(); lineNr++ ) {
1303                        String s = contents.get(lineNr);
1304                        s = saveStartAndEndOfLine(s);
1305                        if ( s.indexOf("*/") >= 0 ) return lineNr;
1306                        if ( s.indexOf("</pre>") >= 0 ) return lineNr;
1307                        s = removeFromBeginingTrim(s,"*");
1308                        if ( !rawMode ) s = s.trim(); // in raw-mode save spaces
1309                        if ( s.length() <= 0 ) {
1310                            if ( rawMode ) // In raw-mode empty lines preserved 
1311                                outLines(endOfLine, lineNr+1);
1312                            continue;
1313                        }
1314                        s = isPseudoComment(s); 
1315                        
1316                        if ( ( s.charAt(0)== '@' ) && !jUnitAnnotate(s) ) return lineNr;
1317                        s = handleAnnotationInComments(s);
1318                        if ( checkVariables(s)) continue;
1319
1320                        if ( s.indexOf(STARTOFTEST) >= 0 ) {
1321                            checkMethodThrows(contents.get(lineNr+1));
1322                            createHeaderAndFooter(s);
1323                            continue;
1324                        }
1325
1326                        if ( handleImports(s) ) continue; // Is this needed anymore?
1327                        
1328                        if ( methodName == null ) continue;
1329
1330                        if ( indexOfNotInQuotes(s, SEPARATORLINE) >= 0 ) continue;
1331
1332                        if ( indexOfNotInQuotes(s, INITLINE) >= 0 ) { // new init
1333                            if ( vars == null && EVERYLINEINITS1.equals(INITLINE) ) everyLineInits = true;
1334                            else outFooter(lineNr+1);
1335                            continue;
1336                        }
1337
1338                        if ( indexOfNotInQuotes(s, EVERYLINEINITS1) >= 0 || 
1339                             indexOfNotInQuotes(s, EVERYLINEINITS2) >= 0 ) {
1340                            everyLineInits = true;
1341                            continue;
1342                        }
1343                        
1344                        if ( isTableLine(s) ) { handleTableLine(s); continue;  }
1345
1346                        handleSentences(s);
1347                    }
1348                } finally {
1349                    if ( !methodHeaderPrinted && initCmds.size() > 0 ) outNewHeader();
1350                    if ( template.size() > 0 && printedTableLines == 0 ) {
1351                        if ( !methodHeaderPrinted  ) outNewHeader();
1352                        addOut(template,LINENUMBERMARKER,""+(lineNr+1));
1353                    }
1354                    outFooter(0);
1355                }
1356                return lineNr;
1357            }
1358        }
1359
1360
1361        /** 
1362         * Returns integer i by at least two characters
1363         * @param i
1364         * @return i by at least two characters
1365         */
1366        private static String two(int i) {
1367            if ( i >= 10 || i < 0 ) return "" + i;
1368            return "0" + i;
1369        }
1370        
1371
1372        /**
1373         * Returns version string for class comments
1374         */
1375        private static String getComTestVersion() {
1376            Calendar cal = Calendar.getInstance();
1377            String timestamp = ""+cal.get(Calendar.YEAR) + "." + two(cal.get(Calendar.MONTH)+1) + "." +
1378                               two(cal.get(Calendar.DAY_OF_MONTH)) + " " +
1379                               two(cal.get(Calendar.HOUR_OF_DAY)) + ":" + two(cal.get(Calendar.MINUTE)) + ":" +
1380                               two(cal.get(Calendar.SECOND));
1381            return " * @version " + timestamp + " // "+ COMTESTID;
1382        }
1383        
1384        
1385        /**
1386         * Scans String list and deletes all lines written by ComTest
1387         * @param testContents list to scan
1388         * @param outlines list where is the result (all lines with \n)
1389         */
1390        private static void deleteComTestLines(Strings testContents,Strings outlines) { // NOPMD by vesal on 13.1.2008 15:30
1391            if ( testContents == null ) return;
1392            int blankLines = 0;
1393            boolean inOwnWrite = false;
1394            for (String s : testContents ) {
1395                if ( ( indexOfNotInQuotes(s, COMTESTID) >= 0 ) || ( indexOfNotInQuotes(s, COMTESTIDOLD) >= 0 ) ) {
1396                    if ( indexOfNotInQuotes(s,"@version") >= 0 ) outlines.add(getComTestVersion()+"\n");  
1397                    else if ( indexOfNotInQuotes(s, COMTESTBEGIN) >= 0 ) inOwnWrite = true;
1398                    else if ( indexOfNotInQuotes(s, COMTESTEND) >= 0 ) inOwnWrite = false;
1399                    continue;
1400                }    
1401                if ( inOwnWrite ) continue;
1402                
1403                if ( s.trim().length() == 0 ) {
1404                    blankLines++;
1405                    if ( blankLines >= 2) continue;
1406                } else blankLines = 0;
1407                
1408                outlines.add(s+"\n");
1409               
1410            }
1411            
1412            // Delete last closing }
1413            for ( int ir = outlines.size()-1; ir >= 0; ir--) {
1414               String s = outlines.get(ir); 
1415               if ( indexOfNotInQuotes(s, "}") >= 0 ) {
1416                   outlines.removeRange(ir,outlines.size());
1417                   return;
1418               }
1419            }
1420        }
1421
1422
1423        /**
1424         * May change outFileName to same directory, if no package
1425         * @param fileName name to use as a base name 
1426         * @throws IOException 
1427         */
1428        private void checkFileNameOnceAgain(String fileName) throws IOException  {
1429            if ( testFilePackageName.length() > 0 ) return;
1430            directory = "";
1431            findNames(fileName);
1432        }
1433
1434        
1435        /**
1436         * Find outName and className from fileName
1437         * @param fileName name to look;
1438         * @return true
1439         * @throws IOException 
1440         */
1441        public boolean findNames(String fileName) throws IOException {
1442            File file = new File(fileName);
1443            String name = file.getName();
1444            String dir = removeAllAfter(file.getPath(),name);
1445            File iniFile = new File(dir,COMTESTINI);
1446            comTestIni = iniFile.getCanonicalPath();
1447            
1448            if ( directory != null && directory.length() > 0 ) {
1449                File outdir = new File(directory);
1450                if ( outdir.isAbsolute() ) dir = directory;
1451                else dir = dir + directory;
1452            }
1453            className = justFilePart(name);
1454            outFileName = addToName(name,"Test");
1455            testClassName = justFilePart(outFileName);
1456            File outfile = new File(dir,outFileName); 
1457            outDirName = dir;
1458            outFileName = outfile.getCanonicalPath();
1459            return true;            
1460        }
1461        
1462        /**
1463         * Finds the package line from contents.
1464         * Substitutes to packageName
1465         * @param contents where to find
1466         * @return true if found
1467         */
1468        private boolean findPackage(Strings contents) {
1469            String pack;
1470            for (String s:contents) {
1471                if ( indexOfNotInQuotes(s, "package") >= 0 ) {
1472                    pack = removeFromBegining(s,"package").trim();
1473                    pack = removeAllAfter(pack,";").trim();
1474                    if ( packageName.length() == 0 ) packageName = pack;
1475                    originalPackageName = pack;
1476                    return true;
1477                }    
1478            }
1479            return false;
1480        }
1481
1482        /**
1483         * Reads possible old file and removes all ComTest generated code
1484         * and create the output to outlines
1485         * @throws IOException if outPutFile is not possible to open
1486         */
1487        private void createTestFileBegin() throws IOException {
1488            String packageline = "";
1489            
1490            Strings testContents = getFileContents(outFileName);
1491            outlines = new Strings();
1492            deleteComTestLines(testContents,outlines);
1493            if ( packageName.length() > 0 ) {
1494                String pack = packageName;
1495                if ( ".".equals(pack) ) pack = originalPackageName; 
1496                else if ( pack.charAt(0) == '.' ) {
1497                    pack = originalPackageName + "." + pack.substring(1);
1498                }
1499                if ( originalPackageName.length() == 0 ) pack = "";
1500                packageline = "package " + pack + ";";
1501                testFilePackageName = pack;
1502            }
1503
1504            imports.add("import static org.junit.Assert.*;");
1505            imports.add("import org.junit.*;");
1506
1507            if ( outlines.size() > 2 ) return;
1508
1509            if ( testFilePackageName.length() > 0 ) addOutln(packageline);
1510            addOutln("");
1511            addOutln("/**");
1512            addOutln(" * Test class made by ComTest");
1513            addOutln(getComTestVersion()); 
1514            addOutln(" *"); 
1515            addOutln(" */");
1516            addOutln("public class " + testClassName + " {");           
1517        }
1518
1519
1520        /**
1521         * Creates the inner part of test class
1522         */
1523        private void createTestFileInner() { // NOPMD by vesal on 13.1.2008 15:30
1524            boolean javaComment=false;
1525            for (int i=0; i < contents.size(); i++ ) {
1526                String s = contents.get(i).trim();
1527                if ( checkVariables(s) ) continue;
1528                if ( javaComment && indexOfNotInQuotes(s,"*/")>=0 ) {
1529                    javaComment = false;
1530                    continue;
1531                }
1532                if ( !javaComment ) {
1533                    int isc = indexOfNotInQuotes(s, "/*"); 
1534                    int iec = indexOfNotInQuotes(s, "*/",QUOTES,isc+1); 
1535                    if ( isc >= 0 && iec < 0 ) javaComment = true;
1536                    continue;
1537                }
1538                if ( s.indexOf(STARTOFTEST) >= 0 ) {
1539                    CommentCodeHandler codeHandler = new CommentCodeHandler();  // NOPMD by vesal on 13.1.2008 15:31
1540                    i = codeHandler.readCode(i)-1;
1541                }    
1542            }
1543        }
1544     
1545        /**
1546         * Check the selected line by ComTest options
1547         * @param line what to check
1548         * @param all to check all or only part
1549         *
1550         * @example
1551         * <pre name="test">
1552         *  TestHelpper t = new TestHelpper();
1553         *  t.scanner.checkOptionLine("    * #import imp1; // ", true);
1554         *  t.imports.get(0) === "import imp1;";
1555         *  t.scanner.checkOptionLine("    *  #import imp2; // ", true);
1556         *  t.imports.size() === 1; 
1557         *  t.scanner.checkOptionLine("    // #import imp2; // ", true);
1558         *  t.imports.get(1) === "import imp2;";
1559         *  t.scanner.checkOptionLine("    //#import imp3; // ", true);
1560         *  t.imports.get(2) === "import imp3;";
1561         *  t.scanner.checkOptionLine("    *#import imp4; // ", true);
1562         *  t.imports.get(3) === "import imp4;";
1563         *  t.scanner.checkOptionLine("    //  #import imp5; // ", true);
1564         *  t.imports.size() === 4; 
1565         * </pre>
1566         */
1567        public void checkOptionLine(String line, boolean all) {
1568            String s = line.trim(); 
1569            if ( s.matches("^\\* ?[^ ].*") ) s = s.substring(1).trim();     // allow: * #import anywhere
1570            else if ( s.matches("^// ?[^ ].*") ) s = s.substring(2).trim(); // or: // #import 
1571            s = removeAllAfter(s, "//");
1572            if ( handleDirAndPackage(s) ) return;
1573            if ( handleImports(s) ) return;
1574            if ( !all ) return;
1575            if ( checkVariables(s) ) return;
1576        }
1577
1578        /**
1579         * Check the selected contents by ComTest options
1580         * @param contents what to check
1581         * @param all to check all or only part
1582         *
1583         */
1584        private void checkOptions(Strings contents, boolean all) {
1585            if ( contents == null ) return;
1586            for (int i=0; i < contents.size(); i++ ) {
1587                checkOptionLine(contents.get(i),all);
1588            }
1589        }
1590
1591        
1592       /**
1593        * Adds the line s+"\n" to outlines if it is not there already.
1594        * The line "is there" if trim of it starts with s  
1595        * @param i place to add if not already there
1596        * @param s line to add if not found
1597        * @return next place to add so i+1 if added, i if not
1598        */ 
1599       private int addIfNotAllready(int i,String s) {
1600           for ( String st:outlines ) 
1601               if ( st.trim().startsWith(s ) ) return i;
1602           outlines.add(i,s+"\n");
1603           return i+1;
1604       }
1605        
1606        
1607        /**
1608         * Add imports-list to outlines
1609         * Also if doStaticImport is true then add also static import
1610         */
1611        private void addImports() { // NOPMD by vesal on 13.1.2008 21:08
1612            int i; 
1613            boolean javaComment=false;
1614
1615            for (i=0; i<outlines.size()-1; i++) {
1616                String s = outlines.get(i).trim();
1617                if ( javaComment ) { 
1618                  if ( indexOfNotInQuotes(s,"*/")>=0 ) javaComment = false;
1619                  continue;
1620                }
1621                int isc = indexOfNotInQuotes(s, "/*"); 
1622                int iec = indexOfNotInQuotes(s, "*/",QUOTES,isc+1); 
1623                if ( isc >= 0 && iec < 0 ) {
1624                    javaComment = true;
1625                    continue;
1626                }    
1627                if ( isEmpty(s) )                { break;      }
1628                if ( s.indexOf("package") >= 0 ) { i++; break; }
1629                if ( s.indexOf("class")   >= 0 ) { break;      }
1630                if ( s.indexOf("import")  >= 0 ) { break;      }
1631            }
1632            outlines.add(i++,"// " + COMTESTID+ " " + COMTESTBEGIN + "\n");    
1633            for ( String s:imports ) i = addIfNotAllready(i,s);
1634            if ( doStaticImport ) i = addIfNotAllready(i,"import static " + originalPackageName + "." + className +".*;");
1635            if ( !noAutoImport && !originalPackageName.equals(testFilePackageName) ) i = addIfNotAllready(i,JAVAIMPOPRT + originalPackageName + "." + className +";");
1636            outlines.add(i++,"// " + COMTESTID+ " " + COMTESTEND + "\n");    
1637        }
1638
1639        /**
1640         * Climb up from filename and find first ComTest.ini
1641         * and read that
1642         * @param filename name with path to start climbing
1643         */
1644        private void checkIniOptions(String filename) {
1645            File file = new File(filename);
1646            String name = file.getName();
1647            String dir = removeAllAfter(file.getPath(),name);
1648            while ( !isEmpty(dir) ) {
1649              Strings iniContents = getFileContents(dir+name);
1650              if ( iniContents != null ) {
1651                checkOptions(iniContents,true);
1652                return;
1653              }
1654              dir = dir.substring(0,dir.length()-1);
1655              String filepart = justFilePart(dir);
1656              dir = dir.substring(0,dir.length()-filepart.length());
1657            }  
1658        }
1659        
1660        /**
1661         * Scan the contents and make a test class
1662         * @param fileName filename read to contents
1663         * @return outName if ok, null if something wrong
1664         */
1665        public String scan(String fileName) {
1666            PrintWriter out = null;
1667            boolean status = false;
1668            try {
1669                findNames(fileName);
1670                checkIniOptions(comTestIni);
1671                findPackage(contents);
1672                checkOptions(contents,false);
1673                findNames(fileName); // May have changed due options
1674                createTestFileBegin();
1675                createTestFileInner();
1676                addOut("}");
1677                addImports();
1678                
1679                checkFileNameOnceAgain(fileName);
1680                
1681                status = new File(outDirName).mkdirs();
1682                out = new PrintWriter(new FileWriter(outFileName));
1683                println(out,outlines,"");
1684                return outFileName;
1685            } catch (IOException e) {
1686                String s = "";
1687                if ( !status ) s = "\nNot able to make dir: " + outDirName;
1688                System.err.println(fileName +  " => " + outFileName + " " + e.getMessage() + s);
1689                return null;
1690            } finally {
1691                if ( out != null ) out.close();
1692            }
1693        }
1694
1695    }
1696
1697
1698    /**
1699     * Process the file and make a unit test
1700     * @param fileName the name of the file to process
1701     * @return handled filename in success, null otherwise
1702     */
1703    public static String processFile(String fileName) {
1704        if ( !fileName.endsWith(".java") ) return null;
1705        //if ( fileName.endsWith("Test.java") ) return null;
1706        Strings contents = getFileContents(fileName);
1707        if ( contents == null ) return null;
1708        if ( contents.size() < 1) return null;
1709
1710        ComTestScanner comtest = new ComTestScanner(contents);
1711        return comtest.scan(fileName);
1712    }
1713
1714
1715    /**
1716     * @param args name of files to process
1717     */
1718    public static void main(String[] args) {
1719        
1720        if ( args.length == 0 ) {
1721            System.out.println("Run: java comtest.ComTest filename[s]");
1722            System.out.println("or: java -jar comtest.jar filename[s]");
1723            return;
1724        }
1725
1726        for ( int i = 0; i < args.length; i++ ) {
1727            String outname = processFile(args[i]);
1728            if ( outname == null ) System.out.println(args[i] + " error");
1729            else System.out.println(args[i] + " => " + outname + " ok");
1730        }   
1731    }
1732
1733    /**
1734     * A class to help testing ComTest-functions.
1735     * @author vesal
1736     *
1737     */
1738    public static class TestHelpper {
1739        /** contents of the files       */
1740        public Strings contents;
1741        
1742        /** Global test scanner         */
1743        public ComTest.ComTestScanner scanner;
1744        
1745        /** Global test codeHandler     */
1746        public ComTest.ComTestScanner.CommentCodeHandler codeHandler;
1747        
1748        /** Global list for test initCmnds */
1749        public Strings initCmnds;
1750        
1751        /** Global list for test templates */
1752        public Strings template;
1753        
1754        
1755        /** Global list for test outLines  */
1756        public Strings outLines;
1757        
1758        /** Import lines */
1759        public Strings imports;
1760        
1761        /** 
1762         * Initializer for TestHelper      
1763         */
1764        public TestHelpper() {
1765          contents = new Strings();
1766          scanner = new ComTest.ComTestScanner(contents);
1767          try { scanner.findNames(TEST);  } catch (java.io.IOException e) { 
1768              System.err.println(e.getMessage());
1769          } 
1770          codeHandler = scanner.createCommentCodeHandler();
1771          initCmnds = codeHandler.getInitCmnds();
1772          template  = codeHandler.template;
1773          outLines = scanner.getOutlines();
1774          imports = scanner.imports;
1775        }  
1776        
1777    }
1778    
1779}
1780