Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 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.model; |
12 |
|
13 |
|
14 |
import junit.framework.Test; |
15 |
|
16 |
import org.eclipse.core.runtime.CoreException; |
17 |
import org.eclipse.jdt.core.*; |
18 |
import org.eclipse.jdt.core.compiler.IProblem; |
19 |
|
20 |
public class AccessRestrictionsTests extends ModifyingResourceTests { |
21 |
static class ProblemRequestor extends AbstractJavaModelTests.ProblemRequestor { |
22 |
ProblemRequestor (String source) { |
23 |
if (source != null) |
24 |
unitSource = source.toCharArray(); |
25 |
} |
26 |
ProblemRequestor() { |
27 |
} |
28 |
public void acceptProblem(IProblem problem) { |
29 |
super.acceptProblem(problem); |
30 |
} |
31 |
} |
32 |
|
33 |
protected ProblemRequestor problemRequestor; |
34 |
|
35 |
public AccessRestrictionsTests(String name) { |
36 |
super(name); |
37 |
} |
38 |
|
39 |
// Use this static initializer to specify subset for tests |
40 |
// All specified tests which do not belong to the class are skipped... |
41 |
static { |
42 |
// Names of tests to run, like "testXXX" |
43 |
//TESTS_NAMES = new String[] { "test004" }; |
44 |
// Numbers of tests to run: "test<number>" will be run for each number of this array |
45 |
//TESTS_NUMBERS = new int[] { 1 }; |
46 |
// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last } |
47 |
//TESTS_RANGE = new int[] { 16, -1 }; |
48 |
} |
49 |
|
50 |
public static Test suite() { |
51 |
return buildTestSuite(AccessRestrictionsTests.class); |
52 |
} |
53 |
|
54 |
protected void assertProblems(String message, String expected) { |
55 |
assertProblems(message, expected, this.problemRequestor); |
56 |
} |
57 |
|
58 |
/* |
59 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266 |
60 |
* Ensures that a problem is created for a reference to a member of a type that is not |
61 |
* accessible in a prereq project, even though it is accessed through an intermediate |
62 |
* accessible mother class. |
63 |
* Checking methods. |
64 |
*/ |
65 |
public void test001() throws CoreException { |
66 |
ICompilationUnit x1 = null, x2 = null, y = null, z = null; |
67 |
try { |
68 |
WorkingCopyOwner owner = new WorkingCopyOwner(){}; |
69 |
createJavaProject( |
70 |
"P1", |
71 |
new String[] {"src"}, |
72 |
new String[] {"JCL_LIB"}, |
73 |
"bin"); |
74 |
this.problemRequestor = new ProblemRequestor(); |
75 |
x1 = getWorkingCopy( |
76 |
"/P1/src/p/X1.java", |
77 |
"package p;\n" + |
78 |
"public class X1 {\n" + |
79 |
" void foo() {\n" + |
80 |
" }\n" + |
81 |
"}", |
82 |
owner, |
83 |
this.problemRequestor); |
84 |
assertProblems( |
85 |
"Unexpected problems", |
86 |
"----------\n----------\n----------\n----------\n" |
87 |
); |
88 |
this.problemRequestor = new ProblemRequestor(); |
89 |
x2 = getWorkingCopy( |
90 |
"/P1/src/p/X2.java", |
91 |
"package p;\n" + |
92 |
"public class X2 extends X1 {\n" + |
93 |
" void bar() {\n" + |
94 |
" }\n" + |
95 |
"}", owner, |
96 |
this.problemRequestor); |
97 |
assertProblems( |
98 |
"Unexpected problems", |
99 |
"----------\n----------\n----------\n----------\n" |
100 |
); |
101 |
IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, |
102 |
new String[] {"JCL_LIB"}, "bin"); |
103 |
IClasspathEntry[] classpath = p2.getRawClasspath(); |
104 |
int length = classpath.length; |
105 |
System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length); |
106 |
classpath[length] = createSourceEntry("P2", "/P1", "-p/X1"); |
107 |
p2.setRawClasspath(classpath, null); |
108 |
// check the most basic case |
109 |
String src = |
110 |
"package p;\n" + |
111 |
"public class Z extends X1 {\n" + |
112 |
"}"; |
113 |
this.problemRequestor = new ProblemRequestor(src); |
114 |
z = getWorkingCopy( |
115 |
"/P2/src/p/Z.java", |
116 |
src, |
117 |
owner, |
118 |
this.problemRequestor); |
119 |
assertProblems( |
120 |
"Unexpected problems value", |
121 |
"----------\n----------\n----------\n" + |
122 |
"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + |
123 |
" public class Z extends X1 {\n" + |
124 |
" ^^\n" + |
125 |
"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + |
126 |
"----------\n" |
127 |
); |
128 |
// check the specifics of this test case |
129 |
src = |
130 |
"package p;\n" + |
131 |
"public class Y extends X2 {\n" + |
132 |
" void foobar() {\n" + |
133 |
" foo(); // accesses X1.foo, should trigger an error\n" + |
134 |
" bar(); // accesses X2.bar, OK\n" + |
135 |
" }\n" + |
136 |
"}"; |
137 |
this.problemRequestor = new ProblemRequestor(src); |
138 |
y = getWorkingCopy( |
139 |
"/P2/src/p/Y.java", |
140 |
src, |
141 |
owner, |
142 |
this.problemRequestor); |
143 |
assertProblems( |
144 |
"Unexpected problems value", |
145 |
"----------\n" + |
146 |
"----------\n" + |
147 |
"----------\n" + |
148 |
"1. ERROR in /P2/src/p/Y.java (at line 4)\n" + |
149 |
" foo(); // accesses X1.foo, should trigger an error\n" + |
150 |
" ^^^^^\n" + |
151 |
"Access restriction: The method foo() from the type X1 is not accessible due to restriction on required project P1\n" + |
152 |
"----------\n" |
153 |
); |
154 |
} finally { |
155 |
if (x1 != null) |
156 |
x1.discardWorkingCopy(); |
157 |
if (x2 != null) |
158 |
x2.discardWorkingCopy(); |
159 |
if (y != null) |
160 |
y.discardWorkingCopy(); |
161 |
if (z != null) |
162 |
z.discardWorkingCopy(); |
163 |
deleteProjects(new String[] {"P1", "P2"}); |
164 |
} |
165 |
} |
166 |
|
167 |
/* |
168 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266 |
169 |
* Ensures that a problem is created for a reference to a member of a type that is not |
170 |
* accessible in a prereq project, even though it is accessed through an intermediate |
171 |
* accessible mother class. |
172 |
* Checking members. |
173 |
*/ |
174 |
public void test002() throws CoreException { |
175 |
ICompilationUnit x1 = null, x2 = null, y = null, z = null; |
176 |
try { |
177 |
WorkingCopyOwner owner = new WorkingCopyOwner(){}; |
178 |
createJavaProject( |
179 |
"P1", |
180 |
new String[] {"src"}, |
181 |
new String[] {"JCL_LIB"}, |
182 |
"bin"); |
183 |
this.problemRequestor = new ProblemRequestor(); |
184 |
x1 = getWorkingCopy( |
185 |
"/P1/src/p/X1.java", |
186 |
"package p;\n" + |
187 |
"public class X1 {\n" + |
188 |
" int m1;\n" + |
189 |
"}", |
190 |
owner, |
191 |
this.problemRequestor); |
192 |
assertProblems( |
193 |
"Unexpected problems", |
194 |
"----------\n----------\n----------\n----------\n" |
195 |
); |
196 |
this.problemRequestor = new ProblemRequestor(); |
197 |
x2 = getWorkingCopy( |
198 |
"/P1/src/p/X2.java", |
199 |
"package p;\n" + |
200 |
"public class X2 extends X1 {\n" + |
201 |
" char m2;\n" + |
202 |
"}", |
203 |
owner, |
204 |
this.problemRequestor); |
205 |
assertProblems( |
206 |
"Unexpected problems", |
207 |
"----------\n----------\n----------\n----------\n" |
208 |
); |
209 |
IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, |
210 |
new String[] {"JCL_LIB"}, "bin"); |
211 |
IClasspathEntry[] classpath = p2.getRawClasspath(); |
212 |
int length = classpath.length; |
213 |
System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length); |
214 |
classpath[length] = createSourceEntry("P2", "/P1", "-p/X1"); |
215 |
p2.setRawClasspath(classpath, null); |
216 |
// check the most basic case |
217 |
String src = |
218 |
"package p;\n" + |
219 |
"public class Z extends X1 {\n" + |
220 |
"}"; |
221 |
this.problemRequestor = new ProblemRequestor(src); |
222 |
z = getWorkingCopy( |
223 |
"/P2/src/p/Z.java", |
224 |
src, |
225 |
owner, |
226 |
this.problemRequestor); |
227 |
assertProblems( |
228 |
"Unexpected problems value", |
229 |
"----------\n----------\n----------\n" + |
230 |
"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + |
231 |
" public class Z extends X1 {\n" + |
232 |
" ^^\n" + |
233 |
"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + |
234 |
"----------\n" |
235 |
); |
236 |
// check the specifics of this test case |
237 |
src = |
238 |
"package p;\n" + |
239 |
"public class Y extends X2 {\n" + |
240 |
" void foobar() {\n" + |
241 |
" int l1 = m1; // accesses X1.m1, should trigger an error\n" + |
242 |
" char l2 = m2; // accesses X2.m2, OK\n" + |
243 |
" }\n" + |
244 |
"}"; |
245 |
this.problemRequestor = new ProblemRequestor(src); |
246 |
y = getWorkingCopy( |
247 |
"/P2/src/p/Y.java", |
248 |
src, |
249 |
owner, |
250 |
this.problemRequestor); |
251 |
assertProblems( |
252 |
"Unexpected problems value", |
253 |
"----------\n" + |
254 |
"----------\n" + |
255 |
"----------\n" + |
256 |
"1. ERROR in /P2/src/p/Y.java (at line 4)\n" + |
257 |
" int l1 = m1; // accesses X1.m1, should trigger an error\n" + |
258 |
" ^^\n" + |
259 |
"Access restriction: The field m1 from the type X1 is not accessible due to restriction on required project P1\n" + |
260 |
"----------\n" |
261 |
); |
262 |
} finally { |
263 |
if (x1 != null) |
264 |
x1.discardWorkingCopy(); |
265 |
if (x2 != null) |
266 |
x2.discardWorkingCopy(); |
267 |
if (y != null) |
268 |
y.discardWorkingCopy(); |
269 |
if (z != null) |
270 |
z.discardWorkingCopy(); |
271 |
deleteProjects(new String[] {"P1", "P2"}); |
272 |
} |
273 |
} |
274 |
|
275 |
/* |
276 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266 |
277 |
* Ensures that a problem is created for a reference to a member of a type that is not |
278 |
* accessible in a prereq project, even though it is accessed through an intermediate |
279 |
* accessible mother class. |
280 |
* Checking member types. |
281 |
*/ |
282 |
public void test003() throws CoreException { |
283 |
ICompilationUnit x1 = null, x2 = null, y = null, z = null; |
284 |
try { |
285 |
WorkingCopyOwner owner = new WorkingCopyOwner(){}; |
286 |
createJavaProject( |
287 |
"P1", |
288 |
new String[] {"src"}, |
289 |
new String[] {"JCL_LIB"}, |
290 |
"bin"); |
291 |
this.problemRequestor = new ProblemRequestor(); |
292 |
x1 = getWorkingCopy( |
293 |
"/P1/src/p/X1.java", |
294 |
"package p;\n" + |
295 |
"public class X1 {\n" + |
296 |
" class C1 {\n" + |
297 |
" protected C1 (int dummy) {}\n" + |
298 |
" protected void foo() {}\n" + |
299 |
" }\n" + |
300 |
" interface I1 {}\n" + |
301 |
"}", |
302 |
owner, |
303 |
this.problemRequestor); |
304 |
assertProblems( |
305 |
"Unexpected problems", |
306 |
"----------\n----------\n----------\n----------\n" |
307 |
); |
308 |
this.problemRequestor = new ProblemRequestor(); |
309 |
x2 = getWorkingCopy( |
310 |
"/P1/src/p/X2.java", |
311 |
"package p;\n" + |
312 |
"public class X2 extends X1 {\n" + |
313 |
" class C2 {}\n" + |
314 |
" interface I2 {}\n" + |
315 |
"}", |
316 |
owner, |
317 |
this.problemRequestor); |
318 |
assertProblems( |
319 |
"Unexpected problems", |
320 |
"----------\n----------\n----------\n----------\n" |
321 |
); |
322 |
IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, |
323 |
new String[] {"JCL_LIB"}, "bin"); |
324 |
IClasspathEntry[] classpath = p2.getRawClasspath(); |
325 |
int length = classpath.length; |
326 |
System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length); |
327 |
classpath[length] = createSourceEntry("P2", "/P1", "-p/X1"); |
328 |
p2.setRawClasspath(classpath, null); |
329 |
// check the most basic case |
330 |
String src = |
331 |
"package p;\n" + |
332 |
"public class Z extends X1 {\n" + |
333 |
"}"; |
334 |
this.problemRequestor = new ProblemRequestor(src); |
335 |
z = getWorkingCopy( |
336 |
"/P2/src/p/Z.java", |
337 |
src, |
338 |
owner, |
339 |
this.problemRequestor); |
340 |
assertProblems( |
341 |
"Unexpected problems value", |
342 |
"----------\n----------\n----------\n" + |
343 |
"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + |
344 |
" public class Z extends X1 {\n" + |
345 |
" ^^\n" + |
346 |
"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + |
347 |
"----------\n" |
348 |
); |
349 |
// check the specifics of this test case |
350 |
src = |
351 |
"package p;\n" + |
352 |
"public class Y extends X2 {\n" + |
353 |
" class C3a extends C1 { // error\n" + |
354 |
" C3a() {\n" + |
355 |
" super(0);\n" + |
356 |
" foo(); // error\n" + |
357 |
" }\n" + |
358 |
" }\n" + |
359 |
" class C3c extends C2 implements I2 {}\n" + |
360 |
" void foobar() {\n" + |
361 |
" C1 m1 = // error\n" + |
362 |
" new C1(0); // error\n" + |
363 |
" C2 m2 = new C2();\n" + |
364 |
" }\n" + |
365 |
"}"; |
366 |
this.problemRequestor = new ProblemRequestor(src); |
367 |
y = getWorkingCopy( |
368 |
"/P2/src/p/Y.java", |
369 |
src, |
370 |
owner, |
371 |
this.problemRequestor); |
372 |
assertProblems( |
373 |
"Unexpected problems value", |
374 |
"----------\n" + |
375 |
"----------\n" + |
376 |
"----------\n" + |
377 |
"1. ERROR in /P2/src/p/Y.java (at line 3)\n" + |
378 |
" class C3a extends C1 { // error\n" + |
379 |
" ^^\n" + |
380 |
"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + |
381 |
"----------\n" + |
382 |
"2. ERROR in /P2/src/p/Y.java (at line 5)\n" + |
383 |
" super(0);\n" + |
384 |
" ^^^^^^^^\n" + |
385 |
"Access restriction: The constructor X1.C1(int) is not accessible due to restriction on required project P1\n" + |
386 |
"----------\n" + |
387 |
"3. ERROR in /P2/src/p/Y.java (at line 6)\n" + |
388 |
" foo(); // error\n" + |
389 |
" ^^^^^\n" + |
390 |
"Access restriction: The method foo() from the type X1.C1 is not accessible due to restriction on required project P1\n" + |
391 |
"----------\n" + |
392 |
"4. ERROR in /P2/src/p/Y.java (at line 11)\n" + |
393 |
" C1 m1 = // error\n" + |
394 |
" ^^\n" + |
395 |
"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + |
396 |
"----------\n" + |
397 |
"5. ERROR in /P2/src/p/Y.java (at line 12)\n" + |
398 |
" new C1(0); // error\n" + |
399 |
" ^^^^^^^^^\n" + |
400 |
"Access restriction: The constructor X1.C1(int) is not accessible due to restriction on required project P1\n" + |
401 |
"----------\n" + |
402 |
"6. ERROR in /P2/src/p/Y.java (at line 12)\n" + |
403 |
" new C1(0); // error\n" + |
404 |
" ^^\n" + |
405 |
"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + |
406 |
"----------\n" |
407 |
); |
408 |
} finally { |
409 |
if (x1 != null) |
410 |
x1.discardWorkingCopy(); |
411 |
if (x2 != null) |
412 |
x2.discardWorkingCopy(); |
413 |
if (y != null) |
414 |
y.discardWorkingCopy(); |
415 |
if (z != null) |
416 |
z.discardWorkingCopy(); |
417 |
deleteProjects(new String[] {"P1", "P2"}); |
418 |
} |
419 |
} |
420 |
|
421 |
/* |
422 |
* Discouraged access message - type via discouraged rule. |
423 |
*/ |
424 |
public void test004() throws CoreException { |
425 |
ICompilationUnit x1 = null, z = null; |
426 |
try { |
427 |
WorkingCopyOwner owner = new WorkingCopyOwner(){}; |
428 |
createJavaProject( |
429 |
"P1", |
430 |
new String[] {"src"}, |
431 |
new String[] {"JCL_LIB"}, |
432 |
"bin"); |
433 |
this.problemRequestor = new ProblemRequestor(); |
434 |
x1 = getWorkingCopy( |
435 |
"/P1/src/p/X1.java", |
436 |
"package p;\n" + |
437 |
"public class X1 {\n" + |
438 |
" class C1 {}\n" + |
439 |
" interface I1 {}\n" + |
440 |
"}", |
441 |
owner, |
442 |
this.problemRequestor); |
443 |
assertProblems( |
444 |
"Unexpected problems", |
445 |
"----------\n----------\n----------\n----------\n" |
446 |
); |
447 |
IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, |
448 |
new String[] {"JCL_LIB"}, "bin"); |
449 |
IClasspathEntry[] classpath = p2.getRawClasspath(); |
450 |
int length = classpath.length; |
451 |
System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length); |
452 |
classpath[length] = createSourceEntry("P2", "/P1", "~p/X1"); |
453 |
p2.setRawClasspath(classpath, null); |
454 |
String src = |
455 |
"package p;\n" + |
456 |
"public class Z extends X1 {\n" + |
457 |
"}"; |
458 |
this.problemRequestor = new ProblemRequestor(src); |
459 |
z = getWorkingCopy( |
460 |
"/P2/src/p/Z.java", |
461 |
src, |
462 |
owner, |
463 |
this.problemRequestor); |
464 |
assertProblems( |
465 |
"Unexpected problems value", |
466 |
"----------\n" + |
467 |
"----------\n" + |
468 |
"----------\n" + |
469 |
"1. WARNING in /P2/src/p/Z.java (at line 2)\n" + |
470 |
" public class Z extends X1 {\n" + |
471 |
" ^^\n" + |
472 |
"Discouraged access: The type X1 is not accessible due to restriction on required project P1\n" + |
473 |
"----------\n" |
474 |
); |
475 |
} finally { |
476 |
if (x1 != null) |
477 |
x1.discardWorkingCopy(); |
478 |
if (z != null) |
479 |
z.discardWorkingCopy(); |
480 |
deleteProjects(new String[] {"P1", "P2"}); |
481 |
} |
482 |
} |
483 |
} |