DefaultTaskExecutionService implements TaskExecutionService { @Override public void execute(TaskParameters parameters) { TaskExecutionContext executionContext = null; LOG.trace("Starting execution for {}", parameters); try { validateIfRequired(parameters); executionContext = new TaskExecutionContext(context.getTask(parameters), parameters.isLenient()); LOG.info("Starting task ({}) execution.", executionContext.task()); preExecution(executionContext); actualExecution(parameters, executionContext); postExecution(executionContext); } catch (InvalidTaskParametersException i) { LOG.error("Task execution failed due to invalid parameters: " + String.join(". ", i.getReasons()), i); executionFailed(i, executionContext); } catch (TaskException e) { LOG.error(String.format("Task (%s) execution failed.", ofNullable(executionContext).map(c -> c.task().toString()).orElse("")), e); executionFailed(e, executionContext); } catch (RuntimeException e) { executionFailed(e, executionContext); throw e; } }  @Override void execute(TaskParameters parameters); void validate(TaskParameters parameters);  }
@Test public void testExecute() { TestListenerStart listener = TestListenerFactory.newStartListener(); GlobalNotificationContext.getContext().addListener(listener); victim.execute(parameters); assertTrue(listener.isStarted()); }
@Test public void testInvalidParameters() throws TaskException { parameters.setVersion(PdfVersion.VERSION_1_4); parameters.setCompress(true); victim.execute(parameters); verify(task, never()).before(eq(parameters), any()); verify(task, never()).after(); verify(task, never()).execute(parameters); }
@Test public void testNegativeBeforeExecution() throws TaskException { doThrow(new TaskExecutionException("Mock exception")).when(task).before(any(TaskParameters.class), any(TaskExecutionContext.class)); SingleTaskOutput output = mock(SingleTaskOutput.class); parameters.setOutput(output); TestUtils.setProperty(victim, "context", context); victim.execute(parameters); verify(task).before(eq(parameters), any()); verify(task).after(); verify(task, never()).execute(parameters); }