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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
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;
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;
37 // import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
38 // import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
40 public class AbstractCompilerTest extends TestCase {
42 private static int possibleComplianceLevels = -1;
44 protected String complianceLevel;
46 public void checkParsePHP(char[] source, String expectedSyntaxErrorDiagnosis) {
49 UnitParser parser = new UnitParser(new ProblemReporter(
50 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
51 new CompilerOptions(getCompilerOptions()),
52 new DefaultProblemFactory(Locale.getDefault())));
54 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
55 CompilationResult compilationResult = new CompilationResult(sourceUnit,
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,
67 StringBuffer buffer = new StringBuffer(100);
68 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
69 IProblem[] problems = compilationResult.getAllProblems();
70 int count = problems.length;
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");
80 + (problems[i].isError() ? ". ERROR"
83 + new String(problems[i].getOriginatingFileName())
86 buffer.append(((DefaultProblem) problems[i])
87 .errorReportSource(unitSource));
89 buffer.append(problems[i].getMessage());
91 } catch (Exception e) {
92 StringWriter stringWriter = new StringWriter();
93 e.printStackTrace(new PrintWriter(stringWriter));
94 buffer.append(stringWriter.getBuffer());
96 buffer.append("----------\n");
100 String computedSyntaxErrorDiagnosis = buffer.toString();
101 if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
103 .println(Util.displayString(computedSyntaxErrorDiagnosis));
105 assertEquals("Invalid syntax error diagnosis",
106 expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis);
109 public void checkParseHTML(char[] source,
110 String expectedSyntaxErrorDiagnosis) {
111 // String testName) {
113 UnitParser parser = new UnitParser(new ProblemReporter(
114 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
115 new CompilerOptions(getCompilerOptions()),
116 new DefaultProblemFactory(Locale.getDefault())));
118 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
119 CompilationResult compilationResult = new CompilationResult(sourceUnit,
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,
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");
144 + (problems[i].isError() ? ". ERROR"
147 + new String(problems[i].getOriginatingFileName())
148 .replace('/', '\\'));
150 buffer.append(((DefaultProblem) problems[i])
151 .errorReportSource(unitSource));
153 buffer.append(problems[i].getMessage());
155 } catch (Exception e) {
156 StringWriter stringWriter = new StringWriter();
157 e.printStackTrace(new PrintWriter(stringWriter));
158 buffer.append(stringWriter.getBuffer());
160 buffer.append("----------\n");
164 String computedSyntaxErrorDiagnosis = buffer.toString();
165 if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
167 .println(Util.displayString(computedSyntaxErrorDiagnosis));
169 assertEquals("Invalid syntax error diagnosis",
170 expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis);
174 * Returns the possible compliance levels this VM instance can run.
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;
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");
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);
200 // possibleComplianceLevels |= F_1_4;
202 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
204 // possibleComplianceLevels |= F_1_5;
208 // return possibleComplianceLevels;
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)
215 public static Test suiteForComplianceLevel(String complianceLevel,
216 Class setupClass, ArrayList testClasses) {
218 if (testClasses.size() == 1) {
219 suite = new TestSuite((Class) testClasses.get(0), complianceLevel);
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);
229 // call the setup constructor with the suite and compliance level
231 Constructor constructor = setupClass.getConstructor(new Class[] {
232 Test.class, String.class });
233 Test setUp = (Test) constructor.newInstance(new Object[] { suite,
236 } catch (IllegalAccessException e) {
238 } catch (InstantiationException e) {
240 } catch (InvocationTargetException e) {
241 e.getTargetException().printStackTrace();
242 } catch (NoSuchMethodException e) {
249 public AbstractCompilerTest(String name) {
253 protected Map getCompilerOptions() {
254 Map options = new CompilerOptions().getMap();
256 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning,
257 CompilerOptions.IGNORE);
258 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning,
259 CompilerOptions.IGNORE);
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);
272 public String getName() {
273 String name = super.getName();
274 if (this.complianceLevel != null) {
275 name = this.complianceLevel + " - " + name;
280 protected void checkPHP(String strEval) {
281 checkPHP(strEval, "");
284 protected void checkPHP(String strEval, String expectedSyntaxErrorDiagnosis) {
286 System.out.println("\n------------------------------------");
287 System.out.println(strEval);
289 checkParsePHP(strEval.toCharArray(), expectedSyntaxErrorDiagnosis);
292 protected void checkHTML(String strEval) {
294 System.out.println("\n------------------------------------");
295 System.out.println(strEval);
297 checkParseHTML(strEval.toCharArray(), "");