1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 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.net.MalformedURLException;
17 import java.net.URL;
18 import java.util.HashMap;
19 
20 import junit.framework.Test;
21 
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.IncrementalProjectBuilder;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.jdt.core.*;
29 import org.eclipse.jdt.core.search.*;
30 import org.eclipse.jdt.core.tests.model.AbstractJavaSearchTests.JavaSearchResultCollector;
31 import org.eclipse.jdt.core.tests.model.AbstractJavaSearchTests.TypeNameMatchCollector;
32 import org.eclipse.jdt.internal.core.JavaModelManager;
33 import org.eclipse.jdt.internal.core.index.IndexLocation;
34 import org.eclipse.jdt.internal.core.search.indexing.IndexManager;
35 
36 /**
37  * Tests the Java search engine accross multiple projects.
38  */
39 public class JavaSearchScopeTests extends ModifyingResourceTests implements IJavaSearchConstants {
JavaSearchScopeTests(String name)40 public JavaSearchScopeTests(String name) {
41 	super(name);
42 }
suite()43 public static Test suite() {
44 	return buildModelTestSuite(JavaSearchScopeTests.class);
45 }
46 // Use this static initializer to specify subset for tests
47 // All specified tests which do not belong to the class are skipped...
48 static {
49 //	TESTS_NAMES = new String[] { "testMethodOccurences" };
50 //  TESTS_NUMBERS = new int[] { 101426 };
51 //	TESTS_RANGE = new int[] { 16, -1 };
52 }
53 
54 @Override
tearDown()55 protected void tearDown() throws Exception {
56 	// Cleanup caches
57 	JavaModelManager manager = JavaModelManager.getJavaModelManager();
58 	manager.containers = new HashMap<>(5);
59 	manager.variables = new HashMap<>(5);
60 
61 	super.tearDown();
62 }
63 /*
64  * Ensures that a Java search scope with SOURCES only is correct.
65  */
testSources()66 public void testSources() throws CoreException {
67 	try {
68 		IJavaProject project = createJavaProject("P");
69 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.SOURCES);
70 		assertScopeEquals(
71 			"JavaSearchScope on [\n" +
72 			"	/P\n" +
73 			"]",
74 			scope);
75 	} finally {
76 		deleteProject("P");
77 	}
78 }
79 /*
80  * Ensures that a Java search scope with APPLICATION_LIBRARIES only is correct
81  * (external jar case)
82  */
testApplicationLibrairiesExternalJar()83 public void testApplicationLibrairiesExternalJar() throws CoreException {
84 	try {
85 		IJavaProject project = createJavaProject("P");
86 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
87 		assertScopeEquals(
88 			"JavaSearchScope on [\n" +
89 			"	"+  getExternalJCLPath().toOSString() +"\n" +
90 			"]",
91 			scope);
92 	} finally {
93 		deleteProject("P");
94 	}
95 }
96 /*
97  * Ensures that a Java search scope with APPLICATION_LIBRARIES only is correct
98  * (internal jar and class folder cases)
99  */
testApplicationLibrairiesJarAndClassFolder()100 public void testApplicationLibrairiesJarAndClassFolder() throws CoreException {
101 	try {
102 		IJavaProject project = createJavaProject("P", new String[] {"src"}, new String[] {"/P/internal.jar", "/P/classfolder"}, "bin");
103 		createFile("/P/internal.jar", new byte[0]);
104 		createFolder("/P/classfolder");
105 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
106 		assertScopeEquals(
107 			"JavaSearchScope on [\n" +
108 			"	/P/classfolder\n" +
109 			"	/P/internal.jar\n" +
110 			"]",
111 			scope);
112 	} finally {
113 		deleteProject("P");
114 	}
115 }
testApplicationLibrairiesNonExistingJarAndClassFolder()116 public void testApplicationLibrairiesNonExistingJarAndClassFolder() throws CoreException {
117 	try {
118 		IJavaProject project = createJavaProject("P", new String[] {"src"}, new String[] {"/P/internal.jar", "/P/classfolder"}, "bin");
119 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
120 		assertScopeEquals(
121 			"JavaSearchScope on [\n" +
122 			"	/P/classfolder\n" +
123 			"	/P/internal.jar\n" +
124 			"]",
125 			scope);
126 	} finally {
127 		deleteProject("P");
128 	}
129 }
130 /*
131  * Ensures that a Java search scope with APPLICATION_LIBRARIES only is correct
132  * (classpath variable case)
133  */
testApplicationLibrairiesClasspathVariable()134 public void testApplicationLibrairiesClasspathVariable() throws CoreException {
135 	try {
136 		VariablesInitializer.setInitializer(new ClasspathInitializerTests.DefaultVariableInitializer(new String[] {"TEST_LIB", "/P/lib.jar"}));
137 		IJavaProject project = createJavaProject("P", new String[] {}, new String[] {"TEST_LIB"}, "");
138 		createFile("/P/lib.jar", new byte[0]);
139 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
140 		assertScopeEquals(
141 			"JavaSearchScope on [\n" +
142 			"	/P/lib.jar\n" +
143 			"]",
144 			scope);
145 	} finally {
146 		deleteProject("P");
147 		VariablesInitializer.reset();
148 	}
149 }
testApplicationLibrairiesNonExistingClasspathVariable()150 public void testApplicationLibrairiesNonExistingClasspathVariable() throws CoreException {
151 	try {
152 		VariablesInitializer.setInitializer(new ClasspathInitializerTests.DefaultVariableInitializer(new String[] {"TEST_LIB", "/P/lib.jar"}));
153 		IJavaProject project = createJavaProject("P", new String[] {}, new String[] {"TEST_LIB"}, "");
154 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
155 		assertScopeEquals(
156 			"JavaSearchScope on [\n" +
157 			"	/P/lib.jar\n" +
158 			"]",
159 			scope);
160 	} finally {
161 		deleteProject("P");
162 		VariablesInitializer.reset();
163 	}
164 }
165 /*
166  * Ensures that a Java search scope with APPLICATION_LIBRARIES only is correct
167  * (classpath container case)
168  */
testApplicationLibrairiesClasspathContainer()169 public void testApplicationLibrairiesClasspathContainer() throws CoreException {
170 	try {
171 		ContainerInitializer.setInitializer(new DefaultContainerInitializer(new String[] {"P", "/P/lib.jar"}));
172 		IJavaProject project = createJavaProject("P", new String[] {}, new String[] {"org.eclipse.jdt.core.tests.model.TEST_CONTAINER"}, "");
173 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.APPLICATION_LIBRARIES);
174 		assertScopeEquals(
175 			"JavaSearchScope on [\n" +
176 			"	/P/lib.jar\n" +
177 			"]",
178 			scope);
179 	} finally {
180 		deleteProject("P");
181 	}
182 }
183 /*
184  * Ensures that a Java search scope with SYSTEM_LIBRARIES only is correct
185  * (classpath container case)
186  */
testSystemLibraries()187 public void testSystemLibraries() throws CoreException {
188 	try {
189 		DefaultContainerInitializer intializer = new DefaultContainerInitializer(new String[] {"P", "/P/lib.jar"}) {
190 			@Override
191 			protected DefaultContainer newContainer(char[][] libPaths) {
192 				return new DefaultContainer(libPaths) {
193 					@Override
194 					public int getKind() {
195 						return IClasspathContainer.K_SYSTEM;
196 					}
197 				};
198 			}
199 		};
200 		ContainerInitializer.setInitializer(intializer);
201 		IJavaProject project = createJavaProject("P", new String[] {}, new String[] {"org.eclipse.jdt.core.tests.model.TEST_CONTAINER"}, "");
202 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.SYSTEM_LIBRARIES);
203 		assertScopeEquals(
204 			"JavaSearchScope on [\n" +
205 			"	/P/lib.jar\n" +
206 			"]",
207 			scope);
208 	} finally {
209 		deleteProject("P");
210 	}
211 }
212 /*
213  * Ensures that a Java search scope with SOURCES | REFERENCED_PROJECTS is correct
214  * (direct reference case)
215  */
testSourcesOrDirectReferencedProjects()216 public void testSourcesOrDirectReferencedProjects() throws CoreException {
217 	try {
218 		createJavaProject("P1");
219 		IJavaProject project = createJavaProject("P2", new String[] {"src"}, new String[] {}, new String[] {"/P1"}, "bin");
220 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS);
221 		assertScopeEquals(
222 			"JavaSearchScope on [\n" +
223 			"	/P1\n" +
224 			"	/P2/src\n" +
225 			"]",
226 			scope);
227 	} finally {
228 		deleteProject("P1");
229 		deleteProject("P2");
230 	}
231 }
232 /*
233  * Ensures that a Java search scope with SOURCES | REFERENCED_PROJECTS is correct
234  * (reference through a container case)
235  */
testSourcesOrContainerReferencedProjects()236 public void testSourcesOrContainerReferencedProjects() throws CoreException {
237 	try {
238 		createJavaProject("P1");
239 		ContainerInitializer.setInitializer(new DefaultContainerInitializer(new String[] {"P2", "/P1"}));
240 		IJavaProject project = createJavaProject("P2", new String[] {"src"}, new String[] {"org.eclipse.jdt.core.tests.model.TEST_CONTAINER"}, "bin");
241 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project}, IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS);
242 		assertScopeEquals(
243 			"JavaSearchScope on [\n" +
244 			"	/P1\n" +
245 			"	/P2/src\n" +
246 			"]",
247 			scope);
248 	} finally {
249 		deleteProject("P1");
250 		deleteProject("P2");
251 	}
252 }
253 /*
254  * Ensures that a Java project is enclosed in a scope on the project (proj=src)
255  * (resourcePath case)
256  */
testScopeEncloses01()257 public void testScopeEncloses01() throws CoreException {
258 	try {
259 		IJavaProject project = createJavaProject("P");
260 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
261 		assertTrue("scope on P should enclose \"/P\"", scope.encloses("/P"));
262 	} finally {
263 		deleteProject("P");
264 	}
265 }
266 /*
267  * Ensures that a Java project is enclosed in a scope on the project (proj=src)
268  * (Java element case)
269  */
testScopeEncloses02()270 public void testScopeEncloses02() throws CoreException {
271 	try {
272 		IJavaProject project = createJavaProject("P");
273 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
274 		assertTrue("scope on P should enclose P", scope.encloses(project));
275 	} finally {
276 		deleteProject("P");
277 	}
278 }
279 /*
280  * Ensures that a root is enclosed in a scope on the project (proj=src)
281  * (resource path with traling slash case)
282  */
testScopeEncloses03()283 public void testScopeEncloses03() throws CoreException {
284 	try {
285 		IJavaProject project = createJavaProject("P");
286 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
287 		assertTrue("scope on P should enclose \"/P/\"", scope.encloses("/P/"));
288 	} finally {
289 		deleteProject("P");
290 	}
291 }
292 /*
293  * Ensures that a root is enclosed in a scope on the project (proj=src)
294  * (Java element case)
295  */
testScopeEncloses04()296 public void testScopeEncloses04() throws CoreException {
297 	try {
298 		IJavaProject project = createJavaProject("P");
299 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
300 		IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getProject());
301 		assertTrue("scope on P should enclose root P", scope.encloses(root));
302 	} finally {
303 		deleteProject("P");
304 	}
305 }
306 /*
307  * Ensures that a package is enclosed in a scope on the project (proj=src)
308  * (resource path case)
309  */
testScopeEncloses05()310 public void testScopeEncloses05() throws CoreException {
311 	try {
312 		IJavaProject project = createJavaProject("P");
313 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
314 		assertTrue("scope on P should enclose \"/P/x/y\"", scope.encloses("/P/x/y"));
315 	} finally {
316 		deleteProject("P");
317 	}
318 }
319 /*
320  * Ensures that a package is enclosed in a scope on the project (proj=src)
321  * (resource path with trailing slash case)
322  */
testScopeEncloses06()323 public void testScopeEncloses06() throws CoreException {
324 	try {
325 		IJavaProject project = createJavaProject("P");
326 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
327 		assertTrue("scope on P should enclose \"/P/x/y/\"", scope.encloses("/P/x/y/"));
328 	} finally {
329 		deleteProject("P");
330 	}
331 }
332 /*
333  * Ensures that a package is enclosed in a scope on the project (proj=src)
334  * (Java element case)
335  */
testScopeEncloses07()336 public void testScopeEncloses07() throws CoreException {
337 	try {
338 		IJavaProject project = createJavaProject("P");
339 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
340 		IPackageFragment pkg = getPackage("/P/x/y");
341 		assertTrue("scope on P should enclose package x.y", scope.encloses(pkg));
342 	} finally {
343 		deleteProject("P");
344 	}
345 }
346 /*
347  * Ensures that a default package is enclosed in a scope on the project (proj=src)
348  * (Java element case)
349  */
testScopeEncloses08()350 public void testScopeEncloses08() throws CoreException {
351 	try {
352 		IJavaProject project = createJavaProject("P");
353 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
354 		IPackageFragment pkg = getPackage("/P");
355 		assertTrue("scope on P should enclose default package", scope.encloses(pkg));
356 	} finally {
357 		deleteProject("P");
358 	}
359 }
360 /*
361  * Ensures that a compilation unit is enclosed in a scope on the project (proj=src)
362  * (resource path case)
363  */
testScopeEncloses09()364 public void testScopeEncloses09() throws CoreException {
365 	try {
366 		IJavaProject project = createJavaProject("P");
367 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
368 		assertTrue("scope on P should enclose \"/P/x/y/A.java\"", scope.encloses("/P/x/y/A.java"));
369 	} finally {
370 		deleteProject("P");
371 	}
372 }
373 /*
374  * Ensures that a compilation unit is enclosed in a scope on the project (proj=src)
375  * (Java element case)
376  */
testScopeEncloses10()377 public void testScopeEncloses10() throws CoreException {
378 	try {
379 		IJavaProject project = createJavaProject("P");
380 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
381 		ICompilationUnit cu = getCompilationUnit("/P/x/y/A.java");
382 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
383 	} finally {
384 		deleteProject("P");
385 	}
386 }
387 /*
388  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj=src)
389  * (resource path case)
390  */
testScopeEncloses11()391 public void testScopeEncloses11() throws CoreException {
392 	try {
393 		IJavaProject project = createJavaProject("P");
394 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
395 		assertTrue("scope on P should enclose \"/P/A.java\"", scope.encloses("/P/A.java"));
396 	} finally {
397 		deleteProject("P");
398 	}
399 }
400 /*
401  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj=src)
402  * (Java element case)
403  */
testScopeEncloses12()404 public void testScopeEncloses12() throws CoreException {
405 	try {
406 		IJavaProject project = createJavaProject("P");
407 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
408 		ICompilationUnit cu = getCompilationUnit("/P/A.java");
409 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
410 	} finally {
411 		deleteProject("P");
412 	}
413 }
414 /*
415  * Ensures that a source type is enclosed in a scope on the project (proj=src)
416  * (Java element case)
417  */
testScopeEncloses13()418 public void testScopeEncloses13() throws CoreException {
419 	try {
420 		IJavaProject project = createJavaProject("P");
421 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
422 		ICompilationUnit cu = getCompilationUnit("/P/x/y/A.java");
423 		IType type = cu.getType("A");
424 		assertTrue("scope on P should enclose type A", scope.encloses(type));
425 	} finally {
426 		deleteProject("P");
427 	}
428 }
429 /*
430  * Ensures that a Java project is not enclosed in a scope on the project (proj != src)
431  * (resourcePath case)
432  */
testScopeEncloses14()433 public void testScopeEncloses14() throws CoreException {
434 	try {
435 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
436 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
437 		assertFalse("scope on P should not enclose \"/P\"", scope.encloses("/P"));
438 	} finally {
439 		deleteProject("P");
440 	}
441 }
442 /*
443  * Ensures that a Java project is not enclosed in a scope on the project (proj != src)
444  * (resourcePath case)
445  */
testScopeEncloses14b()446 public void testScopeEncloses14b() throws CoreException {
447 	try {
448 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
449 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
450 		assertFalse("scope on P should not enclose \"/\"", scope.encloses("/"));
451 	} finally {
452 		deleteProject("P");
453 	}
454 }
455 /*
456  * Ensures that a Java project is not enclosed in a scope on the project (proj != src)
457  * (resourcePath case)
458  */
testScopeEncloses14c()459 public void testScopeEncloses14c() throws CoreException {
460 	try {
461 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
462 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
463 		assertFalse("scope on P should not enclose \"\"", scope.encloses(""));
464 	} finally {
465 		deleteProject("P");
466 	}
467 }
468 /*
469  * Ensures that a Java project is not enclosed in a scope on the project (proj != src)
470  * (Java element case)
471  */
testScopeEncloses15()472 public void testScopeEncloses15() throws CoreException {
473 	try {
474 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
475 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
476 		assertFalse("scope on P should enclose P", scope.encloses(project));
477 	} finally {
478 		deleteProject("P");
479 	}
480 }
481 /*
482  * Ensures that a root is enclosed in a scope on the project (proj != src)
483  * (resource path case)
484  */
testScopeEncloses16()485 public void testScopeEncloses16() throws CoreException {
486 	try {
487 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
488 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
489 		assertTrue("scope on P should enclose \"/P/src\"", scope.encloses("/P/src"));
490 	} finally {
491 		deleteProject("P");
492 	}
493 }
494 /*
495  * Ensures that a root is enclosed in a scope on the project (proj != src)
496  * (resource path with traling slash case)
497  */
testScopeEncloses17()498 public void testScopeEncloses17() throws CoreException {
499 	try {
500 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
501 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
502 		assertTrue("scope on P should enclose \"/P/src/\"", scope.encloses("/P/src/"));
503 	} finally {
504 		deleteProject("P");
505 	}
506 }
507 /*
508  * Ensures that a root is enclosed in a scope on the project (proj != src)
509  * (Java element case)
510  */
testScopeEncloses18()511 public void testScopeEncloses18() throws CoreException {
512 	try {
513 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
514 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
515 		IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getProject().getFolder("src"));
516 		assertTrue("scope on P should enclose root P/src", scope.encloses(root));
517 	} finally {
518 		deleteProject("P");
519 	}
520 }
521 /*
522  * Ensures that a package is enclosed in a scope on the project (proj != src)
523  * (resource path case)
524  */
testScopeEncloses19()525 public void testScopeEncloses19() throws CoreException {
526 	try {
527 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
528 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
529 		assertTrue("scope on P should enclose \"/P/src/x/y\"", scope.encloses("/P/src/x/y"));
530 	} finally {
531 		deleteProject("P");
532 	}
533 }
534 /*
535  * Ensures that a package is enclosed in a scope on the project (proj != src)
536  * (resource path with trailing slash case)
537  */
testScopeEncloses20()538 public void testScopeEncloses20() throws CoreException {
539 	try {
540 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
541 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
542 		assertTrue("scope on P should enclose \"/P/src/x/y/\"", scope.encloses("/P/src/x/y/"));
543 	} finally {
544 		deleteProject("P");
545 	}
546 }
547 /*
548  * Ensures that a package is enclosed in a scope on the project (proj != src)
549  * (Java element case)
550  */
testScopeEncloses21()551 public void testScopeEncloses21() throws CoreException {
552 	try {
553 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
554 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
555 		IPackageFragment pkg = getPackage("/P/src/x/y");
556 		assertTrue("scope on P should enclose package x.y", scope.encloses(pkg));
557 	} finally {
558 		deleteProject("P");
559 	}
560 }
561 /*
562  * Ensures that a default package is enclosed in a scope on the project (proj != src)
563  * (Java element case)
564  */
testScopeEncloses22()565 public void testScopeEncloses22() throws CoreException {
566 	try {
567 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
568 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
569 		IPackageFragment pkg = getPackage("/P/src");
570 		assertTrue("scope on P should enclose default package", scope.encloses(pkg));
571 	} finally {
572 		deleteProject("P");
573 	}
574 }
575 /*
576  * Ensures that a compilation unit is enclosed in a scope on the project (proj != src)
577  * (resource path case)
578  */
testScopeEncloses23()579 public void testScopeEncloses23() throws CoreException {
580 	try {
581 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
582 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
583 		assertTrue("scope on P should enclose \"/P/src/x/y/A.java\"", scope.encloses("/P/src/x/y/A.java"));
584 	} finally {
585 		deleteProject("P");
586 	}
587 }
588 /*
589  * Ensures that a compilation unit is enclosed in a scope on the project (proj != src)
590  * (Java element case)
591  */
testScopeEncloses24()592 public void testScopeEncloses24() throws CoreException {
593 	try {
594 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
595 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
596 		ICompilationUnit cu = getCompilationUnit("/P/src/x/y/A.java");
597 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
598 	} finally {
599 		deleteProject("P");
600 	}
601 }
602 /*
603  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj != src)
604  * (resource path case)
605  */
testScopeEncloses25()606 public void testScopeEncloses25() throws CoreException {
607 	try {
608 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
609 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
610 		assertTrue("scope on P should enclose \"/P/src/A.java\"", scope.encloses("/P/src/A.java"));
611 	} finally {
612 		deleteProject("P");
613 	}
614 }
615 /*
616  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj != src)
617  * (Java element case)
618  */
testScopeEncloses26()619 public void testScopeEncloses26() throws CoreException {
620 	try {
621 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
622 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
623 		ICompilationUnit cu = getCompilationUnit("/P/src/A.java");
624 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
625 	} finally {
626 		deleteProject("P");
627 	}
628 }
629 /*
630  * Ensures that a source type is enclosed in a scope on the project (proj != src)
631  * (Java element case)
632  */
testScopeEncloses27()633 public void testScopeEncloses27() throws CoreException {
634 	try {
635 		IJavaProject project = createJavaProject("P", new String[] {"src"}, "bin");
636 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
637 		ICompilationUnit cu = getCompilationUnit("/P/src/x/y/A.java");
638 		IType type = cu.getType("A");
639 		assertTrue("scope on P should enclose type A", scope.encloses(type));
640 	} finally {
641 		deleteProject("P");
642 	}
643 }
644 /*
645  * Ensures that a Java project is not enclosed in a scope on the project (proj != src/)
646  * (resourcePath case)
647  */
testScopeEncloses28()648 public void testScopeEncloses28() throws CoreException {
649 	try {
650 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
651 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
652 		assertFalse("scope on P should not enclose \"/P\"", scope.encloses("/P"));
653 	} finally {
654 		deleteProject("P");
655 	}
656 }
657 /*
658  * Ensures that a Java project is not enclosed in a scope on the project (proj != src/)
659  * (resourcePath case)
660  */
testScopeEncloses28b()661 public void testScopeEncloses28b() throws CoreException {
662 	try {
663 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
664 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
665 		assertFalse("scope on P should not enclose \"/P\"", scope.encloses("/"));
666 	} finally {
667 		deleteProject("P");
668 	}
669 }
670 /*
671  * Ensures that a Java project is not enclosed in a scope on the project (proj != src/)
672  * (resourcePath case)
673  */
testScopeEncloses28c()674 public void testScopeEncloses28c() throws CoreException {
675 	try {
676 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
677 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
678 		assertFalse("scope on P should not enclose \"/P\"", scope.encloses(""));
679 	} finally {
680 		deleteProject("P");
681 	}
682 }
683 /*
684  * Ensures that a Java project is not enclosed in a scope on the project (proj != src/)
685  * (Java element case)
686  */
testScopeEncloses29()687 public void testScopeEncloses29() throws CoreException {
688 	try {
689 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
690 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
691 		assertFalse("scope on P should enclose P", scope.encloses(project));
692 	} finally {
693 		deleteProject("P");
694 	}
695 }
696 /*
697  * Ensures that a root is enclosed in a scope on the project (proj != src/)
698  * (resource path case)
699  */
testScopeEncloses30()700 public void testScopeEncloses30() throws CoreException {
701 	try {
702 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
703 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
704 		assertTrue("scope on P should enclose \"/P/src\"", scope.encloses("/P/src"));
705 	} finally {
706 		deleteProject("P");
707 	}
708 }
709 /*
710  * Ensures that a root is enclosed in a scope on the project (proj != src/)
711  * (resource path with traling slash case)
712  */
testScopeEncloses31()713 public void testScopeEncloses31() throws CoreException {
714 	try {
715 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
716 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
717 		assertTrue("scope on P should enclose \"/P/src/\"", scope.encloses("/P/src/"));
718 	} finally {
719 		deleteProject("P");
720 	}
721 }
722 /*
723  * Ensures that a root is enclosed in a scope on the project (proj != src/)
724  * (Java element case)
725  */
testScopeEncloses32()726 public void testScopeEncloses32() throws CoreException {
727 	try {
728 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
729 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
730 		IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getProject().getFolder("src"));
731 		assertTrue("scope on P should enclose root P/src", scope.encloses(root));
732 	} finally {
733 		deleteProject("P");
734 	}
735 }
736 /*
737  * Ensures that a package is enclosed in a scope on the project (proj != src/)
738  * (resource path case)
739  */
testScopeEncloses33()740 public void testScopeEncloses33() throws CoreException {
741 	try {
742 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
743 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
744 		assertTrue("scope on P should enclose \"/P/src/x/y\"", scope.encloses("/P/src/x/y"));
745 	} finally {
746 		deleteProject("P");
747 	}
748 }
749 /*
750  * Ensures that a package is enclosed in a scope on the project (proj != src/)
751  * (resource path with trailing slash case)
752  */
testScopeEncloses34()753 public void testScopeEncloses34() throws CoreException {
754 	try {
755 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
756 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
757 		assertTrue("scope on P should enclose \"/P/src/x/y/\"", scope.encloses("/P/src/x/y/"));
758 	} finally {
759 		deleteProject("P");
760 	}
761 }
762 /*
763  * Ensures that a package is enclosed in a scope on the project (proj != src/)
764  * (Java element case)
765  */
testScopeEncloses35()766 public void testScopeEncloses35() throws CoreException {
767 	try {
768 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
769 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
770 		IPackageFragment pkg = getPackage("/P/src/x/y");
771 		assertTrue("scope on P should enclose package x.y", scope.encloses(pkg));
772 	} finally {
773 		deleteProject("P");
774 	}
775 }
776 /*
777  * Ensures that a default package is enclosed in a scope on the project (proj != src/)
778  * (Java element case)
779  */
testScopeEncloses36()780 public void testScopeEncloses36() throws CoreException {
781 	try {
782 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
783 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
784 		IPackageFragment pkg = getPackage("/P/src");
785 		assertTrue("scope on P should enclose default package", scope.encloses(pkg));
786 	} finally {
787 		deleteProject("P");
788 	}
789 }
790 /*
791  * Ensures that a compilation unit is enclosed in a scope on the project (proj != src/)
792  * (resource path case)
793  */
testScopeEncloses37()794 public void testScopeEncloses37() throws CoreException {
795 	try {
796 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
797 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
798 		assertTrue("scope on P should enclose \"/P/src/x/y/A.java\"", scope.encloses("/P/src/x/y/A.java"));
799 	} finally {
800 		deleteProject("P");
801 	}
802 }
803 /*
804  * Ensures that a compilation unit is enclosed in a scope on the project (proj != src/)
805  * (Java element case)
806  */
testScopeEncloses38()807 public void testScopeEncloses38() throws CoreException {
808 	try {
809 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
810 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
811 		ICompilationUnit cu = getCompilationUnit("/P/src/x/y/A.java");
812 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
813 	} finally {
814 		deleteProject("P");
815 	}
816 }
817 /*
818  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj != src/)
819  * (resource path case)
820  */
testScopeEncloses39()821 public void testScopeEncloses39() throws CoreException {
822 	try {
823 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
824 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
825 		assertTrue("scope on P should enclose \"/P/src/A.java\"", scope.encloses("/P/src/A.java"));
826 	} finally {
827 		deleteProject("P");
828 	}
829 }
830 /*
831  * Ensures that a compilation unit in the default package is enclosed in a scope on the project (proj != src/)
832  * (Java element case)
833  */
testScopeEncloses40()834 public void testScopeEncloses40() throws CoreException {
835 	try {
836 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
837 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
838 		ICompilationUnit cu = getCompilationUnit("/P/src/A.java");
839 		assertTrue("scope on P should enclose compilation unit A.java", scope.encloses(cu));
840 	} finally {
841 		deleteProject("P");
842 	}
843 }
844 /*
845  * Ensures that a source type is enclosed in a scope on the project (proj != src/)
846  * (Java element case)
847  */
testScopeEncloses41()848 public void testScopeEncloses41() throws CoreException {
849 	try {
850 		IJavaProject project = createJavaProject("P", new String[] {"src/"}, "bin");
851 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
852 		ICompilationUnit cu = getCompilationUnit("/P/src/x/y/A.java");
853 		IType type = cu.getType("A");
854 		assertTrue("scope on P should enclose type A", scope.encloses(type));
855 	} finally {
856 		deleteProject("P");
857 	}
858 }
859 
860 /**
861  * Bug 101022: [search] JUnit Test Runner on folder runs tests outside directory
862  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=101022"
863  */
testBug101022()864 public void testBug101022() throws CoreException {
865 	try {
866 		IJavaProject project = createJavaProject("P1", new String[] {"src", "test", "test2"}, "bin");
867 		createFile(
868 			"/P1/src/Test.java",
869 			"public class Test {\n" +
870 			"	protected void foo() {}\n" +
871 			"}"
872 		);
873 		createFile(
874 			"/P1/test/Test.java",
875 			"public class Test {\n" +
876 			"	protected void foo() {}\n" +
877 			"}"
878 		);
879 		createFile(
880 			"/P1/test2/Test.java",
881 			"public class Test {\n" +
882 			"	protected void foo() {}\n" +
883 			"}"
884 		);
885 		IPackageFragmentRoot root = project.getPackageFragmentRoot(getFolder("/P1/test"));
886 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {root});
887 		JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
888 		resultCollector.showProject();
889 		search("foo", METHOD, DECLARATIONS, scope, resultCollector);
890 		assertSearchResults(
891 			"test/Test.java [in P1] void Test.foo() [foo]",
892 			resultCollector);
893 	}
894 	finally {
895 		deleteProject("P1");
896 	}
897 }
898 
899 /**
900  * Bug 101426: Search doesn't work with imported plugin
901  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=101426"
902  */
testBug101426()903 public void testBug101426() throws CoreException {
904 	try {
905 		IJavaProject project = createJavaProject("P1", new String[] {"src/", "test/", "test2/"}, new String[] {"JCL_LIB"}, "bin");
906 		createFile(
907 			"/P1/src/Test.java",
908 			"public interface ITest {\n" +
909 			"}"
910 		);
911 		createFile(
912 			"/P1/test/Test.java",
913 			"public class Test {\n" +
914 			"	ITest test;\n" +
915 			"}"
916 		);
917 		createFile(
918 			"/P1/test2/Test.java",
919 			"public class Test2 {\n" +
920 			"	ITest test;\n" +
921 			"}"
922 		);
923 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {project});
924 		JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
925 		resultCollector.showProject();
926 		search("ITest", TYPE, REFERENCES, scope, resultCollector);
927 		assertSearchResults(
928 			"test/Test.java [in P1] Test.test [ITest]\n" +
929 			"test2/Test.java [in P1] Test2.test [ITest]",
930 			resultCollector);
931 	}
932 	finally {
933 		deleteProject("P1");
934 	}
935 }
936 
937 /**
938  * Bug 101777: [search] selecting class with a main type ignores the default package
939  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=101777"
940  */
testBug101777()941 public void testBug101777() throws CoreException {
942 	try {
943 		IJavaProject project = createJavaProject("P1");
944 		createFile(
945 			"/P1/Test.java",
946 			"public class Test {\n" +
947 			"	public static void main(String[] args) {}\n" +
948 			"}"
949 		);
950 		IPackageFragment[] fragments = project.getPackageFragments();
951 		IPackageFragment defaultFragment = null;
952 		for (int i = 0; i < fragments.length; i++) {
953 			IPackageFragment fragment = fragments[i];
954 			if (fragment.getElementName().length() == 0) {
955 				defaultFragment = fragment;
956 				break;
957 			}
958 		}
959 		assertNotNull("We should have a default fragment for project P1!", defaultFragment);
960 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {defaultFragment});
961 		JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
962 		resultCollector.showProject();
963 		search("main(String[]) void", METHOD, DECLARATIONS, scope, resultCollector);
964 		assertSearchResults(
965 			"Test.java [in P1] void Test.main(String[]) [main]",
966 			resultCollector);
967 	}
968 	finally {
969 		deleteProject("P1");
970 	}
971 }
972 
973 /**
974  * Bug 119203: Search doesn't work with imported plugin
975  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=119203"
976  * WARNING: Fix for this bug has been disabled due to bad regression
977  *
978  * Bug 127048: [search] References to Java element 'CorrectionEngine' not found
979  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=127048"
980  */
testBug119203()981 public void testBug119203() throws CoreException {
982 	try {
983 		IJavaProject project = createJavaProject("P1", new String[] {"src"}, "bin");
984 		createFile(
985 			"/P1/src/Test.java",
986 			"public class Test {\n" +
987 			"}"
988 		);
989 		createFile(
990 			"/P1/src/X.java",
991 			"public class X {\n" +
992 			"	Test test;\n" +
993 			"}"
994 		);
995 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
996 		JavaSearchResultCollector resultCollector = new JavaSearchResultCollector();
997 		resultCollector.showProject();
998 		search("Test", TYPE, REFERENCES, scope, resultCollector);
999 		assertSearchResults(
1000 			"src/X.java [in P1] X.test [Test]",
1001 			resultCollector);
1002 	}
1003 	finally {
1004 		deleteProject("P1");
1005 	}
1006 }
1007 /**
1008  * @bug 179199: [search] Open type throws NPE during Items filtering
1009  * @test Ensure that NPE does no longer happen when output location is also set as class folder in a project build path
1010  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=179199"
1011  */
testBug179199()1012 public void testBug179199() throws CoreException {
1013 	try {
1014 		// Create project and files
1015 		IJavaProject project = createJavaProject("P1", new String[] {"src"}, new String[] {"bin"}, "bin");
1016 		createFile(
1017 			"/P1/src/Test.java",
1018 			"public class Test {\n" +
1019 			"}"
1020 		);
1021 		createFile(
1022 			"/P1/src/X.java",
1023 			"public class X {\n" +
1024 			"	Test test;\n" +
1025 			"}"
1026 		);
1027 		waitUntilIndexesReady();
1028 
1029 		// Build to create .class files
1030 		project.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
1031 		waitForAutoBuild();
1032 
1033 		// Remove source file to peek the class file in 'bin' instead while searching all type names
1034 		deleteFile("/P1/src/Test.java");
1035 
1036 		// Index the output location as it is a library for the project
1037 		IndexManager indexManager = JavaModelManager.getIndexManager();
1038 		indexManager.indexLibrary(new Path("/P1/bin"), project.getProject(), null);
1039 		waitUntilIndexesReady();
1040 
1041 		// Search for all types
1042 		IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
1043 		TypeNameMatchCollector collector = new TypeNameMatchCollector();
1044 		new SearchEngine().searchAllTypeNames(
1045 			null,
1046 			SearchPattern.R_EXACT_MATCH,
1047 			"Test".toCharArray(),
1048 			SearchPattern.R_PREFIX_MATCH,
1049 			IJavaSearchConstants.TYPE,
1050 			scope,
1051 			collector,
1052 			IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
1053 			null);
1054 
1055 		// Verify results
1056 		assertSearchResults(
1057 			"Test (not open) [in Test.class [in <default> [in bin [in P1]]]]",
1058 			collector
1059 		);
1060 	}
1061 	finally {
1062 		deleteProject("P1");
1063 	}
1064 }
1065 /**
1066  * @bug 250211: [search] Organize Imports Hangs
1067  * @test Ensure that JavaSearchScope creation does not take too much time.<br>
1068  * Note that this test does not make any assertion, it just creates several projects
1069  * with a huge dependency tree and create a scope on all of them.<br>
1070  * If the bug was back again, then this test would never finish!
1071  * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=250211"
1072  */
testBug250211()1073 public void testBug250211() throws CoreException {
1074 	final int max = 50;
1075 	final IJavaProject[] projects = new IJavaProject[max];
1076 	try {
1077 		JavaCore.run(new IWorkspaceRunnable() {
1078 			public void run(IProgressMonitor monitor) throws CoreException {
1079 				for (int i = 0; i < max; i++){
1080 					String projectName = "P"+i;
1081 					String[] dependents = new String[max-1];
1082 					boolean[] exportedProjects = new boolean[max-1];
1083 					for (int j = 0; j < max; j++){
1084 						int idx = -1;
1085 						if (j < i) {
1086 							idx = j;
1087 						} else if (j > i) {
1088 							idx = j-1;
1089 						}
1090 						if (idx != -1) {
1091 							dependents[idx] = "/P"+j;
1092 							exportedProjects[idx] = true; // export all projects
1093 						}
1094 					}
1095 					projects[i] = createJavaProject(projectName, new String[]{"src"}, new String[]{"JCL_LIB"}, dependents, exportedProjects, "bin");
1096 				}
1097 			}
1098 		},
1099 		null);
1100 		SearchEngine.createJavaSearchScope(projects);
1101 	}
1102 	finally {
1103 		for (int i = 0; i < max; i++){
1104 			assertNotNull("Unexpected null project!", projects[i]);
1105 			deleteProject(projects[i]);
1106 		}
1107 	}
1108 }
1109 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=397818
testBug397818()1110 public void testBug397818() throws CoreException {
1111 	try {
1112 		createJavaProject("P1", new String[] {"src"}, new String[] {}, "bin");
1113 
1114 		createFolder("/P1/new folder");
1115 		IFile newFile = createFile("/P1/new folder/testindex.index", "");
1116 		try {
1117 			URL newURL = newFile.getLocationURI().toURL();
1118 			IndexLocation indexLoc = IndexLocation.createIndexLocation(newURL);
1119 			assertTrue("Malformed index location", indexLoc.getIndexFile().exists());
1120 		} catch (MalformedURLException e) {
1121 			fail("Malformed index location");
1122 		}
1123 	} finally {
1124 		deleteProject("P1");
1125 	}
1126 }
1127 }
1128