1 /*******************************************************************************
2 * Copyright (c) 2000, 2009 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.jdt.core.tests.model;
15
16 import java.io.IOException;
17
18 import junit.framework.Test;
19
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.jdt.core.*;
26 import org.eclipse.jdt.core.search.*;
27 import org.eclipse.jdt.core.tests.model.AbstractJavaSearchTests.JavaSearchResultCollector;
28 import org.eclipse.jdt.core.tests.model.AbstractJavaSearchTests.TypeNameMatchCollector;
29 import org.eclipse.jdt.internal.core.search.matching.PatternLocator;
30
31 /**
32 * Tests the Java search engine accross multiple projects.
33 */
34 public class JavaSearchMultipleProjectsTests extends ModifyingResourceTests implements IJavaSearchConstants {
35 private final static int UI_DECLARATIONS = DECLARATIONS|IGNORE_DECLARING_TYPE|IGNORE_RETURN_TYPE;
JavaSearchMultipleProjectsTests(String name)36 public JavaSearchMultipleProjectsTests(String name) {
37 super(name);
38 }
suite()39 public static Test suite() {
40 return buildModelTestSuite(JavaSearchMultipleProjectsTests.class);
41 }
42 // Use this static initializer to specify subset for tests
43 // All specified tests which do not belong to the class are skipped...
44 static {
45 // TESTS_NAMES = new String[] { "testJavaSearchScopeBug101426" };
46 // TESTS_NUMBERS = new int[] { 101426 };
47 // TESTS_RANGE = new int[] { 16, -1 };
48 // TESTS_PREFIX = "testScopeEncloses";
49 }
50
51 /**
52 * Field occurences in 2 working copies within 2 projects (one prereq this other one).
53 * (regression test for bug 41534 incorrect shadowing reported by rename [refactoring])
54 */
testFieldOccurencesInWorkingCopies()55 public void testFieldOccurencesInWorkingCopies() throws CoreException {
56 ICompilationUnit wc1 = null, wc2 = null;
57 try {
58 // setup project P1
59 IJavaProject p1 = createJavaProject("P1");
60 createFolder("/P1/p1");
61 createFile(
62 "/P1/p1/X.java",
63 "package p1;\n" +
64 "public class X {\n" +
65 " public static int FOO;\n" +
66 "}"
67 );
68
69 // setup project P2
70 IJavaProject p2 = createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P1"}, "");
71 createFolder("/P2/p2");
72 createFile(
73 "/P2/p2/Y.java",
74 "package p2;\n" +
75 "import p1.X;\n" +
76 "public class Y {\n" +
77 " int bar() {\n" +
78 " return X.FOO;\n" +
79 "}"
80 );
81
82 // create working copies and rename X.FOO to X.BAR in these working copies
83 wc1 = getCompilationUnit("P1/p1/X.java").getWorkingCopy(null);
84 wc1.getBuffer().setContents(
85 "package p1;\n" +
86 "public class X {\n" +
87 " public static int BAR;\n" +
88 "}"
89 );
90 wc1.reconcile(ICompilationUnit.NO_AST, false, null, null);
91 wc2 = getCompilationUnit("P2/p2/Y.java").getWorkingCopy(null);
92 wc2.getBuffer().setContents(
93 "package p2;\n" +
94 "import p1.X;\n" +
95 "public class Y {\n" +
96 " int bar() {\n" +
97 " return X.BAR;\n" +
98 "}"
99 );
100
101 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
102 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
103 resultCollector.showProject();
104 IField field = wc1.getType("X").getField("BAR");
105 SearchPattern pattern = SearchPattern.createPattern(field, ALL_OCCURRENCES);
106 new SearchEngine(new ICompilationUnit[] {wc1, wc2}).search(
107 pattern,
108 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
109 scope,
110 resultCollector,
111 null);
112 assertEquals(
113 "Unexpected occurences of field p1.X.BAR",
114 "p1/X.java [in P1] p1.X.BAR [BAR]\n" +
115 "p2/Y.java [in P2] int p2.Y.bar() [BAR]",
116 resultCollector.toString());
117 } finally {
118 if (wc1 != null) {
119 wc1.discardWorkingCopy();
120 }
121 if (wc2 != null) {
122 wc2.discardWorkingCopy();
123 }
124 deleteProject("P1");
125 deleteProject("P2");
126 }
127 }
128 /**
129 * Search for references in a hierarchy should find matches in super type.
130 * (regression test for bug 31748 [search] search for reference is broken 2.1 M5)
131 */
testHierarchyScope1()132 public void testHierarchyScope1() throws CoreException {
133 try {
134 createJavaProject("P1");
135 createFolder("/P1/p");
136 createFile(
137 "/P1/p/X.java",
138 "package p;\n" +
139 "public class X {\n" +
140 " protected void foo() {\n" +
141 " }\n" +
142 " void bar() {\n" +
143 " foo();\n" +
144 " }\n" +
145 "}"
146 );
147 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P1"}, "");
148 createFile(
149 "/P2/Y.java",
150 "import p.X;\n" +
151 "public class Y extends X {\n" +
152 " protected void foo() {\n" +
153 " }\n" +
154 "}"
155 );
156 ICompilationUnit cu = getCompilationUnit("/P2/Y.java");
157 IType type = cu.getType("Y");
158 IMethod method = type.getMethod("foo", new String[] {});
159 IJavaSearchScope scope = SearchEngine.createHierarchyScope(type);
160 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
161 resultCollector.showProject();
162 search(
163 method,
164 REFERENCES,
165 scope,
166 resultCollector);
167 assertSearchResults(
168 "p/X.java [in P1] void p.X.bar() [foo()]",
169 resultCollector);
170 } finally {
171 deleteProject("P1");
172 deleteProject("P2");
173 }
174 }
175 /**
176 * Search for references in a hierarchy should find matches in super type.
177 * (regression test for bug 31748 [search] search for reference is broken 2.1 M5)
178 */
testHierarchyScope2()179 public void testHierarchyScope2() throws CoreException {
180 try {
181 createJavaProject("P1");
182 createFolder("/P1/p");
183 createFile(
184 "/P1/p/X.java",
185 "package p;\n" +
186 "public class X {\n" +
187 " protected void foo() {\n" +
188 " }\n" +
189 " void bar() {\n" +
190 " foo();\n" +
191 " }\n" +
192 "}"
193 );
194 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P1"}, "");
195 createFile(
196 "/P2/Y.java",
197 "import p.X;\n" +
198 "public class Y extends X {\n" +
199 " protected void foo() {\n" +
200 " }\n" +
201 "}"
202 );
203 createFile(
204 "/P2/Z.java",
205 "public class Z extends Y {\n" +
206 " protected void foo() {\n" +
207 " }\n" +
208 "}"
209 );
210
211 ICompilationUnit cu = getCompilationUnit("/P2/Z.java");
212 IType type = cu.getType("Z");
213 IMethod method = type.getMethod("foo", new String[] {});
214 IJavaSearchScope scope = SearchEngine.createHierarchyScope(type);
215 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
216 resultCollector.showProject();
217 search(
218 method,
219 REFERENCES,
220 scope,
221 resultCollector);
222 assertSearchResults(
223 "p/X.java [in P1] void p.X.bar() [foo()]",
224 resultCollector);
225 } finally {
226 deleteProject("P1");
227 deleteProject("P2");
228 }
229 }
230 /**
231 * Search for references in a hierarchy should find matches in super type.
232 * (regression test for bug 35755 Search in hierarchy misses dependent projects )
233 */
testHierarchyScope3()234 public void testHierarchyScope3() throws CoreException {
235 try {
236 createJavaProject("P1");
237 createFolder("/P1/p");
238 createFile(
239 "/P1/p/X.java",
240 "package p;\n" +
241 "public class X {\n" +
242 " protected void foo() {\n" +
243 " }\n" +
244 "}"
245 );
246 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P1"}, "");
247 createFolder("/P2/q");
248 createFile(
249 "/P2/q/Y.java",
250 "package q;\n" +
251 "import p.X;\n" +
252 "public class Y extends X {\n" +
253 " void bar() {\n" +
254 " foo();\n" +
255 " }\n" +
256 "}"
257 );
258
259 ICompilationUnit cu = getCompilationUnit("/P1/p/X.java");
260 IType type = cu.getType("X");
261 IMethod method = type.getMethod("foo", new String[] {});
262 IJavaSearchScope scope = SearchEngine.createHierarchyScope(type);
263 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
264 resultCollector.showProject();
265 search(
266 method,
267 REFERENCES,
268 scope,
269 resultCollector);
270 assertSearchResults(
271 "q/Y.java [in P2] void q.Y.bar() [foo()]",
272 resultCollector);
273 } finally {
274 deleteProject("P1");
275 deleteProject("P2");
276 }
277 }
278 /**
279 * Search for references in a hierarchy should not find inaccurate match if reference is indirect.
280 * (regression test for bug 35755 Search in hierarchy misses dependent projects )
281 */
testHierarchyScope4()282 public void testHierarchyScope4() throws CoreException {
283 try {
284 createJavaProject("P0");
285 createFolder("/P0/p0");
286 createFile(
287 "/P0/p0/X.java",
288 "package p0;\n" +
289 "public class X {\n" +
290 " public static X TheX;\n" +
291 " public void foo() {\n" +
292 " }\n" +
293 "}"
294 );
295 createJavaProject("P1", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P0"}, "");
296 createFolder("/P1/p1");
297 createFile(
298 "/P1/p1/T.java",
299 "package p1;\n" +
300 "import p0.X;\n" +
301 "public class T {\n" +
302 " public X zork() {\n" +
303 " return X.TheX;\n" +
304 " }\n" +
305 "}"
306 );
307 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P0", "/P1"}, "");
308 createFolder("/P2/p2");
309 createFile(
310 "/P2/p2/Y.java",
311 "package p2;\n" +
312 "import p0.X;\n" +
313 "import p1.T;\n" +
314 "public class Y extends X {\n" +
315 " public void bar() {\n" +
316 " new T().zork().foo();\n" +
317 " }\n" +
318 "}"
319 );
320 createJavaProject("P3", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P0", "/P2"}, "");
321 createFolder("/P3/p3");
322 createFile(
323 "/P3/p3/Z.java",
324 "package p3;\n" +
325 "import p0.X;\n" +
326 "import p2.Y;\n" +
327 "public class Z extends Y {\n" +
328 " static {\n" +
329 " X.TheX = new Z(); // zork() will actually answer an instance of Z\n" +
330 " }\n" +
331 " public void foo() {\n" +
332 " } // refs should find one in Y.bar()\n" +
333 "}"
334 );
335
336 ICompilationUnit cu = getCompilationUnit("/P3/p3/Z.java");
337 IType type = cu.getType("Z");
338 IMethod method = type.getMethod("foo", new String[] {});
339 IJavaSearchScope scope = SearchEngine.createHierarchyScope(type);
340 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
341 resultCollector.showAccuracy(true);
342 resultCollector.showProject();
343 search(
344 method,
345 REFERENCES,
346 scope,
347 resultCollector);
348 assertSearchResults(
349 "p2/Y.java [in P2] void p2.Y.bar() [foo()] EXACT_MATCH",
350 resultCollector);
351 } finally {
352 deleteProjects(new String[] {"P0", "P1", "P2", "P3"});
353 }
354 }
355 /**
356 * Method occurences with 2 unrelated projects that contain the same source.
357 * (regression test for bug 33800 search: reporting too many method occurrences)
358 */
testMethodOccurences()359 public void testMethodOccurences() throws CoreException {
360 try {
361 // setup project P1
362 IJavaProject p1 = createJavaProject("P1");
363 createFolder("/P1/p");
364 createFile(
365 "/P1/p/I.java",
366 "package p;\n" +
367 "public interface I {\n" +
368 " void method(Object o);\n" +
369 "}"
370 );
371 createFile(
372 "/P1/p/C.java",
373 "package p;\n" +
374 "public class C implements I {\n" +
375 " void method(Object o) {\n" +
376 " }\n" +
377 "}"
378 );
379
380 // copy to project P2
381 p1.getProject().copy(new Path("/P2"), false, null);
382 IJavaProject p2 = getJavaProject("P2");
383
384 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
385 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
386 resultCollector.showProject();
387 IMethod method = getCompilationUnit("/P1/p/I.java").getType("I").getMethod("method", new String[] {"QObject;"});
388 search(
389 method,
390 ALL_OCCURRENCES,
391 scope,
392 resultCollector);
393 assertSearchResults(
394 "Unexpected occurences of method p.I.method(Object)",
395 "p/C.java [in P1] void p.C.method(Object) [method]\n" +
396 "p/I.java [in P1] void p.I.method(Object) [method]",
397 resultCollector);
398 } finally {
399 deleteProject("P1");
400 deleteProject("P2");
401 }
402 }
403 /**
404 * Package declaration with 2 unrelated projects that contain the same source.
405 * (regression test for bug 46276 Search for package declarations incorrectly finds matches in clone project)
406 */
testPackageDeclaration()407 public void testPackageDeclaration() throws CoreException {
408 try {
409 // setup project P1
410 IJavaProject p1 = createJavaProject("P1");
411 createFolder("/P1/p");
412 createFile(
413 "/P1/p/X.java",
414 "package p;\n" +
415 "public class X {\n" +
416 "}"
417 );
418
419 // copy to project P2
420 p1.getProject().copy(new Path("/P2"), false, null);
421 IJavaProject p2 = getJavaProject("P2");
422
423 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
424 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
425 resultCollector.showProject();
426 IPackageFragment pkg = getPackage("/P1/p");
427 search(
428 pkg,
429 DECLARATIONS,
430 scope,
431 resultCollector);
432 assertSearchResults(
433 "Unexpected package declarations",
434 "p [in P1] p",
435 resultCollector);
436 } finally {
437 deleteProject("P1");
438 deleteProject("P2");
439 }
440 }
441 /**
442 * Package reference with fragments in 2 different source projects.
443 * (regression test for bug 47415 [Search] package references confused with multiple fragments)
444 */
testPackageReference1()445 public void testPackageReference1() throws CoreException {
446 try {
447 // setup project P1
448 IJavaProject p1 = createJavaProject("P1");
449 createFolder("/P1/p");
450 createFile(
451 "/P1/p/X.java",
452 "package p;\n" +
453 "public class X {\n" +
454 "}"
455 );
456
457 // setup project P2
458 IJavaProject p2 = createJavaProject(
459 "P2",
460 new String[] {""},
461 new String[] {"JCL_LIB"},
462 new String[] {"/P1"},
463 "");
464 createFolder("/P2/p");
465 createFile(
466 "/P2/p/Y.java",
467 "package p;\n" +
468 "public class Y {\n" +
469 "}"
470 );
471
472 // create package references
473 createFolder("/P2/q");
474 createFile(
475 "/P2/q/Z.java",
476 "package q;\n" +
477 "import p.X;\n" +
478 "import p.Y;\n" +
479 "public class Z {\n" +
480 " X onlyHereForTheImport = null;\n" +
481 " Y alsoOnlyHereForTheImport = null;\n" +
482 " void foo(){\n" +
483 " p.X x = (p.X)null;\n" +
484 " p.Y y = (p.Y)null;\n" +
485 " }\n" +
486 "}"
487 );
488
489 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
490 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
491 IPackageFragment pkg = getPackage("/P1/p");
492 search(
493 pkg,
494 REFERENCES,
495 scope,
496 resultCollector);
497 assertSearchResults(
498 "Unexpected package references",
499 "q/Z.java [p]\n" +
500 "q/Z.java void q.Z.foo() [p]\n" +
501 "q/Z.java void q.Z.foo() [p]",
502 resultCollector);
503 } finally {
504 deleteProject("P1");
505 deleteProject("P2");
506 }
507 }
508 /**
509 * Package reference with fragments in 2 different binary projects.
510 * (regression test for bug 47415 [Search] package references confused with multiple fragments)
511 */
testPackageReference2()512 public void testPackageReference2() throws CoreException, IOException {
513 try {
514 // setup project JavaSearchMultipleProjects1
515 IJavaProject p1 = setUpJavaProject("JavaSearchMultipleProjects1");
516
517 // setup project JavaSearchMultipleProjects2
518 IJavaProject p2 = setUpJavaProject("JavaSearchMultipleProjects2");
519
520 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
521 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
522 IPackageFragment pkg = getPackage("/JavaSearchMultipleProjects1/lib/p");
523 search(
524 pkg,
525 REFERENCES,
526 scope,
527 resultCollector);
528 assertSearchResults(
529 "Unexpected package references",
530 "src/q/Z.java [p]\n" +
531 "src/q/Z.java void q.Z.foo() [p]\n" +
532 "src/q/Z.java void q.Z.foo() [p]",
533 resultCollector);
534 } finally {
535 deleteProject("JavaSearchMultipleProjects1");
536 deleteProject("JavaSearchMultipleProjects2");
537 }
538 }
539 /**
540 * Method reference with 2 working copies in 2 different project.
541 * (regression test for bug 57749 Search in working copies doesn't find all matches)
542 */
testReferenceInWorkingCopies()543 public void testReferenceInWorkingCopies() throws CoreException {
544 ICompilationUnit workingCopy1 = null;
545 ICompilationUnit workingCopy2 = null;
546 try {
547 // setup project P1
548 IJavaProject p1 = createJavaProject("P1");
549 createFolder("/P1/p1");
550 createFile(
551 "/P1/p1/X.java",
552 "package p1;\n" +
553 "public class X {\n" +
554 " void foo() {\n" +
555 " }\n" +
556 "}"
557 );
558 createFile(
559 "/P1/p1/Test.java",
560 "package p1;\n" +
561 "public class Test {\n" +
562 "}"
563 );
564
565 // setup project P2
566 IJavaProject p2 = createJavaProject(
567 "P2",
568 new String[] {""},
569 new String[] {"JCL_LIB"},
570 new String[] {"/P1"},
571 "");
572 createFolder("/P2/p2");
573 createFile(
574 "/P2/p2/Y.java",
575 "package p2;\n" +
576 "public class Y {\n" +
577 "}"
578 );
579 // need a second potential match to see the problem
580 createFile(
581 "/P2/p2/Z.java",
582 "public class Z {\n" +
583 " void bar(p1.Test test) {\n" +
584 " }\n" +
585 " void foo() {\n" +
586 " bar(null);\n" + // potential match
587 " }\n" +
588 "}"
589 );
590
591 // create working copies
592 WorkingCopyOwner owner = new WorkingCopyOwner() {};
593 workingCopy1 = getCompilationUnit("/P1/p1/X.java").getWorkingCopy(owner, null/*no progress monitor*/);
594 workingCopy1.getBuffer().setContents(
595 "package p1;\n" +
596 "public class X {\n" +
597 " void bar(Test test) {\n" +
598 " }\n" +
599 "}"
600 );
601 workingCopy1.makeConsistent(null);
602 workingCopy2 = getCompilationUnit("/P2/p2/Y.java").getWorkingCopy(owner, null/*no progress monitor*/);
603 workingCopy2.getBuffer().setContents(
604 "package p2;\n" +
605 "import p1.X;\n" +
606 "public class Y {\n" +
607 " void fred() {\n" +
608 " new X().bar(null);\n" +
609 " }\n" +
610 "}"
611 );
612 workingCopy2.makeConsistent(null);
613
614 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1, p2});
615 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
616 IMethod method = workingCopy1.getType("X").getMethod("bar", new String[] {"QTest;"});
617 new SearchEngine(owner).search(
618 SearchPattern.createPattern(method, REFERENCES),
619 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
620 scope,
621 resultCollector,
622 null
623 );
624 assertSearchResults(
625 "Unexpected package references",
626 "p2/Y.java void p2.Y.fred() [bar(null)]",
627 resultCollector);
628 } finally {
629 if (workingCopy1 != null) workingCopy1.discardWorkingCopy();
630 if (workingCopy2 != null) workingCopy1.discardWorkingCopy();
631 deleteProject("P1");
632 deleteProject("P2");
633 }
634 }
635 /**
636 * Type declaration in external jar file that is shared by 2 projects.
637 * (regression test for bug 27485 SearchEngine returns wrong java element when searching in an archive that is included by two distinct java projects.)
638 */
testTypeDeclarationInJar()639 public void testTypeDeclarationInJar() throws CoreException {
640 try {
641 IJavaProject p1 = createJavaProject("P1", new String[] {}, new String[] {"JCL_LIB"}, "");
642 IJavaProject p2 = createJavaProject("P2", new String[] {}, new String[] {"JCL_LIB"}, "");
643
644 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p1});
645 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
646 resultCollector.showProject();
647 search(
648 "Object",
649 TYPE,
650 DECLARATIONS,
651 scope,
652 resultCollector);
653 assertSearchResults(
654 "Unexpected result in scope of P1",
655 getExternalJCLPathString() + " [in P1] java.lang.Object",
656 resultCollector);
657
658 scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p2});
659 resultCollector = new JavaSearchResultCollector();
660 resultCollector.showProject();
661 search(
662 "Object",
663 TYPE,
664 DECLARATIONS,
665 scope,
666 resultCollector);
667 assertSearchResults(
668 "Unexpected result in scope of P2",
669 getExternalJCLPathString() + " [in P2] java.lang.Object",
670 resultCollector);
671 } finally {
672 deleteProject("P1");
673 deleteProject("P2");
674 }
675 }
676
677 /**
678 * Bug 151189: [search] Declaration search does not find all matches
679 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=151189"
680 */
testBug151189_Workspace()681 public void testBug151189_Workspace() throws CoreException {
682 try {
683 // setup project P1
684 /*IJavaProject p1 = */createJavaProject("P1");
685 createFolder("/P1/pack");
686 createFile(
687 "/P1/pack/Declaration.java",
688 "package pack;\n" +
689 "public class Declaration implements Interface {\n" +
690 " public void doOperation(int val) {}\n" +
691 "}\n"
692 );
693 createFile(
694 "/P1/pack/Interface.java",
695 "package pack;\n" +
696 "public interface Interface {\n" +
697 " void doOperation(int val);\n" +
698 "}\n"
699 );
700
701 // setup project P2
702 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P1" }, "");
703 createFolder("/P2/test");
704 createFile(
705 "/P2/test/Declaration_bis.java",
706 "package test;\n" +
707 "import pack.Interface;\n" +
708 "public class Declaration_bis implements Interface {\n" +
709 " public void doOperation(int val) {}\n" +
710 "}\n"
711 );
712
713 // Get method
714 IMethod method = getCompilationUnit("/P2/test/Declaration_bis.java").getType("Declaration_bis").getMethod("doOperation", new String[] {"I"});
715
716 // search method declaration in workspace scope
717 IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); //JavaSearchScope(new IJavaElement[] {p1, p2});
718 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
719 resultCollector.showProject();
720 search(
721 method,
722 DECLARATIONS,
723 scope,
724 resultCollector);
725 assertSearchResults(
726 "Unexpected declarations of method test.Declaration_bis.doOperation(int)",
727 "test/Declaration_bis.java [in P2] void test.Declaration_bis.doOperation(int) [doOperation]",
728 resultCollector);
729
730 // search method declaration in workspace scope with JDT-UI flags
731 resultCollector = new JavaSearchResultCollector();
732 resultCollector.showProject();
733 search(
734 method,
735 UI_DECLARATIONS,
736 scope,
737 resultCollector);
738 assertSearchResults(
739 "Unexpected declarations of method test.Declaration_bis.doOperation(int)",
740 "pack/Declaration.java [in P1] void pack.Declaration.doOperation(int) [doOperation]\n" +
741 "pack/Interface.java [in P1] void pack.Interface.doOperation(int) [doOperation]\n" +
742 "test/Declaration_bis.java [in P2] void test.Declaration_bis.doOperation(int) [doOperation]",
743 resultCollector);
744 } finally {
745 deleteProject("P1");
746 deleteProject("P2");
747 }
748 }
testBug151189_Project()749 public void testBug151189_Project() throws CoreException {
750 try {
751 // setup project P1
752 createJavaProject("P1");
753 createFolder("/P1/pack");
754 createFile(
755 "/P1/pack/Declaration.java",
756 "package pack;\n" +
757 "public class Declaration implements Interface {\n" +
758 " public void doOperation(int val) {}\n" +
759 "}\n"
760 );
761 createFile(
762 "/P1/pack/Interface.java",
763 "package pack;\n" +
764 "public interface Interface {\n" +
765 " void doOperation(int val);\n" +
766 "}\n"
767 );
768
769 // setup project P2
770 IJavaProject p2 = createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P1" }, "");
771 createFolder("/P2/test");
772 createFile(
773 "/P2/test/Declaration_bis.java",
774 "package test;\n" +
775 "import pack.Interface;\n" +
776 "public class Declaration_bis implements Interface {\n" +
777 " public void doOperation(int val) {}\n" +
778 "}\n"
779 );
780
781 // Get method
782 IMethod method = getCompilationUnit("/P2/test/Declaration_bis.java").getType("Declaration_bis").getMethod("doOperation", new String[] {"I"});
783
784 // search method declaration in project scope
785 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p2});
786 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
787 resultCollector.showProject();
788 search(
789 method,
790 UI_DECLARATIONS,
791 scope,
792 resultCollector);
793 assertSearchResults(
794 "Unexpected declarations of method test.Declaration_bis.doOperation(int)",
795 "pack/Declaration.java [in P1] void pack.Declaration.doOperation(int) [doOperation]\n" +
796 "pack/Interface.java [in P1] void pack.Interface.doOperation(int) [doOperation]\n" +
797 "test/Declaration_bis.java [in P2] void test.Declaration_bis.doOperation(int) [doOperation]",
798 resultCollector);
799 } finally {
800 deleteProject("P1");
801 deleteProject("P2");
802 }
803 }
804
805 /**
806 * @bug 163072: [search] method reference reports wrong potential matches
807 * @test Ensure that there's no potential match while searching in two projects having 1.4 and 1.5 compliances
808 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=163072"
809 */
testBug163072()810 public void testBug163072() throws CoreException {
811 try {
812 // setup project P1
813 /*IJavaProject p1 = */createJavaProject("P1"); // standard project using 1.4 compliance
814 createFolder("/P1/test");
815 createFile(
816 "/P1/test/Test.java",
817 "package test;\n" +
818 "public class Test {\n" +
819 " public Object getType() {\n" +
820 " return null;\n" +
821 " }\n" +
822 " public void foo() {\n" +
823 " if (getType() == null) {\n" +
824 " System.out.println(\"null\");\n" +
825 " }\n" +
826 " }\n" +
827 "}\n"
828 );
829
830 // setup project P2
831 createJavaProject("P2", new String[] {""}, new String[] {"JCL15_LIB"}, new String[] { "/P1" }, "", "1.5");
832 createFolder("/P2/pack");
833 createFile(
834 "/P2/pack/FactoryContainer.java",
835 "package pack;\n" +
836 "public class FactoryContainer {\n" +
837 " public enum FactoryType { PLUGIN }\n" +
838 " public FactoryType getType() {\n" +
839 " return FactoryType.PLUGIN;\n" +
840 " }\n" +
841 "}\n"
842 );
843 createFile(
844 "/P2/pack/Reference.java",
845 "package pack;\n" +
846 "public class Reference {\n" +
847 " private final FactoryContainer _fc;\n" +
848 " public Reference() {\n" +
849 " _fc = new FactoryContainer();\n" +
850 " }\n" +
851 " boolean foo() {\n" +
852 " return _fc.getType() == FactoryContainer.FactoryType.PLUGIN;\n" +
853 " }\n" +
854 "}\n"
855 );
856
857 // Get method
858 IMethod method = getCompilationUnit("/P1/test/Test.java").getType("Test").getMethod("getType", new String[0]);
859 assertTrue("Method 'Test.getType()' should exist!", method.exists());
860
861 // search method declaration in workspace scope
862 IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); //JavaSearchScope(new IJavaElement[] {p1, p2});
863 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
864 resultCollector.showProject();
865 resultCollector.showAccuracy(true);
866 search(method, REFERENCES, scope, resultCollector);
867 assertSearchResults(
868 "Unexpected references of method Test.getType()",
869 "test/Test.java [in P1] void test.Test.foo() [getType()] EXACT_MATCH",
870 resultCollector);
871 } finally {
872 deleteProject("P1");
873 deleteProject("P2");
874 }
875 }
876
877 /**
878 * @bug 167743: [search] Open Type Dialog cannot find types from projects migrated from 3.2.1 workspace
879 * @test Ensure that types are found even in default package fragment root
880 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=167743"
881 */
testBug167743()882 public void testBug167743() throws CoreException {
883 try {
884 IJavaProject p = createJavaProject("P");
885 createFolder("/P/test");
886 createFile(
887 "/P/test/TestClass.java",
888 "package test;\n" +
889 "public class Test {\n" +
890 "}\n"
891 );
892
893 // Search all type names with TypeNameMatchRequestor
894 AbstractJavaSearchTests.TypeNameMatchCollector collector = new AbstractJavaSearchTests.TypeNameMatchCollector() {
895 @Override
896 public String toString(){
897 return toFullyQualifiedNamesString();
898 }
899 };
900 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { p });
901 new SearchEngine().searchAllTypeNames(
902 null,
903 SearchPattern.R_EXACT_MATCH,
904 new char[] { '*' },
905 SearchPattern.R_PATTERN_MATCH,
906 IJavaSearchConstants.TYPE,
907 scope,
908 collector,
909 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
910 null);
911 // Search all type names with TypeNameRequestor
912 SearchTests.SearchTypeNameRequestor requestor = new SearchTests.SearchTypeNameRequestor();
913 new SearchEngine().searchAllTypeNames(
914 null,
915 SearchPattern.R_EXACT_MATCH,
916 new char[] { '*' },
917 SearchPattern.R_PATTERN_MATCH,
918 IJavaSearchConstants.TYPE,
919 scope,
920 requestor,
921 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
922 null);
923 // Should have same types with these 2 searches
924 assertEquals("Invalid number of types found!", requestor.size(), collector.size());
925 assertEquals("Found types sounds not to be correct", requestor.toString(), collector.toString());
926 } finally {
927 deleteProject("P");
928 }
929 }
930
931 /**
932 * @bug 176831: [search] No search results due to malformed search scope
933 * @test Verify that type are found in rt.jar even if it's added as a library on the classpath
934 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=176831"
935 */
testBug176831()936 public void testBug176831() throws CoreException {
937 try {
938 // Create projects and files
939 final IJavaProject p1 = createJavaProject("P1", new String[] {"src"}, null, new String[] {"/P2"}, "bin");
940 final IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, new String[] { getExternalJCLPathString() }, "bin");
941
942 // Create scope and search
943 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { p1, p2 }, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.REFERENCED_PROJECTS);
944 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
945 resultCollector.showProject();
946 resultCollector.showAccuracy(true);
947 new SearchEngine().search(
948 SearchPattern.createPattern("toString", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH),
949 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
950 scope,
951 resultCollector,
952 null
953 );
954 assertSearchResults(
955 "Unexpected references to /P1/p/A.java",
956 ""+ getExternalJCLPathString() + " [in P2] java.lang.String java.lang.Object.toString() EXACT_MATCH",
957 resultCollector);
958 } finally {
959 deleteProject("P1");
960 deleteProject("P2");
961 }
962 }
testBug176831b()963 public void testBug176831b() throws CoreException {
964 try {
965 // Create projects and files
966 final IJavaProject p1 = createJavaProject("P1", new String[] {"src"}, null, new String[] {"/P2"}, "bin");
967 final IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, null, new String[] {"/P3"}, "bin");
968 final IJavaProject p3 = createJavaProject("P3", new String[] {"src"}, new String[] { getExternalJCLPathString() }, "bin");
969
970 // Create scope and search
971 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { p1, p2, p3 }, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.REFERENCED_PROJECTS);
972 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
973 resultCollector.showProject();
974 resultCollector.showAccuracy(true);
975 new SearchEngine().search(
976 SearchPattern.createPattern("toString", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH),
977 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
978 scope,
979 resultCollector,
980 null
981 );
982 assertSearchResults(
983 "Unexpected references to /P1/p/A.java",
984 ""+ getExternalJCLPathString() + " [in P3] java.lang.String java.lang.Object.toString() EXACT_MATCH",
985 resultCollector);
986 } finally {
987 deleteProject("P1");
988 deleteProject("P2");
989 deleteProject("P3");
990 }
991 }
992
993 /**
994 * @bug 195228: [search] Invalid path in open type dialog
995 * @test Verify that correct types are found even with project and source folders in the classpath
996 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=195228"
997 */
testBug195228()998 public void testBug195228() throws CoreException {
999 try {
1000 // Create projects and files
1001 final IJavaProject project = createJavaProject("P1", new String[] {"src"}, "bin");
1002 createFolder("/P1/src/pack1/pack2");
1003 createFile(
1004 "/P1/src/pack1/pack2/X.java",
1005 "package pack1.pack2;\n" +
1006 "public class X {}"
1007 );
1008 createFile(
1009 "/P1/src/pack1/Y.java",
1010 "package pack1;\n" +
1011 "public class Y {}"
1012 );
1013 createFile(
1014 "/P1/test.properties",
1015 "bug=195228"
1016 );
1017 // Create additional projects to force the rehash of the workspace scope while creating it
1018 createJavaProject("P2", new String[] {"src"}, "bin");
1019 createJavaProject("P3", new String[] {"src"}, "bin");
1020 IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
1021
1022 // Store all types found in project
1023 TypeNameMatchCollector requestor = new TypeNameMatchCollector();
1024 new SearchEngine().searchAllTypeNames(
1025 null,
1026 SearchPattern.R_EXACT_MATCH,
1027 null,
1028 SearchPattern.R_PREFIX_MATCH,
1029 IJavaSearchConstants.TYPE,
1030 scope,
1031 requestor,
1032 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1033 null);
1034 String allTypes = requestor.toString();
1035
1036 // Add project folder to classpath with inclusion and exclusion patterns
1037 getWorkspace().run(new IWorkspaceRunnable() {
1038 public void run(IProgressMonitor monitor) throws CoreException {
1039 IClasspathEntry[] entries = project.getRawClasspath();
1040 int length = entries.length;
1041 System.arraycopy(entries, 0, entries = new IClasspathEntry[length+1], 0, length);
1042 entries[length] = JavaCore.newSourceEntry(new Path("/P1"), new IPath[] { new Path("test.properties") }, new IPath[] { new Path("src/") }, null);
1043 project.setRawClasspath(entries, null);
1044 }
1045 }, null);
1046
1047 // Search for all types and verify that same are found
1048 requestor = new TypeNameMatchCollector();
1049 new SearchEngine().searchAllTypeNames(
1050 null,
1051 SearchPattern.R_EXACT_MATCH,
1052 null,
1053 SearchPattern.R_PREFIX_MATCH,
1054 IJavaSearchConstants.TYPE,
1055 scope,
1056 requestor,
1057 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1058 null);
1059 assertEquals("Should found same types!", allTypes, requestor.toString());
1060 } finally {
1061 deleteProject("P1");
1062 deleteProject("P2");
1063 deleteProject("P3");
1064 }
1065 }
1066
1067 /**
1068 * @bug 199392: [search] Type Dialog Error 'Items filtering ... Reason: Class file name must end with .class'
1069 * @test Ensure that types are found even in project which name ends either with ".jar" or ".zip"
1070 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=199392"
1071 */
testBug199392_Jar()1072 public void testBug199392_Jar() throws CoreException {
1073 try {
1074 IJavaProject project = createJavaProject("Test.jar");
1075 createFolder("/Test.jar/test");
1076 createFile(
1077 "/Test.jar/test/MyClass.java",
1078 "package test;\n" +
1079 "public class MyClass {\n" +
1080 "}\n"
1081 );
1082
1083 // Search all type names with TypeNameMatchRequestor
1084 AbstractJavaSearchTests.TypeNameMatchCollector collector = new AbstractJavaSearchTests.TypeNameMatchCollector() {
1085 @Override
1086 public String toString(){
1087 return toFullyQualifiedNamesString();
1088 }
1089 };
1090 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
1091 new SearchEngine().searchAllTypeNames(
1092 null,
1093 SearchPattern.R_EXACT_MATCH,
1094 new char[] { 'M', 'y' },
1095 SearchPattern.R_CAMELCASE_MATCH,
1096 IJavaSearchConstants.TYPE,
1097 scope,
1098 collector,
1099 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1100 null);
1101 assertEquals("Found types sounds not to be correct",
1102 "test.MyClass",
1103 collector.toString()
1104 );
1105 } finally {
1106 deleteProject("Test.jar");
1107 }
1108 }
testBug199392_Jar_SamePartCount()1109 public void testBug199392_Jar_SamePartCount() throws CoreException {
1110 try {
1111 IJavaProject project = createJavaProject("Test.jar");
1112 createFolder("/Test.jar/test");
1113 createFile(
1114 "/Test.jar/test/MyClass.java",
1115 "package test;\n" +
1116 "public class MyClass {\n" +
1117 "}\n"
1118 );
1119
1120 // Search all type names with TypeNameMatchRequestor
1121 AbstractJavaSearchTests.TypeNameMatchCollector collector = new AbstractJavaSearchTests.TypeNameMatchCollector() {
1122 @Override
1123 public String toString(){
1124 return toFullyQualifiedNamesString();
1125 }
1126 };
1127 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
1128 new SearchEngine().searchAllTypeNames(
1129 null,
1130 SearchPattern.R_EXACT_MATCH,
1131 new char[] { 'M', 'y' },
1132 SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH,
1133 IJavaSearchConstants.TYPE,
1134 scope,
1135 collector,
1136 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1137 null);
1138 assertEquals("Found types sounds not to be correct",
1139 "",
1140 collector.toString()
1141 );
1142 } finally {
1143 deleteProject("Test.jar");
1144 }
1145 }
testBug199392_Zip()1146 public void testBug199392_Zip() throws CoreException {
1147 try {
1148 IJavaProject project = createJavaProject("Test.zip");
1149 createFolder("/Test.zip/test");
1150 createFile(
1151 "/Test.zip/test/MyClass.java",
1152 "package test;\n" +
1153 "public class MyClass {\n" +
1154 "}\n"
1155 );
1156
1157 // Search all type names with TypeNameMatchRequestor
1158 AbstractJavaSearchTests.TypeNameMatchCollector collector = new AbstractJavaSearchTests.TypeNameMatchCollector() {
1159 @Override
1160 public String toString(){
1161 return toFullyQualifiedNamesString();
1162 }
1163 };
1164 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
1165 new SearchEngine().searchAllTypeNames(
1166 null,
1167 SearchPattern.R_EXACT_MATCH,
1168 new char[] { 'M', 'y' },
1169 SearchPattern.R_CAMELCASE_MATCH,
1170 IJavaSearchConstants.TYPE,
1171 scope,
1172 collector,
1173 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1174 null);
1175 assertEquals("Found types sounds not to be correct",
1176 "test.MyClass",
1177 collector.toString()
1178 );
1179 } finally {
1180 deleteProject("Test.zip");
1181 }
1182 }
testBug199392_Zip_SamePartCount()1183 public void testBug199392_Zip_SamePartCount() throws CoreException {
1184 try {
1185 IJavaProject project = createJavaProject("Test.zip");
1186 createFolder("/Test.zip/test");
1187 createFile(
1188 "/Test.zip/test/MyClass.java",
1189 "package test;\n" +
1190 "public class MyClass {\n" +
1191 "}\n"
1192 );
1193
1194 // Search all type names with TypeNameMatchRequestor
1195 AbstractJavaSearchTests.TypeNameMatchCollector collector = new AbstractJavaSearchTests.TypeNameMatchCollector() {
1196 @Override
1197 public String toString(){
1198 return toFullyQualifiedNamesString();
1199 }
1200 };
1201 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
1202 new SearchEngine().searchAllTypeNames(
1203 null,
1204 SearchPattern.R_EXACT_MATCH,
1205 new char[] { 'M', 'y' },
1206 SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH,
1207 IJavaSearchConstants.TYPE,
1208 scope,
1209 collector,
1210 IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1211 null);
1212 assertEquals("Found types sounds not to be correct",
1213 "",
1214 collector.toString()
1215 );
1216 } finally {
1217 deleteProject("Test.zip");
1218 }
1219 }
1220
1221 /**
1222 * @bug 210689: [search] Import references not found on working copies not written on disk
1223 * @test Ensure that import references are found when searching on working copies not written on disk
1224 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=210689"
1225 */
testBug210689()1226 public void testBug210689() throws CoreException {
1227 try {
1228 // setup project P0
1229 createJavaProject("P0");
1230 createFolder("/P0/p");
1231 createFile(
1232 "/P0/p/A.java",
1233 "package p;\n" +
1234 "public class A {}\n"
1235 );
1236
1237 // setup project P1
1238 createJavaProject("P1");
1239 createFolder("/P1/p");
1240 createFile(
1241 "/P1/p/A.java",
1242 "package p;\n" +
1243 "public class A {}\n"
1244 );
1245
1246 // setup project P2
1247 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P1" }, "");
1248 createFolder("/P2/p");
1249 createFile(
1250 "/P2/p/B.java",
1251 "package p;\n" +
1252 "public class B {}\n"
1253 );
1254 createFile(
1255 "/P2/p/Ref.java",
1256 "package p;\n" +
1257 "public class Ref {\n" +
1258 " A a;\n" +
1259 " B b;\n" +
1260 "}\n"
1261 );
1262
1263 // Create OR pattern
1264 IType typeA0 = getCompilationUnit("/P0/p/A.java").getType("A");
1265 IType typeA1 = getCompilationUnit("/P1/p/A.java").getType("A");
1266 SearchPattern rightPattern = SearchPattern.createPattern(typeA0, REFERENCES);
1267 SearchPattern leftPattern = SearchPattern.createPattern(typeA1, REFERENCES);
1268 SearchPattern pattern = SearchPattern.createOrPattern(leftPattern, rightPattern);
1269 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
1270 resultCollector.showProject();
1271 resultCollector.showAccuracy(true);
1272 new SearchEngine().search(
1273 pattern,
1274 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
1275 SearchEngine.createWorkspaceScope(),
1276 resultCollector,
1277 null
1278 );
1279 assertSearchResults(
1280 "Unexpected references to /P1/p/A.java",
1281 "p/Ref.java [in P2] p.Ref.a [A] EXACT_MATCH",
1282 resultCollector);
1283 } finally {
1284 deleteProject("P0");
1285 deleteProject("P1");
1286 deleteProject("P2");
1287 }
1288 }
1289
1290 /**
1291 * @bug 229128: JDT Search finding matches in working copies that are not part of scope
1292 * @test Ensure that an annotation reference is not found in a working copy if outside the scope
1293 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=229128"
1294 */
testBug229128()1295 public void testBug229128() throws CoreException {
1296 ICompilationUnit[] copies = new ICompilationUnit[2];
1297 try {
1298 // setup project P1
1299 createJavaProject("P1", new String[] {""}, new String[] {"JCL15_LIB"}, "", "1.5");
1300 createFolder("/P1/p");
1301 createFile(
1302 "/P1/p/MyAnnot.java",
1303 "package p;\n" +
1304 "public @interface MyAnnot {\n" +
1305 "}\n"
1306 );
1307 copies[0] = getWorkingCopy(
1308 "/P1/p/X.java",
1309 "package p;\n" +
1310 "@MyAnnot\n" +
1311 "public class X {\n" +
1312 "}\n",
1313 null/*default working copy owner*/
1314 );
1315
1316 // setup project P2
1317 IJavaProject p2 = createJavaProject("P2", new String[] {""}, new String[] {"JCL15_LIB"}, new String[] { "/P1" }, "", "1.5");
1318 createFolder("/P2/q");
1319 copies[1] = getWorkingCopy(
1320 "/P2/q/Y.java",
1321 "package q;\n" +
1322 "@p.MyAnnot\n" +
1323 "public class Y {\n" +
1324 "}\n",
1325 null/*default working copy owner*/
1326 );
1327
1328 // Get annotation type
1329 IType type = getCompilationUnit("/P1/p/MyAnnot.java").getType("MyAnnot");
1330
1331 // search annotation type reference in P2 scope
1332 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p2}, false/*don't include referenced projects*/);
1333 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
1334 resultCollector.showProject();
1335 resultCollector.showAccuracy(true);
1336 search(type, ANNOTATION_TYPE_REFERENCE, scope, resultCollector);
1337 assertSearchResults(
1338 "Unexpected references of annotation type MyAnnot",
1339 "q/Y.java [in P2] q.Y [p.MyAnnot] EXACT_MATCH",
1340 resultCollector);
1341 } finally {
1342 discardWorkingCopies(copies);
1343 deleteProject("P1");
1344 deleteProject("P2");
1345 }
1346 }
1347
1348 /**
1349 * @bug 229951: StackOverflowError during JavaSearchScope.add for large workspace
1350 * @test Ensure that no StackOverFlowError occurs when searching in a project referencing a cycle
1351 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=229951"
1352 */
testBug229951a()1353 public void testBug229951a() throws Exception {
1354 try {
1355 createJavaProject("P1");
1356 createFile("/P1/test.jar", "");
1357 editFile(
1358 "/P1/.classpath",
1359 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1360 "<classpath>\n" +
1361 " <classpathentry kind=\"lib\" path=\"test.jar\"/>\n" +
1362 " <classpathentry exported=\"true\" kind=\"src\" path=\"/P2\"/>\n" +
1363 " <classpathentry kind=\"output\" path=\"bin\"/>\n" +
1364 "</classpath>"
1365 );
1366 createJavaProject("P2");
1367 createFile("/P2/test.jar", "");
1368 editFile(
1369 "/P2/.classpath",
1370 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1371 "<classpath>\n" +
1372 " <classpathentry kind=\"lib\" path=\"test.jar\"/>\n" +
1373 " <classpathentry exported=\"true\" kind=\"src\" path=\"/P1\"/>\n" +
1374 " <classpathentry kind=\"output\" path=\"bin\"/>\n" +
1375 "</classpath>"
1376 );
1377 createJavaProject("P3", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P2"}, "");
1378 createFile(
1379 "/P3/X229951.java",
1380 "public class X229951 {\n" +
1381 "}"
1382 );
1383 IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
1384 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
1385 search("X229951", TYPE, DECLARATIONS, scope, resultCollector);
1386 assertSearchResults(
1387 "Unexpected references of annotation type MyAnnot",
1388 "X229951.java X229951 [X229951]",
1389 resultCollector);
1390 } finally {
1391 deleteProjects(new String[] {"P1", "P2", "P3"});
1392 }
1393 }
1394
1395 /**
1396 * @bug 229951: StackOverflowError during JavaSearchScope.add for large workspace
1397 * @test Ensure that no StackOverFlowError occurs when creating a search scope on a project referencing a cycle
1398 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=229951"
1399 */
testBug229951b()1400 public void testBug229951b() throws Exception {
1401 try {
1402 createJavaProject("P1");
1403 createFile("/P1/test.jar", "");
1404 editFile(
1405 "/P1/.classpath",
1406 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1407 "<classpath>\n" +
1408 " <classpathentry kind=\"lib\" path=\"test.jar\"/>\n" +
1409 " <classpathentry exported=\"true\" kind=\"src\" path=\"/P2\"/>\n" +
1410 " <classpathentry kind=\"output\" path=\"bin\"/>\n" +
1411 "</classpath>"
1412 );
1413 createJavaProject("P2");
1414 createFile("/P2/test.jar", "");
1415 editFile(
1416 "/P2/.classpath",
1417 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1418 "<classpath>\n" +
1419 " <classpathentry kind=\"lib\" path=\"test.jar\"/>\n" +
1420 " <classpathentry exported=\"true\" kind=\"src\" path=\"/P1\"/>\n" +
1421 " <classpathentry kind=\"output\" path=\"bin\"/>\n" +
1422 "</classpath>"
1423 );
1424 IJavaProject p3 = createJavaProject("P3", new String[] {""}, new String[] {"JCL_LIB"}, new String[] {"/P2"}, "");
1425 createFile(
1426 "/P3/X229951.java",
1427 "public class X229951 {\n" +
1428 "}"
1429 );
1430 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {p3});
1431 assertScopeEquals(
1432 "JavaSearchScope on [\n" +
1433 " /P3\n" +
1434 " " + getExternalJCLPathString() + "\n" +
1435 "]",
1436 scope);
1437 } finally {
1438 deleteProjects(new String[] {"P1", "P2", "P3"});
1439 }
1440 }
1441
1442 /**
1443 * @bug 250454: [search] Cannot find method references between projects
1444 * @test Ensure that search does not find illegal references with given projects setup
1445 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=250454"
1446 */
testBug250454()1447 public void testBug250454() throws CoreException {
1448 try {
1449 // setup project P0
1450 createJavaProject("P0");
1451 createFolder("/P0/p");
1452 createFile(
1453 "/P0/p/Shape.java",
1454 "package p;\n" +
1455 "public interface Shape {\n" +
1456 " public void f();\n" +
1457 "}\n"
1458 );
1459
1460 // setup project P1
1461 createJavaProject("P1", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P0" }, "");
1462 createFolder("/P1/p");
1463 createFile(
1464 "/P1/p/Square.java",
1465 "package p;\n" +
1466 "public class Square implements Shape {\n" +
1467 " public void f() {}\n" +
1468 "}\n"
1469 );
1470
1471 // setup project P2
1472 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB"}, new String[] { "/P0" }, "");
1473 createFolder("/P2/p");
1474 createFile(
1475 "/P2/p/ShapeUser.java",
1476 "package p;\n" +
1477 "public class ShapeUser {\n" +
1478 " public void useShape(Shape p_shape) {\n" +
1479 " p_shape.f();\n" +
1480 " }\n"
1481 );
1482
1483 // Perform search
1484 IType type = getCompilationUnit("/P1/p/Square.java").getType("Square");
1485 IMethod method = type.getMethod("f", new String[0]);
1486 SearchPattern pattern = SearchPattern.createPattern(method, REFERENCES);
1487 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
1488 resultCollector.showProject();
1489 resultCollector.showAccuracy(true);
1490 resultCollector.showFlavors = PatternLocator.SUPER_INVOCATION_FLAVOR;
1491 new SearchEngine().search(
1492 pattern,
1493 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
1494 SearchEngine.createWorkspaceScope(),
1495 resultCollector,
1496 null
1497 );
1498 assertSearchResults(
1499 "Unexpected references to "+method,
1500 "p/ShapeUser.java [in P2] void p.ShapeUser.useShape(Shape) [f()] EXACT_MATCH SUPER INVOCATION",
1501 resultCollector);
1502 } finally {
1503 deleteProject("P0");
1504 deleteProject("P1");
1505 deleteProject("P2");
1506 }
1507 }
testBug250454_jars()1508 public void testBug250454_jars() throws CoreException, IOException {
1509 String jarPath = getExternalPath()+"b250454.jar";
1510 try {
1511 // setup jar
1512 String[] pathsAndContents= new String[] {
1513 "p/Shape.java",
1514 "package p;\n" +
1515 "public interface Shape {\n" +
1516 " public void f();\n" +
1517 "}\n"
1518 };
1519 createJar(pathsAndContents, jarPath);
1520
1521 // setup project P1
1522 createJavaProject("P1", new String[] {""}, new String[] {"JCL_LIB", jarPath}, "");
1523 createFolder("/P1/p");
1524 createFile(
1525 "/P1/p/Square.java",
1526 "package p;\n" +
1527 "public class Square implements Shape {\n" +
1528 " public void f() {}\n" +
1529 "}\n"
1530 );
1531
1532 // setup project P2
1533 createJavaProject("P2", new String[] {""}, new String[] {"JCL_LIB", jarPath}, "");
1534 createFolder("/P2/p");
1535 createFile(
1536 "/P2/p/ShapeUser.java",
1537 "package p;\n" +
1538 "public class ShapeUser {\n" +
1539 " public void useShape(Shape p_shape) {\n" +
1540 " p_shape.f();\n" +
1541 " }\n"
1542 );
1543
1544 // Perform search
1545 IType type = getCompilationUnit("/P1/p/Square.java").getType("Square");
1546 IMethod method = type.getMethod("f", new String[0]);
1547 SearchPattern pattern = SearchPattern.createPattern(method, REFERENCES);
1548 JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
1549 resultCollector.showProject();
1550 resultCollector.showAccuracy(true);
1551 resultCollector.showFlavors = PatternLocator.SUPER_INVOCATION_FLAVOR;
1552 new SearchEngine().search(
1553 pattern,
1554 new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
1555 SearchEngine.createWorkspaceScope(),
1556 resultCollector,
1557 null
1558 );
1559 assertSearchResults(
1560 "Unexpected references to "+method,
1561 "p/ShapeUser.java [in P2] void p.ShapeUser.useShape(Shape) [f()] EXACT_MATCH SUPER INVOCATION",
1562 resultCollector);
1563 } finally {
1564 deleteExternalFile(jarPath);
1565 deleteProject("P1");
1566 deleteProject("P2");
1567 }
1568 }
1569 }
1570