KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > metainfservices > ExportActionTest


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 package org.netbeans.modules.apisupport.metainfservices;
20
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.jmi.reflect.RefClass;
29 import javax.jmi.reflect.RefException;
30 import javax.jmi.reflect.RefFeatured;
31 import javax.jmi.reflect.RefObject;
32 import javax.jmi.reflect.RefPackage;
33 import org.netbeans.jmi.javamodel.ClassDefinition;
34 import org.netbeans.jmi.javamodel.Constructor;
35 import org.netbeans.jmi.javamodel.Element;
36 import org.netbeans.jmi.javamodel.ElementPartKind;
37 import org.netbeans.jmi.javamodel.Field;
38 import org.netbeans.jmi.javamodel.JavaClass;
39 import org.netbeans.jmi.javamodel.JavaDoc;
40 import org.netbeans.jmi.javamodel.Method;
41 import org.netbeans.jmi.javamodel.MultipartId;
42 import org.netbeans.jmi.javamodel.Resource;
43 import org.netbeans.junit.NbTestCase;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileSystem;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.filesystems.LocalFileSystem;
48 import org.openide.util.Lookup;
49 import org.openide.util.lookup.Lookups;
50
51 /**
52  *
53  * @author jarda
54  */

55 public class ExportActionTest extends NbTestCase {
56     
57     public ExportActionTest(String JavaDoc testName) {
58         super(testName);
59     }
60
61     protected void setUp() throws Exception JavaDoc {
62         clearWorkDir();
63     }
64
65     protected void tearDown() throws Exception JavaDoc {
66     }
67
68     public void testGenerateFiles() throws Exception JavaDoc {
69         LocalFileSystem lfs = new LocalFileSystem();
70         lfs.setRootDirectory(getWorkDir());
71
72         FileSystem fs = lfs;
73         FileObject src = FileUtil.createFolder(fs.getRoot(), "src");
74
75         ArrayList JavaDoc<String JavaDoc> files = new ArrayList JavaDoc<String JavaDoc>();
76         files.add("META-INF/services/java.lang.Object");
77         files.add("META-INF/services/java.lang.Runnable");
78
79         ExportAction.createFiles(R.class.getName(), files, src);
80
81         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[] { src.getURL() }, getClass().getClassLoader());
82         Lookup l = Lookups.metaInfServices(loader);
83
84         Runnable JavaDoc r = l.lookup(Runnable JavaDoc.class);
85
86         assertNotNull("Runnable found", r);
87         assertEquals("It is my class", R.class, r.getClass());
88
89
90         ExportAction.createFiles(Q.class.getName(), files, src);
91
92         l = Lookups.metaInfServices(loader);
93         Set JavaDoc<Class JavaDoc<? extends Runnable JavaDoc>> all = l.lookupResult(Runnable JavaDoc.class).allClasses();
94
95         assertEquals(2, all.size());
96         assertTrue("Q is there", all.contains(Q.class));
97         assertTrue("R is there", all.contains(R.class));
98     }
99
100     public void testRemovesAnnotations() throws Exception JavaDoc {
101         JavaClassImpl impl = new JavaClassImpl();
102         impl.setName("org.tst.Test");
103         impl.setSimpleName("Test");
104
105         JavaClassImpl par = new JavaClassImpl();
106         par.setName("org.par.Parent <X,Y>");
107         par.setSimpleName("Parent <X,Y>");
108
109         impl.setSuperClass(par);
110
111         JavaClassImpl obj = new JavaClassImpl();
112         obj.setName("java.lang.Object");
113         obj.setSimpleName("Object");
114
115         par.setSuperClass(obj);
116
117         ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
118         ExportAction.findInterfaces(impl, names);
119
120         assertEquals("Three", 3, names.size());
121
122         for (String JavaDoc n : names) {
123             if (n.indexOf("<") >= 0) {
124                 fail("Contains wrong char: " + n);
125             }
126             if (n.endsWith(" ")) {
127                 fail("Ends with space:[" + n + "]");
128             }
129         }
130     }
131
132     public static class R extends Object JavaDoc implements Runnable JavaDoc {
133         public void run() {
134         }
135     }
136
137     public static class Q implements Runnable JavaDoc {
138         public void run() {
139         }
140     }
141
142     private static final class JavaClassImpl implements JavaClass {
143         private String JavaDoc simpleName;
144         private String JavaDoc name;
145         private JavaClass superClass;
146         public List JavaDoc<JavaClass> interfaces = Collections.emptyList();
147
148         public Collection JavaDoc findSubTypes(boolean recursively) {
149             fail("Not implemented");
150             return null;
151         }
152
153         public boolean isInterface() {
154             fail("Not implemented");
155             return false;
156         }
157
158         public void setInterface(boolean newValue) {
159             fail("Not implemented");
160         }
161
162         public String JavaDoc getSimpleName() {
163             return simpleName;
164         }
165
166         public void setSimpleName(String JavaDoc newValue) {
167             simpleName = newValue;
168         }
169
170         public boolean isInner() {
171             return false;
172         }
173
174         public Collection JavaDoc getSubClasses() {
175             fail("Not implemented");
176             return null;
177         }
178
179         public Collection JavaDoc getImplementors() {
180             fail("Not implemented");
181             return null;
182         }
183
184         public int getStartOffset() {
185             return 0;
186         }
187
188         public int getEndOffset() {
189             return 0;
190         }
191
192         public Resource getResource() {
193             fail("Not implemented");
194             return null;
195         }
196
197         public int getPartStartOffset(ElementPartKind part) {
198             fail("Not implemented");
199             return 0;
200         }
201
202         public int getPartEndOffset(ElementPartKind part) {
203             fail("Not implemented");
204             return 0;
205         }
206
207         public void replaceChild(Element oldChild, Element newChild) {
208         }
209
210         public List JavaDoc getChildren() {
211             fail("Not implemented");
212             return null;
213         }
214
215         public boolean isValid() {
216             return true;
217         }
218
219         public Element duplicate() {
220             try {
221                 return (JavaClass)clone();
222             } catch (CloneNotSupportedException JavaDoc ex) {
223                 throw new IllegalStateException JavaDoc(ex);
224             }
225         }
226
227         public boolean refIsInstanceOf(RefObject refObject, boolean b) {
228             fail("Not implemented");
229             return false;
230         }
231
232         public RefClass refClass() {
233             fail("No");
234             return null;
235         }
236
237         public RefFeatured refImmediateComposite() {
238             fail("No");
239             return null;
240         }
241
242         public RefFeatured refOutermostComposite() {
243             fail("No");
244             return null;
245         }
246
247         public void refDelete() {
248         }
249
250         public void refSetValue(RefObject refObject, Object JavaDoc object) {
251         }
252
253         public void refSetValue(String JavaDoc string, Object JavaDoc object) {
254         }
255
256         public Object JavaDoc refGetValue(RefObject refObject) {
257             fail("No");
258             return null;
259         }
260
261         public Object JavaDoc refGetValue(String JavaDoc string) {
262             fail("No");
263             return null;
264         }
265
266         public Object JavaDoc refInvokeOperation(RefObject refObject, List JavaDoc list) throws RefException {
267             fail("No");
268             return null;
269         }
270
271         public Object JavaDoc refInvokeOperation(String JavaDoc string, List JavaDoc list) throws RefException {
272             fail("No");
273             return null;
274         }
275
276         public RefObject refMetaObject() {
277             fail("No");
278             return null;
279         }
280
281         public RefPackage refImmediatePackage() {
282             fail("No");
283             return null;
284         }
285
286         public RefPackage refOutermostPackage() {
287             fail("No");
288             return null;
289         }
290
291         public String JavaDoc refMofId() {
292             fail("No");
293             return null;
294         }
295
296         public Collection JavaDoc refVerifyConstraints(boolean b) {
297             fail("No");
298             return null;
299         }
300
301         public boolean isDeprecated() {
302             fail("No");
303             return false;
304         }
305
306         public void setDeprecated(boolean newValue) {
307         }
308
309         public ClassDefinition getDeclaringClass() {
310             fail("No");
311             return null;
312         }
313
314         public int getModifiers() {
315             fail("No");
316             return 0;
317         }
318
319         public void setModifiers(int newValue) {
320         }
321
322         public String JavaDoc getJavadocText() {
323             fail("No");
324             return null;
325         }
326
327         public void setJavadocText(String JavaDoc newValue) {
328         }
329
330         public JavaDoc getJavadoc() {
331             fail("No");
332             return null;
333         }
334
335         public void setJavadoc(JavaDoc newValue) {
336         }
337
338         public List JavaDoc getAnnotations() {
339             fail("No");
340             return null;
341         }
342
343         public String JavaDoc getName() {
344             return name;
345         }
346
347         public void setName(String JavaDoc newValue) {
348             this.name = newValue;
349         }
350
351         public Collection JavaDoc getReferences() {
352             fail("No");
353             return null;
354         }
355
356         public Field getField(String JavaDoc name, boolean includeSupertypes) {
357             fail("No");
358             return null;
359         }
360
361         public Method getMethod(String JavaDoc name, List JavaDoc parameters, boolean includeSupertypes) {
362             fail("No");
363             return null;
364         }
365
366         public JavaClass getInnerClass(String JavaDoc simpleName, boolean includeSupertypes) {
367             fail("No");
368             return null;
369         }
370
371         public Constructor getConstructor(List JavaDoc parameters, boolean includeSupertypes) {
372             fail("No");
373             return null;
374         }
375
376         public boolean isSubTypeOf(ClassDefinition javaClass) {
377             fail("No");
378             return false;
379         }
380
381         public List JavaDoc getContents() {
382             fail("No");
383             return null;
384         }
385
386         public MultipartId getSuperClassName() {
387             fail("No");
388             return null;
389         }
390
391         public void setSuperClassName(MultipartId newValue) {
392         }
393
394         public List JavaDoc getInterfaceNames() {
395             fail("No");
396             return null;
397         }
398
399         public List JavaDoc getFeatures() {
400             fail("No");
401             return null;
402         }
403
404         public List JavaDoc getInterfaces() {
405             return interfaces;
406         }
407
408         public JavaClass getSuperClass() {
409             return superClass;
410         }
411
412         public void setSuperClass(JavaClass newValue) {
413             this.superClass = newValue;
414         }
415
416         public List JavaDoc getTypeParameters() {
417             fail("No");
418             return null;
419         }
420
421     }
422 }
423
Popular Tags