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;
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import net.sourceforge.phpdt.core.compiler.IProblem;
24 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
25 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
26 import net.sourceforge.phpdt.internal.compiler.batch.CompilationUnit;
27 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
28 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
29 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
30 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
31 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
32 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
34 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
35 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
37 public class AbstractCompilerTest extends TestCase {
39 // public static final String COMPLIANCE_1_3 = "1.3";
40 // public static final String COMPLIANCE_1_4 = "1.4";
41 // public static final String COMPLIANCE_1_5 = "1.5";
43 // public static final int F_1_3 = 0x1;
44 // public static final int F_1_4 = 0x2;
45 // public static final int F_1_5 = 0x4;
47 private static int possibleComplianceLevels = -1;
49 protected String complianceLevel;
50 public void checkParsePHP(
52 String expectedSyntaxErrorDiagnosis) {
58 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
59 //new CompilerOptions(getCompilerOptions()),
60 new DefaultProblemFactory(Locale.getDefault())));
62 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
63 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
65 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
66 // if (computedUnit.types != null) {
67 // for (int i = computedUnit.types.size(); --i >= 0;){
68 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
72 StringBuffer buffer = new StringBuffer(100);
73 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
74 IProblem[] problems = compilationResult.getAllProblems();
75 int count = problems.length;
77 char[] unitSource = compilationResult.compilationUnit.getContents();
78 for (int i = 0; i < count; i++) {
79 if (problems[i] != null) {
80 if (problemCount == 0)
81 buffer.append("----------\n");
83 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
84 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
86 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
88 buffer.append(problems[i].getMessage());
90 } catch (Exception e) {
91 StringWriter stringWriter = new StringWriter();
92 e.printStackTrace(new PrintWriter(stringWriter));
93 buffer.append(stringWriter.getBuffer());
95 buffer.append("----------\n");
99 String computedSyntaxErrorDiagnosis = buffer.toString();
100 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
101 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
104 "Invalid syntax error diagnosis",
105 expectedSyntaxErrorDiagnosis,
106 computedSyntaxErrorDiagnosis);
108 public void checkParseHTML(
110 String expectedSyntaxErrorDiagnosis) {
111 // String testName) {
116 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
117 //new CompilerOptions(getCompilerOptions()),
118 new DefaultProblemFactory(Locale.getDefault())));
120 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
121 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
123 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
124 // if (computedUnit.types != null) {
125 // for (int i = computedUnit.types.size(); --i >= 0;){
126 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
130 StringBuffer buffer = new StringBuffer(100);
131 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
132 IProblem[] problems = compilationResult.getAllProblems();
133 int count = problems.length;
134 int problemCount = 0;
135 char[] unitSource = compilationResult.compilationUnit.getContents();
136 for (int i = 0; i < count; i++) {
137 if (problems[i] != null) {
138 if (problemCount == 0)
139 buffer.append("----------\n");
141 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
142 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
144 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
146 buffer.append(problems[i].getMessage());
148 } catch (Exception e) {
149 StringWriter stringWriter = new StringWriter();
150 e.printStackTrace(new PrintWriter(stringWriter));
151 buffer.append(stringWriter.getBuffer());
153 buffer.append("----------\n");
157 String computedSyntaxErrorDiagnosis = buffer.toString();
158 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
159 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
162 "Invalid syntax error diagnosis",
163 expectedSyntaxErrorDiagnosis,
164 computedSyntaxErrorDiagnosis);
167 * Returns the possible compliance levels this VM instance can run.
169 // public static int getPossibleComplianceLevels() {
170 // if (possibleComplianceLevels == -1) {
171 // String compliance = System.getProperty("compliance");
172 // if (compliance != null) {
173 // if (COMPLIANCE_1_3.equals(compliance)) {
174 // possibleComplianceLevels = F_1_3;
175 // } else if (COMPLIANCE_1_4.equals(compliance)) {
176 // possibleComplianceLevels = F_1_4;
177 // } else if (COMPLIANCE_1_5.equals(compliance)) {
178 // possibleComplianceLevels = F_1_5;
180 // System.out.println("Invalid compliance specified (" + compliance + ")");
181 // System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
182 // System.out.println("Defaulting to all possible compliances");
185 // if (possibleComplianceLevels == -1) {
186 // possibleComplianceLevels = F_1_3;
187 // String specVersion = System.getProperty("java.specification.version");
188 // boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
190 // possibleComplianceLevels |= F_1_4;
192 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
194 // possibleComplianceLevels |= F_1_5;
198 // return possibleComplianceLevels;
202 * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
203 * and using the given setup class (CompilerTestSetup or a subclass)
205 // public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
206 // TestSuite all = new TestSuite(suiteName);
207 // int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
208 // if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
209 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
211 // if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
212 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
214 // if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
215 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
221 * Returns a test suite including the tests defined by the given classes for the given complianceLevel
222 * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
224 public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
226 if (testClasses.size() == 1) {
227 suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
229 suite = new TestSuite(complianceLevel);
230 for (int i = 0, length = testClasses.size(); i < length; i++) {
231 Class testClass = (Class)testClasses.get(i);
232 TestSuite innerSuite = new TestSuite(testClass);
233 suite.addTest(innerSuite);
237 // call the setup constructor with the suite and compliance level
239 Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
240 Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
242 } catch (IllegalAccessException e) {
244 } catch (InstantiationException e) {
246 } catch (InvocationTargetException e) {
247 e.getTargetException().printStackTrace();
248 } catch (NoSuchMethodException e) {
255 public AbstractCompilerTest(String name) {
259 // protected Map getCompilerOptions() {
260 // Map options = new CompilerOptions().getMap();
261 // if (COMPLIANCE_1_3.equals(this.complianceLevel)) {
262 // options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_3);
263 // options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
264 // options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
265 // } else if (COMPLIANCE_1_4.equals(this.complianceLevel)) {
266 // options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
267 // options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
268 // options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
269 // } else if (COMPLIANCE_1_5.equals(this.complianceLevel)) {
270 // options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
271 // options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
272 // options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
277 public String getName() {
278 String name = super.getName();
279 if (this.complianceLevel != null) {
280 name = this.complianceLevel + " - " + name;
285 // public void initialize(CompilerTestSetup setUp) {
286 // this.complianceLevel = setUp.complianceLevel;