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.UnitParser;
31 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblem;
32 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
33 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
34 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
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 // public static final String COMPLIANCE_1_3 = "1.3";
42 // public static final String COMPLIANCE_1_4 = "1.4";
43 // public static final String COMPLIANCE_1_5 = "1.5";
45 // public static final int F_1_3 = 0x1;
46 // public static final int F_1_4 = 0x2;
47 // public static final int F_1_5 = 0x4;
49 private static int possibleComplianceLevels = -1;
51 protected String complianceLevel;
52 public void checkParsePHP(
54 String expectedSyntaxErrorDiagnosis) {
60 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
61 new CompilerOptions(getCompilerOptions()),
62 new DefaultProblemFactory(Locale.getDefault())));
64 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
65 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
67 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
68 // if (computedUnit.types != null) {
69 // for (int i = computedUnit.types.size(); --i >= 0;){
70 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
74 StringBuffer buffer = new StringBuffer(100);
75 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
76 IProblem[] problems = compilationResult.getAllProblems();
77 int count = problems.length;
79 char[] unitSource = compilationResult.compilationUnit.getContents();
80 for (int i = 0; i < count; i++) {
81 if (problems[i] != null) {
82 if (problemCount == 0)
83 buffer.append("----------\n");
85 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
86 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
88 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
90 buffer.append(problems[i].getMessage());
92 } catch (Exception e) {
93 StringWriter stringWriter = new StringWriter();
94 e.printStackTrace(new PrintWriter(stringWriter));
95 buffer.append(stringWriter.getBuffer());
97 buffer.append("----------\n");
101 String computedSyntaxErrorDiagnosis = buffer.toString();
102 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
103 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
106 "Invalid syntax error diagnosis",
107 expectedSyntaxErrorDiagnosis,
108 computedSyntaxErrorDiagnosis);
110 public void checkParseHTML(
112 String expectedSyntaxErrorDiagnosis) {
113 // String testName) {
118 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
119 new CompilerOptions(getCompilerOptions()),
120 new DefaultProblemFactory(Locale.getDefault())));
122 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
123 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
125 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
126 // if (computedUnit.types != null) {
127 // for (int i = computedUnit.types.size(); --i >= 0;){
128 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
132 StringBuffer buffer = new StringBuffer(100);
133 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
134 IProblem[] problems = compilationResult.getAllProblems();
135 int count = problems.length;
136 int problemCount = 0;
137 char[] unitSource = compilationResult.compilationUnit.getContents();
138 for (int i = 0; i < count; i++) {
139 if (problems[i] != null) {
140 if (problemCount == 0)
141 buffer.append("----------\n");
143 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
144 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
146 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
148 buffer.append(problems[i].getMessage());
150 } catch (Exception e) {
151 StringWriter stringWriter = new StringWriter();
152 e.printStackTrace(new PrintWriter(stringWriter));
153 buffer.append(stringWriter.getBuffer());
155 buffer.append("----------\n");
159 String computedSyntaxErrorDiagnosis = buffer.toString();
160 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
161 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
164 "Invalid syntax error diagnosis",
165 expectedSyntaxErrorDiagnosis,
166 computedSyntaxErrorDiagnosis);
169 * Returns the possible compliance levels this VM instance can run.
171 // public static int getPossibleComplianceLevels() {
172 // if (possibleComplianceLevels == -1) {
173 // String compliance = System.getProperty("compliance");
174 // if (compliance != null) {
175 // if (COMPLIANCE_1_3.equals(compliance)) {
176 // possibleComplianceLevels = F_1_3;
177 // } else if (COMPLIANCE_1_4.equals(compliance)) {
178 // possibleComplianceLevels = F_1_4;
179 // } else if (COMPLIANCE_1_5.equals(compliance)) {
180 // possibleComplianceLevels = F_1_5;
182 // System.out.println("Invalid compliance specified (" + compliance + ")");
183 // System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
184 // System.out.println("Defaulting to all possible compliances");
187 // if (possibleComplianceLevels == -1) {
188 // possibleComplianceLevels = F_1_3;
189 // String specVersion = System.getProperty("java.specification.version");
190 // boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
192 // possibleComplianceLevels |= F_1_4;
194 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
196 // possibleComplianceLevels |= F_1_5;
200 // return possibleComplianceLevels;
204 * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
205 * and using the given setup class (CompilerTestSetup or a subclass)
207 // public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
208 // TestSuite all = new TestSuite(suiteName);
209 // int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
210 // if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
211 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
213 // if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
214 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
216 // if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
217 // all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
223 * Returns a test suite including the tests defined by the given classes for the given complianceLevel
224 * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
226 public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
228 if (testClasses.size() == 1) {
229 suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
231 suite = new TestSuite(complianceLevel);
232 for (int i = 0, length = testClasses.size(); i < length; i++) {
233 Class testClass = (Class)testClasses.get(i);
234 TestSuite innerSuite = new TestSuite(testClass);
235 suite.addTest(innerSuite);
239 // call the setup constructor with the suite and compliance level
241 Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
242 Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
244 } catch (IllegalAccessException e) {
246 } catch (InstantiationException e) {
248 } catch (InvocationTargetException e) {
249 e.getTargetException().printStackTrace();
250 } catch (NoSuchMethodException e) {
257 public AbstractCompilerTest(String name) {
261 protected Map getCompilerOptions() {
262 Map options = new CompilerOptions().getMap();
264 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning, CompilerOptions.IGNORE);
265 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning, CompilerOptions.IGNORE);
266 options.put(CompilerOptions.OPTION_PHPBadStyleUppercaseIdentifierWarning, CompilerOptions.IGNORE);
267 options.put(CompilerOptions.OPTION_PHPIncludeNotExistWarning, CompilerOptions.IGNORE);
268 options.put(CompilerOptions.OPTION_UninitializedLocalVariableWarning, CompilerOptions.IGNORE);
273 public String getName() {
274 String name = super.getName();
275 if (this.complianceLevel != null) {
276 name = this.complianceLevel + " - " + name;
281 // public void initialize(CompilerTestSetup setUp) {
282 // this.complianceLevel = setUp.complianceLevel;