protected @Override void setUp() throws Exception { MockLookup.setLayersAndInstances(TestUtil.testProjectFactory()); ProjectManager.getDefault().reset(); FileOwnerQuery.reset(); scratch = TestUtil.makeScratchDir(this); projdir = scratch.createFolder("my-project"); projdir.createFolder("testproject"); randomfile = scratch.createData("randomfile"); projfile = projdir.createData("projfile"); FileObject projsubdir = projdir.createFolder("projsubdir"); projfile2 = projsubdir.createData("projfile2"); subprojdir = projdir.createFolder("subproject"); subprojdir.createFolder("testproject"); subprojfile = subprojdir.createData("subprojfile"); scratch.createFolder("external1").createFolder("subdir").createData("file"); scratch.createFolder("external2").createFolder("subdir").createData("file"); scratch.createFolder("external3").createFolder("subproject").createFolder("testproject"); p = ProjectManager.getDefault().findProject(projdir); assertNotNull("found a project successfully", p); // make jar:file:/.../projdir/foo.jar!/zipfile/zippedfile FileObject foojar = TestFileUtils.writeZipFile(projdir, "foo.jar", "zipdir/zippedfile:"); FileObject foojarRoot = FileUtil.getArchiveRoot(foojar); assertNotNull("have an archive in " + foojar, foojarRoot); zippedfile = foojarRoot.getFileObject("zipdir/zippedfile"); assertNotNull("zippedfile found in it", zippedfile); hashedFile = TestFileUtils.writeZipFile(projdir, ".#webapp.jar.1.45", "zipdir/zippedfile:"); foojarRoot = FileUtil.getArchiveRoot(hashedFile); assertNotNull("have an archive in " + hashedFile, foojarRoot); hashedFile = foojarRoot.getFileObject("zipdir/zippedfile"); }
private FileObject getArchivedFile(FileObject fileObject) { // ZIP and JAR archives if (FileUtil.isArchiveFile(fileObject)) { try { fileObject = FileUtil.getArchiveRoot(fileObject).getChildren()[0]; } catch (Exception e) { throw new RuntimeException( "The archive can't be opened, be sure it has no password and contains a single file, without folders"); } } else { // GZ or BZIP2 archives boolean isGz = fileObject.getExt().equalsIgnoreCase("gz"); boolean isBzip = fileObject.getExt().equalsIgnoreCase("bz2"); if (isGz || isBzip) { try { String[] splittedFileName = fileObject.getName().split("\\."); if (splittedFileName.length < 2) { return fileObject; } String fileExt1 = splittedFileName[splittedFileName.length - 1]; String fileExt2 = splittedFileName[splittedFileName.length - 2]; File tempFile = null; if (fileExt1.equalsIgnoreCase("tar")) { String fname = fileObject.getName().replaceAll("\\.tar$", ""); fname = fname.replace(fileExt2, ""); tempFile = File.createTempFile(fname, "." + fileExt2); // Untar & unzip if (isGz) { tempFile = getGzFile(fileObject, tempFile, true); } else { tempFile = getBzipFile(fileObject, tempFile, true); } } else { String fname = fileObject.getName(); fname = fname.replace(fileExt1, ""); tempFile = File.createTempFile(fname, "." + fileExt1); // Unzip if (isGz) { tempFile = getGzFile(fileObject, tempFile, false); } else { tempFile = getBzipFile(fileObject, tempFile, false); } } tempFile.deleteOnExit(); tempFile = FileUtil.normalizeFile(tempFile); fileObject = FileUtil.toFileObject(tempFile); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } } return fileObject; }
private String createSpecURL() { File file = InstalledFileLocator.getDefault() .locate(SPEC_ARCHIVE_NAME, "org.netbeans.modules.css.editor", false); // NoI18N if (file != null) { try { URL urll = file.toURI().toURL(); // toURI should escape the illegal characters like spaces assert FileUtil.isArchiveFile(urll); return FileUtil.getArchiveRoot(urll).toExternalForm(); } catch (java.net.MalformedURLException e) { // should not happen LOGGER.log( Level.SEVERE, String.format("Error obtaining archive root URL for file '%s'", file.getAbsolutePath()), e); // NOI18N } } else { LOGGER.warning( String.format( "Cannot locate the css documentation file '%s'.", SPEC_ARCHIVE_NAME)); // NOI18N } return null; }
@Override public URL[] getRoots() { ResolvedDependencies rd = rd(); if (rd == null) { // corrupt project metadata return new URL[0]; } ResolvedDependency d = rd.get(dep.getID()); if (d == null) { // temporary workaround for mis-created project deps Logger.getLogger(DependenciesClasspathImpl.class.getName()) .log(Level.WARNING, "Unresolvable dependency {0} in {1}", new Object[] {dep, get()}); return new URL[0]; } assert d != null : "Dependency " + dep + " resolves to null"; List<URL> urls = new ArrayList<URL>(); File f = d.resolveFile(ArtifactKind.ORIGIN); if (f != null) { if (d.getKind().isProjectDependency()) { FileObject fo = FileUtil.toFileObject(f); if (fo != null) { Project p = FileOwnerQuery.getOwner(fo); if (p != null) { try { URL url = p.getProjectDirectory().getURL(); AntArtifactProvider prov = p.getLookup().lookup(AntArtifactProvider.class); for (AntArtifact a : prov.getBuildArtifacts()) { if (JavaProjectConstants.ARTIFACT_TYPE_JAR.equals(a.getType())) { URI[] uris = a.getArtifactLocations(); for (URI u : uris) { url = new URL(u.toString()); if (FileUtil.isArchiveFile(url)) { url = FileUtil.getArchiveRoot(url); urls.add(url); } } } } } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); } catch (FileStateInvalidException ex) { Exceptions.printStackTrace(ex); } } } } else { if (f != null) { try { URL url = f.toURI().toURL(); if (FileUtil.isArchiveFile(url)) { url = FileUtil.getArchiveRoot(url); } urls.add(url); } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); } } } } return urls.toArray(new URL[urls.size()]); }
public void testForeignClassBundler() throws Exception { // #155091 and deps TestFileUtils.writeFile( d, "a/pom.xml", "<project xmlns='http://maven.apache.org/POM/4.0.0'>" + "<modelVersion>4.0.0</modelVersion>" + "<groupId>grp</groupId>" + "<artifactId>art</artifactId>" + "<packaging>jar</packaging>" + "<version>0</version>" + "</project>"); FileObject src = FileUtil.createFolder(d, "a/src/main/java"); File repo = EmbedderFactory.getProjectEmbedder().getLocalRepositoryFile(); File art = new File( repo, "grp" + File.separator + "art" + File.separator + "0" + File.separator + "art-0.jar"); URL root = FileUtil.getArchiveRoot(Utilities.toURI(art).toURL()); Project p = ProjectManager.getDefault().findProject(d.getFileObject("a")); FileOwnerQuery.markExternalOwner( Utilities.toURI(art), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); MavenFileOwnerQueryImpl.getInstance() .registerCoordinates("grp", "art", "0", d.getFileObject("a").toURL()); SourceForBinaryQuery.Result2 r = SourceForBinaryQuery.findSourceRoots2(root); assertEquals(Collections.singletonList(src), Arrays.asList(r.getRoots())); assertTrue(r.preferSources()); TestFileUtils.writeFile( d, "b/pom.xml", "<project xmlns='http://maven.apache.org/POM/4.0.0'>" + "<modelVersion>4.0.0</modelVersion>" + "<groupId>grp</groupId>" + "<artifactId>art</artifactId>" + "<packaging>war</packaging>" + "<version>0</version>" + "</project>"); src = FileUtil.createFolder(d, "b/src/main/java"); art = new File( repo, "grp" + File.separator + "art" + File.separator + "0" + File.separator + "art-0.jar"); FileOwnerQuery.markExternalOwner( Utilities.toURI(art), ProjectManager.getDefault().findProject(d.getFileObject("b")), FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); root = FileUtil.getArchiveRoot(Utilities.toURI(art).toURL()); p = ProjectManager.getDefault().findProject(d.getFileObject("b")); FileOwnerQuery.markExternalOwner( Utilities.toURI(art), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); MavenFileOwnerQueryImpl.getInstance() .registerCoordinates("grp", "art", "0", d.getFileObject("b").toURL()); r = SourceForBinaryQuery.findSourceRoots2(root); assertEquals(Collections.singletonList(src), Arrays.asList(r.getRoots())); assertFalse(r.preferSources()); // 215242 assert that output dir does prefer sources r = SourceForBinaryQuery.findSourceRoots2(new URL(d.toURL(), "b/target/classes/")); assertEquals(Collections.singletonList(src), Arrays.asList(r.getRoots())); assertTrue(r.preferSources()); }