@Test
  public void testProvider() {
    CurrentStepRunIndexProvider provider = new CurrentStepRunIndexProvider();

    TestStep mockStep1 = Mockito.mock(TestStep.class);
    Mockito.when(mockStep1.getId()).thenReturn("1234");

    TestStep mockStep2 = Mockito.mock(TestStep.class);
    Mockito.when(mockStep2.getId()).thenReturn("2345");

    TestStepResult mockResult1 = Mockito.mock(TestStepResult.class);
    Mockito.when(mockResult1.getTestStep()).thenReturn(mockStep1);

    TestStepResult mockResult2 = Mockito.mock(TestStepResult.class);
    Mockito.when(mockResult2.getTestStep()).thenReturn(mockStep2);

    List<TestStepResult> resultList = new ArrayList<TestStepResult>();
    resultList.add(mockResult1);
    resultList.add(mockResult2);
    resultList.add(mockResult2);
    resultList.add(mockResult1);
    resultList.add(mockResult2);

    WsdlTestRunContext context = Mockito.mock(WsdlTestRunContext.class);
    AbstractTestCaseRunner runner = Mockito.mock(AbstractTestCaseRunner.class);
    Mockito.when(context.getTestRunner()).thenReturn(runner);
    Mockito.when(runner.getResults()).thenReturn(resultList);

    Mockito.when(context.getCurrentStep()).thenReturn(mockStep1);
    Assert.assertEquals("2", provider.getValue(context));

    Mockito.when(context.getCurrentStep()).thenReturn(mockStep2);
    Assert.assertEquals("3", provider.getValue(context));
  }
  public LoadTestLogErrorEntry(
      String type, String error, TestStepResult result, ImageIcon icon, int threadIndex) {
    this.icon = icon;
    this.type = type;
    this.error = error;
    this.result = result;
    this.threadIndex = threadIndex;
    this.targetStepName = result == null ? null : result.getTestStep().getName();

    timestamp = result == null ? System.currentTimeMillis() : result.getTimeStamp();
  }
  public void exportToFile(String fileName) throws IOException {
    PrintWriter writer = new PrintWriter(fileName);

    writer.write(new Date(timestamp).toString());
    writer.write(":");
    writer.write(targetStepName);
    writer.write(":");
    writer.write(error);
    writer.write(":");
    writer.print(threadIndex);
    writer.println();
    if (result != null) {
      writer.println("----------------------------------------------------");
      result.writeTo(writer);
    } else if (discarded) {
      writer.println("-> Discarded");
    }

    writer.close();
  }
    @Override
    public void beforeStep(
        TestCaseRunner testRunner, SecurityTestRunContext runContext, TestStepResult tsr) {
      if (tsr.getTestStep().getId().equals(testStep.getId())) {
        int count = securityTest.getStepSecurityApplicableScansCount(tsr);
        progressBar.getModel().setMaximum(count);

        if (securityTest.getSecurityScansMap().get(testStep.getId()) != null
            && securityTest.getSecurityScansMap().get(testStep.getId()).size() > 0) {
          progressBar.setString(STATE_RUN);
          progressBar.setForeground(OK_COLOR);
        }
        progressBar.setBackground(Color.white);
        progressBar.setValue(0);
        counterLabel.setText("");
        counterLabel.setOpaque(false);

        ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
      }
    }
  public String assertResult(
      LoadTestRunner loadTestRunner,
      LoadTestRunContext context,
      TestStepResult result,
      TestCaseRunner testRunner,
      TestCaseRunContext runContext) {
    WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
    LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();

    TestStep step = result.getTestStep();
    if (targetStepMatches(step)) {
      int index = step.getTestCase().getIndexOfTestStep(step);

      long average = statisticsModel.getStatistic(index, Statistic.AVERAGE);
      long count = statisticsModel.getStatistic(index, Statistic.AVERAGE);
      if (count > minRequests && (count % sampleInterval == 0) && average >= maxAverage) {
        return returnErrorOrFail(
            "Average [" + average + "] exceeds limit [" + maxAverage + "]",
            maxErrors,
            loadTestRunner,
            context);
      }
    } else if (ALL_TEST_STEPS.equals(getTargetStep())) {
      long average = statisticsModel.getStatistic(LoadTestStatistics.TOTAL, Statistic.AVERAGE);
      long count = statisticsModel.getStatistic(LoadTestStatistics.TOTAL, Statistic.COUNT);
      if (count > minRequests && (count % sampleInterval == 0) && average >= maxAverage) {
        return returnErrorOrFail(
            "Average [" + average + "] exceeds limit [" + maxAverage + "]",
            maxErrors,
            loadTestRunner,
            context);
      }
    }

    return null;
  }
 public ActionList getActions() {
   return result == null ? null : result.getActions();
 }
  public TestStepResult run(TestCaseRunner testRunner, TestCaseRunContext testRunContext) {
    WsdlMessageExchangeTestStepResult result = new WsdlMessageExchangeTestStepResult(this);

    testCaseRunner = null;

    if (targetTestCase != null) {
      Enum runMode = getRunMode();

      if (runMode == RunTestCaseRunModeTypeConfig.PARALLELL) {
        runningTestCase = createTestCase(targetTestCase);
      } else {
        runningTestCase = targetTestCase;

        TestCaseRunner targetTestRunner = SoapUI.getTestMonitor().getTestRunner(targetTestCase);
        if (targetTestRunner != null && targetTestRunner.getStatus() == TestRunner.Status.RUNNING) {
          if (runMode == RunTestCaseRunModeTypeConfig.SINGLETON_AND_FAIL) {
            result.setStatus(TestStepStatus.FAILED);
            result.addMessage("Target TestCase is already running");
            result.stopTimer();
            runningTestCase = null;
          } else {
            targetTestRunner.waitUntilFinished();
          }
        }
      }

      if (runningTestCase != null) {
        synchronized (runningTestCase) {
          for (TestRunListener listener : testRunListeners)
            runningTestCase.addTestRunListener(listener);

          StringList returnProperties = getReturnProperties();
          Map<String, TestProperty> props = getProperties();
          for (String key : props.keySet()) {
            if (runningTestCase.hasProperty(key) && !returnProperties.contains(key)) {
              String value = props.get(key).getValue();
              runningTestCase.setPropertyValue(
                  key, PropertyExpander.expandProperties(testRunContext, value));
            }
          }

          currentLabel = getLabel();
          runningTestCase.addTestRunListener(testRunListener);

          // StringToObjectMap properties = new StringToObjectMap();
          // for( String name : testRunContext.getPropertyNames() )
          // properties.put( name, testRunContext.getProperty( name ));

          result.startTimer();
          testCaseRunner = runningTestCase.run(new StringToObjectMap(), true);
          testCaseRunner.waitUntilFinished();
          result.stopTimer();

          for (String key : returnProperties) {
            if (runningTestCase.hasProperty(key))
              setPropertyValue(key, runningTestCase.getPropertyValue(key));
          }

          // aggregate results
          for (TestStepResult testStepResult : testCaseRunner.getResults()) {
            result.addMessage(
                testStepResult.getTestStep().getName()
                    + " - "
                    + testStepResult.getStatus()
                    + " - "
                    + testStepResult.getTimeTaken());
            for (String msg : testStepResult.getMessages()) {
              result.addMessage("- " + msg);
            }

            if (testStepResult instanceof MessageExchangeTestStepResult) {
              result.addMessages(
                  ((MessageExchangeTestStepResult) testStepResult).getMessageExchanges());
            }
          }

          switch (testCaseRunner.getStatus()) {
            case CANCELED:
              result.setStatus(TestStepStatus.CANCELED);
              break;
            case FAILED:
              result.setStatus(TestStepStatus.FAILED);
              break;
            case FINISHED:
              result.setStatus(TestStepStatus.OK);
              break;
            default:
              result.setStatus(TestStepStatus.UNKNOWN);
              break;
          }

          for (TestRunListener listener : testRunListeners)
            runningTestCase.removeTestRunListener(listener);

          if (runMode == RunTestCaseRunModeTypeConfig.PARALLELL) runningTestCase.release();

          runningTestCase = null;
          testCaseRunner = null;
        }
      }
    } else {
      result.setStatus(TestStepStatus.FAILED);
      result.addMessage("Missing testCase in project");
      result.stopTimer();
    }

    return result;
  }
  @Override
  public void afterStep(
      TestCaseRunner testRunner, TestCaseRunContext runContext, TestStepResult result) {
    super.afterStep(testRunner, runContext, result);
    TestStep currentStep = runContext.getCurrentStep();

    if (currentStep instanceof Assertable) {
      Assertable requestStep = (Assertable) currentStep;
      for (int c = 0; c < requestStep.getAssertionCount(); c++) {
        TestAssertion assertion = requestStep.getAssertionAt(c);
        log.info("Assertion [" + assertion.getName() + "] has status " + assertion.getStatus());
        if (assertion.getStatus() == AssertionStatus.FAILED) {
          for (AssertionError error : assertion.getErrors()) {
            log.error("ASSERTION FAILED -> " + error.getMessage());
          }

          assertions.add(assertion);
          assertionResults.put(assertion, (WsdlTestStepResult) result);
        }

        testAssertionCount++;
      }
    }

    String countPropertyName = currentStep.getName() + " run count";
    Long count = (Long) runContext.getProperty(countPropertyName);
    if (count == null) {
      count = new Long(0);
    }

    runContext.setProperty(countPropertyName, new Long(count.longValue() + 1));

    if (result.getStatus() == TestStepStatus.FAILED || exportAll) {
      try {
        String exportSeparator = System.getProperty(SOAPUI_EXPORT_SEPARATOR, "-");

        TestCase tc = currentStep.getTestCase();
        String nameBase =
            StringUtils.createFileName(tc.getTestSuite().getName(), '_')
                + exportSeparator
                + StringUtils.createFileName(tc.getName(), '_')
                + exportSeparator
                + StringUtils.createFileName(currentStep.getName(), '_')
                + "-"
                + count.longValue()
                + "-"
                + result.getStatus();

        WsdlTestCaseRunner callingTestCaseRunner =
            (WsdlTestCaseRunner) runContext.getProperty("#CallingTestCaseRunner#");

        if (callingTestCaseRunner != null) {
          WsdlTestCase ctc = callingTestCaseRunner.getTestCase();
          WsdlRunTestCaseTestStep runTestCaseTestStep =
              (WsdlRunTestCaseTestStep) runContext.getProperty("#CallingRunTestCaseStep#");

          nameBase =
              StringUtils.createFileName(ctc.getTestSuite().getName(), '_')
                  + exportSeparator
                  + StringUtils.createFileName(ctc.getName(), '_')
                  + exportSeparator
                  + StringUtils.createFileName(runTestCaseTestStep.getName(), '_')
                  + exportSeparator
                  + StringUtils.createFileName(tc.getTestSuite().getName(), '_')
                  + exportSeparator
                  + StringUtils.createFileName(tc.getName(), '_')
                  + exportSeparator
                  + StringUtils.createFileName(currentStep.getName(), '_')
                  + "-"
                  + count.longValue()
                  + "-"
                  + result.getStatus();
        }

        String absoluteOutputFolder = getAbsoluteOutputFolder(ModelSupport.getModelItemProject(tc));
        String fileName = absoluteOutputFolder + File.separator + nameBase + ".txt";

        if (result.getStatus() == TestStepStatus.FAILED) {
          log.error(currentStep.getName() + " failed, exporting to [" + fileName + "]");
        }

        new File(fileName).getParentFile().mkdirs();

        PrintWriter writer = new PrintWriter(fileName);
        result.writeTo(writer);
        writer.close();

        // write attachments
        if (result instanceof MessageExchange) {
          Attachment[] attachments = ((MessageExchange) result).getResponseAttachments();
          if (attachments != null && attachments.length > 0) {
            for (int c = 0; c < attachments.length; c++) {
              fileName = nameBase + "-attachment-" + (c + 1) + ".";

              Attachment attachment = attachments[c];
              String contentType = attachment.getContentType();
              if (!"application/octet-stream".equals(contentType)
                  && contentType != null
                  && contentType.indexOf('/') != -1) {
                fileName += contentType.substring(contentType.lastIndexOf('/') + 1);
              } else {
                fileName += "dat";
              }

              fileName = absoluteOutputFolder + File.separator + fileName;

              FileOutputStream outFile = new FileOutputStream(fileName);
              Tools.writeAll(outFile, attachment.getInputStream());
              outFile.close();
            }
          }
        }

        exportCount++;
      } catch (Exception e) {
        log.error("Error saving failed result: " + e, e);
      }
    }

    testStepCount++;
  }