Program: 53056
Project: 53056
Buggy source:
@SuppressWarnings("String_Node_Str") public static int runTests(final CommandRunnerParams params,Iterable<TestRule> tests,ExecutionContext executionContext,final TestRunningOptions options,ListeningExecutorService service,BuildEngine buildEngine,final StepRunner stepRunner) throws IOException, ExecutionException, InterruptedException {
  ImmutableSet<JavaLibrary> rulesUnderTest;
  if (options.isCodeCoverageEnabled()) {
    rulesUnderTest=getRulesUnderTest(tests);
    if (!rulesUnderTest.isEmpty()) {
      try {
        JavaLibrary library=rulesUnderTest.iterator().next();
        stepRunner.runStepForBuildTarget(new MakeCleanDirectoryStep(library.getProjectFilesystem(),JacocoConstants.getJacocoOutputDir(library.getProjectFilesystem())),Optional.<BuildTarget>absent());
      }
 catch (      StepFailedException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
        return 1;
      }
    }
  }
 else {
    rulesUnderTest=ImmutableSet.of();
  }
  final ImmutableSet<String> testTargets=FluentIterable.from(tests).transform(HasBuildTarget.TO_TARGET).transform(Functions.toStringFunction()).toSet();
  final int totalNumberOfTests=Iterables.size(tests);
  params.getBuckEventBus().post(TestRunEvent.started(options.isRunAllTests(),options.getTestSelectorList(),options.shouldExplainTestSelectorList(),testTargets));
  List<ListenableFuture<TestResults>> results=Lists.newArrayList();
  TestRuleKeyFileHelper testRuleKeyFileHelper=new TestRuleKeyFileHelper(buildEngine);
  final AtomicInteger lastReportedTestSequenceNumber=new AtomicInteger();
  final List<TestRun> separateTestRuns=Lists.newArrayList();
  List<TestRun> parallelTestRuns=Lists.newArrayList();
  for (  final TestRule test : tests) {
    boolean isTestRunRequired;
    isTestRunRequired=isTestRunRequiredForTest(test,buildEngine,executionContext,testRuleKeyFileHelper,options.isResultsCacheEnabled(),!options.getTestSelectorList().isEmpty(),!options.getEnvironmentOverrides().isEmpty());
    final Map<String,UUID> testUUIDMap=new HashMap<>();
    final AtomicReference<TestStatusMessageEvent.Started> currentTestStatusMessageEvent=new AtomicReference<>();
    TestRule.TestReportingCallback testReportingCallback=new TestRule.TestReportingCallback(){
      @Override public void testsDidBegin(){
        LOG.debug("String_Node_Str",test.getBuildTarget());
      }
      @Override public void statusDidBegin(      TestStatusMessage didBeginMessage){
        LOG.debug("String_Node_Str",didBeginMessage);
        TestStatusMessageEvent.Started startedEvent=TestStatusMessageEvent.started(didBeginMessage);
        TestStatusMessageEvent.Started previousEvent=currentTestStatusMessageEvent.getAndSet(startedEvent);
        Preconditions.checkState(previousEvent == null,"String_Node_Str",previousEvent);
        params.getBuckEventBus().post(startedEvent);
      }
      @Override public void statusDidEnd(      TestStatusMessage didEndMessage){
        LOG.debug("String_Node_Str",didEndMessage);
        TestStatusMessageEvent.Started previousEvent=currentTestStatusMessageEvent.getAndSet(null);
        Preconditions.checkState(previousEvent != null,"String_Node_Str",previousEvent);
        params.getBuckEventBus().post(TestStatusMessageEvent.finished(previousEvent,didEndMessage));
      }
      @Override public void testDidBegin(      String testCaseName,      String testName){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testCaseName,testName);
        UUID testUUID=UUID.randomUUID();
        testUUIDMap.put(testCaseName + "String_Node_Str" + testName,testUUID);
        params.getBuckEventBus().post(TestSummaryEvent.started(testUUID,testCaseName,testName));
      }
      @Override public void testDidEnd(      TestResultSummary testResultSummary){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testResultSummary);
        UUID testUUID=testUUIDMap.get(testResultSummary.getTestCaseName() + "String_Node_Str" + testResultSummary.getTestName());
        Preconditions.checkNotNull(testUUID);
        params.getBuckEventBus().post(TestSummaryEvent.finished(testUUID,testResultSummary));
      }
      @Override public void testsDidEnd(      List<TestCaseSummary> testCaseSummaries){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testCaseSummaries);
      }
    }
