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