1 19 20 package org.netbeans.modules.java.j2seplatform.libraries; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.jar.JarOutputStream ; 27 import java.util.zip.ZipEntry ; 28 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 29 import org.netbeans.api.project.TestUtil; 30 import org.netbeans.core.startup.layers.ArchiveURLMapper; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.modules.java.j2seplatform.platformdefinition.JavaPlatformProviderImpl; 33 import org.netbeans.modules.project.libraries.DefaultLibraryImplementation; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.Lookup; 36 import org.openide.util.Utilities; 37 import org.openide.util.lookup.Lookups; 38 import org.netbeans.modules.masterfs.MasterURLMapper; 39 40 42 47 public class JavadocForBinaryQueryLibraryImplTest extends NbTestCase implements Lookup.Provider { 48 49 private Lookup lookup; 50 51 52 public JavadocForBinaryQueryLibraryImplTest(java.lang.String testName) { 53 super(testName); 54 TestUtil.setLookup(Lookups.proxy(this)); 55 } 56 57 private String getBase() throws Exception { 58 File dir = getWorkDir(); 59 if (Utilities.isWindows()) { 60 dir = new File (dir.getCanonicalPath()); 61 } 62 return dir.toString(); 63 } 64 65 protected void setUp() throws Exception { 66 System.setProperty("netbeans.user", getWorkDirPath()); 67 super.setUp(); 68 clearWorkDir(); 69 } 70 71 private void setupLibraries() throws Exception { 72 File dir = new File (getBase()); 73 74 String libPath = dir.toString() + "/library1"; 76 File library = createJar(new File (libPath), "library1.jar", new String []{"Main.class"}); 77 File javadoc = new File (libPath+"/javadoc1"); 78 javadoc.mkdir(); 79 registerLibrary("library1", library, javadoc); 80 81 libPath = dir.toString() + "/library2"; 83 library = createJar(new File (libPath), "library2.jar", new String []{"Main.class"}); 84 javadoc = createJar(new File (libPath), "library2javadoc.jar", new String []{"index.html"}); 85 registerLibrary("library2", library, javadoc); 86 87 libPath = dir.toString() + "/library3"; 89 library = new File (libPath+"/library3"); 90 library.mkdirs(); 91 javadoc = new File (libPath+"/javadoc3"); 92 javadoc.mkdirs(); 93 registerLibrary("library3", library, javadoc); 94 95 FileUtil.toFileObject(dir).getFileSystem().refresh(false); 97 } 98 99 private File createJar(File folder, String name, String resources[]) throws Exception { 100 folder.mkdirs(); 101 File f = new File (folder,name); 102 if (!f.exists()) { 103 f.createNewFile(); 104 } 105 JarOutputStream jos = new JarOutputStream (new FileOutputStream (f)); 106 for (int i = 0; i < resources.length; i++) { 107 jos.putNextEntry(new ZipEntry (resources[i])); 108 } 109 jos.close(); 110 return f; 111 } 112 113 private void registerLibrary(final String libName, final File cp, final File javadoc) throws Exception { 114 DefaultLibraryImplementation lib; 115 lib = new DefaultLibraryImplementation("j2se", new String []{"classpath", "javadoc"}); 116 ArrayList l = new ArrayList (); 117 URL u = cp.toURI().toURL(); 118 if (cp.getPath().endsWith(".jar")) { 119 u = FileUtil.getArchiveRoot(u); 120 } 121 l.add(u); 122 lib.setContent("classpath", l); 123 l = new ArrayList (); 124 u = javadoc.toURI().toURL(); 125 if (javadoc.getPath().endsWith(".jar")) { 126 u = FileUtil.getArchiveRoot(u); 127 } 128 l.add(u); 129 lib.setContent("javadoc", l); 130 LibraryProviderImpl prov = LibraryProviderImpl.getDefault(); 131 prov.addLibrary(lib); 132 } 133 134 public void testQuery() throws Exception { 135 setupLibraries(); 136 137 File f = new File (getBase()+"/library1/library1.jar"); 139 URL u = f.toURI().normalize().toURL(); 140 u = FileUtil.getArchiveRoot(u); 141 URL urls[] = JavadocForBinaryQuery.findJavadoc(u).getRoots(); 142 assertEquals(1, urls.length); 143 String base = new File (getBase()).toURI().toString(); 144 assertEquals(base+"library1/javadoc1/", urls[0].toExternalForm()); 145 146 f = new File (getBase()+"/library2/library2.jar"); 148 String us = f.toURI().normalize().toString(); 149 us = "jar:" + us + "!/"; 150 u = new URL (us); 151 urls = JavadocForBinaryQuery.findJavadoc(u).getRoots(); 152 assertEquals(1, urls.length); 153 assertEquals("jar:"+base+"library2/library2javadoc.jar!/", urls[0].toExternalForm()); 154 155 f = new File (getBase()+"/library3/library3"); 157 u = f.toURI().normalize().toURL(); 158 urls = JavadocForBinaryQuery.findJavadoc(u).getRoots(); 159 assertEquals(1, urls.length); 160 assertEquals(base+"library3/javadoc3/", urls[0].toExternalForm()); 161 } 162 163 public synchronized Lookup getLookup() { 164 if (this.lookup == null) { 165 this.lookup = Lookups.fixed ( 166 new Object [] { 167 LibraryProviderImpl.getDefault(), 168 new JavaPlatformProviderImpl (), 169 new ArchiveURLMapper (), 170 new JavadocForBinaryQueryLibraryImpl(), 171 new MasterURLMapper(), 172 }); 173 } 174 return this.lookup; 175 } 176 177 } 178 | Popular Tags |