;
    List<Step> steps;
    if (isTestRunRequired) {
      params.getBuckEventBus().post(IndividualTestEvent.started(testTargets));
      ImmutableList.Builder<Step> stepsBuilder=ImmutableList.builder();
      Preconditions.checkState(buildEngine.isRuleBuilt(test.getBuildTarget()));
      List<Step> testSteps=test.runTests(executionContext,options,testReportingCallback);
      if (!testSteps.isEmpty()) {
        stepsBuilder.addAll(testSteps);
        stepsBuilder.add(testRuleKeyFileHelper.createRuleKeyInDirStep(test));
      }
      steps=stepsBuilder.build();
    }
 else {
      steps=ImmutableList.of();
    }
    TestRun testRun=TestRun.of(test,steps,getCachingStatusTransformingCallable(isTestRunRequired,test.interpretTestResults(executionContext,!options.getTestSelectorList().isEmpty(),options.isDryRun())),testReportingCallback);
    if (test.runTestSeparately()) {
      LOG.debug("String_Node_Str",test);
      separateTestRuns.add(testRun);
    }
 else {
      LOG.debug("String_Node_Str",test);
      parallelTestRuns.add(testRun);
    }
  }
  final StepRunner.StepRunningCallback testStepRunningCallback=new StepRunner.StepRunningCallback(){
    @Override public void stepsWillRun(    Optional<BuildTarget> buildTarget){
      Preconditions.checkState(buildTarget.isPresent());
      LOG.debug("String_Node_Str",buildTarget);
      params.getBuckEventBus().post(TestRuleEvent.started(buildTarget.get()));
    }
    @Override public void stepsDidRun(    Optional<BuildTarget> buildTarget){
      Preconditions.checkState(buildTarget.isPresent());
      LOG.debug("String_Node_Str",buildTarget);
      params.getBuckEventBus().post(TestRuleEvent.finished(buildTarget.get()));
    }
  }
;
  for (  TestRun testRun : parallelTestRuns) {
    ListenableFuture<TestResults> testResults=stepRunner.runStepsAndYieldResult(testRun.getSteps(),testRun.getTestResultsCallable(),Optional.of(testRun.getTest().getBuildTarget()),service,testStepRunningCallback);
    results.add(transformTestResults(params,testResults,testRun.getTest(),testRun.getTestReportingCallback(),testTargets,lastReportedTestSequenceNumber,totalNumberOfTests));
  }
  ListenableFuture<List<TestResults>> parallelTestStepsFuture=Futures.allAsList(results);
  final List<TestResults> completedResults=Lists.newArrayList();
  final ListeningExecutorService directExecutorService=MoreExecutors.newDirectExecutorService();
  ListenableFuture<Void> uberFuture=stepRunner.addCallback(parallelTestStepsFuture,new FutureCallback<List<TestResults>>(){
    @Override public void onSuccess(    List<TestResults> parallelTestResults){
      LOG.debug("String_Node_Str");
      completedResults.addAll(parallelTestResults);
      List<ListenableFuture<TestResults>> separateResultsList=Lists.newArrayList();
      for (      TestRun testRun : separateTestRuns) {
        separateResultsList.add(transformTestResults(params,stepRunner.runStepsAndYieldResult(testRun.getSteps(),testRun.getTestResultsCallable(),Optional.of(testRun.getTest().getBuildTarget()),directExecutorService,testStepRunningCallback),testRun.getTest(),testRun.getTestReportingCallback(),testTargets,lastReportedTestSequenceNumber,totalNumberOfTests));
      }
      ListenableFuture<List<TestResults>> serialResults=Futures.allAsList(separateResultsList);
      try {
        completedResults.addAll(serialResults.get());
      }
 catch (      ExecutionException e) {
        LOG.error(e,"String_Node_Str");
        throw new HumanReadableException(e,"String_Node_Str");
      }
catch (      InterruptedException e) {
        LOG.error(e,"String_Node_Str");
        try {
          serialResults.cancel(true);
        }
 catch (        CancellationException ignored) {
        }
        Thread.currentThread().interrupt();
        throw new HumanReadableException(e,"String_Node_Str");
      }
      LOG.debug("String_Node_Str");
    }
    @Override public void onFailure(    Throwable e){
      LOG.error(e,"String_Node_Str");
      throw new HumanReadableException(e,"String_Node_Str");
    }
  }
,directExecutorService);
  try {
    uberFuture.get();
  }
 catch (  ExecutionException e) {
    e.printStackTrace(params.getConsole().getStdErr());
    return 1;
  }
