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.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 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
37 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
39 public class AbstractCompilerTest extends TestCase {
41 private static int possibleComplianceLevels = -1;
43 protected String complianceLevel;
44 public void checkParsePHP(
46 String expectedSyntaxErrorDiagnosis) {
52 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
53 new CompilerOptions(getCompilerOptions()),
54 new DefaultProblemFactory(Locale.getDefault())));
56 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
57 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
59 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
60 // if (computedUnit.types != null) {
61 // for (int i = computedUnit.types.size(); --i >= 0;){
62 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
66 StringBuffer buffer = new StringBuffer(100);
67 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
68 IProblem[] problems = compilationResult.getAllProblems();
69 int count = problems.length;
71 char[] unitSource = compilationResult.compilationUnit.getContents();
72 for (int i = 0; i < count; i++) {
73 if (problems[i] != null) {
74 if (problemCount == 0)
75 buffer.append("----------\n");
77 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
78 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
80 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
82 buffer.append(problems[i].getMessage());
84 } catch (Exception e) {
85 StringWriter stringWriter = new StringWriter();
86 e.printStackTrace(new PrintWriter(stringWriter));
87 buffer.append(stringWriter.getBuffer());
89 buffer.append("----------\n");
93 String computedSyntaxErrorDiagnosis = buffer.toString();
94 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
95 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
98 "Invalid syntax error diagnosis",
99 expectedSyntaxErrorDiagnosis,
100 computedSyntaxErrorDiagnosis);
102 public void checkParseHTML(
104 String expectedSyntaxErrorDiagnosis) {
105 // String testName) {
110 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
111 new CompilerOptions(getCompilerOptions()),
112 new DefaultProblemFactory(Locale.getDefault())));
114 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
115 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
117 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
118 // if (computedUnit.types != null) {
119 // for (int i = computedUnit.types.size(); --i >= 0;){
120 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
124 StringBuffer buffer = new StringBuffer(100);
125 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
126 IProblem[] problems = compilationResult.getAllProblems();
127 int count = problems.length;
128 int problemCount = 0;
129 char[] unitSource = compilationResult.compilationUnit.getContents();
130 for (int i = 0; i < count; i++) {
131 if (problems[i] != null) {
132 if (problemCount == 0)
133 buffer.append("----------\n");
135 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
136 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
138 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
140 buffer.append(problems[i].getMessage());
142 } catch (Exception e) {
143 StringWriter stringWriter = new StringWriter();
144 e.printStackTrace(new PrintWriter(stringWriter));
145 buffer.append(stringWriter.getBuffer());
147 buffer.append("----------\n");
151 String computedSyntaxErrorDiagnosis = buffer.toString();
152 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
153 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
156 "Invalid syntax error diagnosis",
157 expectedSyntaxErrorDiagnosis,
158 computedSyntaxErrorDiagnosis);
161 * Returns the possible compliance levels this VM instance can run.
163 // public static int getPossibleComplianceLevels() {
164 // if (possibleComplianceLevels == -1) {
165 // String compliance = System.getProperty("compliance");
166 // if (compliance != null) {
167 // if (COMPLIANCE_1_3.equals(compliance)) {
168 // possibleComplianceLevels = F_1_3;
169 // } else if (COMPLIANCE_1_4.equals(compliance)) {
170 // possibleComplianceLevels = F_1_4;
171 // } else if (COMPLIANCE_1_5.equals(compliance)) {
172 // possibleComplianceLevels = F_1_5;
174 // System.out.println("Invalid compliance specified (" + compliance + ")");
175 // System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
176 // System.out.println("Defaulting to all possible compliances");
179 // if (possibleComplianceLevels == -1) {
180 // possibleComplianceLevels = F_1_3;
181 // String specVersion = System.getProperty("java.specification.version");
182 // boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
184 // possibleComplianceLevels |= F_1_4;
186 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
188 // possibleComplianceLevels |= F_1_5;
192 // return possibleComplianceLevels;
196 * Returns a test suite including the tests defined by the given classes for the given complianceLevel
197 * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
199 public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
201 if (testClasses.size() == 1) {
202 suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
204 suite = new TestSuite(complianceLevel);
205 for (int i = 0, length = testClasses.size(); i < length; i++) {
206 Class testClass = (Class)testClasses.get(i);
207 TestSuite innerSuite = new TestSuite(testClass);
208 suite.addTest(innerSuite);
212 // call the setup constructor with the suite and compliance level
214 Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
215 Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
217 } catch (IllegalAccessException e) {
219 } catch (InstantiationException e) {
221 } catch (InvocationTargetException e) {
222 e.getTargetException().printStackTrace();
223 } catch (NoSuchMethodException e) {
230 public AbstractCompilerTest(String name) {
234 protected Map getCompilerOptions() {
235 Map options = new CompilerOptions().getMap();
237 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning, CompilerOptions.IGNORE);
238 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning, CompilerOptions.IGNORE);
239 options.put(CompilerOptions.OPTION_PHPBadStyleUppercaseIdentifierWarning, CompilerOptions.IGNORE);
240 options.put(CompilerOptions.OPTION_PHPIncludeNotExistWarning, CompilerOptions.IGNORE);
241 options.put(CompilerOptions.OPTION_UninitializedLocalVariableWarning, CompilerOptions.IGNORE);
242 options.put(CompilerOptions.OPTION_CodeCannotBeReachedWarning, CompilerOptions.IGNORE);
246 public String getName() {
247 String name = super.getName();
248 if (this.complianceLevel != null) {
249 name = this.complianceLevel + " - " + name;
254 // public void initialize(CompilerTestSetup setUp) {
255 // this.complianceLevel = setUp.complianceLevel;