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