catch (  InterruptedException e) {
    try {
      uberFuture.cancel(true);
    }
 catch (    CancellationException ignored) {
    }
    Thread.currentThread().interrupt();
    throw e;
  }
  params.getBuckEventBus().post(TestRunEvent.finished(testTargets,completedResults));
  Optional<String> path=options.getPathToXmlTestOutput();
  if (path.isPresent()) {
    try (Writer writer=Files.newWriter(new File(path.get()),Charsets.UTF_8)){
      writeXmlOutput(completedResults,writer);
    }
   }
  if (options.isCodeCoverageEnabled() && !rulesUnderTest.isEmpty()) {
    try {
      Optional<DefaultJavaPackageFinder> defaultJavaPackageFinderOptional=Optional.fromNullable(params.getBuckConfig().createDefaultJavaPackageFinder());
      stepRunner.runStepForBuildTarget(getReportCommand(rulesUnderTest,defaultJavaPackageFinderOptional,new JavaBuckConfig(params.getBuckConfig()).getDefaultJavaOptions().getJavaRuntimeLauncher(),params.getCell().getFilesystem(),JacocoConstants.getJacocoOutputDir(params.getCell().getFilesystem()),options.getCoverageReportFormat(),options.getCoverageReportTitle(),options.getCoverageIncludes(),options.getCoverageExcludes()),Optional.<BuildTarget>absent());
    }
 catch (    StepFailedException e) {
      params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
      return 1;
    }
  }
  boolean failures=Iterables.any(completedResults,new Predicate<TestResults>(){
    @Override public boolean apply(    TestResults results){
      LOG.debug("String_Node_Str",results);
      return !results.isSuccess();
    }
  }
);
  return failures ? TEST_FAILURES_EXIT_CODE : 0;
}

