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;
45 public void checkParsePHP(
47 String expectedSyntaxErrorDiagnosis) {
53 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
54 new CompilerOptions(getCompilerOptions()),
55 new DefaultProblemFactory(Locale.getDefault())));
57 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
58 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
60 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, true);
61 // if (computedUnit.types != null) {
62 // for (int i = computedUnit.types.size(); --i >= 0;){
63 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
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");
78 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
79 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
81 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
83 buffer.append(problems[i].getMessage());
85 } catch (Exception e) {
86 StringWriter stringWriter = new StringWriter();
87 e.printStackTrace(new PrintWriter(stringWriter));
88 buffer.append(stringWriter.getBuffer());
90 buffer.append("----------\n");
94 String computedSyntaxErrorDiagnosis = buffer.toString();
95 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
96 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
99 "Invalid syntax error diagnosis",
100 expectedSyntaxErrorDiagnosis,
101 computedSyntaxErrorDiagnosis);
103 public void checkParseHTML(
105 String expectedSyntaxErrorDiagnosis) {
106 // String testName) {
111 DefaultErrorHandlingPolicies.proceedWithAllProblems(),
112 new CompilerOptions(getCompilerOptions()),
113 new DefaultProblemFactory(Locale.getDefault())));
115 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
116 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
118 CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult, false);
119 // if (computedUnit.types != null) {
120 // for (int i = computedUnit.types.size(); --i >= 0;){
121 // ((TypeDeclaration)computedUnit.types.get(i)).parseMethod(parser, computedUnit);
125 StringBuffer buffer = new StringBuffer(100);
126 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
127 IProblem[] problems = compilationResult.getAllProblems();
128 int count = problems.length;
129 int problemCount = 0;
130 char[] unitSource = compilationResult.compilationUnit.getContents();
131 for (int i = 0; i < count; i++) {
132 if (problems[i] != null) {
133 if (problemCount == 0)
134 buffer.append("----------\n");
136 buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
137 buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
139 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
141 buffer.append(problems[i].getMessage());
143 } catch (Exception e) {
144 StringWriter stringWriter = new StringWriter();
145 e.printStackTrace(new PrintWriter(stringWriter));
146 buffer.append(stringWriter.getBuffer());
148 buffer.append("----------\n");
152 String computedSyntaxErrorDiagnosis = buffer.toString();
153 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
154 System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
157 "Invalid syntax error diagnosis",
158 expectedSyntaxErrorDiagnosis,
159 computedSyntaxErrorDiagnosis);
162 * Returns the possible compliance levels this VM instance can run.
164 // public static int getPossibleComplianceLevels() {
165 // if (possibleComplianceLevels == -1) {
166 // String compliance = System.getProperty("compliance");
167 // if (compliance != null) {
168 // if (COMPLIANCE_1_3.equals(compliance)) {
169 // possibleComplianceLevels = F_1_3;
170 // } else if (COMPLIANCE_1_4.equals(compliance)) {
171 // possibleComplianceLevels = F_1_4;
172 // } else if (COMPLIANCE_1_5.equals(compliance)) {
173 // possibleComplianceLevels = F_1_5;
175 // System.out.println("Invalid compliance specified (" + compliance + ")");
176 // System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
177 // System.out.println("Defaulting to all possible compliances");
180 // if (possibleComplianceLevels == -1) {
181 // possibleComplianceLevels = F_1_3;
182 // String specVersion = System.getProperty("java.specification.version");
183 // boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
185 // possibleComplianceLevels |= F_1_4;
187 // boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
189 // possibleComplianceLevels |= F_1_5;
193 // return possibleComplianceLevels;
197 * Returns a test suite including the tests defined by the given classes for the given complianceLevel
198 * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
200 public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
202 if (testClasses.size() == 1) {
203 suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
205 suite = new TestSuite(complianceLevel);
206 for (int i = 0, length = testClasses.size(); i < length; i++) {
207 Class testClass = (Class)testClasses.get(i);
208 TestSuite innerSuite = new TestSuite(testClass);
209 suite.addTest(innerSuite);
213 // call the setup constructor with the suite and compliance level
215 Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
216 Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
218 } catch (IllegalAccessException e) {
220 } catch (InstantiationException e) {
222 } catch (InvocationTargetException e) {
223 e.getTargetException().printStackTrace();
224 } catch (NoSuchMethodException e) {
231 public AbstractCompilerTest(String name) {
235 protected Map getCompilerOptions() {
236 Map options = new CompilerOptions().getMap();
238 options.put(CompilerOptions.OPTION_PHPVarDeprecatedWarning, CompilerOptions.IGNORE);
239 options.put(CompilerOptions.OPTION_PHPBadStyleKeywordWarning, CompilerOptions.IGNORE);
240 options.put(CompilerOptions.OPTION_PHPBadStyleUppercaseIdentifierWarning, CompilerOptions.IGNORE);
241 options.put(CompilerOptions.OPTION_PHPIncludeNotExistWarning, CompilerOptions.IGNORE);
242 options.put(CompilerOptions.OPTION_UninitializedLocalVariableWarning, CompilerOptions.IGNORE);
243 options.put(CompilerOptions.OPTION_CodeCannotBeReachedWarning, CompilerOptions.IGNORE);
247 public String getName() {
248 String name = super.getName();
249 if (this.complianceLevel != null) {
250 name = this.complianceLevel + " - " + name;
254 protected void checkHTML(String strEval) {
256 System.out.println("\n------------------------------------");
257 System.out.println(strEval);
260 strEval.toCharArray(),
262 // parser.phpParserTester(strEval, 1);
264 // private void checkHTML(String strEval) {
265 // if (Scanner.DEBUG) {
266 // System.out.println("\n------------------------------------");
267 // System.out.println(strEval);
269 // parser.parse(strEval);
272 // * The JUnit setup method
274 // protected void setUp() {
275 // parser = new Parser(null);
278 // public void initialize(CompilerTestSetup setUp) {
279 // this.complianceLevel = setUp.complianceLevel;