Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 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 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
package org.netbeans.modules.java.source.ui; |
45 |
|
46 |
import org.netbeans.api.java.source.*; |
47 |
import com.sun.org.apache.bcel.internal.generic.AALOAD; |
48 |
import java.beans.PropertyChangeListener; |
49 |
import java.beans.PropertyChangeSupport; |
50 |
import java.io.File; |
51 |
import java.io.IOException; |
52 |
import java.io.OutputStreamWriter; |
53 |
import java.io.PrintWriter; |
54 |
import java.net.URL; |
55 |
import java.util.*; |
56 |
import java.util.concurrent.CountDownLatch; |
57 |
import java.util.concurrent.TimeUnit; |
58 |
import javax.lang.model.element.ElementKind; |
59 |
import javax.lang.model.element.TypeElement; |
60 |
import javax.swing.event.ChangeListener; |
61 |
import org.netbeans.api.java.classpath.ClassPath; |
62 |
import org.netbeans.api.java.classpath.GlobalPathRegistry; |
63 |
import org.netbeans.api.java.platform.JavaPlatformManager; |
64 |
import org.netbeans.api.java.queries.SourceForBinaryQuery; |
65 |
import org.netbeans.junit.MockServices; |
66 |
import org.netbeans.junit.NbTestCase; |
67 |
import org.netbeans.modules.java.source.parsing.FileObjects; |
68 |
import org.netbeans.modules.java.source.ui.JavaTypeProvider; |
69 |
import org.netbeans.modules.java.source.usages.ClassIndexManager; |
70 |
import org.netbeans.modules.java.source.usages.ClassIndexManagerEvent; |
71 |
import org.netbeans.modules.java.source.usages.ClassIndexManagerListener; |
72 |
import org.netbeans.modules.java.source.usages.IndexUtil; |
73 |
import org.netbeans.modules.parsing.api.indexing.IndexingManager; |
74 |
import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater; |
75 |
import org.netbeans.modules.parsing.lucene.support.IndexManager; |
76 |
import org.netbeans.spi.java.classpath.ClassPathFactory; |
77 |
import org.netbeans.spi.java.classpath.ClassPathImplementation; |
78 |
import org.netbeans.spi.java.classpath.ClassPathProvider; |
79 |
import org.netbeans.spi.java.classpath.PathResourceImplementation; |
80 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
81 |
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; |
82 |
import org.netbeans.spi.jumpto.type.JumptoAccessor; |
83 |
import org.netbeans.spi.jumpto.type.SearchType; |
84 |
import org.netbeans.spi.jumpto.type.TypeDescriptor; |
85 |
import org.netbeans.spi.jumpto.type.TypeProvider; |
86 |
import org.openide.filesystems.FileLock; |
87 |
import org.openide.filesystems.FileObject; |
88 |
import org.openide.filesystems.FileStateInvalidException; |
89 |
import org.openide.filesystems.FileSystem; |
90 |
import org.openide.filesystems.FileUtil; |
91 |
|
92 |
/** |
93 |
* Test setup taken from org.netbeans.api.java.source.ClassIndexTest. |
94 |
* |
95 |
* @author Tomas Zezula |
96 |
* @author markiewb |
97 |
*/ |
98 |
public class JavaTypeProviderTest extends NbTestCase { |
99 |
|
100 |
private static FileObject srcRoot; |
101 |
private static FileObject binRoot2; |
102 |
private static FileObject libSrc2; |
103 |
private static ClassPath sourcePath; |
104 |
private static ClassPath compilePath; |
105 |
private static ClassPath bootPath; |
106 |
private static JavaTypeProviderTest.MutableCp spiCp; |
107 |
private static JavaTypeProviderTest.MutableCp spiSrc; |
108 |
private JavaTypeProvider provider; |
109 |
|
110 |
public JavaTypeProviderTest(String name) { |
111 |
super(name); |
112 |
} |
113 |
String[] cA = {"ClassA", "org.me.pkg1"}; |
114 |
String[] cB = {"ClassA", "org.me.pkg1sibling"}; |
115 |
String[] cC = {"ClassA", "org.me.pkg1.subpackage"}; |
116 |
String[] cD = {"ClassB", "org.me.pkg2"}; |
117 |
String[] cE = {"ClassA", "com.other"}; |
118 |
|
119 |
@Override |
120 |
protected void setUp() throws Exception { |
121 |
clearWorkDir(); |
122 |
File cache = new File(getWorkDir(), "cache"); //NOI18N |
123 |
cache.mkdirs(); |
124 |
IndexUtil.setCacheFolder(cache); |
125 |
File src = new File(getWorkDir(), "src"); //NOI18N |
126 |
src.mkdirs(); |
127 |
srcRoot = FileUtil.toFileObject(src); |
128 |
srcRoot.createFolder("foo"); //NOI18N |
129 |
src = new File(getWorkDir(), "src2"); //NOI18N |
130 |
src.mkdirs(); |
131 |
src = new File(getWorkDir(), "lib"); //NOI18N |
132 |
src.mkdirs(); |
133 |
src = new File(getWorkDir(), "lib2"); //NOI18N |
134 |
src.mkdirs(); |
135 |
binRoot2 = FileUtil.toFileObject(src); |
136 |
src = new File(getWorkDir(), "lib2Src"); //NOI18N |
137 |
src.mkdirs(); |
138 |
libSrc2 = FileUtil.toFileObject(src); |
139 |
spiSrc = new JavaTypeProviderTest.MutableCp(Collections.singletonList(ClassPathSupport.createResource(srcRoot.getURL()))); |
140 |
sourcePath = ClassPathFactory.createClassPath(spiSrc); |
141 |
spiCp = new JavaTypeProviderTest.MutableCp(); |
142 |
compilePath = ClassPathFactory.createClassPath(spiCp); |
143 |
bootPath = JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(); |
144 |
MockServices.setServices(JavaTypeProviderTest.ClassPathProviderImpl.class, JavaTypeProviderTest.SFBQ.class); |
145 |
|
146 |
//create some types to be searched for later |
147 |
String[][] testTypes = { |
148 |
cA, cB, cC, cD, cE |
149 |
}; |
150 |
|
151 |
for (String[] entry : testTypes) { |
152 |
createJavaFile(srcRoot, entry[1], entry[0]); |
153 |
} |
154 |
IndexingManager.getDefault().refreshIndexAndWait(srcRoot.getURL(), null, true); |
155 |
final ClassPath scp = ClassPathSupport.createClassPath(srcRoot); |
156 |
final ClasspathInfo cpInfo = ClasspathInfo.create( |
157 |
ClassPathSupport.createClassPath(new URL[0]), |
158 |
ClassPathSupport.createClassPath(new URL[0]), |
159 |
scp); |
160 |
provider = new JavaTypeProvider(cpInfo, null); |
161 |
|
162 |
} |
163 |
|
164 |
@Override |
165 |
protected void tearDown() throws Exception { |
166 |
MockServices.setServices(); |
167 |
} |
168 |
|
169 |
/** |
170 |
* A full FQN should result in one class type. |
171 |
* |
172 |
* @throws Exception |
173 |
*/ |
174 |
public void testGetDeclaredTypesScopes_FullyQualifiedName_Full() throws Exception { |
175 |
//original behaviour - without packagename |
176 |
{ |
177 |
String[][] expectedResults = {cE, cA, cC, cB}; |
178 |
assertComputeTypeNames(expectedResults, "ClassA", provider); |
179 |
} |
180 |
//original behaviour - without packagename |
181 |
{ |
182 |
String[][] expectedResults = {cD}; |
183 |
assertComputeTypeNames(expectedResults, "ClassB", provider); |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
/** |
189 |
* Tests the behaviour, when no package name is included in searchText. |
190 |
* Packagenames should be ignored. |
191 |
* |
192 |
* @throws Exception |
193 |
*/ |
194 |
public void testGetDeclaredTypesScopes_DefaultBehaviour() throws Exception { |
195 |
//original behaviour - without packagename |
196 |
{ |
197 |
String[][] expectedResults = {cE, cA, cC, cB}; |
198 |
assertComputeTypeNames(expectedResults, "ClassA", provider); |
199 |
} |
200 |
//original behaviour - without packagename |
201 |
{ |
202 |
String[][] expectedResults = {cD}; |
203 |
assertComputeTypeNames(expectedResults, "ClassB", provider); |
204 |
} |
205 |
|
206 |
} |
207 |
|
208 |
/** |
209 |
* A partially given FQN may result in more than one class type. The |
210 |
* packagename is used for retrival of package and subpackages, which |
211 |
* results in more results. |
212 |
* |
213 |
* @throws Exception |
214 |
*/ |
215 |
public void testGetDeclaredTypesScopes_FullyQualifiedName_Partial() throws Exception { |
216 |
//search for classes with prefix "ClassA" and packages with (sub-)package "org.me.pkg1" |
217 |
{ |
218 |
String[][] expectedResults = {cA, cC}; |
219 |
assertComputeTypeNames(expectedResults, "org.me.pkg1.ClassA", provider); |
220 |
} |
221 |
//search for classes with prefix "ClassA" and packages with (sub-)package "org" |
222 |
{ |
223 |
String[][] expectedResults = {cA, cC, cB}; |
224 |
assertComputeTypeNames(expectedResults, "org.ClassA", provider); |
225 |
} |
226 |
//search for classes with prefix "ClassA" and packages with (sub-)package "org.me" |
227 |
{ |
228 |
String[][] expectedResults = {cA, cC, cB}; |
229 |
assertComputeTypeNames(expectedResults, "org.me.ClassA", provider); |
230 |
} |
231 |
//search for classes with prefix "Cl" and packages with (sub-)package "org.me" |
232 |
{ |
233 |
String[][] expectedResults = {cA, cC, cB, cD}; |
234 |
assertComputeTypeNames(expectedResults, "org.me.Cl", provider); |
235 |
} |
236 |
} |
237 |
|
238 |
private FileObject createJavaFile( |
239 |
final FileObject root, |
240 |
final String pkg, |
241 |
final String name, |
242 |
final String content) throws IOException { |
243 |
final FileObject file = FileUtil.createData( |
244 |
root, |
245 |
String.format("%s/%s.java", |
246 |
FileObjects.convertPackage2Folder(pkg), |
247 |
name)); |
248 |
final FileLock lck = file.lock(); |
249 |
try { |
250 |
final PrintWriter out = new PrintWriter(new OutputStreamWriter(file.getOutputStream(lck))); |
251 |
try { |
252 |
out.print(content); |
253 |
} finally { |
254 |
out.close(); |
255 |
} |
256 |
} finally { |
257 |
lck.releaseLock(); |
258 |
} |
259 |
return file; |
260 |
} |
261 |
|
262 |
private void assertComputeTypeNames(String[][] expectedResults, String searchText, TypeProvider provider) { |
263 |
List<TypeDescriptor> results = new ArrayList<TypeDescriptor>(); |
264 |
TypeProvider.Result res = JumptoAccessor.createResult(results); |
265 |
TypeProvider.Context c = JumptoAccessor.createContext(null, searchText, SearchType.PREFIX); |
266 |
|
267 |
provider.computeTypeNames(c, res); |
268 |
|
269 |
assertEquals(expectedResults.length, results.size()); |
270 |
//sort to make the result test run reproducable |
271 |
Collections.sort(results, new Comparator<TypeDescriptor>() { |
272 |
@Override |
273 |
public int compare(TypeDescriptor o1, TypeDescriptor o2) { |
274 |
int compareValue = o1.getSimpleName().compareToIgnoreCase(o2.getSimpleName()); |
275 |
if (compareValue != 0) { |
276 |
return compareValue; |
277 |
} |
278 |
return o1.getContextName().compareToIgnoreCase(o2.getContextName()); |
279 |
} |
280 |
}); |
281 |
|
282 |
for (int i = 0; i < results.size(); i++) { |
283 |
|
284 |
assertEquals("not equals at index " + i, expectedResults[i][0], results.get(i).getSimpleName()); |
285 |
assertEquals("not equals at index " + i, " (" + expectedResults[i][1] + ")", results.get(i).getContextName()); |
286 |
} |
287 |
} |
288 |
|
289 |
private FileObject createJavaFile(FileObject srcRoot1, String packageName, String className) throws IOException { |
290 |
return createJavaFile(srcRoot1, packageName, className, "package " + packageName + ";\npublic class " + className + " {}\n"); |
291 |
} |
292 |
|
293 |
public static class ClassPathProviderImpl implements ClassPathProvider { |
294 |
|
295 |
public ClassPath findClassPath(final FileObject file, final String type) { |
296 |
final FileObject[] roots = sourcePath.getRoots(); |
297 |
for (FileObject root : roots) { |
298 |
if (root.equals(file) || FileUtil.isParentOf(root, file)) { |
299 |
if (type == ClassPath.SOURCE) { |
300 |
return sourcePath; |
301 |
} |
302 |
if (type == ClassPath.COMPILE) { |
303 |
return compilePath; |
304 |
} |
305 |
if (type == ClassPath.BOOT) { |
306 |
return bootPath; |
307 |
} |
308 |
} |
309 |
} |
310 |
if (libSrc2.equals(file) || FileUtil.isParentOf(libSrc2, file)) { |
311 |
if (type == ClassPath.SOURCE) { |
312 |
return ClassPathSupport.createClassPath(new FileObject[]{libSrc2}); |
313 |
} |
314 |
if (type == ClassPath.COMPILE) { |
315 |
return ClassPathSupport.createClassPath(new URL[0]); |
316 |
} |
317 |
if (type == ClassPath.BOOT) { |
318 |
return bootPath; |
319 |
} |
320 |
} |
321 |
return null; |
322 |
} |
323 |
} |
324 |
|
325 |
public static class SFBQ implements SourceForBinaryQueryImplementation { |
326 |
|
327 |
public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { |
328 |
try { |
329 |
if (binaryRoot.equals(binRoot2.getURL())) { |
330 |
return new SourceForBinaryQuery.Result() { |
331 |
public FileObject[] getRoots() { |
332 |
return new FileObject[]{libSrc2}; |
333 |
} |
334 |
|
335 |
public void addChangeListener(ChangeListener l) { |
336 |
} |
337 |
|
338 |
public void removeChangeListener(ChangeListener l) { |
339 |
} |
340 |
}; |
341 |
} |
342 |
} catch (FileStateInvalidException e) { |
343 |
} |
344 |
return null; |
345 |
} |
346 |
} |
347 |
|
348 |
private static final class MutableCp implements ClassPathImplementation { |
349 |
|
350 |
private final PropertyChangeSupport support; |
351 |
private List<? extends PathResourceImplementation> impls; |
352 |
|
353 |
public MutableCp() { |
354 |
this(Collections.<PathResourceImplementation>emptyList()); |
355 |
} |
356 |
|
357 |
public MutableCp(final List<? extends PathResourceImplementation> impls) { |
358 |
assert impls != null; |
359 |
support = new PropertyChangeSupport(this); |
360 |
this.impls = impls; |
361 |
} |
362 |
|
363 |
public List<? extends PathResourceImplementation> getResources() { |
364 |
return impls; |
365 |
} |
366 |
|
367 |
public void addPropertyChangeListener(final PropertyChangeListener listener) { |
368 |
assert listener != null; |
369 |
this.support.addPropertyChangeListener(listener); |
370 |
} |
371 |
|
372 |
public void removePropertyChangeListener(final PropertyChangeListener listener) { |
373 |
assert listener != null; |
374 |
this.support.removePropertyChangeListener(listener); |
375 |
} |
376 |
|
377 |
void setImpls(final List<? extends PathResourceImplementation> impls) { |
378 |
assert impls != null; |
379 |
this.impls = impls; |
380 |
this.support.firePropertyChange(PROP_RESOURCES, null, null); |
381 |
} |
382 |
} |
383 |
} |