1 19 20 package org.netbeans.modules.java.j2seplatform.libraries; 21 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.jar.JarOutputStream ; 29 import java.util.zip.ZipEntry ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 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 51 public class SourceForBinaryQueryLibraryImplTest extends NbTestCase implements Lookup.Provider { 52 53 private Lookup lookup; 54 55 56 public SourceForBinaryQueryLibraryImplTest(java.lang.String testName) { 57 super(testName); 58 TestUtil.setLookup(Lookups.proxy(this)); 59 } 60 61 private String getBase() throws Exception { 62 File dir = getWorkDir(); 63 if (Utilities.isWindows()) { 64 dir = new File (dir.getCanonicalPath()); 65 } 66 return dir.toString(); 67 } 68 69 protected void setUp() throws Exception { 70 System.setProperty("netbeans.user", getWorkDirPath()); 71 super.setUp(); 72 clearWorkDir(); 73 } 74 75 private void setupLibraries() throws Exception { 76 File dir = new File (getBase()); 77 78 String libPath = dir.toString() + "/library1"; 80 File library = createJar(new File (libPath), "library1.jar", new String []{"Main.class"}); 81 File src = new File (libPath+"/src1"); 82 src.mkdir(); 83 registerLibrary("library1", library, src); 84 85 libPath = dir.toString() + "/library2"; 87 library = createJar(new File (libPath), "library2.jar", new String []{"Main.class"}); 88 src = createJar(new File (libPath), "library2src.jar", new String []{"Main.java"}); 89 registerLibrary("library2", library, src); 90 91 libPath = dir.toString() + "/library3"; 93 library = new File (libPath+"/library3"); 94 library.mkdirs(); 95 src = new File (libPath+"/src3"); 96 src.mkdirs(); 97 registerLibrary("library3", library, src); 98 99 FileUtil.toFileObject(dir).getFileSystem().refresh(false); 101 } 102 103 private void setupLibraryForListeningTest () throws Exception { 104 File dir = new File (getBase()); 106 String libPath = dir.toString() + "/library4"; 107 File library = new File (libPath,"library4"); 108 library.mkdirs(); 109 File src = new File (libPath+"/src4"); 110 src.mkdirs(); 111 registerLibrary("library4", library, null); 112 FileUtil.toFileObject(dir).getFileSystem().refresh(false); 114 } 115 116 private File createJar(File folder, String name, String resources[]) throws Exception { 117 folder.mkdirs(); 118 File f = new File (folder,name); 119 if (!f.exists()) { 120 f.createNewFile(); 121 } 122 JarOutputStream jos = new JarOutputStream (new FileOutputStream (f)); 123 for (int i = 0; i < resources.length; i++) { 124 jos.putNextEntry(new ZipEntry (resources[i])); 125 } 126 jos.close(); 127 return f; 128 } 129 130 private void registerLibrary(final String libName, final File cp, final File src) throws Exception { 131 DefaultLibraryImplementation lib; 132 lib = new DefaultLibraryImplementation("j2se", new String []{"classpath", "src"}); 133 lib.setName(libName); 134 ArrayList l = new ArrayList (); 135 URL 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 (); 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 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 { 167 setupLibraries(); 168 169 File f = new File (getBase()+"/library1/library1.jar"); 171 URL u = f.toURI().normalize().toURL(); 172 u = FileUtil.getArchiveRoot(u); 173 FileObject[] fos = SourceForBinaryQuery.findSourceRoots(u).getRoots(); 174 assertEquals(1, fos.length); 175 String base = new File (getBase()).toURI().toString(); 176 assertEquals(base+"library1/src1/", fos[0].getURL().toExternalForm()); 177 178 f = new File (getBase()+"/library2/library2.jar"); 180 String us = f.toURI().normalize().toString(); 181 us = "jar:" + us + "!/"; 182 u = new URL (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 f = new File (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 { 196 setupLibraryForListeningTest(); 197 File f = new File (getBase()+"/library4"); 198 f = new File (f, "library4"); 199 URL 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 srcList = new ArrayList (); 206 File baseDir = new File (getBase()); 207 File libDir = new File (baseDir,"library4"); 208 File srcDir = new File (libDir,"src4"); 209 srcList.add (srcDir.toURI().toURL()); 210 impl.setContent("src", srcList); 211 ChangeEvent [] events = l.getEvents(); 212 assertEquals(1,events.length); 213 l.clearEventQueue(); 214 assertEquals(result.getRoots().length,1); 215 String base = new File (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 [] { 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 { 235 236 private List queue; 237 238 public SFBQResultListener () { 239 this.queue = new ArrayList (); 240 } 241 242 public void clearEventQueue () { 243 this.queue.clear(); 244 } 245 246 public void stateChanged(javax.swing.event.ChangeEvent event) { 247 this.queue.add (event); 248 } 249 250 public ChangeEvent [] getEvents () { 251 return (ChangeEvent []) this.queue.toArray(new ChangeEvent [this.queue.size()]); 252 } 253 } 254 255 } 256 257 | Popular Tags |