Tests for indexing a PHP AST with Lucene search engine;
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / lucene / LuceneTest.java
1 package net.sourceforge.phpdt.core.tests.lucene;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.Locale;
7
8 import net.sourceforge.phpdt.core.compiler.IProblem;
9 import net.sourceforge.phpdt.core.tests.util.AbstractCompilerTest;
10 import net.sourceforge.phpdt.core.tests.util.Util;
11 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
12 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
13 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
14 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
15 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
16 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
17 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
18 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
19 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
20 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
21 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
22
23 public class LuceneTest extends AbstractCompilerTest {
24         public LuceneTest(String name) {
25                 super(name);
26         }
27
28         private void checkPHP(String strEval) {
29                 if (Scanner.DEBUG) {
30                         System.out.println("\n------------------------------------");
31                         System.out.println(strEval);
32                 }
33
34                 String expectedSyntaxErrorDiagnosis = "";
35
36                 UnitParser parser = new UnitParser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(),
37                                 new CompilerOptions(getCompilerOptions()), new DefaultProblemFactory(Locale.getDefault())));
38
39                 ICompilationUnit sourceUnit = new CompilationUnit(strEval.toCharArray(), "", null);
40                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
41
42                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
43
44                 StringBuffer buffer = new StringBuffer(100);
45                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
46                         IProblem[] problems = compilationResult.getAllProblems();
47                         int count = problems.length;
48                         int problemCount = 0;
49                         char[] unitSource = compilationResult.compilationUnit.getContents();
50                         for (int i = 0; i < count; i++) {
51                                 if (problems[i] != null) {
52                                         if (problemCount == 0)
53                                                 buffer.append("----------\n");
54                                         problemCount++;
55                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
56                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
57                                         try {
58                                                 buffer.append(((DefaultProblem) problems[i]).errorReportSource(unitSource));
59                                                 buffer.append("\n");
60                                                 buffer.append(problems[i].getMessage());
61                                                 buffer.append("\n");
62                                         } catch (Exception e) {
63                                                 StringWriter stringWriter = new StringWriter();
64                                                 e.printStackTrace(new PrintWriter(stringWriter));
65                                                 buffer.append(stringWriter.getBuffer());
66                                         }
67                                         buffer.append("----------\n");
68                                 }
69                         }
70                 }
71                 String computedSyntaxErrorDiagnosis = buffer.toString();
72                 if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
73                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
74                 }
75                 assertEquals("Invalid syntax error diagnosis", expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis);
76                 String indexPath = "c:\\temp\\index_store";
77
78                 PHPWriter writer = null;
79                 try {
80                         writer = new PHPWriter(indexPath, true);
81                 } catch (IOException e) {
82                         e.printStackTrace();
83                         return;
84                 }
85                 writer.addDocument(computedUnit, null);
86                 writer.close();
87
88                 PHPSearcher indexSearcher = new PHPSearcher(indexPath);
89
90                 indexSearcher.getIncludeInfo("hello_world");
91                 indexSearcher.getClassInfo("Overlib");
92                 indexSearcher.getAttributeInfo("$ol_closetext");
93                 indexSearcher.getMethodInfo("set");
94
95         }
96
97         /**
98          * Test the PHP Parser with different PHP snippets
99          */
100         public void testPHPParser() {
101                 checkPHP("\r\n" + "    /*\r\n" + "\r\n" + "     This is version 1.11 of class.overlib for php (http://www.php.net) \r\n" + "\r\n"
102                                 + "     written 1999, 2000, 2001 Patrick Hess <hess@dland.de>\r\n" + "\r\n" + " This software is distributed under GPL.\r\n"
103                                 + " \r\n" + "   overLib is from Eric Bosrup (http://www.bosrup.com/web/overlib/)\r\n" + "\r\n"
104                                 + "     This class is just a driver/container, so most of this wonderful\r\n" + " \r\n"
105                                 + "     work is done by Eric Bosrup! Keep this in mind... \r\n" + " \r\n" + "    */\r\n" + "\r\n"
106                                 + "include(\"hello_world\"); \r\n" + "function foo() {" + "} \r\n" + "\r\n" + " class Overlib {\r\n" + "\r\n"
107                                 + "       public $ol_path          = \"modules/Forums\";\r\n" + "\r\n" + "        public $ol_sticky        = false;\r\n" + "\r\n"
108                                 + "       public $ol_align              = 0;\r\n" + "\r\n" + "    public $ol_valign        = 0;\r\n" + "\r\n"
109                                 + "       public $ol_fgcolor       = \"#fcfcfc\";\r\n" + "\r\n" + "       public $ol_bgcolor       = \"#0080C0\";\r\n" + "\r\n"
110                                 + "       public $ol_capcolor      = \"#ffffff\";\r\n" + "\r\n" + "       public $ol_textcolor     = \"\";\r\n" + "\r\n"
111                                 + "       public $ol_closecolor    = \"\";\r\n" + "\r\n" + "      public $ol_textfont      = \"\";\r\n" + "\r\n"
112                                 + "       public $ol_captionfont   = \"\";\r\n" + "\r\n" + "      public $ol_closefont     = \"\";\r\n" + "\r\n"
113                                 + "       public $ol_textsize      = 0;\r\n" + "\r\n" + "         public $ol_captionsize   = 0;\r\n" + "\r\n"
114                                 + "       public $ol_closesize     = 0;\r\n" + "\r\n" + "         public $ol_height        = 0;\r\n" + "\r\n"
115                                 + "       public $ol_width         = 0;\r\n" + "\r\n" + "         public $ol_border        = 3;\r\n" + "\r\n"
116                                 + "       public $ol_offsetx       = 0;\r\n" + "\r\n" + "         public $ol_offsety       = 0;\r\n" + "\r\n"
117                                 + "       public $ol_fgbackground  = \"\";\r\n" + "\r\n" + "      public $ol_bgbackground  = \"\";\r\n" + "\r\n"
118                                 + "       public $ol_closetext     = \"Close\";\r\n" + "\r\n" + "         public $ol_close         = true;\r\n" + "\r\n"
119                                 + "       public $ol_noclosetext   = false;\r\n" + "\r\n" + "     public $ol_autostatus    = false;\r\n" + "\r\n"
120                                 + "       public $ol_autostatuscap = false;\r\n" + "\r\n" + "     public $ol_capicon       = \"images/forum/question.gif\";\r\n"
121                                 + "\r\n" + "      public $ol_snapx         = 0;\r\n" + "\r\n" + "         public $ol_snapy         = 0;\r\n" + "\r\n"
122                                 + "       public $ol_padxl         = 0;\r\n" + "\r\n" + "         public $ol_padxr         = 0;\r\n" + "\r\n"
123                                 + "       public $ol_padyt         = 0;\r\n" + "\r\n" + "         public $ol_padyb         = 0;\r\n" + "\r\n"
124                                 + "       public $ol_fixy          = 0;\r\n" + "\r\n" + "         public $ol_background    = \"\";\r\n" + "\r\n"
125                                 + "       public $ol_fullhtml      = false;\r\n" + "\r\n" + "     public $ol_timeout    = -1;\r\n" + "\r\n"
126                                 + "       public $ol_delay              = -1;\r\n" + "\r\n" + "   public $ol_vauto         = false;\r\n" + "\r\n"
127                                 + "       public $ol_hauto         = false;\r\n" + "\r\n" + "\r\n" + "\r\n" + "   function overLib($path = \"\") {\r\n"
128                                 + "\r\n" + "            if (strlen($path)) $this->ol_path = $path;\r\n" + "\r\n" + "?>\r\n" + "\r\n"
129                                 + "<nolink rel=\'stylesheet\' href=<?php echo \"\'$this->ol_path/overlib.css\' \"; ?> \r\n" + "\r\n"
130                                 + "      type=\'text/css\'>\r\n" + "\r\n"
131                                 + "<div id=\'overDiv\' style=\'position:absolute; visibility:hide; z-index: 1000;\'>\r\n" + "\r\n" + "</div>\r\n" + "\r\n"
132                                 + "<script language=\'javascript\' src=<?php echo \"\'$this->ol_path/overlib.js\'\"; ?>>\r\n" + "\r\n" + "</script>\r\n"
133                                 + "\r\n" + "<?php\r\n" + "\r\n" + "       }  \r\n" + "  \r\n" + " \r\n" + "\r\n" + "      function set($var, $value) {\r\n"
134                                 + "\r\n" + "            $v = \"ol_$var\";\r\n" + "\r\n" + "             $this->$v = $value;\r\n" + "\r\n" + "     }\r\n" + "\r\n" + "\r\n"
135                                 + "\r\n" + "      function get($var) {\r\n" + "\r\n" + "                $v = \"ol_$var\";\r\n" + "\r\n" + "             return($this->$v);\r\n"
136                                 + "\r\n" + "      }\r\n" + "\r\n" + "\r\n" + "\r\n" + "   function over($text, $title = \"\", $status = \"\")\r\n" + "\r\n"
137                                 + "       {\r\n" + "\r\n" + "       $cmd = \"\'$text\'\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "       if(strlen($title)) \r\n"
138                                 + "\r\n" + "            $cmd .= \", CAPTION, \'$title\'\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "      if(strlen($status)) \r\n" + "\r\n"
139                                 + "             $cmd .= \", STATUS, \'$status\'\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "      if($this->ol_sticky)        \r\n" + "\r\n"
140                                 + "             $cmd .= \", STICKY\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if($this->ol_align) {\r\n" + "\r\n"
141                                 + "             switch($this->ol_align) {\r\n" + "\r\n" + "                 case 1: $cmd .= \", LEFT\"; break;\r\n" + "\r\n"
142                                 + "                 case 2: $cmd .= \", CENTER\";       break;\r\n" + "\r\n" + "                    case 3: $cmd .= \", RIGHT\";        break;\r\n" + "\r\n"
143                                 + "                 default:                    break;\r\n" + "\r\n" + "                }\r\n" + "\r\n" + "         }\r\n" + "\r\n" + "\r\n" + "\r\n"
144                                 + "         if($this->ol_valign) {\r\n" + "\r\n" + "            switch($this->ol_valign) {\r\n" + "\r\n"
145                                 + "                 case 1: $cmd .= \", ABOVE\";        break;\r\n" + "\r\n" + "                    case 2: $cmd .= \", BELOW\";        break;\r\n" + "\r\n"
146                                 + "                 default:                    break;\r\n" + "\r\n" + "                }\r\n" + "\r\n" + "         }\r\n" + "\r\n" + "\r\n" + "\r\n"
147                                 + "         if (strlen($this->ol_fgbackground)) {\r\n" + "\r\n"
148                                 + "             $cmd .= \", FGCOLOR, \'\', FGBACKGROUND, \'$this->ol_fgbackground\'\";\r\n" + "\r\n" + "            } else {\r\n" + "\r\n"
149                                 + "             if (strlen($this->ol_fgcolor))\r\n" + "\r\n" + "                        $cmd .= \", FGCOLOR, \'$this->ol_fgcolor\'\";\r\n" + "\r\n"
150                                 + "         }\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if (strlen($this->ol_bgbackground)) {\r\n" + "\r\n"
151                                 + "             $cmd .= \", BGCOLOR, \'\', BGBACKGROUND, \'$this->ol_bgbackground\'\";\r\n" + "\r\n" + "            } else {\r\n" + "\r\n"
152                                 + "             if (strlen($this->ol_bgcolor))\r\n" + "\r\n" + "                        $cmd .= \", BGCOLOR, \'$this->ol_bgcolor\'\";\r\n" + "\r\n"
153                                 + "         }\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if (strlen($this->ol_capcolor))\r\n" + "\r\n"
154                                 + "             $cmd .= \", CAPCOLOR, \'$this->ol_capcolor\'\";\r\n" + "\r\n" + "\r\n" + "\r\n"
155                                 + "         if (strlen($this->ol_textcolor))\r\n" + "\r\n" + "          $cmd .= \", TEXTCOLOR, \'$this->ol_textcolor\'\";\r\n" + "\r\n"
156                                 + "\r\n" + "\r\n" + "       if (strlen($this->ol_closecolor))\r\n" + "\r\n"
157                                 + "             $cmd .= \", CLOSECOLOR, \'$this->ol_closecolor\'\";\r\n" + "\r\n" + "\r\n" + "\r\n"
158                                 + "         if (strlen($this->ol_textfont))\r\n" + "\r\n" + "           $cmd .= \", TEXTFONT, \'$this->ol_textfont\'\";\r\n" + "\r\n"
159                                 + "\r\n" + "\r\n" + "       if (strlen($this->ol_captionfont))\r\n" + "\r\n"
160                                 + "             $cmd .= \", CAPTIONFONT, \'$this->ol_captionfont\'\";\r\n" + "\r\n" + "\r\n" + "\r\n"
161                                 + "         if (strlen($this->ol_closefont))\r\n" + "\r\n" + "          $cmd .= \", CLOSEFONT, \'$this->ol_closefont\'\";\r\n" + "\r\n"
162                                 + "\r\n" + "\r\n" + "       if ($this->ol_textsize)\r\n" + "\r\n" + "           $cmd .= \", TEXTSIZE, $this->ol_textsize\";\r\n"
163                                 + "\r\n" + "\r\n" + "\r\n" + "      if ($this->ol_captionsize)\r\n" + "\r\n"
164                                 + "             $cmd .= \", CAPTIONSIZE, $this->ol_captionsize\";\r\n" + "\r\n" + "\r\n" + "\r\n"
165                                 + "         if ($this->ol_closesize)\r\n" + "\r\n" + "          $cmd .= \", CLOSESIZE, $this->ol_closesize\";\r\n" + "\r\n" + "\r\n"
166                                 + "\r\n" + "        if ($this->ol_width)\r\n" + "\r\n" + "              $cmd .= \", WIDTH, $this->ol_width\";\r\n" + "\r\n" + "\r\n"
167                                 + "\r\n" + "        if ($this->ol_height)\r\n" + "\r\n" + "             $cmd .= \", HEIGHT, $this->ol_height\";\r\n" + "\r\n" + "\r\n"
168                                 + "\r\n" + "        if ($this->ol_border >= 0)\r\n" + "\r\n" + "                $cmd .= \", BORDER, $this->ol_border\";\r\n" + "\r\n"
169                                 + "\r\n" + "\r\n" + "       if ($this->ol_offsetx)\r\n" + "\r\n" + "            $cmd .= \", OFFSETX, $this->ol_offsetx\";\r\n" + "\r\n"
170                                 + "\r\n" + "\r\n" + "       if ($this->ol_offsety)\r\n" + "\r\n" + "            $cmd .= \", OFFSETY, $this->ol_offsety\";\r\n" + "\r\n"
171                                 + "\r\n" + "\r\n" + "       if (strlen($this->ol_closetext))\r\n" + "\r\n"
172                                 + "             $cmd .= \", CLOSETEXT, \'$this->ol_closetext\'\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "       if ($this->ol_noclose)\r\n"
173                                 + "\r\n" + "            $cmd .= \", NOCLOSETEXT\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "      if ($this->ol_autostatus)\r\n" + "\r\n"
174                                 + "             $cmd .= \", AUTOSTATUS\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "       if ($this->ol_autostatuscap)\r\n" + "\r\n"
175                                 + "             $cmd .= \", AUTOSTATUSCAP\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "            if (strlen($this->ol_capicon))\r\n" + "\r\n"
176                                 + "             $cmd .= \", CAPICON, \'$this->ol_capicon\'\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if ($this->ol_snapx)\r\n"
177                                 + "\r\n" + "            $cmd .= \", SNAPX, $this->ol_snapx\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if ($this->ol_snapy)\r\n"
178                                 + "\r\n" + "            $cmd .= \", SNAPY, $this->ol_snapy\";\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if ($this->ol_fixy)\r\n"
179                                 + "\r\n" + "            $cmd .= \", FIXY, $this->ol_fixy\";\r\n" + "\r\n" + "\r\n" + "\r\n"
180                                 + "         if ($this->ol_padxl || $this->ol_padxr)\r\n" + "\r\n"
181                                 + "             $cmd .= \", PADX, $this->ol_padxl, $this->ol_padxr\";\r\n" + "\r\n" + "\r\n" + "\r\n"
182                                 + "         if ($this->ol_padyt || $this->ol_padyb)\r\n" + "\r\n"
183                                 + "             $cmd .= \", PADY, $this->ol_padyt, $this->ol_padyb\";\r\n" + "\r\n" + "\r\n" + "\r\n"
184                                 + "         if (strlen($this->ol_background))\r\n" + "\r\n" + "         $cmd .= \", BACKGROUND, \'$this->ol_background\'\";\r\n"
185                                 + "\r\n" + "\r\n" + "\r\n" + "      if ($this->ol_fullhtml)\r\n" + "\r\n" + "           $cmd .= \", FULLHTML\";\r\n" + "\r\n"
186                                 + "\r\n" + "\r\n" + "       if ($this->ol_timeout >= 0)\r\n" + "\r\n" + "               $cmd .= \", TIMEOUT, $this->ol_timeout\";\r\n"
187                                 + "\r\n" + "\r\n" + "\r\n" + "      if ($this->ol_delay >= 0)\r\n" + "\r\n" + "         $cmd .= \", DELAY, $this->ol_delay\";\r\n"
188                                 + "\r\n" + "\r\n" + "\r\n" + "      if ($this->ol_hauto) {\r\n" + "\r\n" + "            $cmd .= \", HAUTO\";\r\n" + "\r\n"
189                                 + "             $this->ol_hauto = false;\r\n" + "\r\n" + "          }\r\n" + "\r\n" + "\r\n" + "\r\n" + "           if ($this->ol_vauto) {\r\n"
190                                 + "\r\n" + "            $cmd .= \", VAUTO\";\r\n" + "\r\n" + "          $this->ol_hauto = false;\r\n" + "\r\n" + "          }\r\n" + "\r\n"
191                                 + "\r\n" + "\r\n" + "       $output=\" onMouseOver=\\\"return overlib($cmd);\\\" \";\r\n" + "\r\n"
192                                 + "         $output.=\" onMouseOut=\\\"nd();\\\" \";\r\n" + "\r\n" + "\r\n" + "\r\n" + "            return ($output);\r\n" + "\r\n"
193                                 + "       }\r\n" + "\r\n" + "\r\n" + "\r\n" + "   function pover ($text, $title = \"\", $status = \"\") \r\n" + "\r\n"
194                                 + "       {\r\n" + "\r\n" + "       echo $this->over($text, $title, $status);\r\n" + "\r\n" + "   }\r\n" + "\r\n" + "   }\r\n"
195                                 + "\r\n" + "?>\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "\r\n" + "");
196         }
197
198 }