031d81e3183f900bc1fc5d6ed1911a22374db688
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
12
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.ArrayList;
18 import java.util.Locale;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import net.sourceforge.phpdt.core.compiler.IProblem;
24 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
25 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
26 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
27 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
28 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
29 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
30 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
31 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
32 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
33
34 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
35 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
36
37 public class AbstractCompilerTest extends TestCase {
38
39 //      public static final String COMPLIANCE_1_3 = "1.3";
40 //      public static final String COMPLIANCE_1_4 = "1.4";
41 //      public static final String COMPLIANCE_1_5 = "1.5";
42 //
43 //      public static final int F_1_3 = 0x1;
44 //      public static final int F_1_4 = 0x2;
45 //      public static final int F_1_5 = 0x4;
46
47         private static int possibleComplianceLevels = -1;
48
49         protected String complianceLevel;
50         public void checkParsePHP(
51                 char[] source, 
52                 String expectedSyntaxErrorDiagnosis) {
53 //              String testName) {
54
55                 UnitParser parser = 
56                         new UnitParser(
57                                 new ProblemReporter(
58                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
59                                         //new CompilerOptions(getCompilerOptions()), 
60                                         new DefaultProblemFactory(Locale.getDefault())));
61
62                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
63                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
64                 
65                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
66 //              if (computedUnit.types != null) {
67 //                      for (int i = computedUnit.types.size(); --i >= 0;){
68 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
69 //                      }
70 //              }
71
72                 StringBuffer buffer = new StringBuffer(100);
73                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
74                         IProblem[] problems = compilationResult.getAllProblems();
75                         int count = problems.length;
76                         int problemCount = 0;
77                         char[] unitSource = compilationResult.compilationUnit.getContents();
78                         for (int i = 0; i < count; i++) { 
79                                 if (problems[i] != null) {
80                                         if (problemCount == 0)
81                                                 buffer.append("----------\n");
82                                         problemCount++;
83                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
84                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
85                                         try {
86                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
87                                                 buffer.append("\n");
88                                                 buffer.append(problems[i].getMessage());
89                                                 buffer.append("\n");
90                                         } catch (Exception e) {
91                                                 StringWriter stringWriter = new StringWriter();
92                                                 e.printStackTrace(new PrintWriter(stringWriter));
93                                                 buffer.append(stringWriter.getBuffer());
94                                         }
95                                         buffer.append("----------\n");
96                                 }
97                         }
98                 }
99                 String computedSyntaxErrorDiagnosis = buffer.toString();
100                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
101                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
102                 }
103                 assertEquals(
104                         "Invalid syntax error diagnosis",
105                         expectedSyntaxErrorDiagnosis,
106                         computedSyntaxErrorDiagnosis);
107         }
108         public void checkParseHTML(
109                 char[] source, 
110                 String expectedSyntaxErrorDiagnosis) {
111 //              String testName) {
112
113                 UnitParser parser = 
114                         new UnitParser(
115                                 new ProblemReporter(
116                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
117                                         //new CompilerOptions(getCompilerOptions()), 
118                                         new DefaultProblemFactory(Locale.getDefault())));
119
120                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
121                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);       
122                 
123                 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
124 //              if (computedUnit.types != null) {
125 //                      for (int i = computedUnit.types.size(); --i >= 0;){
126 //                              ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
127 //                      }
128 //              }
129
130                 StringBuffer buffer = new StringBuffer(100);
131                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
132                         IProblem[] problems = compilationResult.getAllProblems();
133                         int count = problems.length;
134                         int problemCount = 0;
135                         char[] unitSource = compilationResult.compilationUnit.getContents();
136                         for (int i = 0; i < count; i++) { 
137                                 if (problems[i] != null) {
138                                         if (problemCount == 0)
139                                                 buffer.append("----------\n");
140                                         problemCount++;
141                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
142                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
143                                         try {
144                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
145                                                 buffer.append("\n");
146                                                 buffer.append(problems[i].getMessage());
147                                                 buffer.append("\n");
148                                         } catch (Exception e) {
149                                                 StringWriter stringWriter = new StringWriter();
150                                                 e.printStackTrace(new PrintWriter(stringWriter));
151                                                 buffer.append(stringWriter.getBuffer());
152                                         }
153                                         buffer.append("----------\n");
154                                 }
155                         }
156                 }
157                 String computedSyntaxErrorDiagnosis = buffer.toString();
158                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
159                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
160                 }
161                 assertEquals(
162                         "Invalid syntax error diagnosis",
163                         expectedSyntaxErrorDiagnosis,
164                         computedSyntaxErrorDiagnosis);
165         }
166         /*
167          * Returns the possible compliance levels this VM instance can run.
168          */
169 //      public static int getPossibleComplianceLevels() {
170 //              if (possibleComplianceLevels == -1) {
171 //                      String compliance = System.getProperty("compliance");
172 //                      if (compliance != null) {
173 //                              if (COMPLIANCE_1_3.equals(compliance)) {
174 //                                      possibleComplianceLevels = F_1_3;
175 //                              } else if (COMPLIANCE_1_4.equals(compliance)) {
176 //                                      possibleComplianceLevels = F_1_4;
177 //                              } else if (COMPLIANCE_1_5.equals(compliance)) {
178 //                                      possibleComplianceLevels = F_1_5;
179 //                              } else {
180 //                                      System.out.println("Invalid compliance specified (" + compliance + ")");
181 //                                      System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
182 //                                      System.out.println("Defaulting to all possible compliances");
183 //                              }
184 //                      }
185 //                      if (possibleComplianceLevels == -1) {
186 //                              possibleComplianceLevels = F_1_3;
187 //                              String specVersion = System.getProperty("java.specification.version");
188 //                              boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
189 //                              if (canRun1_4) {
190 //                                      possibleComplianceLevels |= F_1_4;
191 //                              }
192 //                              boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
193 //                              if (canRun1_5) {
194 //                                      possibleComplianceLevels |= F_1_5;
195 //                              }
196 //                      }
197 //              }
198 //              return possibleComplianceLevels;
199 //      }
200
201         /*
202          * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
203          * and using the given setup class (CompilerTestSetup or a subclass)
204          */
205 //      public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
206 //              TestSuite all = new TestSuite(suiteName);
207 //              int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
208 //              if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
209 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
210 //              }
211 //              if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
212 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
213 //              }
214 //              if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
215 //                      all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
216 //              }
217 //              return all;
218 //      }
219
220         /*
221          * Returns a test suite including the tests defined by the given classes for the given complianceLevel 
222          * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
223          */
224         public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
225                 TestSuite suite;
226                 if (testClasses.size() == 1) {
227                         suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
228                 } else {
229                         suite = new TestSuite(complianceLevel);
230                         for (int i = 0, length = testClasses.size(); i < length; i++) {
231                                 Class testClass = (Class)testClasses.get(i);
232                                 TestSuite innerSuite = new TestSuite(testClass);
233                                 suite.addTest(innerSuite);
234                         }
235                 }
236
237                 // call the setup constructor with the suite and compliance level
238                 try {
239                         Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
240                         Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
241                         return setUp;
242                 } catch (IllegalAccessException e) {
243                         e.printStackTrace();
244                 } catch (InstantiationException e) {
245                         e.printStackTrace();
246                 } catch (InvocationTargetException e) {
247                         e.getTargetException().printStackTrace();
248                 } catch (NoSuchMethodException e) {
249                         e.printStackTrace();
250                 }
251
252                 return null;
253         }
254
255         public AbstractCompilerTest(String name) {
256                 super(name);
257         }
258
259 //      protected Map getCompilerOptions() {
260 //              Map options = new CompilerOptions().getMap();
261 //              if (COMPLIANCE_1_3.equals(this.complianceLevel)) {
262 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_3);
263 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
264 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
265 //              } else if (COMPLIANCE_1_4.equals(this.complianceLevel)) {
266 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
267 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
268 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
269 //              } else if (COMPLIANCE_1_5.equals(this.complianceLevel)) {
270 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
271 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
272 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
273 //              }
274 //              return options;
275 //      }
276
277         public String getName() {
278                 String name = super.getName();
279                 if (this.complianceLevel != null) {
280                         name = this.complianceLevel + " - " + name;
281                 }
282                 return name;
283         }
284
285 //      public void initialize(CompilerTestSetup setUp) {
286 //              this.complianceLevel = setUp.complianceLevel;
287 //      }
288 }