SCT now handles exceptions thrown by execution listeners

resolves #341
This commit is contained in:
Glenn Renfro
2017-10-05 17:22:05 -04:00
committed by Michael Minella
parent fd2d4a9124
commit 3f316a0170
4 changed files with 341 additions and 42 deletions

View File

@@ -39,6 +39,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -54,6 +55,17 @@ public class TaskExecutionListenerTests {
private static final String EXCEPTION_MESSAGE = "This was expected";
public static boolean beforeTaskDidFireOnError = false;
public static boolean endTaskDidFireOnError = false;
public static boolean failedTaskDidFireOnError = false;
@BeforeTask
public void setup() {
beforeTaskDidFireOnError = false;
endTaskDidFireOnError = false;
failedTaskDidFireOnError = false;
}
@After
public void tearDown() {
if(context != null && context.isActive()) {
@@ -75,6 +87,58 @@ public class TaskExecutionListenerTests {
verifyListenerResults(true, false, false, taskExecution,taskExecutionListener);
}
/**
* Verify that if a LifecycleProcessor executes all TaskExecutionListeners if BeforeTask throws exception.
*/
@Test
public void testBeforeTaskErrorCreate() {
boolean exceptionFired = false;
try {
setupContextForBeforeTaskErrorAnnotatedListener();
}
catch (Exception exception) {
exceptionFired = true;
}
assertTrue("Exception should have fired", exceptionFired);
assertTrue("BeforeTask Listener should have executed", beforeTaskDidFireOnError);
assertTrue("EndTask Listener should have executed", endTaskDidFireOnError);
assertFalse("FailedTask Listener should have executed", failedTaskDidFireOnError);
}
/**
* Verify that if a LifecycleProcessor executes AfterTask TaskExecutionListeners if FailedTask throws exception.
*/
@Test
public void testFailedTaskErrorCreate() {
boolean exceptionFired = false;
try {
setupContextForFailedTaskErrorAnnotatedListener();
}
catch (Exception exception) {
exceptionFired = true;
}
assertTrue("Exception should have fired", exceptionFired);
assertTrue("EndTask Listener should have executed", endTaskDidFireOnError);
assertFalse("FailedTask Listener should not have executed", failedTaskDidFireOnError);
}
/**
* Verify that if a LifecycleProcessor stores the correct exit code if AfterTask listener fails.
*/
@Test
public void testAfterTaskErrorCreate() {
setupContextForAfterTaskErrorAnnotatedListener();
AfterTaskErrorAnnotationConfiguration.AnnotatedTaskListener taskExecutionListener =
context.getBean(AfterTaskErrorAnnotationConfiguration.AnnotatedTaskListener.class);
context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), new String[0], context));
assertEquals(true,taskExecutionListener.isTaskStartup());
assertEquals(true,taskExecutionListener.isTaskEnd());
assertEquals(TestListener.END_MESSAGE, taskExecutionListener.getTaskExecution().getExitMessage());
assertTrue(taskExecutionListener.getTaskExecution().getErrorMessage().contains("Failed to process @BeforeTask or @AfterTask annotation because: AfterTaskFailure"));
assertNull(taskExecutionListener.getThrowable());
}
/**
* Verify that if a TaskExecutionListener Bean is present that the onTaskEnd method
* is called.
@@ -173,7 +237,7 @@ public class TaskExecutionListenerTests {
}
else if(isTaskEnd){
assertEquals(TestListener.END_MESSAGE, actualListener.getTaskExecution().getExitMessage());
assertNull(actualListener.getTaskExecution().getErrorMessage());
assertEquals(taskExecution.getErrorMessage(), actualListener.getTaskExecution().getErrorMessage());
assertNull(actualListener.getThrowable());
}
else {
@@ -200,6 +264,24 @@ public class TaskExecutionListenerTests {
context.setId("annotatedTask");
}
private void setupContextForBeforeTaskErrorAnnotatedListener(){
context = new AnnotationConfigApplicationContext(TestDefaultConfiguration.class, BeforeTaskErrorAnnotationConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
context.setId("beforeTaskAnnotatedTask");
}
private void setupContextForFailedTaskErrorAnnotatedListener(){
context = new AnnotationConfigApplicationContext(TestDefaultConfiguration.class, FailedTaskErrorAnnotationConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
context.setId("failedTaskAnnotatedTask");
}
private void setupContextForAfterTaskErrorAnnotatedListener(){
context = new AnnotationConfigApplicationContext(TestDefaultConfiguration.class, AfterTaskErrorAnnotationConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
context.setId("afterTaskAnnotatedTask");
}
@Configuration
public static class DefaultAnnotationConfiguration {
@@ -241,6 +323,106 @@ public class TaskExecutionListenerTests {
}
}
@Configuration
public static class BeforeTaskErrorAnnotationConfiguration {
@Bean
public AnnotatedTaskListener annotatedTaskListener() {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener {
@BeforeTask
public void methodA(TaskExecution taskExecution) {
beforeTaskDidFireOnError = true;
throw new TaskExecutionException("BeforeTaskFailure");
}
@AfterTask
public void methodB(TaskExecution taskExecution) {
endTaskDidFireOnError = true;
}
@FailedTask
public void methodC(TaskExecution taskExecution, Throwable throwable) {
failedTaskDidFireOnError = true;
}
}
}
@Configuration
public static class FailedTaskErrorAnnotationConfiguration {
@Bean
public AnnotatedTaskListener annotatedTaskListener() {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener {
@BeforeTask
public void methodA(TaskExecution taskExecution) {
beforeTaskDidFireOnError = true;
throw new TaskExecutionException("BeforeTaskFailure");
}
@AfterTask
public void methodB(TaskExecution taskExecution) {
endTaskDidFireOnError = true;
}
@FailedTask
public void methodC(TaskExecution taskExecution, Throwable throwable) {
failedTaskDidFireOnError = true;
throw new TaskExecutionException("FailedTaskFailure");
}
}
}
@Configuration
public static class AfterTaskErrorAnnotationConfiguration {
@Bean
public AnnotatedTaskListener annotatedTaskListener() {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener extends TestListener{
@BeforeTask
public void methodA(TaskExecution taskExecution) {
isTaskStartup = true;
}
@AfterTask
public void methodB(TaskExecution taskExecution) {
isTaskEnd = true;
this.taskExecution = taskExecution;
this.taskExecution.setExitMessage(END_MESSAGE);
throw new TaskExecutionException("AfterTaskFailure");
}
}
}
@Configuration
public static class DefaultTaskListenerConfiguration {