Fixed version (ground truth):
@SuppressWarnings("String_Node_Str") public static int runTests(final CommandRunnerParams params,Iterable<TestRule> tests,ExecutionContext executionContext,final TestRunningOptions options,ListeningExecutorService service,BuildEngine buildEngine,final StepRunner stepRunner) throws IOException, ExecutionException, InterruptedException {
  ImmutableSet<JavaLibrary> rulesUnderTest;
  if (options.isCodeCoverageEnabled()) {
    rulesUnderTest=getRulesUnderTest(tests);
    if (!rulesUnderTest.isEmpty()) {
      try {
        JavaLibrary library=rulesUnderTest.iterator().next();
        stepRunner.runStepForBuildTarget(new MakeCleanDirectoryStep(library.getProjectFilesystem(),JacocoConstants.getJacocoOutputDir(library.getProjectFilesystem())),Optional.<BuildTarget>absent());
      }
 catch (      StepFailedException e) {
        params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
        return 1;
      }
    }
  }
 else {
    rulesUnderTest=ImmutableSet.of();
  }
  final ImmutableSet<String> testTargets=FluentIterable.from(tests).transform(HasBuildTarget.TO_TARGET).transform(Functions.toStringFunction()).toSet();
  final int totalNumberOfTests=Iterables.size(tests);
  params.getBuckEventBus().post(TestRunEvent.started(options.isRunAllTests(),options.getTestSelectorList(),options.shouldExplainTestSelectorList(),testTargets));
  List<ListenableFuture<TestResults>> results=Lists.newArrayList();
  TestRuleKeyFileHelper testRuleKeyFileHelper=new TestRuleKeyFileHelper(buildEngine);
  final AtomicInteger lastReportedTestSequenceNumber=new AtomicInteger();
  final List<TestRun> separateTestRuns=Lists.newArrayList();
  List<TestRun> parallelTestRuns=Lists.newArrayList();
  for (  final TestRule test : tests) {
    boolean isTestRunRequired;
    isTestRunRequired=isTestRunRequiredForTest(test,buildEngine,executionContext,testRuleKeyFileHelper,options.isResultsCacheEnabled(),!options.getTestSelectorList().isEmpty(),!options.getEnvironmentOverrides().isEmpty(),options.isDryRun());
    final Map<String,UUID> testUUIDMap=new HashMap<>();
    final AtomicReference<TestStatusMessageEvent.Started> currentTestStatusMessageEvent=new AtomicReference<>();
    TestRule.TestReportingCallback testReportingCallback=new TestRule.TestReportingCallback(){
      @Override public void testsDidBegin(){
        LOG.debug("String_Node_Str",test.getBuildTarget());
      }
      @Override public void statusDidBegin(      TestStatusMessage didBeginMessage){
        LOG.debug("String_Node_Str",didBeginMessage);
        TestStatusMessageEvent.Started startedEvent=TestStatusMessageEvent.started(didBeginMessage);
        TestStatusMessageEvent.Started previousEvent=currentTestStatusMessageEvent.getAndSet(startedEvent);
        Preconditions.checkState(previousEvent == null,"String_Node_Str",previousEvent);
        params.getBuckEventBus().post(startedEvent);
      }
      @Override public void statusDidEnd(      TestStatusMessage didEndMessage){
        LOG.debug("String_Node_Str",didEndMessage);
        TestStatusMessageEvent.Started previousEvent=currentTestStatusMessageEvent.getAndSet(null);
        Preconditions.checkState(previousEvent != null,"String_Node_Str",previousEvent);
        params.getBuckEventBus().post(TestStatusMessageEvent.finished(previousEvent,didEndMessage));
      }
      @Override public void testDidBegin(      String testCaseName,      String testName){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testCaseName,testName);
        UUID testUUID=UUID.randomUUID();
        testUUIDMap.put(testCaseName + "String_Node_Str" + testName,testUUID);
        params.getBuckEventBus().post(TestSummaryEvent.started(testUUID,testCaseName,testName));
      }
      @Override public void testDidEnd(      TestResultSummary testResultSummary){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testResultSummary);
        UUID testUUID=testUUIDMap.get(testResultSummary.getTestCaseName() + "String_Node_Str" + testResultSummary.getTestName());
        Preconditions.checkNotNull(testUUID);
        params.getBuckEventBus().post(TestSummaryEvent.finished(testUUID,testResultSummary));
      }
      @Override public void testsDidEnd(      List<TestCaseSummary> testCaseSummaries){
        LOG.debug("String_Node_Str",test.getBuildTarget(),testCaseSummaries);
      }
    }
;
    List<Step> steps;
    if (isTestRunRequired) {
      params.getBuckEventBus().post(IndividualTestEvent.started(testTargets));
      ImmutableList.Builder<Step> stepsBuilder=ImmutableList.builder();
      Preconditions.checkState(buildEngine.isRuleBuilt(test.getBuildTarget()));
      List<Step> testSteps=test.runTests(executionContext,options,testReportingCallback);
      if (!testSteps.isEmpty()) {
        stepsBuilder.addAll(testSteps);
        stepsBuilder.add(testRuleKeyFileHelper.createRuleKeyInDirStep(test));
      }
      steps=stepsBuilder.build();
    }
 else {
      steps=ImmutableList.of();
    }
    TestRun testRun=TestRun.of(test,steps,getCachingStatusTransformingCallable(isTestRunRequired,test.interpretTestResults(executionContext,!options.getTestSelectorList().isEmpty(),options.isDryRun())),testReportingCallback);
    if (test.runTestSeparately()) {
      LOG.debug("String_Node_Str",test);
      separateTestRuns.add(testRun);
    }
 else {
      LOG.debug("String_Node_Str",test);
      parallelTestRuns.add(testRun);
    }
  }
  final StepRunner.StepRunningCallback testStepRunningCallback=new StepRunner.StepRunningCallback(){
    @Override public void stepsWillRun(    Optional<BuildTarget> buildTarget){
      Preconditions.checkState(buildTarget.isPresent());
      LOG.debug("String_Node_Str",buildTarget);
      params.getBuckEventBus().post(TestRuleEvent.started(buildTarget.get()));
    }
    @Override public void stepsDidRun(    Optional<BuildTarget> buildTarget){
      Preconditions.checkState(buildTarget.isPresent());
      LOG.debug("String_Node_Str",buildTarget);
      params.getBuckEventBus().post(TestRuleEvent.finished(buildTarget.get()));
    }
  }
