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 org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jdt.core.*;
21 import org.eclipse.jdt.core.search.IJavaSearchConstants;
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23 import org.eclipse.jdt.core.search.SearchEngine;
24 
25 import junit.framework.Test;
26 
27 public class InclusionPatternsTests extends ModifyingResourceTests {
28 	IJavaProject project;
InclusionPatternsTests(String name)29 public InclusionPatternsTests(String name) {
30 	super(name);
31 }
32 
33 static {
34 //	TESTS_NAMES = new String[] { "testIncludeCUOnly02" };
35 }
suite()36 public static Test suite() {
37 	return buildModelTestSuite(InclusionPatternsTests.class);
38 }
setClasspath(String[] sourceFoldersAndInclusionPatterns)39 protected void setClasspath(String[] sourceFoldersAndInclusionPatterns) throws JavaModelException {
40 	this.project.setRawClasspath(createClasspath(sourceFoldersAndInclusionPatterns, true/*inclusion*/, false/*no exclusion*/), null);
41 }
42 @Override
setUp()43 protected void setUp() throws Exception {
44 	super.setUp();
45 	this.project = createJavaProject( "P", new String[] {"src"}, new String[] {}, new String[] {}, new boolean[] {}, "bin", new String[] {"bin"}, new String[][] {new String[] {}}, new String[][] {new String[] {"**"}}, "1.4");
46 	startDeltas();
47 }
48 
49 @Override
tearDown()50 protected void tearDown() throws Exception {
51 	stopDeltas();
52 	deleteProject("P");
53 	super.tearDown();
54 }
55 /*
56  * Ensure that adding an inclusion on a compilation unit
57  * makes it appear in the children of its package and it is removed from the non-java resources.
58  */
testAddInclusionOnCompilationUnit()59 public void testAddInclusionOnCompilationUnit() throws CoreException {
60 	createFolder("/P/src/p");
61 	createFile(
62 		"/P/src/p/A.java",
63 		"package p;\n" +
64 		"public class A {\n" +
65 		"}"
66 	);
67 
68 	clearDeltas();
69 	setClasspath(new String[] {"/P/src", "**/A.java"});
70 
71 	assertDeltas(
72 		"Unexpected deltas",
73 		"P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
74 		"	src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
75 		"	ResourceDelta(/P/.classpath)[*]"
76 	);
77 
78 	IPackageFragment pkg = getPackage("/P/src/p");
79 	assertSortedElementsEqual(
80 		"Unexpected children",
81 		"A.java [in p [in src [in P]]]",
82 		pkg.getChildren());
83 
84 	assertResourceNamesEqual(
85 		"Unexpected non-java resources",
86 		"",
87 		pkg.getNonJavaResources());
88 }
89 /*
90  * Ensure that adding an inclusion on a folder directly under a project (and prj=src)
91  * makes it disappear from the non-java resources.
92  */
testAddInclusionOnFolderUnderProject()93 public void testAddInclusionOnFolderUnderProject() throws CoreException {
94 	try {
95 		IJavaProject javaProject = createJavaProject("P1", new String[] {""}, "");
96 		createFolder("/P1/doc");
97 
98 		clearDeltas();
99 		javaProject.setRawClasspath(createClasspath(new String[] {"/P1", "doc/"}, true/*inclusion*/, false/*no exclusion*/), null);
100 
101 		assertDeltas(
102 			"Unexpected deltas",
103 			"P1[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
104 			"	<project root>[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
105 			"	ResourceDelta(/P1/.classpath)[*]"
106 		);
107 
108 		IPackageFragmentRoot root = getPackageFragmentRoot("/P1");
109 		assertSortedElementsEqual(
110 			"Unexpected children",
111 			"doc [in <project root> [in P1]]",
112 			root.getChildren());
113 
114 		assertResourceNamesEqual(
115 			"Unexpected non-java resources of project",
116 			".classpath\n" +
117 			".project",
118 			javaProject.getNonJavaResources());
119 	} finally {
120 		deleteProject("P1");
121 	}
122 }
123 /*
124  * Ensure that adding an inclusion on a package
125  * makes it appear in the list of children of its package fragment root
126  * and it is removed from the non-java resources.
127  */
testAddInclusionOnPackage()128 public void testAddInclusionOnPackage() throws CoreException {
129 	createFolder("/P/src/p");
130 
131 	clearDeltas();
132 	setClasspath(new String[] {"/P/src", "p/"});
133 
134 	assertDeltas(
135 		"Unexpected deltas",
136 		"P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
137 		"	src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
138 		"	ResourceDelta(/P/.classpath)[*]"
139 	);
140 
141 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
142 	assertSortedElementsEqual(
143 		"Unexpected children",
144 		"p [in src [in P]]",
145 		root.getChildren());
146 
147 	assertResourceNamesEqual(
148 		"Unexpected non-java resources",
149 		"",
150 		root.getNonJavaResources());
151 }
152 /*
153  * Ensure that adding a file to a folder that is included reports the correct delta.
154  */
testAddToIncludedFolder()155 public void testAddToIncludedFolder() throws CoreException {
156 	createFolder("/P/src/p");
157 
158 	// include folder and its contents
159 	setClasspath(new String[] {"/P/src", "p/"});
160 
161 	clearDeltas();
162 	createFile("/P/src/p/my.txt", "");
163 	assertDeltas(
164 		"Unexpected deltas",
165 		"P[*]: {CHILDREN}\n" +
166 		"	src[*]: {CHILDREN}\n" +
167 		"		p[*]: {CONTENT}\n" +
168 		"			ResourceDelta(/P/src/p/my.txt)[+]"
169 	);
170 
171 	clearDeltas();
172 	deleteFile("/P/src/p/my.txt");
173 	assertDeltas(
174 		"Unexpected deltas",
175 		"P[*]: {CHILDREN}\n" +
176 		"	src[*]: {CHILDREN}\n" +
177 		"		p[*]: {CONTENT}\n" +
178 		"			ResourceDelta(/P/src/p/my.txt)[-]"
179 	);
180 }
181 /*
182  * Ensure that creating an included compilation unit
183  * doesn't make it appear as a non-java resource but it is a child of its package.
184  */
testCreateIncludedCompilationUnit()185 public void testCreateIncludedCompilationUnit() throws CoreException {
186 	setClasspath(new String[] {"/P/src", "**/A.java"});
187 	createFolder("/P/src/p");
188 	IPackageFragment pkg = getPackage("/P/src/p");
189 
190 	clearDeltas();
191 	pkg.createCompilationUnit(
192 		"A.java",
193 		"package p;\n" +
194 		"public class A {\n" +
195 		"}",
196 		false,
197 		null);
198 
199 	assertDeltas(
200 		"Unexpected deltas",
201 		"P[*]: {CHILDREN}\n" +
202 		"	src[*]: {CHILDREN}\n" +
203 		"		p[*]: {CHILDREN}\n" +
204 		"			A.java[+]: {}"
205 	);
206 
207 	assertSortedElementsEqual(
208 		"Unexpected children",
209 		"A.java [in p [in src [in P]]]",
210 		pkg.getChildren());
211 
212 	assertResourceNamesEqual(
213 		"Unexpected non-java resources",
214 		"",
215 		pkg.getNonJavaResources());
216 }
217 /*
218  * Ensure that creating an included package
219  * makes it appear as a child of its package fragment root and not as a non-java resource.
220  */
testCreateIncludedPackage()221 public void testCreateIncludedPackage() throws CoreException {
222 	setClasspath(new String[] {"/P/src", "p/"});
223 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
224 
225 	clearDeltas();
226 	root.createPackageFragment("p", false, null);
227 
228 	assertDeltas(
229 		"Unexpected deltas",
230 		"P[*]: {CHILDREN}\n" +
231 		"	src[*]: {CHILDREN}\n" +
232 		"		p[+]: {}"
233 	);
234 
235 	assertSortedElementsEqual(
236 		"Unexpected children",
237 		"p [in src [in P]]",
238 		root.getChildren());
239 
240 	assertResourceNamesEqual(
241 		"Unexpected non-java resources",
242 		"",
243 		root.getNonJavaResources());
244 }
245 /*
246  * Ensure that creating a file that corresponds to an included compilation unit
247  * makes it appear as a child of its package and not as a non-java resource.
248  */
testCreateResourceIncludedCompilationUnit()249 public void testCreateResourceIncludedCompilationUnit() throws CoreException {
250 	setClasspath(new String[] {"/P/src", "**/A.java"});
251 	createFolder("/P/src/p");
252 
253 	clearDeltas();
254 	createFile(
255 		"/P/src/p/A.java",
256 		"package p;\n" +
257 		"public class A {\n" +
258 		"}"
259 	);
260 
261 	assertDeltas(
262 		"Unexpected deltas",
263 		"P[*]: {CHILDREN}\n" +
264 		"	src[*]: {CHILDREN}\n" +
265 		"		p[*]: {CHILDREN}\n" +
266 		"			A.java[+]: {}"
267 	);
268 
269 	IPackageFragment pkg = getPackage("/P/src/p");
270 	assertSortedElementsEqual(
271 		"Unexpected children",
272 		"A.java [in p [in src [in P]]]",
273 		pkg.getChildren());
274 
275 	assertResourceNamesEqual(
276 		"Unexpected non-java resources",
277 		"",
278 		pkg.getNonJavaResources());
279 }
280 /*
281  * Ensure that creating a file that corresponds to an included compilation unit
282  * in a folder that is not included makes it appear as a child of its package and not as a non-java resource.
283  * (regression test for bug 65234 Inclusion filter not working)
284  */
testCreateResourceIncludedCompilationUnit2()285 public void testCreateResourceIncludedCompilationUnit2() throws CoreException {
286 	setClasspath(new String[] {"/P/src", "p1/p2/p3/A.java"});
287 	createFolder("/P/src/p1/p2/p3");
288 
289 	clearDeltas();
290 	createFile(
291 		"/P/src/p1/p2/p3/A.java",
292 		"package p1.p2.p3;\n" +
293 		"public class A {\n" +
294 		"}"
295 	);
296 
297 	assertDeltas(
298 		"Unexpected deltas",
299 		"P[*]: {CHILDREN}\n" +
300 		"	src[*]: {CHILDREN | CONTENT}\n" +
301 		"		p1.p2.p3[*]: {CHILDREN}\n" +
302 		"			A.java[+]: {}\n" +
303 		"		ResourceDelta(/P/src/p1)[*]"
304 	);
305 
306 	IPackageFragment pkg = getPackage("/P/src/p1/p2/p3");
307 	assertSortedElementsEqual(
308 		"Unexpected children",
309 		"A.java [in p1.p2.p3 [in src [in P]]]",
310 		pkg.getChildren());
311 
312 	assertResourceNamesEqual(
313 		"Unexpected non-java resources",
314 		"",
315 		pkg.getNonJavaResources());
316 }
317 /*
318  * Ensure that creating a folder that corresponds to an included package
319  * makes it appear as a child of its package fragment root and not as a non-java resource.
320  */
testCreateResourceIncludedPackage()321 public void testCreateResourceIncludedPackage() throws CoreException {
322 	setClasspath(new String[] {"/P/src", "p/"});
323 
324 	clearDeltas();
325 	createFolder("/P/src/p");
326 
327 	assertDeltas(
328 		"Unexpected deltas",
329 		"P[*]: {CHILDREN}\n" +
330 		"	src[*]: {CHILDREN}\n" +
331 		"		p[+]: {}"
332 	);
333 
334 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
335 	assertSortedElementsEqual(
336 		"Unexpected children",
337 		"p [in src [in P]]",
338 		root.getChildren());
339 
340 	assertResourceNamesEqual(
341 		"Unexpected non-java resources",
342 		"",
343 		root.getNonJavaResources());
344 }
345 /*
346  * Ensure that creating a folder that is included in a folder that is not included
347  * makes it appear as a child of its package fragment root and not as a non-java resource.
348  * (regression test for bug 65234 Inclusion filter not working)
349  */
testCreateResourceIncludedPackage2()350 public void testCreateResourceIncludedPackage2() throws CoreException {
351 	setClasspath(new String[] {"/P/src", "p1/p2/p3/"});
352 	createFolder("/P/src/p1/p2");
353 
354 	clearDeltas();
355 	createFolder("/P/src/p1/p2/p3");
356 
357 	assertDeltas(
358 		"Unexpected deltas",
359 		"P[*]: {CHILDREN}\n" +
360 		"	src[*]: {CHILDREN | CONTENT}\n" +
361 		"		p1.p2.p3[+]: {}\n" +
362 		"		ResourceDelta(/P/src/p1)[*]"
363 	);
364 
365 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
366 	assertSortedElementsEqual(
367 		"Unexpected children",
368 		"p1.p2.p3 [in src [in P]]",
369 		root.getChildren());
370 
371 	assertResourceNamesEqual(
372 		"Unexpected non-java resources",
373 		"p1",
374 		root.getNonJavaResources());
375 }
376 /*
377  * Ensures that the default package is included if the project is a source folder and one of the
378  * compilation unit of the default package is included.
379  * (regression test for bug 148278 Default-package classes missing in Package Explorer)
380  */
testDefaultPackageProjectIsSource()381 public void testDefaultPackageProjectIsSource() throws CoreException {
382 	setClasspath(new String[] {"/P", "**/*.java"});
383 	deleteFolder("/P/src");
384 	createFile("/P/A.java", "public class A {}");
385 	IPackageFragmentRoot root = getPackageFragmentRoot("/P");
386 	assertElementDescendants(
387 		"Unexpected descendants of root",
388 		"<project root>\n" +
389 		"  <default> (...)\n" +
390 		"    A.java\n" +
391 		"      class A",
392 		root);
393 }
394 /**
395  * Ensure that a type can be resolved if it is included but not its super packages.
396  * (regression test for bug 119161 classes in "deep" packages not fully recognized when using tight inclusion filters)
397  * @deprecated
398  */
testIncludeCUOnly01()399 public void testIncludeCUOnly01() throws CoreException {
400 	setClasspath(new String[] {"/P/src", "p1/p2/*.java|q/*.java"});
401 	addLibraryEntry(getJavaProject("P"), getExternalJCLPathString(), false);
402 	createFolder("/P/src/p1/p2");
403 	createFile(
404 		"/P/src/p1/p2/X.java",
405 		"package p1.p2;\n" +
406 		"public class X {\n" +
407 		"}"
408 	);
409 	ICompilationUnit workingCopy = null;
410 	try {
411 		ProblemRequestor problemRequestor = new ProblemRequestor();
412 		workingCopy = getWorkingCopy(
413 			"/P/src/Y.java",
414 			"import p1.p2.X;\n" +
415 			"public class Y extends X {\n" +
416 			"}",
417 			null/*primary owner*/,
418 			problemRequestor
419 		);
420 		assertProblems(
421 			"Unepected problems",
422 			"----------\n" +
423 			"----------\n",
424 			problemRequestor);
425 	} finally {
426 		if (workingCopy != null)
427 			workingCopy.discardWorkingCopy();
428 	}
429 }
testIncludeCUOnly01_new()430 public void testIncludeCUOnly01_new() throws CoreException {
431 	setClasspath(new String[] {"/P/src", "p1/p2/*.java|q/*.java"});
432 	addLibraryEntry(getJavaProject("P"), getExternalJCLPathString(), false);
433 	createFolder("/P/src/p1/p2");
434 	createFile(
435 		"/P/src/p1/p2/X.java",
436 		"package p1.p2;\n" +
437 		"public class X {\n" +
438 		"}"
439 	);
440 	ICompilationUnit workingCopy = null;
441 	try {
442 		ProblemRequestor problemRequestor = new ProblemRequestor();
443 		workingCopy = getWorkingCopy(
444 			"/P/src/Y.java",
445 			"import p1.p2.X;\n" +
446 			"public class Y extends X {\n" +
447 			"}",
448 			newWorkingCopyOwner(problemRequestor)
449 		);
450 		assertProblems(
451 			"Unepected problems",
452 			"----------\n" +
453 			"----------\n",
454 			problemRequestor);
455 	} finally {
456 		if (workingCopy != null)
457 			workingCopy.discardWorkingCopy();
458 	}
459 }
460 /**
461  * Ensure that a type can be resolved if it is included but not its super packages.
462  * (regression test for bug 119161 classes in "deep" packages not fully recognized when using tight inclusion filters)
463  * @deprecated
464  */
testIncludeCUOnly02()465 public void testIncludeCUOnly02() throws CoreException {
466 	setClasspath(new String[] {"/P/src", "p1/p2/p3/*.java|q/*.java"});
467 	addLibraryEntry(getJavaProject("P"), getExternalJCLPathString(), false);
468 	createFolder("/P/src/p1/p2/p3");
469 	createFile(
470 		"/P/src/p1/p2/p3/X.java",
471 		"package p1.p2.p3;\n" +
472 		"public class X {\n" +
473 		"}"
474 	);
475 	ICompilationUnit workingCopy = null;
476 	try {
477 		ProblemRequestor problemRequestor = new ProblemRequestor();
478 		workingCopy = getWorkingCopy(
479 			"/P/src/Y.java",
480 			"import p1.p2.p3.X;\n" +
481 			"public class Y extends X {\n" +
482 			"}",
483 			null/*primary owner*/,
484 			problemRequestor
485 		);
486 		assertProblems(
487 			"Unepected problems",
488 			"----------\n" +
489 			"----------\n",
490 			problemRequestor);
491 	} finally {
492 		if (workingCopy != null)
493 			workingCopy.discardWorkingCopy();
494 	}
495 }
testIncludeCUOnly02_new()496 public void testIncludeCUOnly02_new() throws CoreException {
497 	setClasspath(new String[] {"/P/src", "p1/p2/p3/*.java|q/*.java"});
498 	addLibraryEntry(getJavaProject("P"), getExternalJCLPathString(), false);
499 	createFolder("/P/src/p1/p2/p3");
500 	createFile(
501 		"/P/src/p1/p2/p3/X.java",
502 		"package p1.p2.p3;\n" +
503 		"public class X {\n" +
504 		"}"
505 	);
506 	ICompilationUnit workingCopy = null;
507 	try {
508 		ProblemRequestor problemRequestor = new ProblemRequestor();
509 		workingCopy = getWorkingCopy(
510 			"/P/src/Y.java",
511 			"import p1.p2.p3.X;\n" +
512 			"public class Y extends X {\n" +
513 			"}",
514 			newWorkingCopyOwner(problemRequestor)
515 		);
516 		assertProblems(
517 			"Unepected problems",
518 			"----------\n" +
519 			"----------\n",
520 			problemRequestor);
521 	} finally {
522 		if (workingCopy != null)
523 			workingCopy.discardWorkingCopy();
524 	}
525 }
526 /*
527  * Ensures that a cu that is not included is not on the classpath of the project.
528  */
testIsOnClasspath1()529 public void testIsOnClasspath1() throws CoreException {
530 	setClasspath(new String[] {"/P/src", "**/B.java"});
531 	createFolder("/P/src/p");
532 	IFile file = createFile(
533 		"/P/src/p/A.java",
534 		"package p;\n" +
535 		"public class A {\n" +
536 		"}"
537 	);
538 	assertTrue("Resource should not be on classpath", !this.project.isOnClasspath(file));
539 
540 	ICompilationUnit cu = getCompilationUnit("/P/src/p/A.java");
541 	assertTrue("CU should not be on classpath", !this.project.isOnClasspath(cu));
542 }
543 /*
544  * Ensures that a cu that is included is on the classpath of the project.
545  */
testIsOnClasspath2()546 public void testIsOnClasspath2() throws CoreException {
547 	setClasspath(new String[] {"/P/src", "**/A.java"});
548 	createFolder("/P/src/p");
549 	IFile file = createFile(
550 		"/P/src/p/A.java",
551 		"package p;\n" +
552 		"public class A {\n" +
553 		"}"
554 	);
555 	assertTrue("Resource should be on classpath", this.project.isOnClasspath(file));
556 
557 	ICompilationUnit cu = getCompilationUnit("/P/src/p/A.java");
558 	assertTrue("CU should be on classpath", this.project.isOnClasspath(cu));
559 }
560 /*
561  * Ensures that a non-java resource that is not included is not on the classpath of the project.
562  */
testIsOnClasspath3()563 public void testIsOnClasspath3() throws CoreException {
564 	setClasspath(new String[] {"/P/src", "**/X.java"});
565 	createFolder("/P/src/p");
566 	IFile file = createFile("/P/src/p/readme.txt", "");
567 	assertTrue("Resource should not be on classpath", !this.project.isOnClasspath(file));
568 }
569 /*
570  * Ensures that a non-java resource that is included is on the classpath of the project.
571  */
testIsOnClasspath4()572 public void testIsOnClasspath4() throws CoreException {
573 	setClasspath(new String[] {"/P/src", "p/**"});
574 	createFolder("/P/src/p");
575 	IFile file = createFile("/P/src/p/readme.txt", "");
576 	assertTrue("Resource should be on classpath", this.project.isOnClasspath(file));
577 }
578 /*
579  * Ensures that moving a folder that contains an included package reports the correct delta.
580  * (regression test for bug 67324 Package Explorer doesn't update included package after moving contents of source folder)
581  */
testMoveFolderContainingPackage1()582 public void testMoveFolderContainingPackage1() throws CoreException {
583 	setClasspath(new String[] {"/P/src", "p1/p2/"});
584 	createFolder("/P/src/p1/p2");
585 
586 	clearDeltas();
587 	getFolder("/P/src/p1").move(new Path("/P/p1"), false, null);
588 	assertDeltas(
589 		"Unexpected deltas",
590 		"P[*]: {CHILDREN | CONTENT}\n" +
591 		"	src[*]: {CHILDREN | CONTENT}\n" +
592 		"		p1.p2[-]: {}\n" +
593 		"		ResourceDelta(/P/src/p1)[-]\n" +
594 		"	ResourceDelta(/P/p1)[+]"
595 	);
596 }
597 /*
598  * Ensures that moving a folder that contains an included package reports the correct delta.
599  * (regression test for bug 67324 Package Explorer doesn't update included package after moving contents of source folder)
600  */
testMoveFolderContainingPackage2()601 public void testMoveFolderContainingPackage2() throws CoreException {
602 	setClasspath(new String[] {"/P/src", "p1/p2/"});
603 	createFolder("/P/p1/p2");
604 
605 	clearDeltas();
606 	getFolder("/P//p1").move(new Path("/P/src/p1"), false, null);
607 	assertDeltas(
608 		"Unexpected deltas",
609 		"P[*]: {CHILDREN | CONTENT}\n" +
610 		"	src[*]: {CHILDREN | CONTENT}\n" +
611 		"		p1.p2[+]: {}\n" +
612 		"		ResourceDelta(/P/src/p1)[+]\n" +
613 		"	ResourceDelta(/P/p1)[-]"
614 	);
615 }
616 /*
617  * Ensures that a non-included nested source folder doesn't appear as a non-java resource of the outer folder.
618  *
619  */
testNestedSourceFolder1()620 public void testNestedSourceFolder1() throws CoreException {
621 	setClasspath(new String[] {"/P/src1", "**/A.java", "/P/src1/src2", ""});
622 	createFolder("/P/src1/src2");
623 	IPackageFragmentRoot root1 = getPackageFragmentRoot("/P/src1");
624 	assertResourceNamesEqual(
625 		"Unexpected non-java resources for /P/src1",
626 		"",
627 		root1.getNonJavaResources());
628 }
629 /*
630  * Ensures that adding a .java file in a nested source folder reports
631  * a delta on the nested source folder and not on the outer one.
632  */
testNestedSourceFolder2()633 public void testNestedSourceFolder2() throws CoreException {
634 	setClasspath(new String[] {"/P/src1", "**/X.java", "/P/src1/src2", ""});
635 	createFolder("/P/src1/src2");
636 
637 	clearDeltas();
638 	createFile(
639 		"/P/src1/src2/A.java",
640 		"public class A {\n" +
641 		"}"
642 	);
643 
644 	assertDeltas(
645 		"Unexpected deltas",
646 		"P[*]: {CHILDREN}\n" +
647 		"	src1/src2[*]: {CHILDREN}\n" +
648 		"		<default>[*]: {CHILDREN}\n" +
649 		"			A.java[+]: {}"
650 	);
651 }
652 /*
653  * Ensures that adding a .txt file in a nested source folder reports
654  * a resource delta on the nested source folder and not on the outer one.
655  */
testNestedSourceFolder3()656 public void testNestedSourceFolder3() throws CoreException {
657 	setClasspath(new String[] {"/P/src1", "**/X.java", "/P/src1/src2", ""});
658 	createFolder("/P/src1/src2");
659 
660 	clearDeltas();
661 	createFile("/P/src1/src2/readme.txt", "");
662 
663 	assertDeltas(
664 		"Unexpected deltas",
665 		"P[*]: {CHILDREN}\n" +
666 		"	src1/src2[*]: {CONTENT}\n" +
667 		"		ResourceDelta(/P/src1/src2/readme.txt)[+]"
668 	);
669 }
670 /*
671  * Ensures that adding a folder in a nested source folder reports
672  * a delta on the nested source folder and not on the outer one.
673  */
testNestedSourceFolder4()674 public void testNestedSourceFolder4() throws CoreException {
675 	setClasspath(new String[] {"/P/src1", "**/X.java", "/P/src1/src2", "**/X.java"});
676 	createFolder("/P/src1/src2");
677 
678 	clearDeltas();
679 	createFolder("/P/src1/src2/p");
680 
681 	assertDeltas(
682 		"Unexpected deltas",
683 		"P[*]: {CHILDREN}\n" +
684 		"	src1/src2[*]: {CHILDREN}\n" +
685 		"		p[+]: {}"
686 	);
687 }
688 /*
689  * Ensures that adding a folder in a outer source folder reports
690  * a delta on the outer source folder and not on the nested one.
691  */
testNestedSourceFolder5()692 public void testNestedSourceFolder5() throws CoreException {
693 	setClasspath(new String[] {"/P/src1", "**/X.java", "/P/src1/src2", ""});
694 	createFolder("/P/src1/src2");
695 
696 	clearDeltas();
697 	createFolder("/P/src1/p");
698 
699 	assertDeltas(
700 		"Unexpected deltas",
701 		"P[*]: {CHILDREN}\n" +
702 		"	src1[*]: {CHILDREN}\n" +
703 		"		p[+]: {}"
704 	);
705 }
706 /*
707  * Ensures that moving a package from an outer source folder to a nested
708  * source folder reports a move delta.
709  */
testNestedSourceFolder6()710 public void testNestedSourceFolder6() throws CoreException {
711 	setClasspath(new String[] {"/P/src1", "**/X.java", "/P/src1/src2", "**/X.java"});
712 	createFolder("/P/src1/src2");
713 	createFolder("/P/src1/p");
714 
715 	clearDeltas();
716 	moveFolder("/P/src1/p", "/P/src1/src2/p");
717 
718 	assertDeltas(
719 		"Unexpected deltas",
720 		"P[*]: {CHILDREN}\n" +
721 		"	src1[*]: {CHILDREN}\n" +
722 		"		p[-]: {MOVED_TO(p [in src1/src2 [in P]])}\n" +
723 		"	src1/src2[*]: {CHILDREN}\n" +
724 		"		p[+]: {MOVED_FROM(p [in src1 [in P]])}"
725 	);
726 }
727 /*
728  * Ensure that removing the inclusion on a compilation unit
729  * makes it disappears from the children of its package and it is added to the non-java resources.
730  */
testRemoveInclusionOnCompilationUnit()731 public void testRemoveInclusionOnCompilationUnit() throws CoreException {
732 	setClasspath(new String[] {"/P/src", "**/A.java"});
733 	createFolder("/P/src/p");
734 	createFile(
735 		"/P/src/p/A.java",
736 		"package p;\n" +
737 		"public class A {\n" +
738 		"}"
739 	);
740 
741 	clearDeltas();
742 	setClasspath(new String[] {"/P/src", "**/B.java"});
743 
744 	assertDeltas(
745 		"Unexpected deltas",
746 		"P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
747 		"	src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
748 		"	ResourceDelta(/P/.classpath)[*]"
749 	);
750 
751 	IPackageFragment pkg = getPackage("/P/src/p");
752 	assertSortedElementsEqual(
753 		"Unexpected children",
754 		"",
755 		pkg.getChildren());
756 
757 	assertResourceNamesEqual(
758 		"Unexpected non-java resources",
759 		"A.java",
760 		pkg.getNonJavaResources());
761 }
762 /*
763  * Ensure that removing the inclusion on a package
764  * makes it disappears from the children of its package fragment root
765  * and it is added to the non-java resources.
766  */
testRemoveInclusionOnPackage()767 public void testRemoveInclusionOnPackage() throws CoreException {
768 	setClasspath(new String[] {"/P/src", "p"});
769 	createFolder("/P/src/p");
770 
771 	clearDeltas();
772 	setClasspath(new String[] {"/P/src", "q"});
773 
774 	assertDeltas(
775 		"Unexpected deltas",
776 		"P[*]: {CHILDREN | CONTENT | RAW CLASSPATH CHANGED | RESOLVED CLASSPATH CHANGED}\n" +
777 		"	src[*]: {ADDED TO CLASSPATH | REMOVED FROM CLASSPATH}\n" +
778 		"	ResourceDelta(/P/.classpath)[*]"
779 	);
780 
781 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
782 	assertSortedElementsEqual(
783 		"Unexpected children",
784 		"<default> [in src [in P]]",
785 		root.getChildren());
786 
787 	assertResourceNamesEqual(
788 		"Unexpected non-java resources",
789 		"p",
790 		root.getNonJavaResources());
791 }
792 /*
793  * Ensure that renaming an included compilation unit so that it is not included any longer
794  * makes it disappears from the children of its package and it is added to the non-java resources.
795  */
testRenameIncludedCompilationUnit()796 public void testRenameIncludedCompilationUnit() throws CoreException {
797 	setClasspath(new String[] {"/P/src", "**/A.java"});
798 	createFolder("/P/src/p");
799 	IFile file = createFile(
800 		"/P/src/p/A.java",
801 		"package p;\n" +
802 		"public class A {\n" +
803 		"}"
804 	);
805 
806 	clearDeltas();
807 	file.move(new Path("/P/src/p/B.java"), false, null);
808 
809 	assertDeltas(
810 		"Unexpected deltas",
811 		"P[*]: {CHILDREN}\n" +
812 		"	src[*]: {CHILDREN}\n" +
813 		"		p[*]: {CHILDREN | CONTENT}\n" +
814 		"			A.java[-]: {}\n" +
815 		"			ResourceDelta(/P/src/p/B.java)[+]"
816 	);
817 
818 	IPackageFragment pkg = getPackage("/P/src/p");
819 	assertSortedElementsEqual(
820 		"Unexpected children",
821 		"",
822 		pkg.getChildren());
823 
824 	assertResourceNamesEqual(
825 		"Unexpected non-java resources",
826 		"B.java",
827 		pkg.getNonJavaResources());
828 }
829 /*
830  * Ensure that renaming an included package so that it is not included any longer
831  * makes it disappears from the children of its package fragment root
832  * and it is added to the non-java resources.
833  */
testRenameIncludedPackage1()834 public void testRenameIncludedPackage1() throws CoreException {
835 	setClasspath(new String[] {"/P/src", "p/"});
836 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
837 	IPackageFragment pkg = root.createPackageFragment("p", false, null);
838 
839 	clearDeltas();
840 	pkg.rename("q", false, null);
841 
842 	assertDeltas(
843 		"Unexpected deltas",
844 		"P[*]: {CHILDREN}\n" +
845 		"	src[*]: {CHILDREN | CONTENT}\n" +
846 		"		p[-]: {}\n" +
847 		"		ResourceDelta(/P/src/q)[+]"
848 	);
849 
850 	assertSortedElementsEqual(
851 		"Unexpected children",
852 		"",
853 		root.getChildren());
854 
855 	assertResourceNamesEqual(
856 		"Unexpected non-java resources",
857 		"q",
858 		root.getNonJavaResources());
859 }
860 /*
861  * Ensure that renaming an included package that has compilation units
862  * so that it is not included any longer doesn't throw a JavaModelException.
863  * (regression test for bug 67297 Renaming included package folder throws JME)
864  */
testRenameIncludedPackage2()865 public void testRenameIncludedPackage2() throws CoreException {
866 	setClasspath(new String[] {"/P/src", "p/"});
867 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
868 	IPackageFragment pkg = root.createPackageFragment("p", false, null);
869 	createFile(
870 		"/P/src/p/X.java",
871 		"package p;\n" +
872 		"public class X {\n" +
873 		"}"
874 	);
875 
876 	clearDeltas();
877 	pkg.rename("q", false, null);
878 
879 	assertDeltas(
880 		"Unexpected deltas",
881 		"P[*]: {CHILDREN}\n" +
882 		"	src[*]: {CHILDREN | CONTENT}\n" +
883 		"		p[-]: {}\n" +
884 		"		ResourceDelta(/P/src/q)[+]"
885 	);
886 
887 	assertSortedElementsEqual(
888 		"Unexpected children",
889 		"",
890 		root.getChildren());
891 
892 	assertResourceNamesEqual(
893 		"Unexpected non-java resources",
894 		"q",
895 		root.getNonJavaResources());
896 }
897 /*
898  * Ensure that renaming a file that corresponds to an included compilation unit so that it is not included any longer
899  * makes it disappears from the children of its package and it is added to the non-java resources.
900  */
testRenameResourceIncludedCompilationUnit()901 public void testRenameResourceIncludedCompilationUnit() throws CoreException {
902 	setClasspath(new String[] {"/P/src", "**/A.java"});
903 	createFolder("/P/src/p");
904 	IFile file = createFile(
905 		"/P/src/p/A.java",
906 		"package p;\n" +
907 		"public class A {\n" +
908 		"}"
909 	);
910 
911 	clearDeltas();
912 	file.move(new Path("/P/src/p/B.java"),  false, null);
913 
914 	assertDeltas(
915 		"Unexpected deltas",
916 		"P[*]: {CHILDREN}\n" +
917 		"	src[*]: {CHILDREN}\n" +
918 		"		p[*]: {CHILDREN | CONTENT}\n" +
919 		"			A.java[-]: {}\n" +
920 		"			ResourceDelta(/P/src/p/B.java)[+]"
921 	);
922 
923 	IPackageFragment pkg = getPackage("/P/src/p");
924 	assertSortedElementsEqual(
925 		"Unexpected children",
926 		"",
927 		pkg.getChildren());
928 
929 	assertResourceNamesEqual(
930 		"Unexpected non-java resources",
931 		"B.java",
932 		pkg.getNonJavaResources());
933 }
934 /*
935  * Ensure that renaming a folder that corresponds to an included package
936  * so that it is not included any longer
937  * makes it disappears afrom the children of its package fragment root
938  * and it is added to the non-java resources.
939  */
testRenameResourceIncludedPackage()940 public void testRenameResourceIncludedPackage() throws CoreException {
941 	setClasspath(new String[] {"/P/src", "p/"});
942 	IFolder folder = createFolder("/P/src/p");
943 
944 	clearDeltas();
945 	folder.move(new Path("/P/src/q"), false, null);
946 
947 	assertDeltas(
948 		"Unexpected deltas",
949 		"P[*]: {CHILDREN}\n" +
950 		"	src[*]: {CHILDREN | CONTENT}\n" +
951 		"		p[-]: {}\n" +
952 		"		ResourceDelta(/P/src/q)[+]"
953 	);
954 
955 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
956 	assertSortedElementsEqual(
957 		"Unexpected children",
958 		"",
959 		root.getChildren());
960 
961 	assertResourceNamesEqual(
962 		"Unexpected non-java resources",
963 		"q",
964 		root.getNonJavaResources());
965 }
966 /*
967  * Ensure that a potential match in the output folder is not indexed.
968  * (regression test for bug 32041 Multiple output folders fooling Java Model)
969  */
testSearchPotentialMatchInOutput()970 public void testSearchPotentialMatchInOutput() throws CoreException {
971 	try {
972 		JavaCore.run(new IWorkspaceRunnable() {
973 			public void run(IProgressMonitor monitor) throws CoreException {
974 				IJavaProject javaProject = createJavaProject("P2", new String[] {}, "bin");
975 				javaProject.setRawClasspath(createClasspath(new String[] {"/P2", "**/X.java", "/P2/src", ""}, true/*inclusion*/, false/*no exclusion*/), null);
976 				createFile(
977 					"/P2/bin/X.java",
978 					"public class X {\n" +
979 					"}"
980 				);
981 			}
982 		}, null);
983 
984 		JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
985 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {getJavaProject("P")});
986 		search(
987 			"X",
988 			IJavaSearchConstants.TYPE,
989 			IJavaSearchConstants.DECLARATIONS,
990 			scope,
991 			resultCollector);
992 		assertEquals("", resultCollector.toString());
993 	} finally {
994 		deleteProject("P2");
995 	}
996 }
997 /*
998  * Ensure search finds matches in an included compilation unit.
999  */
testSearchWithIncludedCompilationUnit1()1000 public void testSearchWithIncludedCompilationUnit1() throws CoreException {
1001 	setClasspath(new String[] {"/P/src", "**/A.java"});
1002 	createFolder("/P/src/p");
1003 	createFile(
1004 		"/P/src/p/A.java",
1005 		"package p;\n" +
1006 		"public class A {\n" +
1007 		"}"
1008 	);
1009 
1010 	JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
1011 	search(
1012 		"A",
1013 		IJavaSearchConstants.TYPE,
1014 		IJavaSearchConstants.DECLARATIONS,
1015 		SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
1016 		resultCollector);
1017 	assertEquals(
1018 		"Unexpected matches found",
1019 		"src/p/A.java p.A [A]",
1020 		resultCollector.toString());
1021 }
1022 /*
1023  * Ensure search doesn't find matches in a compilation unit that was included but that is not any longer.
1024  */
testSearchWithIncludedCompilationUnit2()1025 public void testSearchWithIncludedCompilationUnit2() throws CoreException {
1026 	setClasspath(new String[] {"/P/src", "**/A.java"});
1027 	createFolder("/P/src/p");
1028 	createFile(
1029 		"/P/src/p/A.java",
1030 		"package p;\n" +
1031 		"public class A {\n" +
1032 		"}"
1033 	);
1034 
1035 	setClasspath(new String[] {"/P/src", "**/B.java"});
1036 	JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
1037 	search(
1038 		"A",
1039 		IJavaSearchConstants.TYPE,
1040 		IJavaSearchConstants.DECLARATIONS,
1041 		SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
1042 		resultCollector);
1043 	assertEquals(
1044 		"Unexpected matches found",
1045 		"",
1046 		resultCollector.toString());
1047 }
1048 /*
1049  * Ensure search finds matches in an included package.
1050  * (case of setting the classpath)
1051  */
testSearchWithIncludedPackage1()1052 public void testSearchWithIncludedPackage1() throws CoreException {
1053 	createFolder("/P/src/p");
1054 	createFile(
1055 		"/P/src/p/A.java",
1056 		"package p;\n" +
1057 		"public class A {\n" +
1058 		"}"
1059 	);
1060 	setClasspath(new String[] {"/P/src", "p/"});
1061 
1062 	JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
1063 	search(
1064 		"A",
1065 		IJavaSearchConstants.TYPE,
1066 		IJavaSearchConstants.DECLARATIONS,
1067 		SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
1068 		resultCollector);
1069 	assertEquals(
1070 		"Unexpected matches found",
1071 		"src/p/A.java p.A [A]",
1072 		resultCollector.toString());
1073 }
1074 /*
1075  * Ensure search finds matches in an included package.
1076  * (case of opening the project)
1077  */
testSearchWithIncludedPackage2()1078 public void testSearchWithIncludedPackage2() throws CoreException {
1079 	setClasspath(new String[] {"/P/src", "p/"});
1080 	createFolder("/P/src/p");
1081 	createFile(
1082 		"/P/src/p/A.java",
1083 		"package p;\n" +
1084 		"public class A {\n" +
1085 		"}"
1086 	);
1087 	IProject p = this.project.getProject();
1088 	p.close(null);
1089 	p.open(null);
1090 
1091 	JavaSearchTests.JavaSearchResultCollector resultCollector = new JavaSearchTests.JavaSearchResultCollector();
1092 	search(
1093 		"A",
1094 		IJavaSearchConstants.TYPE,
1095 		IJavaSearchConstants.DECLARATIONS,
1096 		SearchEngine.createJavaSearchScope(new IJavaProject[] {getJavaProject("P")}),
1097 		resultCollector);
1098 	assertEquals(
1099 		"Unexpected matches found",
1100 		"src/p/A.java p.A [A]",
1101 		resultCollector.toString());
1102 }
1103 /*
1104  * Ensure that an included pattern of the form "com/" includes all the subtree
1105  * (regression test for bug 62608 Include pattern ending with slash should include all subtree)
1106  */
testTrailingSlash()1107 public void testTrailingSlash() throws CoreException {
1108 	setClasspath(new String[] {"/P/src", "a/"});
1109 	createFolder("/P/src/a/b/c");
1110 	IPackageFragmentRoot root = getPackageFragmentRoot("/P/src");
1111 	assertSortedElementsEqual(
1112 		"Unexpected children of root",
1113 		"a [in src [in P]]\n" +
1114 		"a.b [in src [in P]]\n" +
1115 		"a.b.c [in src [in P]]",
1116 		root.getChildren());
1117 }
1118 }
1119