Job theJob = managementService.createJobQuery().processInstanceId(processInstance.getId()).singleResult();
assertNotNull(theJob);
// Check if create-event has been dispatched
assertEquals(2, listener.getEventsReceived().size());
ActivitiEvent event = listener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.ENTITY_CREATED, event.getType());
checkEventContext(event, theJob, false);
event = listener.getEventsReceived().get(1);
assertEquals(ActivitiEventType.ENTITY_INITIALIZED, event.getType());
checkEventContext(event, theJob, false);
listener.clearEventsReceived();
// Update the job-entity. Check if update event is dispatched with update job entity
managementService.setJobRetries(theJob.getId(), 5);
assertEquals(1, listener.getEventsReceived().size());
event = listener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.ENTITY_UPDATED, event.getType());
Job updatedJob = (Job) ((ActivitiEntityEvent) event).getEntity();
assertEquals(5, updatedJob.getRetries());
checkEventContext(event, theJob, true);
listener.clearEventsReceived();
// Force timer to fire
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DAY_OF_YEAR, 1);
processEngineConfiguration.getClock().setCurrentTime(tomorrow.getTime());
waitForJobExecutorToProcessAllJobs(2000, 100);
// Check delete-event has been dispatched
assertEquals(3, listener.getEventsReceived().size());
// First, a timer fired event has been dispatched
event = listener.getEventsReceived().get(0);
assertEquals(ActivitiEventType.TIMER_FIRED, event.getType());
checkEventContext(event, theJob, true);
// Next, a delete event has been dispatched
event = listener.getEventsReceived().get(1);
assertEquals(ActivitiEventType.ENTITY_DELETED, event.getType());
checkEventContext(event, theJob, true);
// Finally, a complete event has been dispatched
event = listener.getEventsReceived().get(2);
assertEquals(ActivitiEventType.JOB_EXECUTION_SUCCESS, event.getType());
checkEventContext(event, theJob, true);
}