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     public static class ComTestScanner { // NOPMD by vesal on 13.1.2008 15:29
517         private static final String JAVAIMPOPRT = "import ";
518         private final Strings contents;
519         private final Strings imports = new Strings();
520         private Strings outlines = null;
521         private boolean doStaticImport = false;
522         private boolean noAutoImport = false;
523         private String className = null;
524         private String outFileName = null;
525         private String outDirName  = null;
526         private String testClassName = null;
527         private String packageName = "";
528         private String originalPackageName = "";
529         private String testFilePackageName = "";
530         private String directory = null;
531         private String comTestIni = null;
532         private int printedTableLines = 0;
533         
534         private String COLUMNSEPARATOR      = ComTest.COLUMNSEPARATOR;
535         private String COLUMNCHAR           = ComTest.COLUMNCHAR;
536         private String TEMPLATELINEMARKER   = ComTest.TEMPLATELINEMARKER;
537         private String DELETESENTENCEMARKER = ComTest.DELETESENTENCEMARKER;
538         private String INITLINE             = ComTest.INITLINE;
539         private String EVERYLINEINITS1      = ComTest.EVERYLINEINITS1;
540         private String EVERYLINEINITS2      = ComTest.EVERYLINEINITS2;
541         private String EQUALSMARKER         = ComTest.EQUALSMARKER;
542         private String EQUALSMARKER2        = ComTest.EQUALSMARKER2;
543         private String ALMOSTMARKER1        = ComTest.ALMOSTMARKER1;
544         private String ALMOSTMARKER2        = ComTest.ALMOSTMARKER2;
545         private String REGEXPMARKER1        = ComTest.REGEXPMARKER1;
546         private String REGEXPMARKER2        = ComTest.REGEXPMARKER2;
547         private String TOLERANCE            = ComTest.TOLERANCE;
548         private String THROWSMARKER         = ComTest.THROWSMARKER;
549         private String IMPORTMARKER         = ComTest.IMPORTMARKER;
550         private String PACKAGEMARKER        = ComTest.PACKAGEMARKER;
551         private String DIRECTORYMARKER      = ComTest.DIRECTORYMARKER;
552         
553 
554         /**
555          * Constructs the new ComTestScanner from stringlist contents
556          * @param contents where to scan the comment tests
557          */
558         ComTestScanner(Strings contents) { 
559             this.contents = contents;
560             outlines = new Strings();
561         }
562 
563 
564         
565         /**
566          * Checks if s is for setting imports.  If is, then true
567          * is returned and the import is handled.
568          * @param line line to check
569          * @return true if import line, false otherwise 
570          */
571         private boolean handleImports(String line) { // NOPMD by vesal on 13.1.2008 21:05
572             String s = line.trim();
573             
574             if ( indexOfNotInQuotes(s,NOIMPORT) == 0 ) {
575                 imports.clear();
576                 doStaticImport = false;
577                 noAutoImport = true;
578                 return true;
579             }
580             if ( indexOfNotInQuotes(s,STATICIMPORT) == 0 ) {
581                 doStaticImport = true;
582                 noAutoImport = true;
583                 return true;
584             }
585             if ( indexOfNotInQuotes(s,DYNAMICIMPORT) == 0 ) {
586                 if ( originalPackageName.length() == 0) return true; 
587                 imports.add(JAVAIMPOPRT + originalPackageName + "." + className + ".*;");
588                 noAutoImport = true;
589                 return true;
590             }
591             if ( indexOfNotInQuotes(s,CLASSIMPORT) == 0 ) {
592                 if ( originalPackageName.length() == 0) return true; 
593                 imports.add(JAVAIMPOPRT + originalPackageName + "." + className +";");
594                 noAutoImport = true;
595                 return true;
596             }
597             if ( indexOfNotInQuotes(s,PACKAGEIMPORT) == 0 ) {
598                 if ( originalPackageName.length() == 0) return true; 
599                 imports.add(JAVAIMPOPRT + originalPackageName + ".*;");
600                 noAutoImport = true;
601                 return true;
602             }
603             if ( indexOfNotInQuotes(s, IMPORTMARKER) == 0 ) {
604                 String imp = s.substring(IMPORTMARKER.length()+1).trim();
605                 imports.add(JAVAIMPOPRT + imp);
606                 noAutoImport = true;
607                 return true;
608             }
609             
610             return false;
611         }
612 
613         /**
614          * Handles directory and package specific lines
615          * @param line line to handle
616          * @return true if handled
617          */
618         private boolean handleDirAndPackage(String line) {
619             String s = line.trim();
620             if ( indexOfNotInQuotes(s, DIRECTORYMARKER) == 0 ) {
621                 directory = s.substring(DIRECTORYMARKER.length()).trim();
622                 return true;
623             }
624             
625             if ( indexOfNotInQuotes(s, PACKAGEMARKER) == 0 ) {
626                 packageName = s.substring(PACKAGEMARKER.length()).trim();
627                 return true;
628             }
629             return false;
630         }
631         
632         
633 
634         /**
635          * Check if s is a substitution to specified variable
636          * @param s        string to looks for
637          * @param varName  varName to look
638          * @return         new value for variable, null if not variable
639          */
640         private String checkVariable(String s, String varName) {
641             if ( !s.startsWith("#"+varName+"=") ) return null;
642             String val = s.substring(varName.length()+2).trim();
643             val = val.replace("\"", "");
644             val = val.replace("'", "");
645             return val;
646         }
647         
648         /**
649          * Check is d is substitution to any know variable
650          * @param st string to check
651          * @return true if any variable, false otherwise
652          */
653         private boolean checkVariables(String st) { // NOPMD by vesal on 13.1.2008 15:29
654             String val;
655             String s = removeAllAfter(st,";").trim();
656             val = checkVariable(s,"COLUMNSEPARATOR"     ); if ( val != null ) { COLUMNSEPARATOR      = val; return true; }
657             val = checkVariable(s,"TEMPLATELINEMARKER"  ); if ( val != null ) { TEMPLATELINEMARKER   = val; return true; }
658             val = checkVariable(s,"DELETESENTENCEMARKER"); if ( val != null ) { DELETESENTENCEMARKER = val; return true; }
659             val = checkVariable(s,"COLUMNCHAR"          ); if ( val != null ) { COLUMNCHAR           = val; return true; }
660             val = checkVariable(s,"INITLINE"            ); if ( val != null ) { INITLINE             = val; return true; }
661             val = checkVariable(s,"EVERYLINEINITS1"     ); if ( val != null ) { EVERYLINEINITS1      = val; return true; }
662             val = checkVariable(s,"EVERYLINEINITS2"     ); if ( val != null ) { EVERYLINEINITS2      = val; return true; }
663             val = checkVariable(s,"EQUALSMARKER"        ); if ( val != null ) { EQUALSMARKER         = val; return true; }
664             val = checkVariable(s,"EQUALSMARKER2"       ); if ( val != null ) { EQUALSMARKER2        = val; return true; }
665             val = checkVariable(s,"ALMOSTMARKER1"       ); if ( val != null ) { ALMOSTMARKER1        = val; return true; }
666             val = checkVariable(s,"ALMOSTMARKER2"       ); if ( val != null ) { ALMOSTMARKER2        = val; return true; }
667             val = checkVariable(s,"REGEXPMARKER1"       ); if ( val != null ) { REGEXPMARKER1        = val; return true; }
668             val = checkVariable(s,"REGEXPMARKER2"       ); if ( val != null ) { REGEXPMARKER2        = val; return true; }
669             val = checkVariable(s,"TOLERANCE"           ); if ( val != null ) { TOLERANCE            = val; return true;}
670             val = checkVariable(s,"THROWSMARKER"        ); if ( val != null ) { THROWSMARKER         = val; return true; }
671             val = checkVariable(s,"IMPORTMARKER"        ); if ( val != null ) { IMPORTMARKER         = val; return true; }
672             val = checkVariable(s,"PACKAGEMARKER"       ); if ( val != null ) { PACKAGEMARKER        = val; return true; }
673             val = checkVariable(s,"DIRECTORYMARKER"     ); if ( val != null ) { DIRECTORYMARKER      = val; return true; }
674             return false;
675         }
676 
677 
678         /**
679          * Add string s to outlines 
680          * @param s
681          */
682         private void addOut(String s) {
683             outlines.add(s);
684         }
685 
686         /**
687          * Add string s to outlines with new line
688          * @param s
689          */
690         private void addOutln(String s) {
691             outlines.add(s+"\n");
692         }
693         
694         /**
695          * Add strings from list to outlines by separating from new line
696          * @param list strings to add
697          */
698         private void addOut(Strings list) {
699             for (String s:list) addOut(s);
700         }
701 
702         /**
703          * Prints the stringlist by replacing target-strings by replacement strings
704          * @param out     stream to print
705          * @param list    stringlist to print
706          * @param sep     string to separate items 
707          */
708         private void println(PrintWriter out, Strings list, String sep) {
709             for (String s:list) out.print(s+sep);
710         }
711         
712         
713         /**
714          * Prints the stringlist by replacing target-strings by replacement strings
715          * Separator is not printed after last line
716          * @param list    stringlist to print
717          * @param target  string that is replaced 
718          * @param replacement string by what to replace
719          */
720         private void addOut(Strings list, String target, String replacement) {
721             if ( list.size() == 0 ) return;
722             for (int i=0; i<list.size();i++) 
723                 addOut(list.get(i).replace(target, replacement));
724         }
725         
726         /**
727          * Deletes from the list all lines that contains target
728          * outside of quotes and comments
729          * @param list   where to delete
730          * @param target what is condition for delete
731          * @return the list itself
732          */
733         private Strings delete(Strings list, String target, String quotes) {
734            int ir,iw = 0; 
735            for (ir=0; ir<list.size(); ir++) {
736                String s = list.get(ir);
737                int p = indexOfNotInQuotes(s, target, quotes);
738                int pc = indexOfNotInQuotes(s, "//", quotes);
739                // int p = s.indexOf(target);
740                if ( p < 0 || ( 0 <= pc  && pc < p ) ) list.set(iw++, s);
741            }
742            if ( ir != iw ) list.removeRange(iw,ir);
743            return list;
744         }
745         
746         
747         /**
748          * This method is just for testing purposes to return
749          * current outlines.
750          * @return current outlines.
751          */
752         public Strings getOutlines() {
753             return outlines;
754         }
755         
756         /**
757          * This factory method is just for testing purposes
758          * @return a new handler
759          */
760         public CommentCodeHandler createCommentCodeHandler() {
761             return new CommentCodeHandler(); 
762         }
763         
764         /**
765          * Handles to ComTest-comment and returns the position
766          * of last handled line. 
767          * The main method is readCode and all others are just helper
768          * methods to handle the lines. 
769          * Precondition: inside JavaDoc comment
770          * Postcondition: still inside JavaDoc comment 
771          */
772         public class CommentCodeHandler { // NOPMD by vesal on 13.1.2008 15:29
773             private static final String javaShortComment = "  // ";
774             private int lineNr;
775             private String methodName = null;
776             private final Strings template = new Strings();
777             private final Strings initCmds = new Strings();
778             private final Strings methodHeader = new Strings();
779             private final Strings methodFooter = new Strings();
780             private boolean methodHeaderPrinted = false;
781             private int methodIndex = 1;
782             private String sMethodIndex = "";
783             private Strings vars = null;
784             private Strings values = null;
785             private int initLine = 0;
786             private boolean everyLineInits = false; 
787             private boolean tablePrinted = false;
788             private boolean oneTablePrinted = false;
789             private boolean rawMode = false;
790             private String baseIndent = "  ";
791             private String indent = baseIndent;
792             private final Strings startOfLine = new Strings();
793             private final Strings endOfLine = new Strings();
794             private String methodThrows = "";
795             
796 
797             /**
798              * This is just for testing purposes to return the initCmds
799              * @return initCmnds
800              */
801             public Strings getInitCmnds() {
802                 return initCmds;
803             }
804             
805             
806             /**
807              * Try to find name for next method.  If no name is found, then
808              * a sequence number is used. 
809              * This is very unfinished and works only for nice code
810              * where the method name and type is on the same line.
811              * @param contents where to find the name
812              * @param lineNr where to start
813              * @return test method name
814              */
815             public String tryToFindMethodName(Strings contents, int lineNr) { // NOPMD by vesal on 13.1.2008 15:29
816                 String autoNumber = ""+(lineNr+1);
817                 String name;
818                 String type="";
819                 boolean inComment = true; 
820                 for ( int i=lineNr; i<contents.size(); i++ ) {
821                     String s = contents.get(i);
822                     s = removeAllAfter(s, "//").trim();
823                     if ( s.length() == 0 ) continue;
824                     if ( indexOfNotInQuotes(s,"/*") >= 0 ) { inComment = true; continue; }
825                     if ( indexOfNotInQuotes(s,"*/") >= 0 ) { inComment = false; continue; }
826                     if ( inComment ) continue;
827                     s = remove(s,"public");
828                     s = remove(s,"protected");
829                     s = remove(s,"private");
830                     s = remove(s,"static");
831                     s = remove(s,"final");
832                     s = remove(s,"class");
833                     s = removeAllAfter(s,"extends");
834                     s = removeAllAfter(s,"implements");
835                     s = s.trim();
836                     int p = s.indexOf(' ');
837                     if ( p >= 0 ) { type = s.substring(0,p); s = s.substring(p+1); }
838                     int ic;
839                     for ( ic=0; ic<s.length(); ic++)
840                         if ( !Character.isJavaIdentifierPart(s.charAt(ic)) ) break;
841                     name = s.substring(0,ic);
842                     if ( name.length() == 0 ) name = type; 
843                     if ( name.length() == 0 ) return TEST + autoNumber;
844                     name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
845                     return TEST + name + autoNumber;
846                 }
847                 return TEST + autoNumber;
848             }
849 
850             
851             /**
852              * Check if line is THROWS line.  If it is, correct
853              * the start and end lists as needed.
854              * @param s line to look.
855              * @return line without THROWS sentence
856              */
857             private String checkThrows(String s) {
858                 int p = indexOfNotInQuotes(s, THROWSMARKER);
859                 if ( p < 0 ) return s;
860                 indent = baseIndent + "";
861                 String sentence = s.substring(0,p);
862                 String exception = s.substring(p+THROWSMARKER.length()).trim();
863                 startOfLine.add(0,baseIndent + "  try {\n");
864                 endOfLine.add(0,"\n"+indent + "  fail(\"" + className + ": " + (lineNr+1) +" Did not throw " + exception + "\");\n");
865                 endOfLine.add(1,baseIndent + "  } catch(" + exception + " e){ e.getMessage(); }");
866                 return sentence;
867             }
868             
869             /**
870              * Check if line is THROWS line.  If it is, correct
871              * the start and end lists as needed.
872              * @param s line to look.
873              * @return line without THROWS sentence
874              */
875             private String checkMethodThrows(String s) {
876                 int p = indexOfNotInQuotes(s, THROWSMARKER);
877                 if ( p < 0 ) return s;
878                 String sentence = remove(s.substring(0,p),"*").trim();
879                 if ( sentence.length() > 0 ) return s;
880                 String exception = s.substring(p+THROWSMARKER.length()).trim();
881                 methodThrows = "throws " + exception + " ";
882                 return sentence;
883             }
884             
885             
886             /**
887              * Find comments and put possible try catch to start and end
888              * of the the current line's start and end lists.
889              * @param s line to handle
890              * @return the line where comments and THROWS-sentence is removed.
891              */
892             private String saveStartAndEndOfLine(String s) {
893                 startOfLine.clear();
894                 endOfLine.clear();
895                 indent = baseIndent;
896                 int p = indexOfNotInQuotes(s, "//");
897                 if ( p < 0 ) {
898                   endOfLine.add("\n");
899                   return checkThrows(s);
900                 }
901                 endOfLine.add(" " + s.substring(p) + "\n"); 
902                 return checkThrows(s.substring(0,p)); 
903             }
904 
905 
906             /**
907              * Copies method footer to outlines if method header is
908              * already moved.  Nothing is done if method header
909              * is not copied.
910              * @param realLineNr current line number 
911              */
912             private void outFooter(int realLineNr) {
913                 if ( methodHeaderPrinted ) { 
914                     addOut(methodFooter);
915                     if ( realLineNr > 0 ) initLine = realLineNr;
916                 }
917                 methodHeaderPrinted = false;
918             }
919 
920             
921             /**
922              * Moves header lines to outlines.  Possible method
923              * index markers are replaced by method index.
924              */
925             public void outNewHeader() {
926                 outFooter(0);
927                 addOut(methodHeader, METHODINDEXMARKER, sMethodIndex);
928                 methodIndex++; 
929                 sMethodIndex = ""+methodIndex; 
930                 methodHeaderPrinted = true;
931                 if ( initLine == 0 ) initLine = lineNr+1;
932                 addOutln(baseIndent + javaShortComment + className + ": " + initLine);
933                 addOut(initCmds);
934             }
935             
936             /**
937              * Moves list to outlines.  If header is not moved,
938              * then move it now. Possible target strings are replaced
939              * by replacement strings while moving.
940              * @param list what to move
941              * @param target what is replaced
942              * @param replacement by what to replace
943              */
944             private void outWithHeader(Strings list, String target, String replacement) {
945                 if ( !methodHeaderPrinted ) outNewHeader();
946                 addOut(list,target,replacement);
947             }
948             
949             
950             /**
951              * Add sentences to init section.  Possible line number markers
952              * are replaced by current line number.
953              * @param commands sentences to add
954              * @param realLineNr real line number for current line
955              */
956             private void addInit(Strings commands, int realLineNr) {
957                 if ( initLine == 0 ) initLine = realLineNr;
958                 initCmds.add(commands.replace(LINENUMBERMARKER, ""+realLineNr));
959             }
960             
961             
962             /**
963              * Moves commands to outlines.  If header is not moved,
964              * then move it now. Possible line number markers
965              * are replaced by current line number.
966              * @param commands what to move
967              * @param realLineNr real line number for current line
968              */
969             private void outLines(Strings commands, int realLineNr) {
970                 outWithHeader(commands,LINENUMBERMARKER, ""+realLineNr);
971             }
972             
973             
974             /**
975              * Check if s is one of the JUnit annotation.
976              * @param s line to look
977              * @return true if annotation, false otherwise.
978              */
979             private boolean jUnitAnnotate(String s) {
980                 if ( s.startsWith("@Test") )   return true;
981                 if ( s.startsWith("@Before") ) return true;
982                 if ( s.startsWith("@After") )  return true;
983                 return false;
984             }
985             
986             
987             /**
988              * Annotations in comments must be prefixed with something.
989              * This takes the prefixing away. 
990              * @param s where to look annotation
991              * @return s if no annotation, otherwise annotation with
992              *           prefix info removed. 
993              * @example
994              * <pre name="test">
995              *   TestHelpper t = new TestHelpper();
996              *   t.codeHandler.handleAnnotationInComments("a") === "a";    
997              *   t.codeHandler.handleAnnotationInComments("* #@Test") === "@Test";    
998              *   t.codeHandler.handleAnnotationInComments("* #@example") === "* #@example";    
999              * 
1000             * </pre>    
1001             */
1002            public String handleAnnotationInComments(String s) {
1003                int p = indexOfNotInQuotes(s, "@");
1004                if ( p < 0 ) return s;
1005                String st = s.substring(p);
1006                if ( jUnitAnnotate(st) ) return st; 
1007                return s;
1008            }
1009            
1010            /**
1011             * Deletes \n from the last line of cmds
1012             * @param cmds where to delete
1013             */
1014            private void deleteLastNewLine(Strings cmds) {
1015                if ( cmds.size() <= 0 ) return;
1016                String s = cmds.get(cmds.size()-1);
1017                if ( s.length() == 0 ) return;
1018                if ( s.charAt(s.length()-1) != '\n' ) return;
1019                cmds.set(cmds.size()-1,s.substring(0,s.length()-1)+" "); 
1020            }
1021            
1022            /**
1023             * Check if the line is a table line, so containing either " | "
1024             * or | at the beginning or end.
1025             * @param s line to check
1026             * @return true if was a table line.
1027             * @example
1028             * <pre name="test">
1029             *   TestHelpper t = new TestHelpper();
1030             *   t.codeHandler.isTableLine("") === false;    
1031             *   t.codeHandler.isTableLine("a") === false;    
1032             *   t.codeHandler.isTableLine("a|") === true;    
1033             *   t.codeHandler.isTableLine("|a") === true;    
1034             *   t.codeHandler.isTableLine(" a|b ") === false;    
1035             *   t.codeHandler.isTableLine(" a | b ") === true;    
1036             * </pre>
1037             */
1038            public boolean isTableLine(String s) {
1039                if ( indexOfNotInQuotes(s,COLUMNSEPARATOR) >= 0 ) return true;
1040                if ( s.startsWith(COLUMNCHAR) ) return true;
1041                if ( s.endsWith(COLUMNCHAR) ) return true;
1042                return false;
1043            }
1044
1045            /**
1046             * Handles lines like     "  $s        |  $what |  $result"
1047             * or                     " "aab"      |  "a"   |  "ab"  "
1048             * If there is a $ on the line, it is kept as a header line.
1049             * Otherwise as a normal values line.  From header line
1050             * the variable names a collected.  In normal line the
1051             * variable values a replaced in the corresponding template
1052             * line.  Sentences with unused variable names are removed. 
1053             * @param line line to handle.
1054             */
1055            private void handleTableLine(String line) { // NOPMD by vesal on 13.1.2008 15:29
1056                String s = line;
1057                if ( template.size() == 0 ) return; // No use of table if no template
1058                if ( s.startsWith(COLUMNCHAR) ) s = s.substring(1);
1059                if ( s.endsWith(COLUMNCHAR) ) s = s.substring(1,s.length()-1);
1060                if ( vars == null ) { // || indexOfNotInQuotes(s, TEMPLATELINEMARKER ) >= 0 ) {
1061                    vars = splitBy(s,COLUMNSEPARATOR,COLUMNSEPARATOR.length(),true);
1062                    return;
1063                }
1064                values = splitBy(s,COLUMNSEPARATOR,COLUMNSEPARATOR.length(),true); 
1065                Strings cmds = template.clone();
1066                for (int v=0; v<values.size(); v++ ) {
1067                    if ( v >= vars.size() ) break;
1068                    String value = values.get(v).trim();
1069                    String var = vars.get(v).trim();
1070                    if ( value.equals(DELETESENTENCEMARKER) ) delete(cmds,var,"");
1071                    else cmds.replace(var, value);
1072                }
1073                if ( everyLineInits ) outFooter(lineNr+1);
1074                delete(cmds, "$", QUOTES);  // remove sentences with $ outside ""
1075                deleteLastNewLine(cmds);
1076                cmds.addAll(0, startOfLine);
1077                cmds.add(endOfLine);
1078                outWithHeader(cmds,LINENUMBERMARKER, ""+(lineNr+1));
1079                printedTableLines += cmds.size();
1080
1081                if ( everyLineInits ) 
1082                    outFooter(lineNr+1);
1083                tablePrinted = true;
1084                oneTablePrinted = true;
1085            }
1086
1087            
1088            /**
1089             * Check if there is coming a ; then nothing is done.
1090             * If there is no ; coming, then one ; is added to commands.
1091             * @param sentences where to look ;
1092             * @param i from what index to look ;
1093             * @param commands where to add ; if needed
1094             */
1095            private void checkSemiColon(Strings sentences, int i, Strings commands) {
1096                if ( ( i < sentences.size() - 1 ) &&  
1097                     ( sentences.get(i+1).indexOf(";") >= 0 ) ) return;
1098                commands.add(";");
1099            }
1100            
1101
1102            /**
1103             * Check if line is pseudo comment /.* or  *./
1104             * @return comments handled or s
1105             */
1106            private String isPseudoComment(String line) {
1107                String s = line;
1108                s = s.replaceAll("\\/\\.\\*", "/*"); 
1109                s = s.replaceAll("\\*\\.\\/", "*/");
1110                return s;
1111            }
1112            
1113            /**
1114             * Handles normal lines and table lines from ComTest lines.
1115             * The line is first divided to "sentences" so that
1116             * ; { } are each one "sentence".  Then ===, ~~ is try to
1117             * find and in case of success the corresponding assert is generated.
1118             * If the line has at least on $ sign, all sentences are kept as
1119             * a template sentences.  Also after starting the template, all
1120             * lines are kept as template lines.
1121             * If generated assert-line does not include ; , then it is added.
1122             * @param line line to handle.
1123             * 
1124             * @example
1125             * <pre name="test">
1126             *   TestHelpper t = new TestHelpper();
1127             *     
1128             *   t.codeHandler.handleSentences("int a = 5;");
1129             *   t.initCmnds.get(1) === "int a = 5";
1130             *   
1131             *   t.initCmnds.clear();
1132             *   t.codeHandler.handleSentences("int a = 5; int b = 7;");
1133             *   t.initCmnds.get(0) === "    ";
1134             *   t.initCmnds.get(1) === "int a = 5";
1135             *   t.initCmnds.get(2) === "; ";
1136             *   t.initCmnds.get(3) === "int b = 7";
1137             *   t.initCmnds.get(4) === "; ";
1138             *   
1139             *   t.initCmnds.clear();
1140             *   t.codeHandler.handleSentences("a === 5;");
1141             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 5, a)";
1142             *
1143             *   t.initCmnds.clear();
1144             *   t.codeHandler.handleSentences("get(request,new String[]{\"str\",\"id\"},13,true) === 123;");
1145             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 123, get(request,new String[]{\"str\",\"id\"},13,true))";
1146             *   
1147             *   t.initCmnds.clear();
1148             *   t.codeHandler.handleSentences("\"cat\" =R= \"c.*t\";"); // "cat" =R= "c.*t";
1149             *   t.initCmnds.get(1) === "{ String l=\"cat\",r=\"c.*t\"; if ( !l.matches(r) ) fail(\"From: test line: 1\" + \" does not match: [\"+ l + \"] != [\" + r + \"]\");}";
1150             *   
1151             *   t.initCmnds.clear();
1152             *   t.codeHandler.handleSentences("a ~~~ 6.0;");  
1153             *   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 6.0, a, 0.000001)";
1154             *   
1155             *   t.initCmnds.clear();
1156             *   t.codeHandler.handleSentences("\"a$1\""); /// Not a template line  
1157             *   t.initCmnds.get(1) === "\"a$1\"";
1158             *   
1159             *   t.template.size() === 0;
1160             * </pre>
1161             * 
1162             * 
1163             * @example
1164             * <pre name="test">
1165             *   String s="$a"; s === "$b"; $
1166             *     $a | $b
1167             *    ---------
1168             *      1 | 1    
1169             *   
1170             *  $a; "a".matches("^a$") === false;  // does not generate code
1171             * </pre>
1172             * 
1173             * @example
1174             * <pre name="test">
1175             *   String s=null; s === null; 
1176             *   "a".matches("^a$") === true;  // generate code
1177             * </pre>
1178             * 
1179             * @example
1180             * <pre name="test">
1181             * String s="$a"; s === "$b"; $; 
1182             *     $a | $b
1183             *    ---------
1184             *      $ | $    
1185             *   "a".matches("^a$") === true;  // this generates
1186             * </pre>
1187             */
1188            public void handleSentences(String line) { // NOPMD by vesal on 13.1.2008 15:29
1189                int p;
1190                String eq,tol;
1191                String s = line;
1192                boolean isTemplateLine = indexOfNotInQuotes(s,TEMPLATELINEMARKER) >= 0;
1193                
1194                boolean isRegExp = false;
1195                
1196                Strings commands = new Strings();
1197                commands.add(indent + "  ");
1198                Strings sentences = splitToSentences(s);
1199                if ( tablePrinted ) { template.clear(); tablePrinted = false; everyLineInits = false; vars = null; } // NOPMD by vesal on 13.1.2008 15:30
1200                for ( int i=0; i<sentences.size(); i++ ) {
1201                    String st = sentences.get(i);
1202                    isRegExp = false;
1203                    
1204                    if ( st.length() == 0 ) continue;
1205                    if ( rawMode ) s = removeFromBegining(st," ",1); else s = removeFromBegining(st, " ");
1206                    if ( s.length() == 0 ) continue;
1207                    
1208                    p = indexOfNotInQuotes(s,ALMOSTMARKER1);  eq = ALMOSTMARKER1; tol = ", " + TOLERANCE;  /// ~~~
1209                    if ( p < 0 ) { p = indexOfNotInQuotes(s,ALMOSTMARKER2); eq = ALMOSTMARKER2; }          /// ~~
1210                    if ( p < 0 ) { p = indexOfNotInQuotes(s,EQUALSMARKER);  eq = EQUALSMARKER; tol = ""; } /// ===
1211                    if ( p < 0 ) { p = indexOfNotInQuotes(s,EQUALSMARKER2); eq = EQUALSMARKER2; }          /// =>
1212                    if ( p < 0 ) { p = indexOfNotInQuotes(s,REGEXPMARKER1); eq = REGEXPMARKER1; isRegExp = true; } /// =R=
1213                    if ( p < 0 ) { p = indexOfNotInQuotes(s,REGEXPMARKER2); eq = REGEXPMARKER2; isRegExp = true; } /// =~
1214                    if ( p >= 0 ) {
1215                        String leftSide = s.substring(0,p).trim();
1216                        String rightSide = s.substring(p+eq.length()).trim();
1217                        String message = "\"From: " + className + " line: "+ LINENUMBERMARKER + "\"";
1218                        if ( isRegExp ) {
1219                          commands.add("{ String l="+leftSide+",r="+rightSide+"; if ( !l.matches(r) ) fail(" + message + " + \" does not match: [\"+ l + \"] != [\" + r + \"]\");}");  
1220                        } else { 
1221                          commands.add("assertEquals(" + message + ", "+rightSide + ", " + leftSide + tol + ")");
1222                          checkSemiColon(sentences,i,commands);
1223                        }  
1224                        continue;
1225                    }
1226                    
1227                    commands.add(s);
1228                }
1229                commands.addAll(0,startOfLine);
1230                commands.add(endOfLine);
1231                if ( isTemplateLine || template.size() > 0 ) template.add(commands);
1232                else if ( oneTablePrinted || rawMode )
1233                    outLines(commands,lineNr+1);
1234                else
1235                    addInit(commands,lineNr+1);
1236            }
1237            
1238            
1239            /**
1240             * Creates a header and footer for new ComTest test method.
1241             * If the name is for raw-code, then no method header is done.
1242             * If there is no name for the test method, then next method is
1243             * searched and that method name is used as a test method name. 
1244             * @param s line to start the test.
1245             */
1246            private void createHeaderAndFooter(String s) {
1247                int p = s.indexOf(STARTOFTEST); 
1248                if ( p < 0 ) return;
1249                if ( s.indexOf("JAVA") > 0 ) { // start Raw mode
1250                    methodName = "";
1251                    methodHeader.add("\n");
1252                    methodHeader.add(javaShortComment + COMTESTID + " " + COMTESTBEGIN + "");
1253                    methodFooter.add(javaShortComment + COMTESTID + " " + COMTESTEND + "\n");
1254                    rawMode = true;
1255                    initLine = lineNr+1;
1256                    baseIndent = "";
1257                    return;
1258                }
1259                methodName = TEST + s.substring(p+STARTOFTEST.length());
1260                methodName = removeAllAfter(methodName, "\"");
1261                if ( methodName.equals(TEST)) methodName = tryToFindMethodName(contents,lineNr);
1262                methodHeader.add("\n");
1263                methodHeader.add("\n");
1264                methodHeader.add(javaShortComment + COMTESTID+ " " + COMTESTBEGIN + "\n");
1265                if ( methodThrows.length() == 0 ) // generate simple comment
1266                  methodHeader.add("  /** " +methodName + METHODINDEXMARKER +" */\n");
1267                else { // generate better comments for @throws
1268                    methodHeader.add("  /** \n");
1269                    methodHeader.add("   * " + methodName + METHODINDEXMARKER +" \n");
1270                    String exception = remove(methodThrows, "throws");
1271                    Strings excptions = splitBy(exception, ",");
1272                    for (String exp:excptions) 
1273                      methodHeader.add("   * @throws " + exp.trim() +" \n");
1274                    methodHeader.add("   */\n");
1275                    
1276                }
1277                methodHeader.add("  @Test\n");
1278                methodHeader.add("  public void "+methodName + METHODINDEXMARKER +"() " + methodThrows + "{");
1279                //methodHeader.add("  @Test // " + COMTESTID+ " " + COMTESTBEGIN + "\n");
1280                //methodHeader.add("  public void "+methodName + METHODINDEXMARKER +"() {");
1281                methodFooter.add("  } // " + COMTESTID + " " + COMTESTEND + "\n");
1282                initLine = lineNr+1;
1283            }
1284            
1285
1286            /**
1287             * Reads the code from contents starting from line start
1288             * and returns the line last handled.  
1289             * The ComTest block ends to next @-line or
1290             * to end of pre-tag.
1291             * @param start first line index to handle
1292             * @return last line index handled
1293             */
1294            public int readCode(int start) { // NOPMD by vesal on 13.1.2008 15:30
1295                methodThrows = "";
1296                printedTableLines = 0;
1297                try {
1298                    for ( lineNr = start; lineNr < contents.size(); lineNr++ ) {
1299                        String s = contents.get(lineNr);
1300                        s = saveStartAndEndOfLine(s);
1301                        if ( s.indexOf("*/") >= 0 ) return lineNr;
1302                        if ( s.indexOf("</pre>") >= 0 ) return lineNr;
1303                        s = removeFromBeginingTrim(s,"*");
1304                        if ( !rawMode ) s = s.trim(); // in raw-mode save spaces
1305                        if ( s.length() <= 0 ) {
1306                            if ( rawMode ) // In raw-mode empty lines preserved 
1307                                outLines(endOfLine, lineNr+1);
1308                            continue;
1309                        }
1310                        s = isPseudoComment(s); 
1311                        
1312                        if ( ( s.charAt(0)== '@' ) && !jUnitAnnotate(s) ) return lineNr;
1313                        s = handleAnnotationInComments(s);
1314                        if ( checkVariables(s)) continue;
1315
1316                        if ( s.indexOf(STARTOFTEST) >= 0 ) {
1317                            checkMethodThrows(contents.get(lineNr+1));
1318                            createHeaderAndFooter(s);
1319                            continue;
1320                        }
1321
1322                        if ( handleImports(s) ) continue; // Is this needed anymore?
1323                        
1324                        if ( methodName == null ) continue;
1325
1326                        if ( indexOfNotInQuotes(s, SEPARATORLINE) >= 0 ) continue;
1327
1328                        if ( indexOfNotInQuotes(s, INITLINE) >= 0 ) { // new init
1329                            if ( vars == null && EVERYLINEINITS1.equals(INITLINE) ) everyLineInits = true;
1330                            else outFooter(lineNr+1);
1331                            continue;
1332                        }
1333
1334                        if ( indexOfNotInQuotes(s, EVERYLINEINITS1) >= 0 || 
1335                             indexOfNotInQuotes(s, EVERYLINEINITS2) >= 0 ) {
1336                            everyLineInits = true;
1337                            continue;
1338                        }
1339                        
1340                        if ( isTableLine(s) ) { handleTableLine(s); continue;  }
1341
1342                        handleSentences(s);
1343                    }
1344                } finally {
1345                    if ( !methodHeaderPrinted && initCmds.size() > 0 ) outNewHeader();
1346                    if ( template.size() > 0 && printedTableLines == 0 ) {
1347                        if ( !methodHeaderPrinted  ) outNewHeader();
1348                        addOut(template,LINENUMBERMARKER,""+(lineNr+1));
1349                    }
1350                    outFooter(0);
1351                }
1352                return lineNr;
1353            }
1354        }
1355
1356
1357        /** 
1358         * Returns integer i by at least two characters
1359         * @param i
1360         * @return i by at least two characters
1361         */
1362        private static String two(int i) {
1363            if ( i >= 10 || i < 0 ) return "" + i;
1364            return "0" + i;
1365        }
1366        
1367
1368        /**
1369         * Returns version string for class comments
1370         */
1371        private static String getComTestVersion() {
1372            Calendar cal = Calendar.getInstance();
1373            String timestamp = ""+cal.get(Calendar.YEAR) + "." + two(cal.get(Calendar.MONTH)+1) + "." +
1374                               two(cal.get(Calendar.DAY_OF_MONTH)) + " " +
1375                               two(cal.get(Calendar.HOUR_OF_DAY)) + ":" + two(cal.get(Calendar.MINUTE)) + ":" +
1376                               two(cal.get(Calendar.SECOND));
1377            return " * @version " + timestamp + " // "+ COMTESTID;
1378        }
1379        
1380        
1381        /**
1382         * Scans String list and deletes all lines written by ComTest
1383         * @param testContents list to scan
1384         * @param outlines list where is the result (all lines with \n)
1385         */
1386        private static void deleteComTestLines(Strings testContents,Strings outlines) { // NOPMD by vesal on 13.1.2008 15:30
1387            if ( testContents == null ) return;
1388            int blankLines = 0;
1389            boolean inOwnWrite = false;
1390            for (String s : testContents ) {
1391                if ( ( indexOfNotInQuotes(s, COMTESTID) >= 0 ) || ( indexOfNotInQuotes(s, COMTESTIDOLD) >= 0 ) ) {
1392                    if ( indexOfNotInQuotes(s,"@version") >= 0 ) outlines.add(getComTestVersion()+"\n");  
1393                    else if ( indexOfNotInQuotes(s, COMTESTBEGIN) >= 0 ) inOwnWrite = true;
1394                    else if ( indexOfNotInQuotes(s, COMTESTEND) >= 0 ) inOwnWrite = false;
1395                    continue;
1396                }    
1397                if ( inOwnWrite ) continue;
1398                
1399                if ( s.trim().length() == 0 ) {
1400                    blankLines++;
1401                    if ( blankLines >= 2) continue;
1402                } else blankLines = 0;
1403                
1404                outlines.add(s+"\n");
1405               
1406            }
1407            
1408            // Delete last closing }
1409            for ( int ir = outlines.size()-1; ir >= 0; ir--) {
1410               String s = outlines.get(ir); 
1411               if ( indexOfNotInQuotes(s, "}") >= 0 ) {
1412                   outlines.removeRange(ir,outlines.size());
1413                   return;
1414               }
1415            }
1416        }
1417
1418
1419        /**
1420         * May change outFileName to same directory, if no package
1421         * @param fileName name to use as a base name 
1422         * @throws IOException 
1423         */
1424        private void checkFileNameOnceAgain(String fileName) throws IOException  {
1425            if ( testFilePackageName.length() > 0 ) return;
1426            directory = "";
1427            findNames(fileName);
1428        }
1429
1430        
1431        /**
1432         * Find outName and className from fileName
1433         * @param fileName name to look;
1434         * @return true
1435         * @throws IOException 
1436         */
1437        public boolean findNames(String fileName) throws IOException {
1438            File file = new File(fileName);
1439            String name = file.getName();
1440            String dir = removeAllAfter(file.getPath(),name);
1441            File iniFile = new File(dir,COMTESTINI);
1442            comTestIni = iniFile.getCanonicalPath();
1443            
1444            if ( directory != null && directory.length() > 0 ) {
1445                File outdir = new File(directory);
1446                if ( outdir.isAbsolute() ) dir = directory;
1447                else dir = dir + directory;
1448            }
1449            className = justFilePart(name);
1450            outFileName = addToName(name,"Test");
1451            testClassName = justFilePart(outFileName);
1452            File outfile = new File(dir,outFileName); 
1453            outDirName = dir;
1454            outFileName = outfile.getCanonicalPath();
1455            return true;            
1456        }
1457        
1458        /**
1459         * Finds the package line from contents.
1460         * Substitutes to packageName
1461         * @param contents where to find
1462         * @return true if found
1463         */
1464        private boolean findPackage(Strings contents) {
1465            String pack;
1466            for (String s:contents) {
1467                if ( indexOfNotInQuotes(s, "package") >= 0 ) {
1468                    pack = removeFromBegining(s,"package").trim();
1469                    pack = removeAllAfter(pack,";").trim();
1470                    if ( packageName.length() == 0 ) packageName = pack;
1471                    originalPackageName = pack;
1472                    return true;
1473                }    
1474            }
1475            return false;
1476        }
1477
1478        /**
1479         * Reads possible old file and removes all ComTest generated code
1480         * and create the output to outlines
1481         * @throws IOException if outPutFile is not possible to open
1482         */
1483        private void createTestFileBegin() throws IOException {
1484            String packageline = "";
1485            
1486            Strings testContents = getFileContents(outFileName);
1487            outlines = new Strings();
1488            deleteComTestLines(testContents,outlines);
1489            if ( packageName.length() > 0 ) {
1490                String pack = packageName;
1491                if ( ".".equals(pack) ) pack = originalPackageName; 
1492                else if ( pack.charAt(0) == '.' ) {
1493                    pack = originalPackageName + "." + pack.substring(1);
1494                }
1495                if ( originalPackageName.length() == 0 ) pack = "";
1496                packageline = "package " + pack + ";";
1497                testFilePackageName = pack;
1498            }
1499
1500            imports.add("import static org.junit.Assert.*;");
1501            imports.add("import org.junit.*;");
1502
1503            if ( outlines.size() > 2 ) return;
1504
1505            if ( testFilePackageName.length() > 0 ) addOutln(packageline);
1506            addOutln("");
1507            addOutln("/**");
1508            addOutln(" * Test class made by ComTest");
1509            addOutln(getComTestVersion()); 
1510            addOutln(" *"); 
1511            addOutln(" */");
1512            addOutln("public class " + testClassName + " {");           
1513        }
1514
1515
1516        /**
1517         * Creates the inner part of test class
1518         */
1519        private void createTestFileInner() { // NOPMD by vesal on 13.1.2008 15:30
1520            boolean javaComment=false;
1521            for (int i=0; i < contents.size(); i++ ) {
1522                String s = contents.get(i).trim();
1523                if ( checkVariables(s) ) continue;
1524                if ( javaComment && indexOfNotInQuotes(s,"*/")>=0 ) {
1525                    javaComment = false;
1526                    continue;
1527                }
1528                if ( !javaComment ) {
1529                    int isc = indexOfNotInQuotes(s, "/*"); 
1530                    int iec = indexOfNotInQuotes(s, "*/",QUOTES,isc+1); 
1531                    if ( isc >= 0 && iec < 0 ) javaComment = true;
1532                    continue;
1533                }
1534                if ( s.indexOf(STARTOFTEST) >= 0 ) {
1535                    CommentCodeHandler codeHandler = new CommentCodeHandler();  // NOPMD by vesal on 13.1.2008 15:31
1536                    i = codeHandler.readCode(i)-1;
1537                }    
1538            }
1539        }
1540     
1541        /**
1542         * Check the selected line by ComTest options
1543         * @param line what to check
1544         * @param all to check all or only part
1545         *
1546         * @example
1547         * <pre name="test">
1548         *  TestHelpper t = new TestHelpper();
1549         *  t.scanner.checkOptionLine("    * #import imp1; // ", true);
1550         *  t.imports.get(0) === "import imp1;";
1551         *  t.scanner.checkOptionLine("    *  #import imp2; // ", true);
1552         *  t.imports.size() === 1; 
1553         *  t.scanner.checkOptionLine("    // #import imp2; // ", true);
1554         *  t.imports.get(1) === "import imp2;";
1555         *  t.scanner.checkOptionLine("    //#import imp3; // ", true);
1556         *  t.imports.get(2) === "import imp3;";
1557         *  t.scanner.checkOptionLine("    *#import imp4; // ", true);
1558         *  t.imports.get(3) === "import imp4;";
1559         *  t.scanner.checkOptionLine("    //  #import imp5; // ", true);
1560         *  t.imports.size() === 4; 
1561         * </pre>
1562         */
1563        public void checkOptionLine(String line, boolean all) {
1564            String s = line.trim(); 
1565            if ( s.matches("^\\* ?[^ ].*") ) s = s.substring(1).trim();     // allow: * #import anywhere
1566            else if ( s.matches("^// ?[^ ].*") ) s = s.substring(2).trim(); // or: // #import 
1567            s = removeAllAfter(s, "//");
1568            if ( handleDirAndPackage(s) ) return;
1569            if ( handleImports(s) ) return;
1570            if ( !all ) return;
1571            if ( checkVariables(s) ) return;
1572        }
1573
1574        /**
1575         * Check the selected contents by ComTest options
1576         * @param contents what to check
1577         * @param all to check all or only part
1578         *
1579         */
1580        private void checkOptions(Strings contents, boolean all) {
1581            if ( contents == null ) return;
1582            for (int i=0; i < contents.size(); i++ ) {
1583                checkOptionLine(contents.get(i),all);
1584            }
1585        }
1586
1587        
1588       /**
1589        * Adds the line s+"\n" to outlines if it is not there already.
1590        * The line "is there" if trim of it starts with s  
1591        * @param i place to add if not already there
1592        * @param s line to add if not found
1593        * @return next place to add so i+1 if added, i if not
1594        */ 
1595       private int addIfNotAllready(int i,String s) {
1596           for ( String st:outlines ) 
1597               if ( st.trim().startsWith(s ) ) return i;
1598           outlines.add(i,s+"\n");
1599           return i+1;
1600       }
1601        
1602        
1603        /**
1604         * Add imports-list to outlines
1605         * Also if doStaticImport is true then add also static import
1606         */
1607        private void addImports() { // NOPMD by vesal on 13.1.2008 21:08
1608            int i; 
1609            boolean javaComment=false;
1610
1611            for (i=0; i<outlines.size()-1; i++) {
1612                String s = outlines.get(i).trim();
1613                if ( javaComment ) { 
1614                  if ( indexOfNotInQuotes(s,"*/")>=0 ) javaComment = false;
1615                  continue;
1616                }
1617                int isc = indexOfNotInQuotes(s, "/*"); 
1618                int iec = indexOfNotInQuotes(s, "*/",QUOTES,isc+1); 
1619                if ( isc >= 0 && iec < 0 ) {
1620                    javaComment = true;
1621                    continue;
1622                }    
1623                if ( isEmpty(s) )                { break;      }
1624                if ( s.indexOf("package") >= 0 ) { i++; break; }
1625                if ( s.indexOf("class")   >= 0 ) { break;      }
1626                if ( s.indexOf("import")  >= 0 ) { break;      }
1627            }
1628            outlines.add(i++,"// " + COMTESTID+ " " + COMTESTBEGIN + "\n");    
1629            for ( String s:imports ) i = addIfNotAllready(i,s);
1630            if ( doStaticImport ) i = addIfNotAllready(i,"import static " + originalPackageName + "." + className +".*;");
1631            if ( !noAutoImport && !originalPackageName.equals(testFilePackageName) ) i = addIfNotAllready(i,JAVAIMPOPRT + originalPackageName + "." + className +";");
1632            outlines.add(i++,"// " + COMTESTID+ " " + COMTESTEND + "\n");    
1633        }
1634
1635        /**
1636         * Climb up from filename and find first ComTest.ini
1637         * and read that
1638         * @param filename name with path to start climbing
1639         */
1640        private void checkIniOptions(String filename) {
1641            File file = new File(filename);
1642            String name = file.getName();
1643            String dir = removeAllAfter(file.getPath(),name);
1644            while ( !isEmpty(dir) ) {
1645              Strings iniContents = getFileContents(dir+name);
1646              if ( iniContents != null ) {
1647                checkOptions(iniContents,true);
1648                return;
1649              }
1650              dir = dir.substring(0,dir.length()-1);
1651              String filepart = justFilePart(dir);
1652              dir = dir.substring(0,dir.length()-filepart.length());
1653            }  
1654        }
1655        
1656        /**
1657         * Scan the contents and make a test class
1658         * @param fileName filename read to contents
1659         * @return outName if ok, null if something wrong
1660         */
1661        public String scan(String fileName) {
1662            PrintWriter out = null;
1663            boolean status = false;
1664            try {
1665                findNames(fileName);
1666                checkIniOptions(comTestIni);
1667                findPackage(contents);
1668                checkOptions(contents,false);
1669                findNames(fileName); // May have changed due options
1670                createTestFileBegin();
1671                createTestFileInner();
1672                addOut("}");
1673                addImports();
1674                
1675                checkFileNameOnceAgain(fileName);
1676                
1677                status = new File(outDirName).mkdirs();
1678                out = new PrintWriter(new FileWriter(outFileName));
1679                println(out,outlines,"");
1680                return outFileName;
1681            } catch (IOException e) {
1682                String s = "";
1683                if ( !status ) s = "\nNot able to make dir: " + outDirName;
1684                System.err.println(fileName +  " => " + outFileName + " " + e.getMessage() + s);
1685                return null;
1686            } finally {
1687                if ( out != null ) out.close();
1688            }
1689        }
1690
1691    }
1692
1693
1694    /**
1695     * Process the file and make a unit test
1696     * @param fileName the name of the file to process
1697     * @return handled filename in success, null otherwise
1698     */
1699    public static String processFile(String fileName) {
1700        if ( !fileName.endsWith(".java") ) return null;
1701        //if ( fileName.endsWith("Test.java") ) return null;
1702        Strings contents = getFileContents(fileName);
1703        if ( contents == null ) return null;
1704        if ( contents.size() < 1) return null;
1705
1706        ComTestScanner comtest = new ComTestScanner(contents);
1707        return comtest.scan(fileName);
1708    }
1709
1710
1711    /**
1712     * @param args name of files to process
1713     */
1714    public static void main(String[] args) {
1715        
1716        if ( args.length == 0 ) {
1717            System.out.println("Run: java comtest.ComTest filename[s]");
1718            System.out.println("or: java -jar comtest.jar filename[s]");
1719            return;
1720        }
1721
1722        for ( int i = 0; i < args.length; i++ ) {
1723            String outname = processFile(args[i]);
1724            if ( outname == null ) System.out.println(args[i] + " error");
1725            else System.out.println(args[i] + " => " + outname + " ok");
1726        }   
1727    }
1728
1729    /**
1730     * A class to help testing ComTest-functions.
1731     * @author vesal
1732     *
1733     */
1734    public static class TestHelpper {
1735        /** contents of the files       */
1736        public Strings contents;
1737        
1738        /** Global test scanner         */
1739        public ComTest.ComTestScanner scanner;
1740        
1741        /** Global test codeHandler     */
1742        public ComTest.ComTestScanner.CommentCodeHandler codeHandler;
1743        
1744        /** Global list for test initCmnds */
1745        public Strings initCmnds;
1746        
1747        //* Global list for test templates */
1748        public Strings template;
1749        
1750        
1751        /** Global list for test outLines  */
1752        public Strings outLines;
1753        
1754        /** Import lines */
1755        public Strings imports;
1756        
1757        /** 
1758         * Initializer for TestHelper      
1759         */
1760        public TestHelpper() {
1761          contents = new Strings();
1762          scanner = new ComTest.ComTestScanner(contents);
1763          try { scanner.findNames(TEST);  } catch (java.io.IOException e) { 
1764              System.err.println(e.getMessage());
1765          } 
1766          codeHandler = scanner.createCommentCodeHandler();
1767          initCmnds = codeHandler.getInitCmnds();
1768          template  = codeHandler.template;
1769          outLines = scanner.getOutlines();
1770          imports = scanner.imports;
1771        }  
1772        
1773    }
1774    
1775}
1776