Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.core.tests.compiler.parser; |
12 |
|
13 |
import java.util.Locale; |
14 |
import java.util.Map; |
15 |
|
16 |
import junit.framework.Test; |
17 |
|
18 |
import org.eclipse.jdt.core.tests.util.AbstractCompilerTest; |
19 |
import org.eclipse.jdt.core.tests.util.Util; |
20 |
import org.eclipse.jdt.internal.compiler.CompilationResult; |
21 |
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies; |
22 |
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; |
23 |
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit; |
24 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
25 |
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; |
26 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
27 |
import org.eclipse.jdt.internal.compiler.parser.Parser; |
28 |
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
29 |
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; |
30 |
|
31 |
public class StatementRecoveryTest_1_5 extends AbstractCompilerTest { |
32 |
public static final boolean ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY = false; |
33 |
|
34 |
public static boolean optimizeStringLiterals = false; |
35 |
public static long sourceLevel = ClassFileConstants.JDK1_3; //$NON-NLS-1$ |
36 |
|
37 |
static { |
38 |
// TESTS_NAMES = new String[] { "test0037"}; |
39 |
// TESTS_RANGE = new int[] {10, 20}; |
40 |
} |
41 |
public static Test suite() { |
42 |
return buildAllCompliancesTestSuite(StatementRecoveryTest_1_5.class); |
43 |
} |
44 |
public StatementRecoveryTest_1_5(String testName){ |
45 |
super(testName); |
46 |
} |
47 |
public void checkParse( |
48 |
char[] source, |
49 |
String expectedDietUnitToString, |
50 |
String expectedDietWithStatementRecoveryUnitToString, |
51 |
String expectedDietPlusBodyUnitToString, |
52 |
String expectedDietPlusBodyWithStatementRecoveryUnitToString, |
53 |
String expectedFullUnitToString, |
54 |
String expectedFullWithStatementRecoveryUnitToString, |
55 |
String testName) { |
56 |
|
57 |
/* using regular parser in DIET mode */ |
58 |
if(!ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY){ |
59 |
Parser parser = |
60 |
new Parser( |
61 |
new ProblemReporter( |
62 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
63 |
new CompilerOptions(getCompilerOptions()), |
64 |
new DefaultProblemFactory(Locale.getDefault())), |
65 |
optimizeStringLiterals); |
66 |
parser.setStatementsRecovery(false); |
67 |
|
68 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
69 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
70 |
|
71 |
CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult); |
72 |
String computedUnitToString = computedUnit.toString(); |
73 |
if (!expectedDietUnitToString.equals(computedUnitToString)){ |
74 |
System.out.println(Util.displayString(computedUnitToString)); |
75 |
} |
76 |
assertEquals( |
77 |
"Invalid unit diet structure" + testName, |
78 |
expectedDietUnitToString, |
79 |
computedUnitToString); |
80 |
} |
81 |
/* using regular parser in DIET mode and statementRecoveryEnabled */ |
82 |
if(!ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY){ |
83 |
Parser parser = |
84 |
new Parser( |
85 |
new ProblemReporter( |
86 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
87 |
new CompilerOptions(getCompilerOptions()), |
88 |
new DefaultProblemFactory(Locale.getDefault())), |
89 |
optimizeStringLiterals); |
90 |
|
91 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
92 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
93 |
|
94 |
CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult); |
95 |
String computedUnitToString = computedUnit.toString(); |
96 |
if (!expectedDietWithStatementRecoveryUnitToString.equals(computedUnitToString)){ |
97 |
System.out.println(Util.displayString(computedUnitToString)); |
98 |
} |
99 |
assertEquals( |
100 |
"Invalid unit diet structure with statement recovery enabled" + testName, |
101 |
expectedDietWithStatementRecoveryUnitToString, |
102 |
computedUnitToString); |
103 |
} |
104 |
/* using regular parser in DIET mode + getMethodBodies */ |
105 |
if(!ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY){ |
106 |
Parser parser = |
107 |
new Parser( |
108 |
new ProblemReporter( |
109 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
110 |
new CompilerOptions(getCompilerOptions()), |
111 |
new DefaultProblemFactory(Locale.getDefault())), |
112 |
optimizeStringLiterals); |
113 |
parser.setStatementsRecovery(false); |
114 |
|
115 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
116 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
117 |
|
118 |
CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult); |
119 |
String computedUnitToString = computedUnit.toString(); |
120 |
if (!expectedDietUnitToString.equals(computedUnitToString)){ |
121 |
System.out.println(Util.displayString(computedUnitToString)); |
122 |
} |
123 |
assertEquals( |
124 |
"Invalid unit diet structure" + testName, |
125 |
expectedDietUnitToString, |
126 |
computedUnitToString); |
127 |
if (computedUnit.types != null) { |
128 |
for (int i = computedUnit.types.length; --i >= 0;){ |
129 |
computedUnit.types[i].parseMethod(parser, computedUnit); |
130 |
} |
131 |
} |
132 |
computedUnitToString = computedUnit.toString(); |
133 |
if (!expectedDietPlusBodyUnitToString.equals(computedUnitToString)){ |
134 |
System.out.println(Util.displayString(computedUnitToString)); |
135 |
} |
136 |
|
137 |
assertEquals( |
138 |
"Invalid unit diet+body structure" + testName, |
139 |
expectedDietPlusBodyUnitToString, |
140 |
computedUnitToString); |
141 |
} |
142 |
/* using regular parser in DIET mode + getMethodBodies and statementRecoveryEnabled */ |
143 |
{ |
144 |
Parser parser = |
145 |
new Parser( |
146 |
new ProblemReporter( |
147 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
148 |
new CompilerOptions(getCompilerOptions()), |
149 |
new DefaultProblemFactory(Locale.getDefault())), |
150 |
optimizeStringLiterals); |
151 |
|
152 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
153 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
154 |
|
155 |
CompilationUnitDeclaration computedUnit = parser.dietParse(sourceUnit, compilationResult); |
156 |
String computedUnitToString = computedUnit.toString(); |
157 |
if (!expectedDietWithStatementRecoveryUnitToString.equals(computedUnitToString)){ |
158 |
System.out.println(Util.displayString(computedUnitToString)); |
159 |
} |
160 |
assertEquals( |
161 |
"Invalid unit diet structure" + testName, |
162 |
expectedDietWithStatementRecoveryUnitToString, |
163 |
computedUnitToString); |
164 |
if (computedUnit.types != null) { |
165 |
for (int i = computedUnit.types.length; --i >= 0;){ |
166 |
computedUnit.types[i].parseMethod(parser, computedUnit); |
167 |
} |
168 |
} |
169 |
computedUnitToString = computedUnit.toString(); |
170 |
if (!expectedDietPlusBodyWithStatementRecoveryUnitToString.equals(computedUnitToString)){ |
171 |
System.out.println(Util.displayString(computedUnitToString)); |
172 |
} |
173 |
|
174 |
assertEquals( |
175 |
"Invalid unit diet+body structure with statement recovery enabled" + testName, |
176 |
expectedDietPlusBodyWithStatementRecoveryUnitToString, |
177 |
computedUnitToString); |
178 |
} |
179 |
/* using regular parser in FULL mode */ |
180 |
if(!ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY){ |
181 |
Parser parser = |
182 |
new Parser( |
183 |
new ProblemReporter( |
184 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
185 |
new CompilerOptions(getCompilerOptions()), |
186 |
new DefaultProblemFactory(Locale.getDefault())), |
187 |
optimizeStringLiterals); |
188 |
parser.setStatementsRecovery(false); |
189 |
|
190 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
191 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
192 |
|
193 |
CompilationUnitDeclaration computedUnit = parser.parse(sourceUnit, compilationResult); |
194 |
String computedUnitToString = computedUnit.toString(); |
195 |
if (!expectedFullUnitToString.equals(computedUnitToString)){ |
196 |
System.out.println(Util.displayString(computedUnitToString)); |
197 |
} |
198 |
assertEquals( |
199 |
"Invalid unit full structure" + testName, |
200 |
expectedFullUnitToString, |
201 |
computedUnitToString); |
202 |
|
203 |
} |
204 |
/* using regular parser in FULL mode and statementRecoveryEnabled */ |
205 |
if(!ONLY_DIET_PLUS_BODY_WITH_STATEMENT_RECOVERY){ |
206 |
Parser parser = |
207 |
new Parser( |
208 |
new ProblemReporter( |
209 |
DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
210 |
new CompilerOptions(getCompilerOptions()), |
211 |
new DefaultProblemFactory(Locale.getDefault())), |
212 |
optimizeStringLiterals); |
213 |
|
214 |
ICompilationUnit sourceUnit = new CompilationUnit(source, testName, null); |
215 |
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0); |
216 |
|
217 |
CompilationUnitDeclaration computedUnit = parser.parse(sourceUnit, compilationResult); |
218 |
String computedUnitToString = computedUnit.toString(); |
219 |
if (!expectedFullWithStatementRecoveryUnitToString.equals(computedUnitToString)){ |
220 |
System.out.println(Util.displayString(computedUnitToString)); |
221 |
} |
222 |
assertEquals( |
223 |
"Invalid unit full structure with statement recovery enabled" + testName, |
224 |
expectedFullWithStatementRecoveryUnitToString, |
225 |
computedUnitToString); |
226 |
|
227 |
} |
228 |
} |
229 |
|
230 |
protected Map getCompilerOptions() { |
231 |
Map options = super.getCompilerOptions(); |
232 |
options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5); |
233 |
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5); |
234 |
options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5); |
235 |
return options; |
236 |
} |
237 |
|
238 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=142793 |
239 |
public void test0001() { |
240 |
|
241 |
String s = |
242 |
"package a; \n" |
243 |
+ "public class X { \n" |
244 |
+ " void foo(Collection c) { \n" |
245 |
+ " for(String s: c) { \n" |
246 |
+ " try { \n" |
247 |
+ " foo(); ` \n" |
248 |
+ " } ` \n" |
249 |
+ " } \n" |
250 |
+ " } \n" |
251 |
+ "} \n"; |
252 |
|
253 |
String expectedDietUnitToString = |
254 |
"package a;\n" + |
255 |
"public class X {\n" + |
256 |
" public X() {\n" + |
257 |
" }\n" + |
258 |
" void foo(Collection c) {\n" + |
259 |
" }\n" + |
260 |
"}\n"; |
261 |
|
262 |
String expectedDietWithStatementRecoveryUnitToString = |
263 |
expectedDietUnitToString; |
264 |
|
265 |
String expectedDietPlusBodyUnitToString = |
266 |
"package a;\n" + |
267 |
"public class X {\n" + |
268 |
" public X() {\n" + |
269 |
" super();\n" + |
270 |
" }\n" + |
271 |
" void foo(Collection c) {\n" + |
272 |
" }\n" + |
273 |
"}\n"; |
274 |
|
275 |
String expectedDietPlusBodyWithStatementRecoveryUnitToString = |
276 |
"package a;\n" + |
277 |
"public class X {\n" + |
278 |
" public X() {\n" + |
279 |
" super();\n" + |
280 |
" }\n" + |
281 |
" void foo(Collection c) {\n" + |
282 |
" for (String s : c) \n" + |
283 |
" {\n" + |
284 |
" try \n" + |
285 |
" {\n" + |
286 |
" foo();\n" + |
287 |
" }\n" + |
288 |
" finally\n" + |
289 |
" {\n" + |
290 |
" }\n" + |
291 |
" }\n" + |
292 |
" ;\n" + |
293 |
" }\n" + |
294 |
"}\n"; |
295 |
|
296 |
String expectedFullUnitToString = |
297 |
"package a;\n" + |
298 |
"public class X {\n" + |
299 |
" public X() {\n" + |
300 |
" }\n" + |
301 |
" void foo(Collection c) {\n" + |
302 |
" }\n" + |
303 |
"}\n"; |
304 |
|
305 |
String expectedFullWithStatementRecoveryUnitToString = |
306 |
expectedFullUnitToString; |
307 |
|
308 |
String testName = "<test>"; |
309 |
checkParse( |
310 |
s.toCharArray(), |
311 |
expectedDietUnitToString, |
312 |
expectedDietWithStatementRecoveryUnitToString, |
313 |
expectedDietPlusBodyUnitToString, |
314 |
expectedDietPlusBodyWithStatementRecoveryUnitToString, |
315 |
expectedFullUnitToString, |
316 |
expectedFullWithStatementRecoveryUnitToString, |
317 |
testName); |
318 |
} |
319 |
} |