Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.parsing.spi.indexing.support; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.net.URL; |
47 |
import java.util.Arrays; |
48 |
import java.util.Collection; |
49 |
import java.util.Collections; |
50 |
import java.util.HashMap; |
51 |
import java.util.HashSet; |
52 |
import java.util.IdentityHashMap; |
53 |
import java.util.List; |
54 |
import java.util.Map; |
55 |
import java.util.Set; |
56 |
import javax.swing.event.ChangeListener; |
57 |
import org.netbeans.api.java.classpath.ClassPath; |
58 |
import org.netbeans.api.java.classpath.GlobalPathRegistry; |
59 |
import org.netbeans.api.java.queries.SourceForBinaryQuery; |
60 |
import org.netbeans.junit.MockServices; |
61 |
import org.netbeans.junit.NbTestCase; |
62 |
import org.netbeans.modules.parsing.impl.indexing.Pair; |
63 |
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater; |
64 |
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdaterTest; |
65 |
import org.netbeans.modules.parsing.impl.indexing.friendapi.IndexingController; |
66 |
import org.netbeans.modules.parsing.lucene.support.LowMemoryWatcher; |
67 |
import org.netbeans.modules.parsing.spi.indexing.PathRecognizer; |
68 |
import org.netbeans.spi.java.classpath.ClassPathProvider; |
69 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
70 |
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2; |
71 |
import org.openide.filesystems.FileObject; |
72 |
import org.openide.filesystems.FileUtil; |
73 |
|
74 |
/** |
75 |
* |
76 |
* @author Tomas Zezula |
77 |
*/ |
78 |
public class QuerySupportTest extends NbTestCase { |
79 |
|
80 |
private final Map<String, Set<ClassPath>> registeredClasspaths = new HashMap<String, Set<ClassPath>>(); |
81 |
|
82 |
public QuerySupportTest(String name) { |
83 |
super(name); |
84 |
} |
85 |
|
86 |
@Override |
87 |
protected void setUp() throws Exception { |
88 |
super.setUp(); |
89 |
clearWorkDir(); |
90 |
MockServices.setServices( |
91 |
JavaLikePathRecognizer.class, |
92 |
ScriptingLikePathRecognizer.class, |
93 |
ClassPathProviderImpl.class, |
94 |
SourceForBinaryQueryImpl.class); |
95 |
RepositoryUpdaterTest.waitForRepositoryUpdaterInit(); |
96 |
} |
97 |
|
98 |
@Override |
99 |
protected void tearDown() throws Exception { |
100 |
for(String id : registeredClasspaths.keySet()) { |
101 |
final Set<ClassPath> classpaths = registeredClasspaths.get(id); |
102 |
GlobalPathRegistry.getDefault().unregister(id, classpaths.toArray(new ClassPath[classpaths.size()])); |
103 |
} |
104 |
registeredClasspaths.clear(); |
105 |
ClassPathProviderImpl.register(Collections.<FileObject,Map<String,ClassPath>>emptyMap()); |
106 |
SourceForBinaryQueryImpl.register(Collections.<URL,Pair<Boolean,List<FileObject>>>emptyMap()); |
107 |
LowMemoryWatcher.getInstance().free(); |
108 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
109 |
super.tearDown(); |
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
public void testGetDependentRootsJavaLikeDependenciesAllProjectsOpened() throws IOException, InterruptedException { |
115 |
final FileObject wd = FileUtil.toFileObject(getWorkDir()); |
116 |
final FileObject src1 = FileUtil.createFolder(wd,"src1"); //NOI18N |
117 |
final URL build1 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build1"));//NOI18N |
118 |
final FileObject src2 = FileUtil.createFolder(wd,"src2"); //NOI18N |
119 |
final URL build2 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build2")); //NOI18N |
120 |
final FileObject src3 = FileUtil.createFolder(wd,"src3"); //NOI18N |
121 |
final URL build3 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build3")); //NOI18N |
122 |
final FileObject src4 = FileUtil.createFolder(wd,"src4"); //NOI18N |
123 |
final URL build4 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build4")); //NOI18N |
124 |
|
125 |
final ClassPath src1Source = ClassPathSupport.createClassPath(src1); |
126 |
final ClassPath src2Source = ClassPathSupport.createClassPath(src2); |
127 |
final ClassPath src2Compile = ClassPathSupport.createClassPath(build1); |
128 |
final ClassPath src3Source = ClassPathSupport.createClassPath(src3); |
129 |
final ClassPath src3Compile = ClassPathSupport.createClassPath(build1); |
130 |
final ClassPath src4Source = ClassPathSupport.createClassPath(src4); |
131 |
final ClassPath src4Compile = ClassPathSupport.createClassPath(build2); |
132 |
|
133 |
final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>(); |
134 |
sfbq.put(build1,Pair.of(true,Collections.singletonList(src1))); |
135 |
sfbq.put(build2,Pair.of(true,Collections.singletonList(src2))); |
136 |
sfbq.put(build3,Pair.of(true,Collections.singletonList(src3))); |
137 |
sfbq.put(build4,Pair.of(true,Collections.singletonList(src4))); |
138 |
SourceForBinaryQueryImpl.register(sfbq); |
139 |
|
140 |
|
141 |
final ClassPath boot = ClassPath.EMPTY; |
142 |
final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>(); |
143 |
src1cps.put(JavaLikePathRecognizer.BOOT, boot); |
144 |
src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY); |
145 |
src1cps.put(JavaLikePathRecognizer.SRC, src1Source); |
146 |
final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>(); |
147 |
src2cps.put(JavaLikePathRecognizer.BOOT, boot); |
148 |
src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile); |
149 |
src2cps.put(JavaLikePathRecognizer.SRC, src2Source); |
150 |
final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>(); |
151 |
src3cps.put(JavaLikePathRecognizer.BOOT, boot); |
152 |
src3cps.put(JavaLikePathRecognizer.COMPILE, src3Compile); |
153 |
src3cps.put(JavaLikePathRecognizer.SRC, src3Source); |
154 |
final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>(); |
155 |
src4cps.put(JavaLikePathRecognizer.BOOT, boot); |
156 |
src4cps.put(JavaLikePathRecognizer.COMPILE, src4Compile); |
157 |
src4cps.put(JavaLikePathRecognizer.SRC, src4Source); |
158 |
final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>(); |
159 |
cps.put(src1,src1cps); |
160 |
cps.put(src2,src2cps); |
161 |
cps.put(src3,src3cps); |
162 |
cps.put(src4,src4cps); |
163 |
ClassPathProviderImpl.register(cps); |
164 |
|
165 |
globalPathRegistry_register( |
166 |
JavaLikePathRecognizer.BOOT, |
167 |
new ClassPath[]{ |
168 |
boot |
169 |
}); |
170 |
globalPathRegistry_register( |
171 |
JavaLikePathRecognizer.COMPILE, |
172 |
new ClassPath[]{ |
173 |
src2Compile, |
174 |
src3Compile, |
175 |
src4Compile |
176 |
}); |
177 |
globalPathRegistry_register( |
178 |
JavaLikePathRecognizer.SRC, |
179 |
new ClassPath[]{ |
180 |
src1Source, |
181 |
src2Source, |
182 |
src3Source, |
183 |
src4Source |
184 |
}); |
185 |
|
186 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
187 |
assertEquals( |
188 |
IndexingController.getDefault().getRootDependencies().keySet().toString(), |
189 |
4, |
190 |
IndexingController.getDefault().getRootDependencies().size()); |
191 |
|
192 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true)); |
193 |
assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, true)); |
194 |
assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, true)); |
195 |
assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, true)); |
196 |
|
197 |
|
198 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false)); |
199 |
assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, false)); |
200 |
assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false)); |
201 |
assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, false)); |
202 |
} |
203 |
|
204 |
|
205 |
public void testGetDependentRootsJavaLikeDependenciesProjectsClosed() throws IOException, InterruptedException { |
206 |
final FileObject wd = FileUtil.toFileObject(getWorkDir()); |
207 |
final FileObject src1 = FileUtil.createFolder(wd,"src1"); //NOI18N |
208 |
final URL build1 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build1"));//NOI18N |
209 |
final FileObject src2 = FileUtil.createFolder(wd,"src2"); //NOI18N |
210 |
final URL build2 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build2")); //NOI18N |
211 |
final FileObject src3 = FileUtil.createFolder(wd,"src3"); //NOI18N |
212 |
final URL build3 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build3")); //NOI18N |
213 |
final FileObject src4 = FileUtil.createFolder(wd,"src4"); //NOI18N |
214 |
final URL build4 = FileUtil.urlForArchiveOrDir(new File (getWorkDir(),"build4")); //NOI18N |
215 |
|
216 |
final ClassPath src1Source = ClassPathSupport.createClassPath(src1); |
217 |
final ClassPath src2Source = ClassPathSupport.createClassPath(src2); |
218 |
final ClassPath src2Compile = ClassPathSupport.createClassPath(build1); |
219 |
final ClassPath src3Source = ClassPathSupport.createClassPath(src3); |
220 |
final ClassPath src3Compile = ClassPathSupport.createClassPath(build1); |
221 |
final ClassPath src4Source = ClassPathSupport.createClassPath(src4); |
222 |
final ClassPath src4Compile = ClassPathSupport.createClassPath(build2); |
223 |
|
224 |
final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>(); |
225 |
sfbq.put(build1,Pair.of(true,Collections.singletonList(src1))); |
226 |
sfbq.put(build2,Pair.of(true,Collections.singletonList(src2))); |
227 |
sfbq.put(build3,Pair.of(true,Collections.singletonList(src3))); |
228 |
sfbq.put(build4,Pair.of(true,Collections.singletonList(src4))); |
229 |
SourceForBinaryQueryImpl.register(sfbq); |
230 |
|
231 |
|
232 |
final ClassPath boot = ClassPath.EMPTY; |
233 |
final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>(); |
234 |
src1cps.put(JavaLikePathRecognizer.BOOT, boot); |
235 |
src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY); |
236 |
src1cps.put(JavaLikePathRecognizer.SRC, src1Source); |
237 |
final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>(); |
238 |
src2cps.put(JavaLikePathRecognizer.BOOT, boot); |
239 |
src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile); |
240 |
src2cps.put(JavaLikePathRecognizer.SRC, src2Source); |
241 |
final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>(); |
242 |
src3cps.put(JavaLikePathRecognizer.BOOT, boot); |
243 |
src3cps.put(JavaLikePathRecognizer.COMPILE, src3Compile); |
244 |
src3cps.put(JavaLikePathRecognizer.SRC, src3Source); |
245 |
final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>(); |
246 |
src4cps.put(JavaLikePathRecognizer.BOOT, boot); |
247 |
src4cps.put(JavaLikePathRecognizer.COMPILE, src4Compile); |
248 |
src4cps.put(JavaLikePathRecognizer.SRC, src4Source); |
249 |
final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>(); |
250 |
cps.put(src1,src1cps); |
251 |
cps.put(src2,src2cps); |
252 |
cps.put(src3,src3cps); |
253 |
cps.put(src4,src4cps); |
254 |
ClassPathProviderImpl.register(cps); |
255 |
|
256 |
globalPathRegistry_register( |
257 |
JavaLikePathRecognizer.BOOT, |
258 |
new ClassPath[]{ |
259 |
boot |
260 |
}); |
261 |
globalPathRegistry_register( |
262 |
JavaLikePathRecognizer.COMPILE, |
263 |
new ClassPath[]{ |
264 |
src4Compile |
265 |
}); |
266 |
globalPathRegistry_register( |
267 |
JavaLikePathRecognizer.SRC, |
268 |
new ClassPath[]{ |
269 |
src4Source |
270 |
}); |
271 |
|
272 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
273 |
assertEquals( |
274 |
IndexingController.getDefault().getRootDependencies().keySet().toString(), |
275 |
3, |
276 |
IndexingController.getDefault().getRootDependencies().size()); |
277 |
|
278 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true)); |
279 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src2, true)); |
280 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src1, true)); |
281 |
|
282 |
|
283 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false)); |
284 |
assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false)); |
285 |
assertEquals(new FileObject[] {src1, src2, src4}, QuerySupport.findDependentRoots(src1, false)); |
286 |
} |
287 |
|
288 |
|
289 |
public void testGetDependentRootsJavaLikeBinaryDependencies() throws IOException, InterruptedException { |
290 |
|
291 |
IndexingController.getDefault().getRootDependencies().keySet(); |
292 |
|
293 |
final FileObject wd = FileUtil.toFileObject(getWorkDir()); |
294 |
final FileObject bin1 = FileUtil.createFolder(wd, "bin1"); //NOI18N |
295 |
final FileObject bin1Sources = FileUtil.createFolder(wd, "bin1src"); //NOI18N |
296 |
final FileObject src1 = FileUtil.createFolder(wd,"src1"); //NOI18N |
297 |
final FileObject src2 = FileUtil.createFolder(wd,"src2"); //NOI18N |
298 |
final FileObject src3 = FileUtil.createFolder(wd,"src3"); //NOI18N |
299 |
|
300 |
final ClassPath src1Source = ClassPathSupport.createClassPath(src1); |
301 |
final ClassPath src1Compile = ClassPathSupport.createClassPath(bin1); |
302 |
final ClassPath src2Source = ClassPathSupport.createClassPath(src2); |
303 |
final ClassPath src2Compile = ClassPathSupport.createClassPath(bin1); |
304 |
final ClassPath src3Source = ClassPathSupport.createClassPath(src3); |
305 |
|
306 |
final Map<URL,Pair<Boolean,List<FileObject>>> sfbq = new HashMap<URL, Pair<Boolean,List<FileObject>>>(); |
307 |
sfbq.put(bin1.toURL(),Pair.of(false,Collections.singletonList(bin1Sources))); |
308 |
SourceForBinaryQueryImpl.register(sfbq); |
309 |
|
310 |
|
311 |
final ClassPath boot = ClassPath.EMPTY; |
312 |
final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>(); |
313 |
src1cps.put(JavaLikePathRecognizer.BOOT, boot); |
314 |
src1cps.put(JavaLikePathRecognizer.COMPILE, src1Compile); |
315 |
src1cps.put(JavaLikePathRecognizer.SRC, src1Source); |
316 |
final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>(); |
317 |
src2cps.put(JavaLikePathRecognizer.BOOT, boot); |
318 |
src2cps.put(JavaLikePathRecognizer.COMPILE, src2Compile); |
319 |
src2cps.put(JavaLikePathRecognizer.SRC, src2Source); |
320 |
final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>(); |
321 |
src3cps.put(JavaLikePathRecognizer.BOOT, boot); |
322 |
src3cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY); |
323 |
src3cps.put(JavaLikePathRecognizer.SRC, src3Source); |
324 |
final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>(); |
325 |
cps.put(src1,src1cps); |
326 |
cps.put(src2,src2cps); |
327 |
cps.put(src3,src3cps); |
328 |
ClassPathProviderImpl.register(cps); |
329 |
|
330 |
globalPathRegistry_register( |
331 |
JavaLikePathRecognizer.BOOT, |
332 |
new ClassPath[]{ |
333 |
boot |
334 |
}); |
335 |
globalPathRegistry_register( |
336 |
JavaLikePathRecognizer.COMPILE, |
337 |
new ClassPath[]{ |
338 |
src1Compile, |
339 |
src2Compile |
340 |
}); |
341 |
globalPathRegistry_register( |
342 |
JavaLikePathRecognizer.SRC, |
343 |
new ClassPath[]{ |
344 |
src1Source, |
345 |
src2Source, |
346 |
src3Source |
347 |
}); |
348 |
|
349 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
350 |
assertEquals( |
351 |
IndexingController.getDefault().getRootDependencies().keySet().toString(), |
352 |
3, |
353 |
IndexingController.getDefault().getRootDependencies().size()); |
354 |
|
355 |
assertEquals(new FileObject[] {src1, src2}, QuerySupport.findDependentRoots(bin1Sources, true)); |
356 |
} |
357 |
|
358 |
public void testGetDependentRootsJavaLikeDependenciesWithPeerRoots() throws IOException, InterruptedException { |
359 |
final FileObject wd = FileUtil.toFileObject(getWorkDir()); |
360 |
final FileObject src1a = FileUtil.createFolder(wd,"src1a"); //NOI18N |
361 |
final FileObject src1b = FileUtil.createFolder(wd,"src1b"); //NOI18N |
362 |
|
363 |
final ClassPath src1Source = ClassPathSupport.createClassPath(src1a, src1b); |
364 |
|
365 |
|
366 |
final ClassPath boot = ClassPath.EMPTY; |
367 |
final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>(); |
368 |
src1cps.put(JavaLikePathRecognizer.BOOT, boot); |
369 |
src1cps.put(JavaLikePathRecognizer.COMPILE, ClassPath.EMPTY); |
370 |
src1cps.put(JavaLikePathRecognizer.SRC, src1Source); |
371 |
final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>(); |
372 |
cps.put(src1a,src1cps); |
373 |
cps.put(src1b,src1cps); |
374 |
ClassPathProviderImpl.register(cps); |
375 |
|
376 |
globalPathRegistry_register( |
377 |
JavaLikePathRecognizer.BOOT, |
378 |
new ClassPath[]{ |
379 |
boot |
380 |
}); |
381 |
globalPathRegistry_register( |
382 |
JavaLikePathRecognizer.SRC, |
383 |
new ClassPath[]{ |
384 |
src1Source |
385 |
}); |
386 |
|
387 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
388 |
assertEquals( |
389 |
IndexingController.getDefault().getRootDependencies().keySet().toString(), |
390 |
2, |
391 |
IndexingController.getDefault().getRootDependencies().size()); |
392 |
|
393 |
assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1a, true)); |
394 |
assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1b, true)); |
395 |
|
396 |
assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1a, false)); |
397 |
assertEquals(new FileObject[] {src1a, src1b}, QuerySupport.findDependentRoots(src1b, false)); |
398 |
} |
399 |
|
400 |
public void testGetDependentRootsScriptingLikeDependenciesAllProjectsOpened() throws IOException, InterruptedException { |
401 |
final FileObject wd = FileUtil.toFileObject(getWorkDir()); |
402 |
final FileObject libSrc = FileUtil.createFolder(wd,"libSrc"); //NOI18N |
403 |
final FileObject src1 = FileUtil.createFolder(wd,"src1"); //NOI18N |
404 |
final FileObject src2 = FileUtil.createFolder(wd,"src2"); //NOI18N |
405 |
final FileObject src3 = FileUtil.createFolder(wd,"src3"); //NOI18N |
406 |
final FileObject src4 = FileUtil.createFolder(wd,"src4"); //NOI18N |
407 |
|
408 |
final ClassPath src1Source = ClassPathSupport.createClassPath(src1); |
409 |
final ClassPath src1Compile = ClassPathSupport.createClassPath(libSrc); |
410 |
final ClassPath src2Source = ClassPathSupport.createClassPath(src2); |
411 |
final ClassPath src2Compile = ClassPathSupport.createClassPath(src1); |
412 |
final ClassPath src3Source = ClassPathSupport.createClassPath(src3); |
413 |
final ClassPath src3Compile = ClassPathSupport.createClassPath(src1); |
414 |
final ClassPath src4Source = ClassPathSupport.createClassPath(src4); |
415 |
final ClassPath src4Compile = ClassPathSupport.createClassPath(src2); |
416 |
|
417 |
|
418 |
final Map<String,ClassPath> src1cps = new HashMap<String, ClassPath>(); |
419 |
src1cps.put(ScriptingLikePathRecognizer.COMPILE, src1Compile); |
420 |
src1cps.put(ScriptingLikePathRecognizer.SRC, src1Source); |
421 |
final Map<String,ClassPath> src2cps = new HashMap<String, ClassPath>(); |
422 |
src2cps.put(ScriptingLikePathRecognizer.COMPILE, src2Compile); |
423 |
src2cps.put(ScriptingLikePathRecognizer.SRC, src2Source); |
424 |
final Map<String,ClassPath> src3cps = new HashMap<String, ClassPath>(); |
425 |
src3cps.put(ScriptingLikePathRecognizer.COMPILE, src3Compile); |
426 |
src3cps.put(ScriptingLikePathRecognizer.SRC, src3Source); |
427 |
final Map<String,ClassPath> src4cps = new HashMap<String, ClassPath>(); |
428 |
src4cps.put(ScriptingLikePathRecognizer.COMPILE, src4Compile); |
429 |
src4cps.put(ScriptingLikePathRecognizer.SRC, src4Source); |
430 |
final Map<FileObject,Map<String,ClassPath>> cps = new HashMap<FileObject, Map<String, ClassPath>>(); |
431 |
cps.put(src1,src1cps); |
432 |
cps.put(src2,src2cps); |
433 |
cps.put(src3,src3cps); |
434 |
cps.put(src4,src4cps); |
435 |
ClassPathProviderImpl.register(cps); |
436 |
|
437 |
globalPathRegistry_register( |
438 |
ScriptingLikePathRecognizer.COMPILE, |
439 |
new ClassPath[]{ |
440 |
src2Compile, |
441 |
src3Compile, |
442 |
src4Compile |
443 |
}); |
444 |
globalPathRegistry_register( |
445 |
ScriptingLikePathRecognizer.SRC, |
446 |
new ClassPath[]{ |
447 |
src1Source, |
448 |
src2Source, |
449 |
src3Source, |
450 |
src4Source |
451 |
}); |
452 |
|
453 |
RepositoryUpdater.getDefault().waitUntilFinished(RepositoryUpdaterTest.TIME); |
454 |
assertEquals( |
455 |
IndexingController.getDefault().getRootDependencies().keySet().toString(), |
456 |
5, |
457 |
IndexingController.getDefault().getRootDependencies().size()); |
458 |
|
459 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, true)); |
460 |
assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, true)); |
461 |
assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, true)); |
462 |
assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, true)); |
463 |
assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(libSrc, true)); |
464 |
|
465 |
assertEquals(new FileObject[] {src4}, QuerySupport.findDependentRoots(src4, false)); |
466 |
assertEquals(new FileObject[] {src3}, QuerySupport.findDependentRoots(src3, false)); |
467 |
assertEquals(new FileObject[] {src2, src4}, QuerySupport.findDependentRoots(src2, false)); |
468 |
assertEquals(new FileObject[] {src1, src2, src3, src4}, QuerySupport.findDependentRoots(src1, false)); |
469 |
assertEquals(new FileObject[] {libSrc, src1, src2, src3, src4}, QuerySupport.findDependentRoots(libSrc, false)); |
470 |
} |
471 |
|
472 |
private void assertEquals(FileObject[] expected, Collection<FileObject> res) { |
473 |
final Set<FileObject> set = new HashSet<FileObject>(); |
474 |
Collections.addAll(set, expected); |
475 |
for (FileObject fo : res) { |
476 |
assertTrue( |
477 |
String.format("expected: %s res: %s", Arrays.toString(expected), res), //NOI18N |
478 |
set.remove(fo)); |
479 |
} |
480 |
assertTrue( |
481 |
String.format("expected: %s res: %s", Arrays.toString(expected), res), //NOI8N |
482 |
set.isEmpty()); |
483 |
} |
484 |
|
485 |
private void globalPathRegistry_register(String id, ClassPath [] classpaths) { |
486 |
Set<ClassPath> set = registeredClasspaths.get(id); |
487 |
if (set == null) { |
488 |
set = Collections.newSetFromMap(new IdentityHashMap<ClassPath, Boolean>()); |
489 |
registeredClasspaths.put(id, set); |
490 |
} |
491 |
for (ClassPath cp : classpaths) { |
492 |
set.add(cp); |
493 |
} |
494 |
GlobalPathRegistry.getDefault().register(id, classpaths); |
495 |
} |
496 |
|
497 |
public static final class JavaLikePathRecognizer extends PathRecognizer { |
498 |
|
499 |
public static final String SRC = "source"; //NOI18N |
500 |
public static final String COMPILE = "compile"; //NOI18N |
501 |
public static final String BOOT = "boot"; //NOI18N |
502 |
public static final String MIME_JAVA = "text/x-java"; //NOI18N |
503 |
|
504 |
@Override |
505 |
public Set<String> getSourcePathIds() { |
506 |
return Collections.singleton(SRC); |
507 |
} |
508 |
|
509 |
@Override |
510 |
public Set<String> getLibraryPathIds() { |
511 |
return Collections.<String>emptySet(); |
512 |
} |
513 |
|
514 |
@Override |
515 |
public Set<String> getBinaryLibraryPathIds() { |
516 |
return new HashSet<String>(Arrays.asList(BOOT, COMPILE)); |
517 |
} |
518 |
|
519 |
@Override |
520 |
public Set<String> getMimeTypes() { |
521 |
return Collections.singleton(MIME_JAVA); |
522 |
} |
523 |
|
524 |
} |
525 |
|
526 |
public static final class ScriptingLikePathRecognizer extends PathRecognizer { |
527 |
|
528 |
public static final String SRC = "scripts"; //NOI18N |
529 |
public static final String COMPILE = "included"; //NOI18N |
530 |
public static final String MIME_JAVA = "text/x-script"; //NOI18N |
531 |
|
532 |
@Override |
533 |
public Set<String> getSourcePathIds() { |
534 |
return Collections.singleton(SRC); |
535 |
} |
536 |
|
537 |
@Override |
538 |
public Set<String> getLibraryPathIds() { |
539 |
return Collections.singleton(COMPILE); |
540 |
} |
541 |
|
542 |
@Override |
543 |
public Set<String> getBinaryLibraryPathIds() { |
544 |
return Collections.<String>emptySet(); |
545 |
} |
546 |
|
547 |
@Override |
548 |
public Set<String> getMimeTypes() { |
549 |
return Collections.singleton(MIME_JAVA); |
550 |
} |
551 |
|
552 |
} |
553 |
|
554 |
public static final class ClassPathProviderImpl implements ClassPathProvider { |
555 |
|
556 |
private static volatile Map<FileObject,Map<String,ClassPath>> cps = Collections.emptyMap(); |
557 |
|
558 |
@Override |
559 |
public ClassPath findClassPath(FileObject file, String type) { |
560 |
for (Map.Entry<FileObject,Map<String,ClassPath>> e : cps.entrySet()) { |
561 |
if (e.getKey().equals(file) || FileUtil.isParentOf(e.getKey(), file)) { |
562 |
return e.getValue().get(type); |
563 |
} |
564 |
} |
565 |
return null; |
566 |
} |
567 |
|
568 |
static void register(Map<FileObject,Map<String,ClassPath>> newClassPaths) { |
569 |
cps = newClassPaths; |
570 |
} |
571 |
|
572 |
} |
573 |
|
574 |
public static final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation2 { |
575 |
|
576 |
private static volatile Map<URL, Pair<Boolean,List<FileObject>>> mapping = Collections.emptyMap(); |
577 |
|
578 |
@Override |
579 |
public Result findSourceRoots2(final URL binaryRoot) { |
580 |
final Pair<Boolean,List<FileObject>> sources = mapping.get(binaryRoot); |
581 |
if (sources == null) { |
582 |
return null; |
583 |
} |
584 |
return new Result() { |
585 |
@Override |
586 |
public boolean preferSources() { |
587 |
return sources.first; |
588 |
} |
589 |
|
590 |
@Override |
591 |
public FileObject[] getRoots() { |
592 |
return sources.second.toArray(new FileObject[sources.second.size()]); |
593 |
} |
594 |
|
595 |
@Override |
596 |
public void addChangeListener(ChangeListener l) { |
597 |
} |
598 |
|
599 |
@Override |
600 |
public void removeChangeListener(ChangeListener l) { |
601 |
} |
602 |
}; |
603 |
} |
604 |
|
605 |
@Override |
606 |
public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { |
607 |
return findSourceRoots2(binaryRoot); |
608 |
} |
609 |
|
610 |
static void register(Map<URL,Pair<Boolean,List<FileObject>>> newMapping) { |
611 |
mapping = newMapping; |
612 |
} |
613 |
|
614 |
} |
615 |
|
616 |
} |