1 /*******************************************************************************
2 * Copyright (c) 2000, 2016 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 org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jdt.core.*;
23 import org.eclipse.jdt.core.search.IJavaSearchConstants;
24 import org.eclipse.jdt.core.search.IJavaSearchScope;
25 import org.eclipse.jdt.core.search.SearchEngine;
26
27 import junit.framework.Test;
28
29 public class ExclusionPatternsTests extends ModifyingResourceTests {
30 IJavaProject project;
ExclusionPatternsTests(String name)31 public ExclusionPatternsTests(String name) {
32 super(name);
33 }
setClasspath(String[] sourceFoldersAndExclusionPatterns)34 protected void setClasspath(String[] sourceFoldersAndExclusionPatterns) throws JavaModelException {
35 this.project.setRawClasspath(createClasspath(sourceFoldersAndExclusionPatterns, false/*no inclusion*/, true/*exclusion*/), null);
36 }
37 @Override
setUp()38 protected void setUp() throws Exception {
39 super.setUp();
40 this.project = createJavaProject("P", new String[] {"src"}, "bin");
41 startDeltas();
42 }
43
44 // Use this static initializer to specify subset for tests
45 // All specified tests which do not belong to the class are skipped...
46 static {
47 // TESTS_NAMES = new String[] { "testCreateExcludedPackage2" };
48 // TESTS_NUMBERS = new int[] { 2, 12 };
49 // TESTS_RANGE = new int[] { 16, -1 };
50 }
suite()51 public static Test suite() {
52 return buildModelTestSuite(ExclusionPatternsTests.class);
53 }
54
55 @Override
tearDown()56 protected void tearDown() throws Exception {
57 stopDeltas();
58 deleteProject("P");
59 super.tearDown();
60 }
61 /*
62 * Ensure that adding an exclusion on a compilation unit
63 * makes it disappear from the children of its package and it is added to the non-java resources.
64 */
testAddExclusionOnCompilationUnit()65 public void testAddExclusionOnCompilationUnit() throws CoreException {
66 createFolder("/P/src/p");
67 createFile(
68 "/P/src/p/A.java",
69 "package p;\n" +
70 "public class A {\n" +
71 "}"
72 );
73
74 clearDeltas();
75 setClasspath(new String[] {"/P/src", "**/A.java"});
76
77 assertDeltas(
78 "Unexpected deltas",
79 "P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
80 " src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
81 " ResourceDelta(/P/.classpath)[*]"
82 );
83
84 IPackageFragment pkg = getPackage("/P/src/p");
85 assertSortedElementsEqual(
86 "Unexpected children",
87 "",
88 pkg.getChildren());
89
90 assertResourceNamesEqual(
91 "Unexpected non-java resources",
92 "A.java",
93 pkg.getNonJavaResources());
94 }
95 /*
96 * Ensure that adding an exclusion on a folder directly under a project (and prj=src)
97 * makes it appear as a non-java resources.
98 * (regression test for bug 29374 Excluded folder on project not returned by Java Model)
99 */
testAddExclusionOnFolderUnderProject()100 public void testAddExclusionOnFolderUnderProject() throws CoreException {
101 try {
102 IJavaProject javaProject = createJavaProject("P1", new String[] {""}, "");
103 createFolder("/P1/doc");
104
105 clearDeltas();
106 javaProject.setRawClasspath(createClasspath(new String[] {"/P1", "doc/"}, false/*no inclusion*/, true/*exclusion*/), null);
107
108 assertDeltas(
109 "Unexpected deltas",
110 "P1[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
111 " <project root>[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
112 " ResourceDelta(/P1/.classpath)[*]"
113 );
114
115 IPackageFragmentRoot root = getPackageFragmentRoot("/P1");
116 assertSortedElementsEqual(
117 "Unexpected children",
118 "<default> [in <project root> [in P1]]",
119 root.getChildren());
120
121 assertResourceNamesEqual(
122 "Unexpected non-java resources of project",
123 ".classpath\n" +
124 ".project\n" +
125 "doc",
126 javaProject.getNonJavaResources());
127 } finally {
128 deleteProject("P1");
129 }
130 }
131 /*
132 * Ensure that adding an exclusion on a package
133 * makes it disappear from the children of its package fragment root
134 * and it is added to the non-java resources.
135 */
testAddExclusionOnPackage()136 public void testAddExclusionOnPackage() throws CoreException {
137 createFolder("/P/src/p");
138
139 clearDeltas();
140 setClasspath(new String[] {"/P/src", "p/"});
141
142 assertDeltas(
143 "Unexpected deltas",
144 "P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
145 " src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
146 " ResourceDelta(/P/.classpath)[*]"
147 );
148
149 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
150 assertSortedElementsEqual(
151 "Unexpected children",
152 "<default> [in src [in P]]",
153 root.getChildren());
154
155 assertResourceNamesEqual(
156 "Unexpected non-java resources",
157 "p",
158 root.getNonJavaResources());
159 }
160 /*
161 * Ensure that adding an exclusion on a primary working copy
162 * makes it disappear from the children of its package and it is added to the non-java resources.
163 */
testAddExclusionOnPrimaryWorkingCopy()164 public void testAddExclusionOnPrimaryWorkingCopy() throws CoreException {
165 createFolder("/P/src/p");
166 createFile(
167 "/P/src/p/A.java",
168 "package p;\n" +
169 "public class A {\n" +
170 "}"
171 );
172
173 ICompilationUnit workingCopy = null;
174 try {
175 workingCopy = getCompilationUnit("/P/src/p/A.java");
176 workingCopy.becomeWorkingCopy(null);
177
178 clearDeltas();
179 setClasspath(new String[] {"/P/src", "**/A.java"});
180
181 assertDeltas(
182 "Unexpected deltas",
183 "P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
184 " src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
185 " ResourceDelta(/P/.classpath)[*]"
186 );
187
188 IPackageFragment pkg = getPackage("/P/src/p");
189 assertSortedElementsEqual(
190 "Unexpected children",
191 "",
192 pkg.getChildren());
193
194 assertResourceNamesEqual(
195 "Unexpected non-java resources",
196 "A.java",
197 pkg.getNonJavaResources());
198 } finally {
199 if (workingCopy != null) {
200 workingCopy.discardWorkingCopy();
201 }
202 }
203 }
204 /*
205 * Ensure that adding a file to a folder that is excluded reports the correct delta.
206 * (regression test for bug 29621 Wrong Delta When Adding to Filtered Folder)
207 */
testAddToExcludedFolder()208 public void testAddToExcludedFolder() throws CoreException {
209 createFolder("/P/src/icons");
210
211 // exclude folder and its contents
212 setClasspath(new String[] {"/P/src", "icons/"});
213
214 clearDeltas();
215 createFile("/P/src/icons/my.txt", "");
216 assertDeltas(
217 "Unexpected deltas",
218 "P[*]: {CHILDREN}\n" +
219 " src[*]: {CONTENT}\n" +
220 " ResourceDelta(/P/src/icons)[*]"
221 );
222
223 clearDeltas();
224 deleteFile("/P/src/icons/my.txt");
225 assertDeltas(
226 "Unexpected deltas",
227 "P[*]: {CHILDREN}\n" +
228 " src[*]: {CONTENT}\n" +
229 " ResourceDelta(/P/src/icons)[*]"
230 );
231 }
232 /*
233 * Ensure that creating an excluded compilation unit
234 * doesn't make it appear as a child of its package but it is a non-java resource.
235 */
testCreateExcludedCompilationUnit()236 public void testCreateExcludedCompilationUnit() throws CoreException {
237 setClasspath(new String[] {"/P/src", "**/A.java"});
238 createFolder("/P/src/p");
239 IPackageFragment pkg = getPackage("/P/src/p");
240
241 clearDeltas();
242 pkg.createCompilationUnit(
243 "A.java",
244 "package p;\n" +
245 "public class A {\n" +
246 "}",
247 false,
248 null);
249
250 assertDeltas(
251 "Unexpected deltas",
252 "P[*]: {CHILDREN}\n" +
253 " src[*]: {CHILDREN}\n" +
254 " p[*]: {CONTENT}\n" +
255 " ResourceDelta(/P/src/p/A.java)[+]"
256 );
257
258 assertSortedElementsEqual(
259 "Unexpected children",
260 "",
261 pkg.getChildren());
262
263 assertResourceNamesEqual(
264 "Unexpected non-java resources",
265 "A.java",
266 pkg.getNonJavaResources());
267 }
268 /*
269 * Ensure that crearing an excluded package
270 * doesn't make it appear as a child of its package fragment root but it is a non-java resource.
271 */
testCreateExcludedPackage()272 public void testCreateExcludedPackage() throws CoreException {
273 setClasspath(new String[] {"/P/src", "p/"});
274 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
275
276 clearDeltas();
277 root.createPackageFragment("p", false, null);
278
279 assertDeltas(
280 "Unexpected deltas",
281 "P[*]: {CHILDREN}\n" +
282 " src[*]: {CONTENT}\n" +
283 " ResourceDelta(/P/src/p)[+]"
284 );
285
286 assertSortedElementsEqual(
287 "Unexpected children",
288 "<default> [in src [in P]]",
289 root.getChildren());
290
291 assertResourceNamesEqual(
292 "Unexpected non-java resources",
293 "p",
294 root.getNonJavaResources());
295 }
296 /*
297 * Ensure that creating an excluded package doesn't make it appear as a child of its package fragment root but it is a non-java resource.
298 * (regression test for bug 65637 [model] Excluded package still in Java model)
299 */
testCreateExcludedPackage2()300 public void testCreateExcludedPackage2() throws CoreException {
301 setClasspath(new String[] {"/P/src", "org/*|org/eclipse/*"});
302
303 // Trigger population of cache to check if it is properly invalidated by the delta processor.
304 // See http://bugs.eclipse.org/500714
305 getPackageFragmentRoot("/P/src").getChildren();
306
307 clearDeltas();
308 createFolder("/P/src/org/eclipse/mypack");
309
310 assertDeltas(
311 "Unexpected deltas",
312 "P[*]: {CHILDREN}\n" +
313 " src[*]: {CONTENT}\n" +
314 " ResourceDelta(/P/src/org)[+]"
315 );
316
317 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
318 assertSortedElementsEqual(
319 "Unexpected children",
320 "<default> [in src [in P]]\n" +
321 "org.eclipse.mypack [in src [in P]]",
322 root.getChildren());
323
324 assertResourceNamesEqual(
325 "Unexpected non-java resources",
326 "org",
327 root.getNonJavaResources());
328 }
329 /*
330 * Ensure that creating a folder that represents an excluded and included package
331 * have the correct delta, Java elements and non-Java resources.
332 * (regression test for 67789 Java element delta from refresh contains excluded package)
333 */
testCreateExcludedAndIncludedPackages()334 public void testCreateExcludedAndIncludedPackages() throws CoreException {
335 setClasspath(new String[] {"/P/src", "p1/p2/"});
336 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
337
338 clearDeltas();
339 createFolder("/P/src/p1/p2");
340
341 assertDeltas(
342 "Unexpected deltas",
343 "P[*]: {CHILDREN}\n" +
344 " src[*]: {CHILDREN}\n" +
345 " p1[+]: {}"
346 );
347
348 assertSortedElementsEqual(
349 "Unexpected children",
350 "<default> [in src [in P]]\n" +
351 "p1 [in src [in P]]",
352 root.getChildren());
353
354 assertResourceNamesEqual(
355 "Unexpected non-java resources",
356 "p2",
357 root.getPackageFragment("p1").getNonJavaResources());
358 }
359 /*
360 * Ensure that creating a file that corresponds to an excluded compilation unit
361 * doesn't make it appear as a child of its package but it is a non-java resource.
362 */
testCreateResourceExcludedCompilationUnit()363 public void testCreateResourceExcludedCompilationUnit() throws CoreException {
364 setClasspath(new String[] {"/P/src", "**/A.java"});
365 createFolder("/P/src/p");
366
367 clearDeltas();
368 createFile(
369 "/P/src/p/A.java",
370 "package p;\n" +
371 "public class A {\n" +
372 "}"
373 );
374
375 assertDeltas(
376 "Unexpected deltas",
377 "P[*]: {CHILDREN}\n" +
378 " src[*]: {CHILDREN}\n" +
379 " p[*]: {CONTENT}\n" +
380 " ResourceDelta(/P/src/p/A.java)[+]"
381 );
382
383 IPackageFragment pkg = getPackage("/P/src/p");
384 assertSortedElementsEqual(
385 "Unexpected children",
386 "",
387 pkg.getChildren());
388
389 assertResourceNamesEqual(
390 "Unexpected non-java resources",
391 "A.java",
392 pkg.getNonJavaResources());
393 }
394 /*
395 * Ensure that creating a folder that corresponds to an excluded package
396 * doesn't make it appear as a child of its package fragment root but it is a non-java resource.
397 */
testCreateResourceExcludedPackage()398 public void testCreateResourceExcludedPackage() throws CoreException {
399 setClasspath(new String[] {"/P/src", "p/"});
400
401 clearDeltas();
402 createFolder("/P/src/p");
403
404 assertDeltas(
405 "Unexpected deltas",
406 "P[*]: {CHILDREN}\n" +
407 " src[*]: {CONTENT}\n" +
408 " ResourceDelta(/P/src/p)[+]"
409 );
410
411 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
412 assertSortedElementsEqual(
413 "Unexpected children",
414 "<default> [in src [in P]]",
415 root.getChildren());
416
417 assertResourceNamesEqual(
418 "Unexpected non-java resources",
419 "p",
420 root.getNonJavaResources());
421 }
422 /*
423 * Ensures that a cu that is not excluded is on the classpath of the project.
424 */
testIsOnClasspath1()425 public void testIsOnClasspath1() throws CoreException {
426 setClasspath(new String[] {"/P/src", ""});
427 createFolder("/P/src/p");
428 IFile file = createFile(
429 "/P/src/p/A.java",
430 "package p;\n" +
431 "public class A {\n" +
432 "}"
433 );
434 assertTrue("Resource should be on classpath", this.project.isOnClasspath(file));
435
436 ICompilationUnit cu = getCompilationUnit("/P/src/p/A.java");
437 assertTrue("CU should be on classpath", this.project.isOnClasspath(cu));
438 }
439 /*
440 * Ensures that a cu that is excluded is not on the classpath of the project.
441 */
testIsOnClasspath2()442 public void testIsOnClasspath2() throws CoreException {
443 setClasspath(new String[] {"/P/src", "**/A.java"});
444 createFolder("/P/src/p");
445 IFile file = createFile(
446 "/P/src/p/A.java",
447 "package p;\n" +
448 "public class A {\n" +
449 "}"
450 );
451 assertTrue("Resource should not be on classpath", !this.project.isOnClasspath(file));
452
453 ICompilationUnit cu = getCompilationUnit("/P/src/p/A.java");
454 assertTrue("CU should not be on classpath", !this.project.isOnClasspath(cu));
455 }
456 /*
457 * Ensures that a non-java resource that is not excluded is on the classpath of the project.
458 */
testIsOnClasspath3()459 public void testIsOnClasspath3() throws CoreException {
460 setClasspath(new String[] {"/P/src", ""});
461 createFolder("/P/src/p");
462 IFile file = createFile("/P/src/p/readme.txt", "");
463 assertTrue("Resource should be on classpath", this.project.isOnClasspath(file));
464 }
465 /*
466 * Ensures that a non-java resource that is excluded is not on the classpath of the project.
467 */
testIsOnClasspath4()468 public void testIsOnClasspath4() throws CoreException {
469 setClasspath(new String[] {"/P/src", "p/**"});
470 createFolder("/P/src/p");
471 IFile file = createFile("/P/src/p/readme.txt", "");
472 assertTrue("Resource should not be on classpath", !this.project.isOnClasspath(file));
473 }
474 /*
475 * Ensures that an excluded nested source folder doesn't appear as a non-java resource of the outer folder.
476 * (regression test for bug 28115 Ubiquitous resource in the JavaModel)
477 *
478 */
testNestedSourceFolder1()479 public void testNestedSourceFolder1() throws CoreException {
480 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
481 createFolder("/P/src1/src2");
482 IPackageFragmentRoot root1 = getPackageFragmentRoot("/P/src1");
483 assertResourceNamesEqual(
484 "Unexpected non-java resources for /P/src1",
485 "",
486 root1.getNonJavaResources());
487 }
488 /*
489 * Ensures that adding a .java file in a nested source folder reports
490 * a delta on the nested source folder and not on the outer one.
491 */
testNestedSourceFolder2()492 public void testNestedSourceFolder2() throws CoreException {
493 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
494 createFolder("/P/src1/src2");
495
496 clearDeltas();
497 createFile(
498 "/P/src1/src2/A.java",
499 "public class A {\n" +
500 "}"
501 );
502
503 assertDeltas(
504 "Unexpected deltas",
505 "P[*]: {CHILDREN}\n" +
506 " src1/src2[*]: {CHILDREN}\n" +
507 " <default>[*]: {CHILDREN}\n" +
508 " A.java[+]: {}"
509 );
510 }
511 /*
512 * Ensures that adding a .txt file in a nested source folder reports
513 * a resource delta on the nested source folder and not on the outer one.
514 */
testNestedSourceFolder3()515 public void testNestedSourceFolder3() throws CoreException {
516 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
517 createFolder("/P/src1/src2");
518
519 clearDeltas();
520 createFile("/P/src1/src2/readme.txt", "");
521
522 assertDeltas(
523 "Unexpected deltas",
524 "P[*]: {CHILDREN}\n" +
525 " src1/src2[*]: {CONTENT}\n" +
526 " ResourceDelta(/P/src1/src2/readme.txt)[+]"
527 );
528 }
529 /*
530 * Ensures that adding a folder in a nested source folder reports
531 * a delta on the nested source folder and not on the outer one.
532 */
testNestedSourceFolder4()533 public void testNestedSourceFolder4() throws CoreException {
534 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
535 createFolder("/P/src1/src2");
536
537 clearDeltas();
538 createFolder("/P/src1/src2/p");
539
540 assertDeltas(
541 "Unexpected deltas",
542 "P[*]: {CHILDREN}\n" +
543 " src1/src2[*]: {CHILDREN}\n" +
544 " p[+]: {}"
545 );
546 }
547 /*
548 * Ensures that adding a folder in a outer source folder reports
549 * a delta on the outer source folder and not on the nested one.
550 */
testNestedSourceFolder5()551 public void testNestedSourceFolder5() throws CoreException {
552 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
553 createFolder("/P/src1/src2");
554
555 clearDeltas();
556 createFolder("/P/src1/p");
557
558 assertDeltas(
559 "Unexpected deltas",
560 "P[*]: {CHILDREN}\n" +
561 " src1[*]: {CHILDREN}\n" +
562 " p[+]: {}"
563 );
564 }
565 /*
566 * Ensures that moving a package from an outer source folder to a nested
567 * source folder reports a move delta.
568 */
testNestedSourceFolder6()569 public void testNestedSourceFolder6() throws CoreException {
570 setClasspath(new String[] {"/P/src1", "src2/**", "/P/src1/src2", ""});
571 createFolder("/P/src1/src2");
572 createFolder("/P/src1/p");
573
574 clearDeltas();
575 moveFolder("/P/src1/p", "/P/src1/src2/p");
576
577 assertDeltas(
578 "Unexpected deltas",
579 "P[*]: {CHILDREN}\n" +
580 " src1[*]: {CHILDREN}\n" +
581 " p[-]: {MOVED_TO(p [in src1/src2 [in P]])}\n" +
582 " src1/src2[*]: {CHILDREN}\n" +
583 " p[+]: {MOVED_FROM(p [in src1 [in P]])}"
584 );
585 }
586 /*
587 * Ensure that renaming an excluded compilation unit so that it is not excluded any longer
588 * makes it appears as a child of its package and it is removed from the non-java resources.
589 */
testRenameExcludedCompilationUnit()590 public void testRenameExcludedCompilationUnit() throws CoreException {
591 setClasspath(new String[] {"/P/src", "**/A.java"});
592 createFolder("/P/src/p");
593 IFile file = createFile(
594 "/P/src/p/A.java",
595 "package p;\n" +
596 "public class A {\n" +
597 "}"
598 );
599
600 clearDeltas();
601 file.move(new Path("/P/src/p/B.java"), false, null);
602
603 assertDeltas(
604 "Unexpected deltas",
605 "P[*]: {CHILDREN}\n" +
606 " src[*]: {CHILDREN}\n" +
607 " p[*]: {CHILDREN | CONTENT}\n" +
608 " B.java[+]: {}\n" +
609 " ResourceDelta(/P/src/p/A.java)[-]"
610 );
611
612 IPackageFragment pkg = getPackage("/P/src/p");
613 assertSortedElementsEqual(
614 "Unexpected children",
615 "B.java [in p [in src [in P]]]",
616 pkg.getChildren());
617
618 assertResourceNamesEqual(
619 "Unexpected non-java resources",
620 "",
621 pkg.getNonJavaResources());
622 }
623 /*
624 * Ensure that renaming an excluded package so that it is not excluded any longer
625 * makes it appears as a child of its package fragment root
626 * and it is removed from the non-java resources.
627 */
testRenameExcludedPackage()628 public void testRenameExcludedPackage() throws CoreException {
629 setClasspath(new String[] {"/P/src", "p/"});
630 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
631 IPackageFragment pkg = root.createPackageFragment("p", false, null);
632
633 clearDeltas();
634 pkg.getResource().move(new Path("/P/src/q"), false, null);
635
636 assertDeltas(
637 "Unexpected deltas",
638 "P[*]: {CHILDREN}\n" +
639 " src[*]: {CHILDREN | CONTENT}\n" +
640 " q[+]: {}\n" +
641 " ResourceDelta(/P/src/p)[-]"
642 );
643
644 assertSortedElementsEqual(
645 "Unexpected children",
646 "<default> [in src [in P]]\n" +
647 "q [in src [in P]]",
648 root.getChildren());
649
650 assertResourceNamesEqual(
651 "Unexpected non-java resources",
652 "",
653 root.getNonJavaResources());
654 }
655 /*
656 * Ensure that renaming a file that corresponds to an excluded compilation unit so that it is not excluded any longer
657 * makes it appears as a child of its package and it is removed from the non-java resources.
658 */
testRenameResourceExcludedCompilationUnit()659 public void testRenameResourceExcludedCompilationUnit() throws CoreException {
660 setClasspath(new String[] {"/P/src", "**/A.java"});
661 createFolder("/P/src/p");
662 IFile file = createFile(
663 "/P/src/p/A.java",
664 "package p;\n" +
665 "public class A {\n" +
666 "}"
667 );
668
669 clearDeltas();
670 file.move(new Path("/P/src/p/B.java"), false, null);
671
672 assertDeltas(
673 "Unexpected deltas",
674 "P[*]: {CHILDREN}\n" +
675 " src[*]: {CHILDREN}\n" +
676 " p[*]: {CHILDREN | CONTENT}\n" +
677 " B.java[+]: {}\n" +
678 " ResourceDelta(/P/src/p/A.java)[-]"
679 );
680
681 IPackageFragment pkg = getPackage("/P/src/p");
682 assertSortedElementsEqual(
683 "Unexpected children",
684 "B.java [in p [in src [in P]]]",
685 pkg.getChildren());
686
687 assertResourceNamesEqual(
688 "Unexpected non-java resources",
689 "",
690 pkg.getNonJavaResources());
691 }
692 /*
693 * Ensure search doesn't find matches in an excluded compilation unit.
694 */
testSearchWithExcludedCompilationUnit1()695 public void testSearchWithExcludedCompilationUnit1() throws CoreException {
696 setClasspath(new String[] {"/P/src", "**/A.java"});
697 createFolder("/P/src/p");
698 createFile(
699 "/P/src/p/A.java",
700 "package p;\n" +
701 "public class A {\n" +
702 "}"
703 );
704
705 JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
706 search(
707 "A",
708 IJavaSearchConstants.TYPE,
709 IJavaSearchConstants.DECLARATIONS,
710 SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
711 resultCollector);
712 assertEquals(
713 "Unexpected matches found",
714 "",
715 resultCollector.toString());
716 }
717 /*
718 * Ensure search find matches in a compilation unit that was excluded but that is not any longer.
719 */
testSearchWithExcludedCompilationUnit2()720 public void testSearchWithExcludedCompilationUnit2() throws CoreException {
721 setClasspath(new String[] {"/P/src", "A.java"});
722 createFolder("/P/src/p");
723 createFile(
724 "/P/src/p/A.java",
725 "package p;\n" +
726 "public class A {\n" +
727 "}"
728 );
729
730 setClasspath(new String[] {"/P/src", ""});
731 JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
732 search(
733 "A",
734 IJavaSearchConstants.TYPE,
735 IJavaSearchConstants.DECLARATIONS,
736 SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
737 resultCollector);
738 assertEquals(
739 "Unexpected matches found",
740 "src/p/A.java p.A [A]",
741 resultCollector.toString());
742 }
743 /*
744 * Ensure that removing a folder that represents an excluded and included package
745 * have the correct delta, Java elements and non-Java resources.
746 * (regression test for 67789 Java element delta from refresh contains excluded package)
747 */
testRemoveExcludedAndIncludedPackages()748 public void testRemoveExcludedAndIncludedPackages() throws CoreException {
749 setClasspath(new String[] {"/P/src", "p1/p2/"});
750 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
751 createFolder("/P/src/p1/p2");
752
753 clearDeltas();
754 deleteFolder("/P/src/p1");
755
756 assertDeltas(
757 "Unexpected deltas",
758 "P[*]: {CHILDREN}\n" +
759 " src[*]: {CHILDREN}\n" +
760 " p1[-]: {}"
761 );
762
763 assertSortedElementsEqual(
764 "Unexpected children",
765 "<default> [in src [in P]]",
766 root.getChildren());
767
768 assertResourceNamesEqual(
769 "Unexpected non-java resources",
770 "",
771 root.getNonJavaResources());
772 }
773
774 /*
775 * Ensure that renaming a folder that corresponds to an excluded package
776 * so that it is not excluded any longer
777 * makes it appears as a child of its package fragment root
778 * and it is removed from the non-java resources.
779 */
testRenameResourceExcludedPackage()780 public void testRenameResourceExcludedPackage() throws CoreException {
781 setClasspath(new String[] {"/P/src", "p/"});
782 IFolder folder = createFolder("/P/src/p");
783
784 clearDeltas();
785 folder.move(new Path("/P/src/q"), false, null);
786
787 assertDeltas(
788 "Unexpected deltas",
789 "P[*]: {CHILDREN}\n" +
790 " src[*]: {CHILDREN | CONTENT}\n" +
791 " q[+]: {}\n" +
792 " ResourceDelta(/P/src/p)[-]"
793 );
794
795 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
796 assertSortedElementsEqual(
797 "Unexpected children",
798 "<default> [in src [in P]]\n" +
799 "q [in src [in P]]",
800 root.getChildren());
801
802 assertResourceNamesEqual(
803 "Unexpected non-java resources",
804 "",
805 root.getNonJavaResources());
806 }
807 /*
808 * Ensure that a potential match in the output folder is not indexed.
809 * (regression test for bug 32041 Multiple output folders fooling Java Model)
810 */
testSearchPotentialMatchInOutput()811 public void testSearchPotentialMatchInOutput() throws CoreException {
812 try {
813 JavaCore.run(new IWorkspaceRunnable() {
814 public void run(IProgressMonitor monitor) throws CoreException {
815 IJavaProject javaProject = createJavaProject("P2", new String[] {}, "bin");
816 javaProject.setRawClasspath(createClasspath(new String[] {"/P2", "src/", "/P2/src", ""}, false/*no inclusion*/, true/*exclusion*/), null);
817 createFile(
818 "/P2/bin/X.java",
819 "public class X {\n" +
820 "}"
821 );
822 }
823 }, null);
824
825 JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
826 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {getJavaProject("P")});
827 search(
828 "X",
829 IJavaSearchConstants.TYPE,
830 IJavaSearchConstants.DECLARATIONS,
831 scope,
832 resultCollector);
833 assertEquals("", resultCollector.toString());
834 } finally {
835 deleteProject("P2");
836 }
837 }
838 /*
839 * Ensure that removing the exclusion on a compilation unit
840 * makes it appears as a child of its package and it is removed from the non-java resources.
841 */
testRemoveExclusionOnCompilationUnit()842 public void testRemoveExclusionOnCompilationUnit() throws CoreException {
843 setClasspath(new String[] {"/P/src", "A.java"});
844 createFolder("/P/src/p");
845 createFile(
846 "/P/src/p/A.java",
847 "package p;\n" +
848 "public class A {\n" +
849 "}"
850 );
851
852 clearDeltas();
853 setClasspath(new String[] {"/P/src", ""});
854
855 assertDeltas(
856 "Unexpected deltas",
857 "P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
858 " src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
859 " ResourceDelta(/P/.classpath)[*]"
860 );
861
862 IPackageFragment pkg = getPackage("/P/src/p");
863 assertSortedElementsEqual(
864 "Unexpected children",
865 "A.java [in p [in src [in P]]]",
866 pkg.getChildren());
867
868 assertResourceNamesEqual(
869 "Unexpected non-java resources",
870 "",
871 pkg.getNonJavaResources());
872 }
873 /*
874 * Ensure that removing the exclusion on a package
875 * makes it appears as a child of its package fragment root
876 * and it is removed from the non-java resources.
877 */
testRemoveExclusionOnPackage()878 public void testRemoveExclusionOnPackage() throws CoreException {
879 setClasspath(new String[] {"/P/src", "p"});
880 createFolder("/P/src/p");
881
882 clearDeltas();
883 setClasspath(new String[] {"/P/src", ""});
884
885 assertDeltas(
886 "Unexpected deltas",
887 "P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
888 " src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
889 " ResourceDelta(/P/.classpath)[*]"
890 );
891
892 IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
893 assertSortedElementsEqual(
894 "Unexpected children",
895 "<default> [in src [in P]]\n" +
896 "p [in src [in P]]",
897 root.getChildren());
898
899 assertResourceNamesEqual(
900 "Unexpected non-java resources",
901 "",
902 root.getNonJavaResources());
903 }
904 }
905