1 19 20 package org.netbeans.modules.apisupport.project.queries; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.zip.ZipEntry ; 29 import java.util.zip.ZipOutputStream ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.java.queries.SourceForBinaryQuery; 33 import org.netbeans.modules.apisupport.project.TestBase; 34 import org.netbeans.modules.apisupport.project.Util; 35 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.URLMapper; 39 40 45 public class GlobalSourceForBinaryImplTest extends TestBase { 46 47 private static final String LOADERS_XML = 50 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 51 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n" + 52 "<type>org.netbeans.modules.apisupport.project</type>\n" + 53 "<configuration>\n" + 54 "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n" + 55 "<code-name-base>org.openide.loaders</code-name-base>\n" + 56 "</data>\n" + 57 "</configuration>\n" + 58 "</project>\n"; 59 60 public GlobalSourceForBinaryImplTest(String name) { 61 super(name); 62 } 63 64 public void testFindSourceRootForZipWithFirstLevelDepthNbBuild() throws Exception { 65 File nbSrcZip = generateNbSrcZip(""); 66 NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip)); 67 68 URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar")); 69 URL loadersSrcURL = new URL (Util.urlForJar(nbSrcZip), "openide/loaders/src/"); 70 assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL)); 71 } 72 73 public void testFindSourceRootForZipWithSecondLevelDepthNbBuild() throws Exception { 74 File nbSrcZip = generateNbSrcZip("netbeans-src/"); 75 NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip)); 76 77 URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar")); 78 URL loadersSrcURL = new URL (Util.urlForJar(nbSrcZip), "netbeans-src/openide/loaders/src/"); 79 assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL)); 80 } 81 82 public void testBehaviourWithNonZipFile() throws Exception { 84 GlobalSourceForBinaryImpl.quiet = true; 85 File nbSrcZip = new File (getWorkDir(), "wrong-nbsrc.zip"); 86 nbSrcZip.createNewFile(); 87 NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip)); 88 URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar")); 89 SourceForBinaryQuery.findSourceRoots(loadersURL).getRoots(); 90 } 91 92 public void testResolveSpecialNBSrcPaths() throws Exception { 93 String [] srcDirs = {"xtest/nbjunit/src", 94 "xtest/nbjunit/ide/src", 95 "performance/insanelib/src"}; 96 String [] xtestJars = {"xtest/lib/nbjunit.jar", 97 "xtest/lib/nbjunit-ide.jar", 98 "xtest/lib/insanelib.jar"}; 99 String [] ideJars = {"testtools/modules/org-netbeans-modules-nbjunit.jar", 100 "testtools/modules/org-netbeans-modules-nbjunit-ide.jar", 101 "testtools/modules/ext/insanelib.jar"}; 102 for (int i = 0; i < srcDirs.length; i++) { 103 if(!file(srcDirs[i]).isDirectory()) { 104 System.err.println("Skipping testResolveSpecialNBSrcPaths since "+srcDirs[i]+" is not checked out"); 105 continue; 106 } 107 assertRoot(Util.urlForJar(file(xtestJars[i])), 108 FileUtil.toFileObject(file(srcDirs[i]))); 109 File jarFile = new File (file("nbbuild/netbeans"), ideJars[i]); 110 if (jarFile.exists()) { 111 assertResolved(ideJars[i], srcDirs[i]); 112 } else { 113 assertEquals("no resolved root", 0, 114 SourceForBinaryQuery.findSourceRoots(Util.urlForJar(jarFile)).getRoots().length); 115 } 116 } 117 } 118 119 private void assertResolved(String jarInNBBuild, String dirInNBSrc) { 120 File jarFile = new File (file("nbbuild/netbeans"), jarInNBBuild); 121 assertRoot(Util.urlForJar(jarFile), FileUtil.toFileObject((file(dirInNBSrc)))); 122 } 123 124 public void testListeningToNbPlatform() throws Exception { 125 File nbSrcZip = generateNbSrcZip(""); 126 URL loadersURL = Util.urlForJar(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM + "/modules/org-openide-loaders.jar")); 127 SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots(loadersURL); 128 assertNotNull("got result", res); 129 ResultChangeListener resultCL = new ResultChangeListener(); 130 res.addChangeListener(resultCL); 131 assertFalse("not changed yet", resultCL.changed); 132 assertEquals("non source root", 0, res.getRoots().length); 133 NbPlatform.getDefaultPlatform().addSourceRoot(Util.urlForJar(nbSrcZip)); 134 assertTrue("changed yet", resultCL.changed); 135 assertEquals("one source root", 1, res.getRoots().length); 136 URL loadersSrcURL = new URL (Util.urlForJar(nbSrcZip), "openide/loaders/src/"); 137 assertRoot(loadersURL, URLMapper.findFileObject(loadersSrcURL)); 138 } 139 140 private File generateNbSrcZip(String topLevelEntry) throws IOException { 141 File zip = new File (getWorkDir(), "nbsrc.zip"); 142 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zip)); 143 if (topLevelEntry.length() != 0) { 144 zos.putNextEntry(new ZipEntry (topLevelEntry)); 145 } 146 zos.putNextEntry(new ZipEntry (topLevelEntry + "nbbuild/")); 147 zos.putNextEntry(new ZipEntry (topLevelEntry + "nbbuild/nbproject/")); 148 zos.putNextEntry(new ZipEntry (topLevelEntry + "nbbuild/nbproject/project.xml")); 149 zos.putNextEntry(new ZipEntry (topLevelEntry + "openide/")); 150 zos.putNextEntry(new ZipEntry (topLevelEntry + "openide/loaders/")); 151 zos.putNextEntry(new ZipEntry (topLevelEntry + "openide/loaders/src/")); 152 zos.putNextEntry(new ZipEntry (topLevelEntry + "openide/loaders/nbproject/")); 153 ZipEntry loadersXML = new ZipEntry (topLevelEntry + "openide/loaders/nbproject/project.xml"); 154 zos.putNextEntry(loadersXML); 155 try { 156 zos.write(LOADERS_XML.getBytes()); 157 } finally { 158 zos.close(); 159 } 160 return zip; 161 } 162 163 private static void assertRoot(final URL loadersURL, final FileObject loadersSrcFO) { 164 assertEquals("right results for " + loadersURL, 165 Collections.singletonList(loadersSrcFO), 166 Arrays.asList(SourceForBinaryQuery.findSourceRoots(loadersURL).getRoots())); 167 } 168 169 private static final class ResultChangeListener implements ChangeListener { 170 171 private boolean changed; 172 173 public void stateChanged(ChangeEvent e) { 174 changed = true; 175 } 176 177 } 178 179 } 180 | Popular Tags |