KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > libraries > SourceForBinaryQueryLibraryImplTest


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.libraries;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.jar.JarOutputStream JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.java.queries.SourceForBinaryQuery;
33 import org.netbeans.api.project.TestUtil;
34 import org.netbeans.core.startup.layers.ArchiveURLMapper;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.java.j2seplatform.platformdefinition.JavaPlatformProviderImpl;
37 import org.netbeans.modules.project.libraries.DefaultLibraryImplementation;
38 import org.netbeans.spi.project.libraries.LibraryImplementation;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.util.Lookup;
42 import org.openide.util.Utilities;
43 import org.openide.util.lookup.Lookups;
44 import org.netbeans.modules.masterfs.MasterURLMapper;
45
46
47 /**
48  * J2SELibrarySourceForBinaryQuery test
49  *
50  */

51 public class SourceForBinaryQueryLibraryImplTest extends NbTestCase implements Lookup.Provider {
52     
53     private Lookup lookup;
54     
55     
56     public SourceForBinaryQueryLibraryImplTest(java.lang.String JavaDoc testName) {
57         super(testName);
58         TestUtil.setLookup(Lookups.proxy(this));
59     }
60     
61     private String JavaDoc getBase() throws Exception JavaDoc {
62         File JavaDoc dir = getWorkDir();
63         if (Utilities.isWindows()) {
64             dir = new File JavaDoc(dir.getCanonicalPath());
65         }
66         return dir.toString();
67     }
68     
69     protected void setUp() throws Exception JavaDoc {
70         System.setProperty("netbeans.user", getWorkDirPath());
71         super.setUp();
72         clearWorkDir();
73     }
74     
75     private void setupLibraries() throws Exception JavaDoc {
76         File JavaDoc dir = new File JavaDoc(getBase());
77         
78         // create library1:
79
String JavaDoc libPath = dir.toString() + "/library1";
80         File JavaDoc library = createJar(new File JavaDoc(libPath), "library1.jar", new String JavaDoc[]{"Main.class"});
81         File JavaDoc src = new File JavaDoc(libPath+"/src1");
82         src.mkdir();
83         registerLibrary("library1", library, src);
84         
85         // create library2:
86
libPath = dir.toString() + "/library2";
87         library = createJar(new File JavaDoc(libPath), "library2.jar", new String JavaDoc[]{"Main.class"});
88         src = createJar(new File JavaDoc(libPath), "library2src.jar", new String JavaDoc[]{"Main.java"});
89         registerLibrary("library2", library, src);
90         
91         // create library3:
92
libPath = dir.toString() + "/library3";
93         library = new File JavaDoc(libPath+"/library3");
94         library.mkdirs();
95         src = new File JavaDoc(libPath+"/src3");
96         src.mkdirs();
97         registerLibrary("library3", library, src);
98                                 
99         // refresh FS
100
FileUtil.toFileObject(dir).getFileSystem().refresh(false);
101     }
102     
103     private void setupLibraryForListeningTest () throws Exception JavaDoc {
104         // create library4:
105
File JavaDoc dir = new File JavaDoc(getBase());
106         String JavaDoc libPath = dir.toString() + "/library4";
107         File JavaDoc library = new File JavaDoc(libPath,"library4");
108         library.mkdirs();
109         File JavaDoc src = new File JavaDoc(libPath+"/src4");
110         src.mkdirs();
111         registerLibrary("library4", library, null);
112         // refresh FS
113
FileUtil.toFileObject(dir).getFileSystem().refresh(false);
114     }
115     
116     private File JavaDoc createJar(File JavaDoc folder, String JavaDoc name, String JavaDoc resources[]) throws Exception JavaDoc {
117         folder.mkdirs();
118         File JavaDoc f = new File JavaDoc(folder,name);
119         if (!f.exists()) {
120             f.createNewFile();
121         }
122         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
123         for (int i = 0; i < resources.length; i++) {
124             jos.putNextEntry(new ZipEntry JavaDoc(resources[i]));
125         }
126         jos.close();
127         return f;
128     }
129     
130     private void registerLibrary(final String JavaDoc libName, final File JavaDoc cp, final File JavaDoc src) throws Exception JavaDoc {
131         DefaultLibraryImplementation lib;
132         lib = new DefaultLibraryImplementation("j2se", new String JavaDoc[]{"classpath", "src"});
133         lib.setName(libName);
134         ArrayList JavaDoc l = new ArrayList JavaDoc();
135         URL JavaDoc u = cp.toURI().toURL();
136         if (cp.getPath().endsWith(".jar")) {
137             u = FileUtil.getArchiveRoot(u);
138         }
139         l.add(u);
140         lib.setContent("classpath", l);
141         if (src != null) {
142             l = new ArrayList JavaDoc();
143             u = src.toURI().toURL();
144             if (src.getPath().endsWith(".jar")) {
145                 u = FileUtil.getArchiveRoot(u);
146             }
147             l.add(u);
148             lib.setContent("src", l);
149         }
150         LibraryProviderImpl prov = LibraryProviderImpl.getDefault();
151         prov.addLibrary(lib);
152     }
153     
154     private LibraryImplementation getLibrary (String JavaDoc name) {
155         LibraryProviderImpl prov = LibraryProviderImpl.getDefault();
156         LibraryImplementation[] impls = prov.getLibraries();
157         for (int i=0; i< impls.length; i++) {
158             if (impls[i].getName().equals (name)) {
159                 return impls[i];
160             }
161         }
162         return null;
163     }
164     
165     
166     public void testQuery() throws Exception JavaDoc {
167         setupLibraries();
168         
169         // library1: test that folder with javadoc is found for the jar
170
File JavaDoc f = new File JavaDoc(getBase()+"/library1/library1.jar");
171         URL JavaDoc u = f.toURI().normalize().toURL();
172         u = FileUtil.getArchiveRoot(u);
173         FileObject[] fos = SourceForBinaryQuery.findSourceRoots(u).getRoots();
174         assertEquals(1, fos.length);
175         String JavaDoc base = new File JavaDoc(getBase()).toURI().toString();
176         assertEquals(base+"library1/src1/", fos[0].getURL().toExternalForm());
177         
178         // library2: test that jar with javadoc is found for the class from library jar
179
f = new File JavaDoc(getBase()+"/library2/library2.jar");
180         String JavaDoc us = f.toURI().normalize().toString();
181         us = "jar:" + us + "!/";
182         u = new URL JavaDoc(us);
183         fos = SourceForBinaryQuery.findSourceRoots(u).getRoots();
184         assertEquals(1, fos.length);
185         assertEquals("jar:"+base+"library2/library2src.jar!/", fos[0].getURL().toExternalForm());
186         
187         // library2: test that folder with javadoc is found for the classpath root from the library
188
f = new File JavaDoc(getBase()+"/library3/library3");
189         u = f.toURI().normalize().toURL();
190         fos = SourceForBinaryQuery.findSourceRoots(u).getRoots();
191         assertEquals(1, fos.length);
192         assertEquals(base+"library3/src3/", fos[0].getURL().toExternalForm());
193     }
194     
195     public void testListening () throws Exception JavaDoc {
196         setupLibraryForListeningTest();
197         File JavaDoc f = new File JavaDoc(getBase()+"/library4");
198         f = new File JavaDoc (f, "library4");
199         URL JavaDoc u = f.toURI().normalize().toURL();
200         SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(u);
201         assertEquals(result.getRoots().length,0);
202         SFBQResultListener l = new SFBQResultListener ();
203         result.addChangeListener(l);
204         LibraryImplementation impl = getLibrary("library4");
205         List JavaDoc srcList = new ArrayList JavaDoc ();
206         File JavaDoc baseDir = new File JavaDoc(getBase());
207         File JavaDoc libDir = new File JavaDoc(baseDir,"library4");
208         File JavaDoc srcDir = new File JavaDoc(libDir,"src4");
209         srcList.add (srcDir.toURI().toURL());
210         impl.setContent("src", srcList);
211         ChangeEvent JavaDoc[] events = l.getEvents();
212         assertEquals(1,events.length);
213         l.clearEventQueue();
214         assertEquals(result.getRoots().length,1);
215         String JavaDoc base = new File JavaDoc(getBase()).toURI().toString();
216         assertEquals(base+"library4/src4/",result.getRoots()[0].getURL().toExternalForm());
217     }
218     
219     public synchronized Lookup getLookup() {
220         if (this.lookup == null) {
221             this.lookup = Lookups.fixed (
222                 new Object JavaDoc[] {
223                     LibraryProviderImpl.getDefault(),
224                     new JavaPlatformProviderImpl (),
225                     new ArchiveURLMapper (),
226                     new J2SELibrarySourceForBinaryQuery (),
227                     new MasterURLMapper(),
228                 });
229         }
230         return this.lookup;
231     }
232     
233     
234     private static class SFBQResultListener implements ChangeListener JavaDoc {
235         
236         private List JavaDoc queue;
237         
238         public SFBQResultListener () {
239             this.queue = new ArrayList JavaDoc ();
240         }
241         
242         public void clearEventQueue () {
243             this.queue.clear();
244         }
245
246         public void stateChanged(javax.swing.event.ChangeEvent JavaDoc event) {
247             this.queue.add (event);
248         }
249         
250         public ChangeEvent JavaDoc[] getEvents () {
251             return (ChangeEvent JavaDoc[]) this.queue.toArray(new ChangeEvent JavaDoc[this.queue.size()]);
252         }
253     }
254     
255 }
256
257
Popular Tags