db97728da171d716945de5eb64e449829298b447
[phpeclipse.git] / net.sourceforge.phpeclipse.tests / src / net / sourceforge / phpdt / core / tests / util / AbstractCompilerTest.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
12
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;
19 import java.util.Map;
20
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;
35
36 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
37 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
38
39 public class AbstractCompilerTest extends TestCase {
40
41         private static int possibleComplianceLevels = -1;
42
43         protected String complianceLevel;
44         public void checkParsePHP(
45                 char[] source,
46                 String expectedSyntaxErrorDiagnosis) {
47 //              String testName) {
48
49                 UnitParser parser =
50                         new UnitParser(
51                                 new ProblemReporter(
52                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(),
53                                         new CompilerOptions(getCompilerOptions()),
54                                         new DefaultProblemFactory(Locale.getDefault())));
55
56                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
57                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
58
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);
63 //                      }
64 //              }
65
66                 StringBuffer buffer = new StringBuffer(100);
67                 if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
68                         IProblem[] problems = compilationResult.getAllProblems();
69                         int count = problems.length;
70                         int problemCount = 0;
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");
76                                         problemCount++;
77                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
78                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
79                                         try {
80                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
81                                                 buffer.append("\n");
82                                                 buffer.append(problems[i].getMessage());
83                                                 buffer.append("\n");
84                                         } catch (Exception e) {
85                                                 StringWriter stringWriter = new StringWriter();
86                                                 e.printStackTrace(new PrintWriter(stringWriter));
87                                                 buffer.append(stringWriter.getBuffer());
88                                         }
89                                         buffer.append("----------\n");
90                                 }
91                         }
92                 }
93                 String computedSyntaxErrorDiagnosis = buffer.toString();
94                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
95                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
96                 }
97                 assertEquals(
98                         "Invalid syntax error diagnosis",
99                         expectedSyntaxErrorDiagnosis,
100                         computedSyntaxErrorDiagnosis);
101         }
102         public void checkParseHTML(
103                 char[] source,
104                 String expectedSyntaxErrorDiagnosis) {
105 //              String testName) {
106
107                 UnitParser parser =
108                         new UnitParser(
109                                 new ProblemReporter(
110                                         DefaultErrorHandlingPolicies.proceedWithAllProblems(),
111                                         new CompilerOptions(getCompilerOptions()),
112                                         new DefaultProblemFactory(Locale.getDefault())));
113
114                 ICompilationUnit sourceUnit = new CompilationUnit(source, "", null);
115                 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
116
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);
121 //                      }
122 //              }
123
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");
134                                         problemCount++;
135                                         buffer.append(problemCount + (problems[i].isError() ? ". ERROR" : ". WARNING"));
136                                         buffer.append(" in " + new String(problems[i].getOriginatingFileName()).replace('/', '\\'));
137                                         try {
138                                                 buffer.append(((DefaultProblem)problems[i]).errorReportSource(unitSource));
139                                                 buffer.append("\n");
140                                                 buffer.append(problems[i].getMessage());
141                                                 buffer.append("\n");
142                                         } catch (Exception e) {
143                                                 StringWriter stringWriter = new StringWriter();
144                                                 e.printStackTrace(new PrintWriter(stringWriter));
145                                                 buffer.append(stringWriter.getBuffer());
146                                         }
147                                         buffer.append("----------\n");
148                                 }
149                         }
150                 }
151                 String computedSyntaxErrorDiagnosis = buffer.toString();
152                 if(!expectedSyntaxErrorDiagnosis.equals(computedSyntaxErrorDiagnosis)) {
153                         System.out.println(Util.displayString(computedSyntaxErrorDiagnosis));
154                 }
155                 assertEquals(
156                         "Invalid syntax error diagnosis",
157                         expectedSyntaxErrorDiagnosis,
158                         computedSyntaxErrorDiagnosis);
159         }
160         /*
161          * Returns the possible compliance levels this VM instance can run.
162          */
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;
173 //                              } else {
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");
177 //                              }
178 //                      }
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);
183 //                              if (canRun1_4) {
184 //                                      possibleComplianceLevels |= F_1_4;
185 //                              }
186 //                              boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
187 //                              if (canRun1_5) {
188 //                                      possibleComplianceLevels |= F_1_5;
189 //                              }
190 //                      }
191 //              }
192 //              return possibleComplianceLevels;
193 //      }
194
195         /*
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)
198          */
199         public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
200                 TestSuite suite;
201                 if (testClasses.size() == 1) {
202                         suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
203                 } else {
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);
209                         }
210                 }
211
212                 // call the setup constructor with the suite and compliance level
213                 try {
214                         Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
215                         Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
216                         return setUp;
217                 } catch (IllegalAccessException e) {
218                         e.printStackTrace();
219                 } catch (InstantiationException e) {
220                         e.printStackTrace();
221                 } catch (InvocationTargetException e) {
222                         e.getTargetException().printStackTrace();
223                 } catch (NoSuchMethodException e) {
224                         e.printStackTrace();
225                 }
226
227                 return null;
228         }
229
230         public AbstractCompilerTest(String name) {
231                 super(name);
232         }
233
234         protected Map getCompilerOptions() {
235                 Map options = new CompilerOptions().getMap();
236                 // ignore warnings
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);
243                 return options;
244         }
245
246         public String getName() {
247                 String name = super.getName();
248                 if (this.complianceLevel != null) {
249                         name = this.complianceLevel + " - " + name;
250                 }
251                 return name;
252         }
253
254 //      public void initialize(CompilerTestSetup setUp) {
255 //              this.complianceLevel = setUp.complianceLevel;
256 //      }
257 }