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.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;
36 public class AbstractCompilerTest extends TestCase {
38 protected String complianceLevel;
40 public void checkParsePHP(char[] source, String expectedSyntaxErrorDiagnosis) {
43 UnitParser parser = new UnitParser(new ProblemReporter(
44 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
45 new CompilerOptions(getCompilerOptions()),
46 new DefaultProblemFactory(Locale.getDefault())));
48 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
49 CompilationResult compilationResult = new CompilationResult(sourceUnit,
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;
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");
66 + (problems[i].isError() ? ". ERROR"
69 + new String(problems[i].getOriginatingFileName())
72 buffer.append(((DefaultProblem) problems[i])
73 .errorReportSource(unitSource));
75 buffer.append(problems[i].getMessage());
77 } catch (Exception e) {
78 StringWriter stringWriter = new StringWriter();
79 e.printStackTrace(new PrintWriter(stringWriter));
80 buffer.append(stringWriter.getBuffer());
82 buffer.append("----------\n");
86 String computedSyntaxErrorDiagnosis = buffer.toString();
87 if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
89 .println(Util.displayString(computedSyntaxErrorDiagnosis));
91 assertEquals("Invalid syntax error diagnosis",
92 expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis);
95 public void checkParseHTML(char[] source,
96 String expectedSyntaxErrorDiagnosis) {
99 UnitParser parser = new UnitParser(new ProblemReporter(
100 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
101 new CompilerOptions(getCompilerOptions()),
102 new DefaultProblemFactory(Locale.getDefault())));
104 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
105 CompilationResult compilationResult = new CompilationResult(sourceUnit,
108 parser.dietParse(sourceUnit, compilationResult, false);
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");
123 + (problems[i].isError() ? ". ERROR"
126 + new String(problems[i].getOriginatingFileName())
127 .replace('/', '\\'));
129 buffer.append(((DefaultProblem) problems[i])
130 .errorReportSource(unitSource));
132 buffer.append(problems[i].getMessage());
134 } catch (Exception e) {
135 StringWriter stringWriter = new StringWriter();
136 e.printStackTrace(new PrintWriter(stringWriter));
137 buffer.append(stringWriter.getBuffer());
139 buffer.append("----------\n");
143 String computedSyntaxErrorDiagnosis = buffer.toString();
144 if (!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
146 .println(Util.displayString(computedSyntaxErrorDiagnosis));
148 assertEquals("Invalid syntax error diagnosis",
149 expectedSyntaxErrorDiagnosis, computedSyntaxErrorDiagnosis);
153 * Returns the possible compliance levels this VM instance can run.
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;
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");
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);
179 // possibleComplianceLevels |= F_1_4;
181 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
183 // possibleComplianceLevels |= F_1_5;
187 // return possibleComplianceLevels;
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)
194 public static Test suiteForComplianceLevel(String complianceLevel,
195 Class setupClass, ArrayList testClasses) {
197 if (testClasses.size() == 1) {
198 suite = new TestSuite((Class) testClasses.get(0), complianceLevel);
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);
208 // call the setup constructor with the suite and compliance level
210 Constructor constructor = setupClass.getConstructor(new Class[] {
211 Test.class, String.class });
212 Test setUp = (Test) constructor.newInstance(new Object[] { suite,
215 } catch (IllegalAccessException e) {
217 } catch (InstantiationException e) {
219 } catch (InvocationTargetException e) {
220 e.getTargetException().printStackTrace();
221 } catch (NoSuchMethodException e) {
228 public AbstractCompilerTest(String name) {
232 protected Map getCompilerOptions() {
233 Map options = new CompilerOptions().getMap();
235 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning,
236 CompilerOptions.IGNORE);
237 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning,
238 CompilerOptions.IGNORE);
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);
251 public String getName() {
252 String name = super.getName();
253 if (this.complianceLevel != null) {
254 name = this.complianceLevel + " - " + name;
259 protected void checkPHP(String strEval) {
260 checkPHP(strEval, "");
263 protected void checkPHP(String strEval, String expectedSyntaxErrorDiagnosis) {
265 System.out.println("\n------------------------------------");
266 System.out.println(strEval);
268 checkParsePHP(strEval.toCharArray(), expectedSyntaxErrorDiagnosis);
271 protected void checkHTML(String strEval) {
273 System.out.println("\n------------------------------------");
274 System.out.println(strEval);
276 checkParseHTML(strEval.toCharArray(), "");