@Before
  public void init() throws Exception {

    earlierExecution =
        MetaDataInstanceFactory.createJobExecutionWithStepExecutions(122L, Arrays.asList("step"));
    earlierExecution.setStatus(BatchStatus.FAILED);
    earlierExecution.setExitStatus(ExitStatus.FAILED);
    earlierExecution.setStartTime(new Date());
    earlierExecution.setEndTime(new Date(earlierExecution.getStartTime().getTime() + 100));
    assertFalse(earlierExecution.isRunning());

    jobExecution =
        MetaDataInstanceFactory.createJobExecutionWithStepExecutions(
            123L, Arrays.asList("first", "step"));
    jobExecution.setStatus(BatchStatus.COMPLETED);
    jobExecution.setExitStatus(ExitStatus.COMPLETED);
    jobExecution.setStartTime(new Date());
    jobExecution.setEndTime(new Date(earlierExecution.getEndTime().getTime() + 100));
    assertFalse(jobExecution.isRunning());

    Iterator<StepExecution> iterator = jobExecution.getStepExecutions().iterator();
    iterator.next();
    StepExecution stepExecution = iterator.next();
    stepExecution.setStatus(BatchStatus.COMPLETED);
    stepExecution.setExitStatus(ExitStatus.COMPLETED.addExitDescription("Foo"));

    metrics = new SimpleJobExecutionMetrics(jobService, "job");
  }
 @Override
 public JobExecutionInfoResource getSerializationValue() {
   JobInstance jobInstance = new JobInstance(1l, "job1");
   JobExecution jobExecution =
       new JobExecution(
           jobInstance,
           2l,
           new JobParametersBuilder()
               .addString("foo", "bar")
               .addDouble("baz", 3.0, false)
               .toJobParameters(),
           "configName.xml");
   jobExecution.setVersion(1);
   jobExecution.setStatus(BatchStatus.STARTED);
   jobExecution.setCreateTime(new Date(0));
   jobExecution.setStartTime(new Date(1000));
   jobExecution.setLastUpdated(new Date(3000));
   StepExecution stepExecution = new StepExecution("step1", jobExecution, 3l);
   stepExecution.setStatus(BatchStatus.STARTED);
   stepExecution.setStartTime(new Date(1000));
   stepExecution.setLastUpdated(new Date(3000));
   jobExecution.addStepExecutions(Arrays.asList(stepExecution));
   JobExecutionInfoResource jobExecutionInfoResource =
       new JobExecutionInfoResource(jobExecution, TimeZone.getTimeZone("America/Chicago"));
   jobExecutionInfoResource.setStepExecutions(
       Arrays.asList(
           new StepExecutionInfoResource(stepExecution, TimeZone.getTimeZone("America/Chicago"))));
   return jobExecutionInfoResource;
 }
 @Override
 public ExitStatus afterStep(StepExecution stepExecution) {
   if (!(stepExecution.getStatus() == BatchStatus.COMPLETED)) {
     return ExitStatus.EXECUTING;
   }
   long expecting = localState.getExpecting();
   boolean timedOut;
   try {
     logger.debug("Waiting for results in step listener...");
     timedOut = !waitForResults();
     logger.debug("Finished waiting for results in step listener.");
   } catch (RuntimeException e) {
     logger.debug("Detected failure waiting for results in step listener.", e);
     stepExecution.setStatus(BatchStatus.FAILED);
     return ExitStatus.FAILED.addExitDescription(e.getClass().getName() + ": " + e.getMessage());
   }
   if (timedOut) {
     stepExecution.setStatus(BatchStatus.FAILED);
     throw new ItemStreamException("Timed out waiting for back log at end of step");
   }
   return ExitStatus.COMPLETED.addExitDescription("Waited for " + expecting + " results.");
 }
  public Collection<StepExecution> getStepExecutions(Long jobExecutionId)
      throws NoSuchJobExecutionException {

    JobExecution jobExecution = jobExecutionDao.getJobExecution(jobExecutionId);
    if (jobExecution == null) {
      throw new NoSuchJobExecutionException("No JobExecution with id=" + jobExecutionId);
    }

    stepExecutionDao.addStepExecutions(jobExecution);

    String jobName =
        jobExecution.getJobInstance() == null ? null : jobExecution.getJobInstance().getJobName();
    Collection<String> missingStepNames = new LinkedHashSet<String>();

    if (jobName != null) {
      missingStepNames.addAll(
          stepExecutionDao.findStepNamesForJobExecution(jobName, "*:partition*"));
      logger.debug("Found step executions in repository: " + missingStepNames);
    }

    Job job = null;
    try {
      job = jobLocator.getJob(jobName);
    } catch (NoSuchJobException e) {
      // expected
    }
    if (job instanceof StepLocator) {
      Collection<String> stepNames = ((StepLocator) job).getStepNames();
      missingStepNames.addAll(stepNames);
      logger.debug("Added step executions from job: " + missingStepNames);
    }

    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
      String stepName = stepExecution.getStepName();
      if (missingStepNames.contains(stepName)) {
        missingStepNames.remove(stepName);
      }
      logger.debug("Removed step executions from job execution: " + missingStepNames);
    }

    for (String stepName : missingStepNames) {
      StepExecution stepExecution = jobExecution.createStepExecution(stepName);
      stepExecution.setStatus(BatchStatus.UNKNOWN);
    }

    return jobExecution.getStepExecutions();
  }
 @Override
 public StepExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
   StepExecution stepExecution = new StepExecution(rs.getString(2), jobExecution, rs.getLong(1));
   stepExecution.setStartTime(rs.getTimestamp(3));
   stepExecution.setEndTime(rs.getTimestamp(4));
   stepExecution.setStatus(BatchStatus.valueOf(rs.getString(5)));
   stepExecution.setCommitCount(rs.getInt(6));
   stepExecution.setReadCount(rs.getInt(7));
   stepExecution.setFilterCount(rs.getInt(8));
   stepExecution.setWriteCount(rs.getInt(9));
   stepExecution.setExitStatus(new ExitStatus(rs.getString(10), rs.getString(11)));
   stepExecution.setReadSkipCount(rs.getInt(12));
   stepExecution.setWriteSkipCount(rs.getInt(13));
   stepExecution.setProcessSkipCount(rs.getInt(14));
   stepExecution.setRollbackCount(rs.getInt(15));
   stepExecution.setLastUpdated(rs.getTimestamp(16));
   stepExecution.setVersion(rs.getInt(17));
   return stepExecution;
 }