/**
   * Blocks until FindBugs has finished scanning
   *
   * @param className file in this project to scan
   * @throws CoreException
   */
  private void scanForBugs(String className) throws CoreException {
    IJavaElement element = testProject.findElement(new Path(className));
    if (element == null) {
      fail("Could not find java class " + className);
      return;
    }
    final AtomicBoolean isWorking = new AtomicBoolean(true);
    FindBugsWorker worker =
        new FindBugsWorker(
            testProject.getProject(),
            new NullProgressMonitor() {
              @Override
              public void done() {
                isWorking.set(false);
              }
            });

    worker.work(Collections.singletonList(new WorkItem(element)));
    // wait for the findBugsWorker to finish
    // 500ms reduces the chance that the IMarkers haven't loaded yet and the tests will fail
    // unpredictably
    // (see JavaProjectHelper discussion about performDummySearch for more info)
    TestingUtils.waitForUiEvents(800);
    while (isWorking.get()) {
      TestingUtils.waitForUiEvents(100);
    }
  }
 private IMarker[] getSortedMarkersFromFile(String fileName) throws JavaModelException {
   IJavaElement fileToScan = TestingUtils.elementFromProject(testProject, fileName);
   IMarker[] markers = TestingUtils.getAllMarkersInResource(fileToScan.getCorrespondingResource());
   // the markers get sorted first by bug name, then by line number, just like
   // the QuickFixTestPackages, so we have a reliable way to fix them
   // so we can assert properties more accurately.
   TestingUtils.sortMarkersByPatterns(markers);
   return markers;
 }
 private void scanUntilMarkers(String testResource) throws CoreException {
   scanForBugs(testResource);
   for (int i = 0; i < 3; i++) {
     IMarker[] markers = TestingUtils.getAllMarkersInResource(testProject, testResource);
     if (markers.length > 0) {
       return;
     }
     System.out.println("Trying again for bugs...");
     clearMarkersAndBugs();
     scanForBugs(testResource);
     TestingUtils.waitForUiEvents(1000);
   }
   fail("Did not find any markers... tried 3 times");
 }
  public static void loadFilesThatNeedFixing() throws CoreException, IOException {
    makeJavaProject();

    TestingUtils.copyBrokenFiles(
        testIProject.getFolder(SRC_FOLDER_NAME),
        new File("classesToFix/"),
        new File("mockLibraries/"));

    // Compiles the code
    testIProject.refreshLocal(IResource.DEPTH_INFINITE, null);
    testIProject.build(IncrementalProjectBuilder.FULL_BUILD, null);

    FindbugsPlugin.setProjectSettingsEnabled(testIProject, null, true);
    UserPreferences userPrefs = FindbugsPlugin.getUserPreferences(testIProject);
    // enables categories like Security, which are disabled by default
    userPrefs.getFilterSettings().clearAllCategories();

    checkFBContribInstalled();
    checkFindSecurityBugsInstalled();

    TestingUtils.waitForUiEvents(100);
  }
  private void assertBugPatternsMatch(List<QuickFixTestPackage> packages, String testResource)
      throws JavaModelException {
    IMarker[] markers = TestingUtils.getAllMarkersInResource(testProject, testResource);
    TestingUtils.sortMarkersByPatterns(markers);

    // packages and markers should now be lined up to match up one to one.
    assertEquals(
        "The number of markers is wrong, check your turned off detectors?",
        packages.size(),
        markers.length);

    TestingUtils.assertBugPatternsMatch(packages, markers);
    TestingUtils.assertLabelsAndDescriptionsMatch(packages, markers, resolutionSource);
    TestingUtils.assertLineNumbersMatch(packages, markers);
    TestingUtils.assertAllMarkersHaveResolutions(markers, resolutionSource);
  }
 private void assertOutputAndInputFilesMatch(String testResource)
     throws JavaModelException, IOException {
   URL expectedFile = getClass().getResource("/fixedClasses/" + testResource);
   IJavaElement actualFile = TestingUtils.elementFromProject(testProject, testResource);
   TestingUtils.assertOutputAndInputFilesMatch(expectedFile, actualFile);
 }
 private void showEditorWindowForFile(String testResource)
     throws JavaModelException, PartInitException {
   JavaUI.openInEditor(TestingUtils.elementFromProject(testProject, testResource), true, true);
 }