;
  for (  TestRun testRun : parallelTestRuns) {
    ListenableFuture<TestResults> testResults=stepRunner.runStepsAndYieldResult(testRun.getSteps(),testRun.getTestResultsCallable(),Optional.of(testRun.getTest().getBuildTarget()),service,testStepRunningCallback);
    results.add(transformTestResults(params,testResults,testRun.getTest(),testRun.getTestReportingCallback(),testTargets,lastReportedTestSequenceNumber,totalNumberOfTests));
  }
  ListenableFuture<List<TestResults>> parallelTestStepsFuture=Futures.allAsList(results);
  final List<TestResults> completedResults=Lists.newArrayList();
  final ListeningExecutorService directExecutorService=MoreExecutors.newDirectExecutorService();
  ListenableFuture<Void> uberFuture=stepRunner.addCallback(parallelTestStepsFuture,new FutureCallback<List<TestResults>>(){
    @Override public void onSuccess(    List<TestResults> parallelTestResults){
      LOG.debug("String_Node_Str");
      completedResults.addAll(parallelTestResults);
      List<ListenableFuture<TestResults>> separateResultsList=Lists.newArrayList();
      for (      TestRun testRun : separateTestRuns) {
        separateResultsList.add(transformTestResults(params,stepRunner.runStepsAndYieldResult(testRun.getSteps(),testRun.getTestResultsCallable(),Optional.of(testRun.getTest().getBuildTarget()),directExecutorService,testStepRunningCallback),testRun.getTest(),testRun.getTestReportingCallback(),testTargets,lastReportedTestSequenceNumber,totalNumberOfTests));
      }
      ListenableFuture<List<TestResults>> serialResults=Futures.allAsList(separateResultsList);
      try {
        completedResults.addAll(serialResults.get());
      }
 catch (      ExecutionException e) {
        LOG.error(e,"String_Node_Str");
        throw new HumanReadableException(e,"String_Node_Str");
      }
catch (      InterruptedException e) {
        LOG.error(e,"String_Node_Str");
        try {
          serialResults.cancel(true);
        }
 catch (        CancellationException ignored) {
        }
        Thread.currentThread().interrupt();
        throw new HumanReadableException(e,"String_Node_Str");
      }
      LOG.debug("String_Node_Str");
    }
    @Override public void onFailure(    Throwable e){
      LOG.error(e,"String_Node_Str");
      throw new HumanReadableException(e,"String_Node_Str");
    }
  }
,directExecutorService);
  try {
    uberFuture.get();
  }
 catch (  ExecutionException e) {
    e.printStackTrace(params.getConsole().getStdErr());
    return 1;
  }
catch (  InterruptedException e) {
    try {
      uberFuture.cancel(true);
    }
 catch (    CancellationException ignored) {
    }
    Thread.currentThread().interrupt();
    throw e;
  }
  params.getBuckEventBus().post(TestRunEvent.finished(testTargets,completedResults));
  Optional<String> path=options.getPathToXmlTestOutput();
  if (path.isPresent()) {
    try (Writer writer=Files.newWriter(new File(path.get()),Charsets.UTF_8)){
      writeXmlOutput(completedResults,writer);
    }
   }
  if (options.isCodeCoverageEnabled() && !rulesUnderTest.isEmpty()) {
    try {
      Optional<DefaultJavaPackageFinder> defaultJavaPackageFinderOptional=Optional.fromNullable(params.getBuckConfig().createDefaultJavaPackageFinder());
      stepRunner.runStepForBuildTarget(getReportCommand(rulesUnderTest,defaultJavaPackageFinderOptional,new JavaBuckConfig(params.getBuckConfig()).getDefaultJavaOptions().getJavaRuntimeLauncher(),params.getCell().getFilesystem(),JacocoConstants.getJacocoOutputDir(params.getCell().getFilesystem()),options.getCoverageReportFormat(),options.getCoverageReportTitle(),options.getCoverageIncludes(),options.getCoverageExcludes()),Optional.<BuildTarget>absent());
    }
 catch (    StepFailedException e) {
      params.getBuckEventBus().post(ConsoleEvent.severe(Throwables.getRootCause(e).getLocalizedMessage()));
      return 1;
    }
  }
  boolean failures=Iterables.any(completedResults,new Predicate<TestResults>(){
    @Override public boolean apply(    TestResults results){
      LOG.debug("String_Node_Str",results);
      return !results.isSuccess();
    }
  }
);
  return failures ? TEST_FAILURES_EXIT_CODE : 0;
}