KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > platformdefinition > DefaultClassPathProviderTest


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.modules.java.j2seplatform.platformdefinition;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.DataOutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.netbeans.api.java.classpath.GlobalPathRegistry;
34 import org.netbeans.api.java.platform.JavaPlatform;
35 import org.netbeans.api.java.platform.JavaPlatformManager;
36 import org.netbeans.api.java.queries.SourceForBinaryQuery;
37
38
39
40 import org.netbeans.api.project.TestUtil;
41 import org.netbeans.core.startup.layers.ArchiveURLMapper;
42
43
44
45 import org.netbeans.junit.NbTestCase;
46 import org.netbeans.modules.masterfs.MasterURLMapper;
47
48 import org.netbeans.spi.java.classpath.ClassPathProvider;
49 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
50 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
51
52
53 import org.openide.filesystems.FileLock;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.util.Lookup;
57
58
59 import org.openide.util.lookup.Lookups;
60
61
62 /**
63  *
64  * @author tom
65  */

66 public class DefaultClassPathProviderTest extends NbTestCase implements Lookup.Provider {
67     
68     private static final int FILE_IN_PACKAGE = 0;
69     private static final int FILE_IN_BAD_PACKAGE = 1;
70     private static final int FILE_IN_DEFAULT_PACKAGE = 2;
71
72     private static final byte[] CLASS_FILE_DATA = {
73         (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, 0x00, 0x00, 0x00, 0x2e, 0x00, 0x0d, 0x0a, 0x00, 0x03, 0x00, 0x0a, 0x07, 0x00, 0x0b, 0x07, 0x00,
74         0x0c, 0x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43,
75         0x6f, 0x64, 0x65, 0x01, 0x00, 0x0f, 0x4c, 0x69, 0x6e, 0x65, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x54, 0x61, 0x62, 0x6c,
76         0x65, 0x01, 0x00, 0x0a, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x01, 0x00, 0x09, 0x54, 0x65, 0x73,
77         0x74, 0x2e, 0x6a, 0x61, 0x76, 0x61, 0x0c, 0x00, 0x04, 0x00, 0x05, 0x01, 0x00, 0x09, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x54,
78         0x65, 0x73, 0x74, 0x01, 0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65,
79         0x63, 0x74, 0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04, 0x00, 0x05,
80         0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2a, (byte)0xb7, 0x00, 0x01,
81         (byte)0xb1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
82         0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09
83     };
84
85     private FileObject srcRoot;
86     private FileObject[] srcFile = new FileObject[3];
87     private FileObject[] compileRoots;
88     private FileObject[] execRoots;
89     private FileObject[] libSourceRoots;
90     private FileObject execTestDir;
91     private Lookup lookup;
92     
93     /** Creates a new instance of DefaultClassPathProviderTest */
94     public DefaultClassPathProviderTest (String JavaDoc testName) {
95         super (testName);
96         TestUtil.setLookup(Lookups.proxy(this));
97     }
98     
99     
100     protected void tearDown () throws Exception JavaDoc {
101         this.srcRoot = null;
102         this.compileRoots = null;
103         super.tearDown();
104     }
105     
106     
107     protected void setUp() throws Exception JavaDoc {
108         this.clearWorkDir();
109         super.setUp();
110         FileObject workDir = FileUtil.toFileObject(this.getWorkDir());
111         assertNotNull("MasterFS is not configured.", workDir);
112         this.srcRoot = workDir.createFolder("src");
113         this.compileRoots = new FileObject[3];
114         for (int i=0; i< this.compileRoots.length; i++) {
115             this.compileRoots[i] = workDir.createFolder("lib_"+Integer.toString(i));
116         }
117         ClassPath cp = ClassPathSupport.createClassPath(this.compileRoots);
118         GlobalPathRegistry.getDefault().register(ClassPath.COMPILE, new ClassPath[] {cp});
119         this.execRoots = new FileObject[2];
120         this.execRoots[0] = this.compileRoots[2];
121         this.execRoots[1] = workDir.createFolder("lib_OnlyExec");
122         cp = ClassPathSupport.createClassPath(this.execRoots);
123         GlobalPathRegistry.getDefault().register (ClassPath.EXECUTE, new ClassPath[]{cp});
124         this.libSourceRoots = new FileObject[2];
125         for (int i=0; i< libSourceRoots.length; i++) {
126             this.libSourceRoots[i] = workDir.createFolder ("libSrc_"+Integer.toString(i));
127         }
128         cp = ClassPathSupport.createClassPath (this.libSourceRoots);
129         GlobalPathRegistry.getDefault().register (ClassPath.SOURCE, new ClassPath[]{cp});
130         execTestDir = workDir.createFolder("exec");
131     }
132     
133     
134     
135     public void testFindClassPath () throws IOException JavaDoc {
136         FileObject artefact = getSourceFile (FILE_IN_PACKAGE);
137         ClassPathProvider cpp = new DefaultClassPathProvider ();
138         ClassPath cp = cpp.findClassPath(artefact, ClassPath.SOURCE);
139         assertNotNull ("DefaultClassPathProvider returned null for SOURCES",cp);
140         assertEquals("Invalid length of classpath for SOURCE",1,cp.getRoots().length);
141         assertRootsEquals ("Invalid classpath roots for SOURCE", cp, new FileObject[] {this.srcRoot});
142         cp = cpp.findClassPath(artefact, ClassPath.COMPILE);
143         assertNotNull ("DefaultClassPathProvider returned null for COMPILE",cp);
144         assertEquals("Invalid length of classpath for COMPILE",this.compileRoots.length + 1, cp.getRoots().length);
145         FileObject[] resRoots = new FileObject[this.compileRoots.length + 1];
146         System.arraycopy(this.compileRoots,0,resRoots,0,this.compileRoots.length);
147         resRoots[this.compileRoots.length] = this.execRoots[1];
148         assertRootsEquals ("Invalid classpath roots for COMPILE", cp, resRoots);
149         cp = cpp.findClassPath(artefact, ClassPath.BOOT);
150         assertNotNull ("DefaultClassPathProvider returned null for BOOT",cp);
151         JavaPlatform dp = JavaPlatformManager.getDefault().getDefaultPlatform();
152         assertEquals("Invalid length of classpath for BOOT",dp.getBootstrapLibraries().getRoots().length, cp.getRoots().length);
153         assertRootsEquals ("Invalid classpath roots for BOOT", cp, dp.getBootstrapLibraries().getRoots());
154
155         artefact = getSourceFile (FILE_IN_DEFAULT_PACKAGE);
156         cp = cpp.findClassPath(artefact, ClassPath.SOURCE);
157         assertNotNull ("DefaultClassPathProvider returned null for SOURCES",cp);
158         assertEquals("Invalid length of classpath for SOURCE",1,cp.getRoots().length);
159         assertRootsEquals ("Invalid classpath roots for SOURCE", cp, new FileObject[] {this.srcRoot});
160         
161         artefact = getSourceFile (FILE_IN_BAD_PACKAGE);
162         cp = cpp.findClassPath(artefact, ClassPath.SOURCE);
163         assertNotNull ("DefaultClassPathProvider returned null for SOURCES",cp);
164         assertEquals("Invalid length of classpath for SOURCE",1,cp.getRoots().length);
165         FileObject badRoot = this.srcRoot.getFileObject ("test");
166         assertRootsEquals ("Invalid classpath roots for SOURCE", cp, new FileObject[] {badRoot}); //ERROR
167
FileObject classFile = getClassFile();
168         cp = cpp.findClassPath(classFile, ClassPath.EXECUTE);
169         assertNotNull ("DefaultClassPathProvider returned null for EXECUTE",cp);
170         assertEquals("Invalid length of classpath for EXECUTE",1,cp.getRoots().length);
171         assertEquals("Illegal classpath for EXECUTE: ",cp.getRoots()[0],this.execTestDir);
172     }
173     
174     
175     private static void assertRootsEquals (String JavaDoc message, ClassPath cp, FileObject[] roots) {
176         Set JavaDoc/*FileObject*/ cpRoots = new HashSet JavaDoc(Arrays.asList(cp.getRoots ()));
177         assertEquals(message, cpRoots.size(), roots.length);
178         for (int i=0; i< roots.length; i++) {
179             if (!cpRoots.contains(roots[i])) {
180                 assertTrue(message, false);
181             }
182         }
183     }
184     
185     private synchronized FileObject getSourceFile (int type) throws IOException JavaDoc {
186         if (this.srcFile[type]==null) {
187             assertNotNull (this.srcRoot);
188             switch (type) {
189                 case FILE_IN_PACKAGE:
190                     this.srcFile[type] = createFile (this.srcRoot,"test","Test","package test;\npublic class Test {}");
191                     break;
192                 case FILE_IN_DEFAULT_PACKAGE:
193                     this.srcFile[type] = createFile (this.srcRoot,null,"DefaultTest","public class DefaultTest {}");
194                     break;
195                 case FILE_IN_BAD_PACKAGE:
196                     this.srcFile[type] = createFile (this.srcRoot,"test","BadTest","package bad;\npublic class BadTest {}");
197                     break;
198                 default:
199                     throw new IllegalArgumentException JavaDoc ();
200             }
201         }
202         return this.srcFile[type];
203     }
204
205     private synchronized FileObject getClassFile () throws IOException JavaDoc {
206         FileObject fo = this.execTestDir.getFileObject("test/Test.class");
207         if (fo == null) {
208             fo = execTestDir.createFolder("test");
209             fo = fo.createData("Test","class");
210             FileLock lock = fo.lock();
211             try {
212                 DataOutputStream JavaDoc out = new DataOutputStream JavaDoc (fo.getOutputStream(lock));
213                 try {
214                     out.write(CLASS_FILE_DATA);
215                     out.flush();
216                 } finally {
217                     out.close();
218                 }
219             } finally {
220                 lock.releaseLock();
221             }
222         }
223         return fo;
224     }
225
226     private static FileObject createFile (FileObject root, String JavaDoc folderName, String JavaDoc name, String JavaDoc body) throws IOException JavaDoc {
227         if (folderName != null) {
228             FileObject tmp = root.getFileObject(folderName,null);
229             if (tmp == null) {
230                 tmp = root.createFolder (folderName);
231             }
232             root = tmp;
233         }
234         FileObject file = root.createData (name,"java");
235         FileLock lock = file.lock();
236         try {
237             PrintWriter JavaDoc out = new PrintWriter JavaDoc ( new OutputStreamWriter JavaDoc (file.getOutputStream(lock)));
238             try {
239                 out.println (body);
240             } finally {
241                 out.close ();
242             }
243         } finally {
244             lock.releaseLock();
245         }
246         return file;
247     }
248     
249     
250     
251     private class SFBQI implements SourceForBinaryQueryImplementation {
252         
253         
254         public SFBQI () {
255         }
256         
257         public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
258             for (int i = 0; i < execRoots.length; i++) {
259                 try {
260                     URL JavaDoc url = execRoots[i].getURL ();
261                     if (url.equals (binaryRoot)) {
262                         return new SourceForBinaryQuery.Result () {
263                     
264                             public FileObject[] getRoots () {
265                                 return libSourceRoots;
266                             }
267                     
268                             public void addChangeListener (ChangeListener JavaDoc l) {
269                             }
270                     
271                             public void removeChangeListener (ChangeListener JavaDoc l) {
272                             }
273                         };
274                     }
275                 } catch (Exception JavaDoc e) {}
276             }
277             return null;
278         }
279     }
280         
281    
282             
283     
284     public synchronized Lookup getLookup() {
285         if (this.lookup == null) {
286             this.lookup = Lookups.fixed (
287                 new Object JavaDoc[] {
288                     new ArchiveURLMapper (),
289                     new MasterURLMapper(),
290                     new JavaPlatformProviderImpl(),
291                     new SFBQI (),
292                 });
293         }
294         return this.lookup;
295     }
296     
297     
298 }
299
Popular Tags