KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > SourceUtilsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.ElementKind;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.util.ElementFilter;
34 import javax.swing.event.ChangeListener JavaDoc;
35 import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
36 import org.netbeans.api.java.source.ClasspathInfo;
37 import org.netbeans.api.java.source.ClasspathInfo.PathKind;
38 import org.netbeans.junit.NbTestCase;
39 import org.netbeans.modules.java.source.ElementHandleAccessor;
40 import org.netbeans.modules.java.source.TestUtil;
41 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
42 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
43 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46
47 /**
48  *
49  * @author Jan Lahoda
50  */

51 public class SourceUtilsTest extends NbTestCase {
52     
53     private JavaSource js;
54     private CompilationInfo info;
55     
56     public SourceUtilsTest(String JavaDoc testName) {
57         super(testName);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61         clearWorkDir();
62         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[] {SFBQImpl.getDefault()});
63     }
64
65     private void prepareTest() throws Exception JavaDoc {
66         File JavaDoc work = TestUtil.createWorkFolder();
67         FileObject workFO = FileUtil.toFileObject(work);
68         
69         assertNotNull(workFO);
70         
71         FileObject sourceRoot = workFO.createFolder("src");
72         FileObject buildRoot = workFO.createFolder("build");
73         FileObject cache = workFO.createFolder("cache");
74         FileObject packageRoot = sourceRoot.createFolder("sourceutils");
75         
76         SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
77         
78         String JavaDoc capitalizedName = "T" + getName().substring(1);
79         
80         TestUtil.copyFiles(FileUtil.toFile(sourceRoot), "sourceutils/" + capitalizedName + ".java");
81         
82         packageRoot.refresh();
83         
84         FileObject testSource = packageRoot.getFileObject(capitalizedName + ".java");
85         
86         assertNotNull(testSource);
87
88         SourceUtilsTestUtil.compileRecursively(sourceRoot);
89         
90         js = JavaSource.forFileObject(testSource);
91         
92         assertNotNull(js);
93         
94         info = SourceUtilsTestUtil.getCompilationInfo(js, JavaSource.Phase.RESOLVED);
95         
96         assertNotNull(info);
97     }
98     
99     public void testGetEnclosingTypeElement() throws Exception JavaDoc {
100         //only a scatch of the test, add testcases as needed:
101
prepareTest();
102         
103         TypeElement test = info.getElements().getTypeElement("sourceutils.TestGetEnclosingTypeElement");
104         
105         assertNotNull(test);
106         
107         ExecutableElement testMethod = ElementFilter.methodsIn(test.getEnclosedElements()).get(0);
108 // TypeElement classInMethod = ElementFilter.typesIn(testMethod.getEnclosedElements()).get(0);
109
// ExecutableElement classInMethodMethod = ElementFilter.methodsIn(classInMethod.getEnclosedElements()).get(0);;
110
// VariableElement classInMethodField = ElementFilter.fieldsIn(classInMethod.getEnclosedElements()).get(0);;
111
// TypeElement classInMethodNestedClass = ElementFilter.typesIn(classInMethod.getEnclosedElements()).get(0);
112
VariableElement testField = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0);
113         TypeElement nestedClass = ElementFilter.typesIn(test.getEnclosedElements()).get(0);
114         ExecutableElement nestedClassMethod = ElementFilter.methodsIn(nestedClass.getEnclosedElements()).get(0);
115         VariableElement nestedClassField = ElementFilter.fieldsIn(nestedClass.getEnclosedElements()).get(0);
116         TypeElement nestedClassNestedClass = ElementFilter.typesIn(nestedClass.getEnclosedElements()).get(0);
117         
118         assertEquals("TestGetEnclosingTypeElement", test.getSimpleName().toString());
119         assertEquals("testMethod", testMethod.getSimpleName().toString());
120 // assertEquals("classInMethod", classInMethod.getSimpleName().toString());
121
// assertEquals("classInMethodMethod", classInMethodMethod.getSimpleName().toString());
122
// assertEquals("classInMethodField", classInMethodField.getSimpleName().toString());
123
// assertEquals("classInMethodNestedClass", classInMethodNestedClass.getSimpleName().toString());
124
assertEquals("testField", testField.getSimpleName().toString());
125         assertEquals("NestedClass", nestedClass.getSimpleName().toString());
126         assertEquals("nestedClassMethod", nestedClassMethod.getSimpleName().toString());
127         assertEquals("nestedClassField", nestedClassField.getSimpleName().toString());
128         assertEquals("NestedClassNestedClass", nestedClassNestedClass.getSimpleName().toString());
129         
130         assertEquals(null, SourceUtils.getEnclosingTypeElement(test));
131         assertEquals(test, SourceUtils.getEnclosingTypeElement(testMethod));
132         assertEquals(test, SourceUtils.getEnclosingTypeElement(testField));
133         assertEquals(test, SourceUtils.getEnclosingTypeElement(nestedClass));
134         assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassMethod));
135         assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassField));
136         assertEquals(nestedClass, SourceUtils.getEnclosingTypeElement(nestedClassNestedClass));
137         
138         try {
139             SourceUtils.getEnclosingTypeElement(test.getEnclosingElement());
140             fail();
141         } catch (IllegalArgumentException JavaDoc e) {
142             //good.
143
}
144     }
145     
146     public void testIsDeprecated1() throws Exception JavaDoc {
147         prepareTest();
148         
149         TypeElement test = info.getElements().getTypeElement("sourceutils.TestIsDeprecated1");
150         
151         assertNotNull(test);
152         
153         ExecutableElement methodDeprecated = findElementBySimpleName("methodDeprecated", ElementFilter.methodsIn(test.getEnclosedElements()));
154         ExecutableElement methodNotDeprecated = findElementBySimpleName("methodNotDeprecated", ElementFilter.methodsIn(test.getEnclosedElements()));
155         VariableElement fieldDeprecated = findElementBySimpleName("fieldDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements()));
156         VariableElement fieldNotDeprecated = findElementBySimpleName("fieldNotDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements()));
157         TypeElement classDeprecated = findElementBySimpleName("classDeprecated", ElementFilter.typesIn(test.getEnclosedElements()));
158         TypeElement classNotDeprecated = findElementBySimpleName("classNotDeprecated", ElementFilter.typesIn(test.getEnclosedElements()));
159         
160         assertFalse(info.getElements().isDeprecated(methodNotDeprecated));
161         assertFalse(info.getElements().isDeprecated(fieldNotDeprecated));
162         assertFalse(info.getElements().isDeprecated(classNotDeprecated));
163
164         assertTrue(info.getElements().isDeprecated(methodDeprecated));
165         assertTrue(info.getElements().isDeprecated(fieldDeprecated));
166         assertTrue(info.getElements().isDeprecated(classDeprecated));
167     }
168     
169     public void testIsDeprecated2() throws Exception JavaDoc {
170         prepareTest();
171         
172         TypeElement test = info.getElements().getTypeElement("sourceutils.TestIsDeprecated2");
173         
174         assertNotNull(test);
175         
176         ExecutableElement methodDeprecated = findElementBySimpleName("methodDeprecated", ElementFilter.methodsIn(test.getEnclosedElements()));
177         ExecutableElement methodNotDeprecated = findElementBySimpleName("methodNotDeprecated", ElementFilter.methodsIn(test.getEnclosedElements()));
178         VariableElement fieldDeprecated = findElementBySimpleName("fieldDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements()));
179         VariableElement fieldNotDeprecated = findElementBySimpleName("fieldNotDeprecated", ElementFilter.fieldsIn(test.getEnclosedElements()));
180         TypeElement classDeprecated = findElementBySimpleName("classDeprecated", ElementFilter.typesIn(test.getEnclosedElements()));
181         TypeElement classNotDeprecated = findElementBySimpleName("classNotDeprecated", ElementFilter.typesIn(test.getEnclosedElements()));
182         
183         assertFalse(info.getElements().isDeprecated(methodNotDeprecated));
184         assertFalse(info.getElements().isDeprecated(fieldNotDeprecated));
185         assertFalse(info.getElements().isDeprecated(classNotDeprecated));
186
187         assertTrue(info.getElements().isDeprecated(methodDeprecated));
188         assertTrue(info.getElements().isDeprecated(fieldDeprecated));
189         assertTrue(info.getElements().isDeprecated(classDeprecated));
190     }
191     
192     public void testGetOutermostEnclosingTypeElement () throws Exception JavaDoc {
193     prepareTest();
194     TypeElement test = info.getElements().getTypeElement("sourceutils.TestGetOutermostEnclosingTypeElement");
195         assertNotNull(test);
196     assertEquals("TestGetOutermostEnclosingTypeElement", test.getSimpleName().toString());
197     
198     ExecutableElement testMethod = ElementFilter.methodsIn(test.getEnclosedElements()).get(0);
199 // TypeElement classInMethod = ElementFilter.typesIn(testMethod.getEnclosedElements()).get(0);
200
// ExecutableElement classInMethodMethod = ElementFilter.methodsIn(classInMethod.getEnclosedElements()).get(0);;
201
// VariableElement classInMethodField = ElementFilter.fieldsIn(classInMethod.getEnclosedElements()).get(0);;
202
// TypeElement classInMethodNestedClass = ElementFilter.typesIn(classInMethod.getEnclosedElements()).get(0);
203
VariableElement testField = ElementFilter.fieldsIn(test.getEnclosedElements()).get(0);
204         TypeElement nestedClass = ElementFilter.typesIn(test.getEnclosedElements()).get(0);
205         ExecutableElement nestedClassMethod = ElementFilter.methodsIn(nestedClass.getEnclosedElements()).get(0);
206         VariableElement nestedClassField = ElementFilter.fieldsIn(nestedClass.getEnclosedElements()).get(0);
207         TypeElement nestedClassNestedClass = ElementFilter.typesIn(nestedClass.getEnclosedElements()).get(0);
208         
209         
210         assertEquals("testMethod", testMethod.getSimpleName().toString());
211 // assertEquals("classInMethod", classInMethod.getSimpleName().toString());
212
// assertEquals("classInMethodMethod", classInMethodMethod.getSimpleName().toString());
213
// assertEquals("classInMethodField", classInMethodField.getSimpleName().toString());
214
// assertEquals("classInMethodNestedClass", classInMethodNestedClass.getSimpleName().toString());
215
assertEquals("testField", testField.getSimpleName().toString());
216         assertEquals("NestedClass", nestedClass.getSimpleName().toString());
217         assertEquals("nestedClassMethod", nestedClassMethod.getSimpleName().toString());
218         assertEquals("nestedClassField", nestedClassField.getSimpleName().toString());
219         assertEquals("NestedClassNestedClass", nestedClassNestedClass.getSimpleName().toString());
220         
221         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(test));
222         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(testMethod));
223         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(testField));
224         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClass));
225         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassMethod));
226         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassField));
227         assertEquals(test, SourceUtils.getOutermostEnclosingTypeElement(nestedClassNestedClass));
228         
229         try {
230             SourceUtils.getOutermostEnclosingTypeElement(test.getEnclosingElement());
231             fail();
232         } catch (IllegalArgumentException JavaDoc e) {
233             //good.
234
}
235     }
236     
237     private <E extends Element> E findElementBySimpleName(String JavaDoc simpleName, List JavaDoc<E> elements) {
238         for (E e : elements) {
239             if (simpleName.contentEquals(e.getSimpleName()))
240                 return e;
241         }
242         
243         fail("Not found element with simple name: " + simpleName);
244         
245         throw new Error JavaDoc("Should never be here!");
246     }
247     
248     public void testGetFQNsForSimpleName() throws Exception JavaDoc {
249 // The method was removed,
250
// tzezula: I am going to create an replacement using lucene
251
// prepareTest();
252
//
253
// List<TypeElement> fqnTEs;
254
// List<String> fqns;
255
//
256
// fqnTEs = SourceUtils.getFQNsForSimpleNamePrefix(info, "List", true);
257
// fqns = new ArrayList<String>();
258
//
259
// for (TypeElement te : fqnTEs) {
260
// fqns.add(te.getQualifiedName().toString());
261
// }
262
//
263
// assertTrue(fqns.remove("java.util.List"));
264
// assertTrue(fqns.remove("java.awt.List"));
265
//
266
// //JDK16 specific:
267
// fqns.remove("com.sun.xml.bind.v2.schemagen.xmlschema.List");
268
//
269
// assertEquals(fqns.toString(), 0, fqns.size());
270
//
271
// fqnTEs = SourceUtils.getFQNsForSimpleNamePrefix(info, "File", true);
272
// fqns = new ArrayList<String>();
273
//
274
// for (TypeElement te : fqnTEs) {
275
// fqns.add(te.getQualifiedName().toString());
276
// }
277
//
278
// assertTrue(fqns.remove("java.io.File"));
279
//
280
// assertEquals(fqns.toString(), 0, fqns.size());
281

282         //XXX: onlyExact
283
}
284     
285     
286     public void testGetFile () throws Exception JavaDoc {
287         File JavaDoc workDir = getWorkDir();
288         FileObject workFo = FileUtil.toFileObject(workDir);
289         assertNotNull (workFo);
290         FileObject src = workFo.createFolder("src");
291         FileObject srcInDefPkg = src.createData("Foo","java");
292         assertNotNull(srcInDefPkg);
293         FileObject sourceFile = src.createFolder("org").createFolder("me").createData("Test", "java");
294         assertNotNull(sourceFile);
295         ClasspathInfo cpInfo = ClasspathInfo.create(ClassPathSupport.createClassPath(new FileObject[0]), ClassPathSupport.createClassPath(new FileObject[0]),
296             ClassPathSupport.createClassPath(new FileObject[]{src}));
297         FileObject cls = cpInfo.getClassPath(PathKind.OUTPUT).getRoots()[0];
298         FileObject classInDefPkg = cls.createData("Foo","class");
299         assertNotNull(classInDefPkg);
300         FileObject classPkg = cls.createFolder("org").createFolder("me");
301         assertNotNull(classPkg);
302         FileObject classFile = classPkg.createData("Test", "class");
303         assertNotNull(classFile);
304         FileObject classFileInnder = classPkg.createData("Test$Inner", "class");
305         assertNotNull(classFileInnder);
306         SFBQImpl.getDefault().register(cls, src);
307         ElementHandle<? extends Element> handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String JavaDoc[] {"org.me.Test"});
308         assertNotNull (handle);
309         FileObject result = SourceUtils.getFile(handle, cpInfo);
310         assertNotNull(result);
311         handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String JavaDoc[] {"org.me.Test$Inner"});
312         result = SourceUtils.getFile(handle,cpInfo);
313         assertNotNull(result);
314         handle = ElementHandleAccessor.INSTANCE.create(ElementKind.PACKAGE, new String JavaDoc[] {"org.me"});
315         result = SourceUtils.getFile(handle,cpInfo);
316         assertNotNull(result);
317         handle = ElementHandleAccessor.INSTANCE.create(ElementKind.CLASS, new String JavaDoc[] {"Foo"});
318         result = SourceUtils.getFile(handle,cpInfo);
319         assertNotNull(result);
320     }
321     
322     
323     private static class SFBQImpl implements SourceForBinaryQueryImplementation {
324         
325         private static SFBQImpl instance;
326         
327         private final Map JavaDoc<URL JavaDoc, FileObject> map = new HashMap JavaDoc<URL JavaDoc, FileObject> ();
328         
329         private SFBQImpl () {
330             
331         }
332         
333         public void register (FileObject bin, FileObject src) throws IOException JavaDoc {
334             map.put(bin.getURL(), src);
335         }
336             
337         public Result findSourceRoots(URL JavaDoc binaryRoot) {
338             final FileObject src = map.get (binaryRoot);
339             if (src != null) {
340                 return new Result() {
341
342                     public FileObject[] getRoots() {
343                         return new FileObject[] {src};
344                     }
345
346                     public void addChangeListener(ChangeListener JavaDoc l) {
347                     }
348
349                     public void removeChangeListener(ChangeListener JavaDoc l) {
350                     }
351                 };
352             }
353             return null;
354         }
355         
356         public static synchronized SFBQImpl getDefault () {
357             if (instance == null) {
358                 instance = new SFBQImpl ();
359             }
360             return instance;
361         }
362     }
363     
364 }
365
Popular Tags