public void testExternalOwnerDisappearingProject() throws Exception { FileObject ext1 = scratch.getFileObject("external1"); FileObject tempPrjMarker = FileUtil.createFolder(scratch, "tempprj/testproject"); FileObject tempPrjDir = tempPrjMarker.getParent(); Project tempPrj = ProjectManager.getDefault().findProject(tempPrjDir); assertNotNull(tempPrj); FileOwnerQuery.markExternalOwner( ext1.toURI(), tempPrj, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner", tempPrj, FileOwnerQuery.getOwner(ext1)); Reference<FileObject> r = new WeakReference<FileObject>(tempPrjDir); tempPrjMarker = tempPrjDir = null; tempPrj = null; assertGC("can be GCed", r); assertNotNull("still has an owner", FileOwnerQuery.getOwner(ext1)); }
public void testIsProjectDirCollectible() throws Exception { Project p2 = ProjectManager.getDefault().findProject(subprojdir); FileObject root = p2.getProjectDirectory(); FileObject ext2 = scratch.getFileObject("external2"); FileObject extfile2 = ext2.getFileObject("subdir/file"); FileOwnerQuery.markExternalOwner( extfile2.toURI(), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); Reference<?> p2WR = new WeakReference<Object>(p2); Reference<?> rootWR = new WeakReference<Object>(root); p2 = null; root = null; ext2 = null; extfile2 = null; subprojdir = null; subprojfile = null; assertGC("project 2 collected", p2WR); assertGC("project 2's project dir collected", rootWR); }
public static List<ErrorDescription> runFindBugs( CompilationInfo info, Preferences customSettings, String singleBug, FileObject sourceRoot, Iterable<? extends String> classNames, FindBugsProgress progress, SigFilesValidator validator) { List<ErrorDescription> result = new ArrayList<ErrorDescription>(); try { Class.forName( "org.netbeans.modules.findbugs.NbClassFactory", true, RunFindBugs.class.getClassLoader()); // NOI18N Project p = new Project(); URL[] binaryRoots = CacheBinaryForSourceQuery.findCacheBinaryRoots(sourceRoot.toURL()).getRoots(); if (classNames == null) { for (URL binary : binaryRoots) { try { p.addFile(new File(binary.toURI()).getAbsolutePath()); } catch (URISyntaxException ex) { Exceptions.printStackTrace(ex); } } } else { ClassPath binary = ClassPathSupport.createClassPath(binaryRoots); List<FileObject> sigFiles = new ArrayList<FileObject>(); for (String className : classNames) { FileObject classFO = binary.findResource(className.replace('.', '/') + ".sig"); // NOI18N if (classFO != null) { sigFiles.add(classFO); } else { LOG.log( Level.WARNING, "Cannot find sig file for: " + className); // TODO: should probably become FINE eventually } } assert validator != null; if (!validator.validate(sigFiles)) return null; for (FileObject classFO : sigFiles) { p.addFile(new File(classFO.toURI()).getAbsolutePath()); } addCompileRootAsSource(p, sourceRoot); } ClassPath compile = ClassPath.getClassPath(sourceRoot, ClassPath.COMPILE); for (FileObject compileRoot : compile.getRoots()) { addCompileRoot(p, compileRoot); } BugCollectionBugReporter r = new BugCollectionBugReporter(p) { @Override protected void emitLine(String line) { LOG.log(Level.FINE, line); } }; r.setPriorityThreshold(Integer.MAX_VALUE); r.setRankThreshold(Integer.MAX_VALUE); FindBugs2 engine = new FindBugs2(); engine.setProject(p); engine.setNoClassOk(true); engine.setBugReporter(r); if (progress != null) { engine.setProgressCallback(progress); } boolean inEditor = validator != null; Preferences settings = customSettings != null ? customSettings : NbPreferences.forModule(RunFindBugs.class).node("global-settings"); UserPreferences preferences; if (singleBug != null) { singleBug = singleBug.substring(PREFIX_FINDBUGS.length()); preferences = forSingleBug(singleBug); } else { preferences = readPreferences(settings, customSettings != null); } if (preferences == null) { // nothing enabled, stop return result; } engine.setUserPreferences(preferences); engine.setDetectorFactoryCollection(DetectorFactoryCollection.instance()); LOG.log(Level.FINE, "Running FindBugs"); engine.execute(); Map<FileObject, List<BugInstance>> file2Bugs = new HashMap<FileObject, List<BugInstance>>(); for (BugInstance b : r.getBugCollection().getCollection()) { if (singleBug != null && !singleBug.equals(b.getBugPattern().getType())) continue; if (singleBug == null && !settings.getBoolean( b.getBugPattern().getType(), customSettings == null && isEnabledByDefault(b.getBugPattern()))) { continue; } SourceLineAnnotation sourceLine = b.getPrimarySourceLineAnnotation(); FileObject sourceFile = null; if (sourceLine != null) { sourceFile = sourceRoot.getFileObject(sourceLine.getSourcePath()); if (sourceFile != null) { List<BugInstance> bugs = file2Bugs.get(sourceFile); if (bugs == null) { file2Bugs.put(sourceFile, bugs = new ArrayList<BugInstance>()); } bugs.add(b); } else { LOG.log( Level.WARNING, "{0}, location: {1}:{2}", new Object[] {b, sourceLine.getSourcePath(), sourceLine.getStartLine()}); } } } for (Entry<FileObject, List<BugInstance>> e : file2Bugs.entrySet()) { int[] lineOffsets = null; FileObject sourceFile = e.getKey(); DataObject d = DataObject.find(sourceFile); EditorCookie ec = d.getLookup().lookup(EditorCookie.class); Document doc = ec.getDocument(); JavaSource js = null; for (BugInstance b : e.getValue()) { SourceLineAnnotation sourceLine = b.getPrimarySourceLineAnnotation(); if (sourceLine.getStartLine() >= 0) { LazyFixList fixes = prepareFixes(b, inEditor, sourceFile, sourceLine.getStartLine(), null); if (doc != null) { result.add( ErrorDescriptionFactory.createErrorDescription( PREFIX_FINDBUGS + b.getType(), Severity.VERIFIER, b.getMessageWithoutPrefix(), b.getBugPattern().getDetailHTML(), fixes, doc, sourceLine.getStartLine())); } else { if (lineOffsets == null) { lineOffsets = computeLineMap(sourceFile, FileEncodingQuery.getEncoding(sourceFile)); } int edLine = 2 * (Math.min(sourceLine.getStartLine(), lineOffsets.length / 2) - 1); result.add( ErrorDescriptionFactory.createErrorDescription( PREFIX_FINDBUGS + b.getType(), Severity.VERIFIER, b.getMessageWithoutPrefix(), b.getBugPattern().getDetailHTML(), fixes, sourceFile, lineOffsets[edLine], lineOffsets[edLine + 1])); } } else { if (js == null) { js = JavaSource.forFileObject(sourceFile); } addByElementAnnotation(b, info, sourceFile, js, result, inEditor); } } } } catch (ClassNotFoundException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } catch (InterruptedException ex) { LOG.log(Level.FINE, null, ex); } return result; }
public void testExternalOwnerFileURI() throws Exception { FileObject ext1 = scratch.getFileObject("external1"); FileObject extfile1 = ext1.getFileObject("subdir/file"); assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile1)); assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(extfile1.toURI())); FileOwnerQuery.markExternalOwner( extfile1.toURI(), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(extfile1.toURI())); assertEquals("not for the projdir through FileObjects", null, FileOwnerQuery.getOwner(ext1)); assertEquals("not for the projdir through URI", null, FileOwnerQuery.getOwner(ext1.toURI())); assertEquals( "and not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); assertEquals( "and not for something else through URI", null, FileOwnerQuery.getOwner(scratch.toURI())); FileObject ext2 = scratch.getFileObject("external2"); FileObject extfile2 = ext2.getFileObject("subdir/file"); assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile2)); assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(extfile2.toURI())); Project p2 = ProjectManager.getDefault().findProject(subprojdir); FileOwnerQuery.markExternalOwner( extfile2.toURI(), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner through FileObjects", p2, FileOwnerQuery.getOwner(extfile2)); assertEquals("now have an owner through URI", p2, FileOwnerQuery.getOwner(extfile2.toURI())); assertEquals("not for the projdir through FileObjects", null, FileOwnerQuery.getOwner(ext2)); assertEquals("not for the projdir through URI", null, FileOwnerQuery.getOwner(ext2.toURI())); assertEquals( "and not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); assertEquals( "and not for something else through URI", null, FileOwnerQuery.getOwner(scratch.toURI())); assertEquals( "still correct for first proj through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); assertEquals( "still correct for first proj through URI", p, FileOwnerQuery.getOwner(extfile1.toURI())); // XXX: unmarking files. }
public void testExternalOwnerURI() throws Exception { FileObject ext1 = scratch.getFileObject("external1"); FileObject extfile1 = ext1.getFileObject("subdir/file"); assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile1)); assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(extfile1)); FileOwnerQuery.markExternalOwner(ext1.toURI(), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(extfile1.toURI())); assertEquals("even for the projdir throught FileObjects", p, FileOwnerQuery.getOwner(ext1)); assertEquals("even for the projdir throught URI", p, FileOwnerQuery.getOwner(ext1.toURI())); assertEquals( "but not for something else throught FileObjects", null, FileOwnerQuery.getOwner(scratch)); assertEquals( "but not for something else throught URI", null, FileOwnerQuery.getOwner(scratch.toURI())); FileObject ext2 = scratch.getFileObject("external2"); FileObject extfile2 = ext2.getFileObject("subdir/file"); assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(extfile2)); assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(extfile2.toURI())); Project p2 = ProjectManager.getDefault().findProject(subprojdir); FileOwnerQuery.markExternalOwner(ext2.toURI(), p2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner through FileObjects", p2, FileOwnerQuery.getOwner(extfile2)); assertEquals("now have an owner through URI", p2, FileOwnerQuery.getOwner(extfile2.toURI())); assertEquals("even for the projdir through FileObjects", p2, FileOwnerQuery.getOwner(ext2)); assertEquals("even for the projdir through URI", p2, FileOwnerQuery.getOwner(ext2)); assertEquals( "but not for something else through FileObjects", null, FileOwnerQuery.getOwner(scratch)); assertEquals( "but not for something else through URI", null, FileOwnerQuery.getOwner(scratch.toURI())); assertEquals( "still correct for first proj through FileObjects", p, FileOwnerQuery.getOwner(extfile1)); assertEquals( "still correct for first proj through URI", p, FileOwnerQuery.getOwner(extfile1.toURI())); FileObject ext3 = scratch.getFileObject("external3"); assertEquals("no owner yet through FileObjects", null, FileOwnerQuery.getOwner(ext3)); assertEquals("no owner yet through URI", null, FileOwnerQuery.getOwner(ext3.toURI())); FileOwnerQuery.markExternalOwner(ext3.toURI(), p, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals("now have an owner through FileObjects", p, FileOwnerQuery.getOwner(ext3)); assertEquals("now have an owner through URI", p, FileOwnerQuery.getOwner(ext3.toURI())); FileObject ext3subproj = ext3.getFileObject("subproject"); Project p3 = FileOwnerQuery.getOwner(ext3subproj); assertNotSame("different project", p, p3); assertEquals( "but subprojects are not part of it", ProjectManager.getDefault().findProject(ext3subproj), p3); FileOwnerQuery.markExternalOwner( ext3.toURI(), null, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT); assertEquals( "unmarking an owner works through FileObjects", null, FileOwnerQuery.getOwner(ext3)); assertEquals( "unmarking an owner works through URI", null, FileOwnerQuery.getOwner(ext3.toURI())); }