NotifyTaskStrategy implements ITaskProcessStrategy { @Override public void process(final ITaskExecutionState state) throws TaskExecutionException, InvalidArgumentException, TaskProcessException { try { state.getTaskAs(IShutdownAwareTask.class).notifyShuttingDown(); } catch (ShutdownAwareTaskNotificationException e) { throw new TaskProcessException(e); } state.execute(); }  @Override void process(final ITaskExecutionState state);  }
@Test public void Should_notifyAndExecuteTask() throws Exception { ITaskExecutionState taskExecutionState = mock(ITaskExecutionState.class); IShutdownAwareTask shutdownAwareTask = mock(IShutdownAwareTask.class); when(taskExecutionState.getTaskAs(same(IShutdownAwareTask.class))).thenReturn(shutdownAwareTask); new NotifyTaskStrategy().process(taskExecutionState); verify(shutdownAwareTask).notifyShuttingDown(); verify(taskExecutionState).execute(); }
@Test(expected = TaskProcessException.class) public void Should_wrapNotificationException() throws Exception { ITaskExecutionState taskExecutionState = mock(ITaskExecutionState.class); IShutdownAwareTask shutdownAwareTask = mock(IShutdownAwareTask.class); when(taskExecutionState.getTaskAs(same(IShutdownAwareTask.class))).thenReturn(shutdownAwareTask); doThrow(ShutdownAwareTaskNotificationException.class).when(shutdownAwareTask).notifyShuttingDown(); new NotifyTaskStrategy().process(taskExecutionState); }