SCT-35 Tasks Support TaskExecutionListener

resolves spring-cloud/spring-cloud-task#35

TaskExecutionListener now supports TaskExecution Params
This commit is contained in:
Glenn Renfro
2016-02-23 10:44:54 -05:00
committed by Michael Minella
parent 91fa6be092
commit 20a56c9581
6 changed files with 331 additions and 2 deletions

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.listener;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.util.TestDefaultConfiguration;
import org.springframework.cloud.task.util.TestDefaultListenerConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
/**
* Verifies that the TaskExecutionListener invocations occur at the appropriate task
* lifecycle stages.
*
* @author Glenn Renfro
*/
public class TaskExecutionListenerTests {
private AnnotationConfigApplicationContext context;
private static final String EXCEPTION_MESSAGE = "This was expected";
@Before
public void setUp() {
context = new AnnotationConfigApplicationContext();
context.setId("testTask");
context.register(TestDefaultListenerConfiguration.class,
TestDefaultConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
}
@After
public void tearDown() {
context.close();
}
@Test
public void testTaskCreate() {
context.refresh();
TestDefaultListenerConfiguration.TestTaskExecutionListener taskExecutionListener =
context.getBean(TestDefaultListenerConfiguration.TestTaskExecutionListener.class);
TaskExecution taskExecution = new TaskExecution(0, null, "wombat",
new Date(), new Date(), null, new ArrayList<String>());
verifyListenerResults(true, false, false, taskExecution,taskExecutionListener);
}
@Test
public void testTaskUpdate() {
context.refresh();
TestDefaultListenerConfiguration.TestTaskExecutionListener taskExecutionListener =
context.getBean(TestDefaultListenerConfiguration.TestTaskExecutionListener.class);
context.publishEvent(new ContextClosedEvent(context));
TaskExecution taskExecution = new TaskExecution(0, 0, "wombat",
new Date(), new Date(), null, new ArrayList<String>());
verifyListenerResults(true, true, false, taskExecution,taskExecutionListener);
}
@Test
public void testTaskFail() {
RuntimeException exception = new RuntimeException(EXCEPTION_MESSAGE);
context.refresh();
context.publishEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], context, exception));
context.publishEvent(new ContextClosedEvent(context));
TestDefaultListenerConfiguration.TestTaskExecutionListener taskExecutionListener =
context.getBean(TestDefaultListenerConfiguration.TestTaskExecutionListener.class);
TaskExecution taskExecution = new TaskExecution(0, 1, "wombat", new Date(),
new Date(), null, new ArrayList<String>());
verifyListenerResults(true, true, true, taskExecution,taskExecutionListener);
}
private void verifyListenerResults (boolean isTaskStartup, boolean isTaskEnd,
boolean isTaskFailed, TaskExecution taskExecution,
TestDefaultListenerConfiguration.TestTaskExecutionListener actualListener){
assertEquals(isTaskStartup,actualListener.isTaskStartup());
assertEquals(isTaskEnd,actualListener.isTaskEnd());
assertEquals(isTaskFailed,actualListener.isTaskFailed());
if(isTaskFailed){
assertEquals(TestDefaultListenerConfiguration.TestTaskExecutionListener.END_MESSAGE, actualListener.getTaskExecution().getExitMessage());
assertNotNull(actualListener.getThrowable());
assertTrue(actualListener.getThrowable() instanceof RuntimeException);
}
else if(isTaskEnd){
assertEquals(TestDefaultListenerConfiguration.TestTaskExecutionListener.END_MESSAGE, actualListener.getTaskExecution().getExitMessage());
assertNull(actualListener.getThrowable());
}
else {
assertEquals(TestDefaultListenerConfiguration.TestTaskExecutionListener.START_MESSAGE, actualListener.getTaskExecution().getExitMessage());
assertNull(actualListener.getThrowable());
}
assertEquals(taskExecution.getExecutionId(), actualListener.getTaskExecution().getExecutionId());
assertEquals(taskExecution.getExitCode(), actualListener.getTaskExecution().getExitCode());
}
}

View File

@@ -30,7 +30,6 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.util;
import org.springframework.cloud.task.listener.TaskExecutionListener;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Initializes the beans needed to TestExecutionListenerTests.
*
* @author Glenn Renfro
*/
@Configuration
public class TestDefaultListenerConfiguration {
@Bean
public TestTaskExecutionListener taskExecutionListener() {
return new TestTaskExecutionListener();
}
public class TestTaskExecutionListener implements TaskExecutionListener {
public static final String START_MESSAGE = "FOO";
public static final String ERROR_MESSAGE = "BAR";
public static final String END_MESSAGE = "BAZ";
private boolean isTaskStartup;
private boolean isTaskEnd;
private boolean isTaskFailed;
private TaskExecution taskExecution;
private Throwable throwable;
@Override
public void onTaskStartup(TaskExecution taskExecution) {
isTaskStartup = true;
this.taskExecution = taskExecution;
this.taskExecution.setExitMessage(START_MESSAGE);
}
@Override
public void onTaskEnd(TaskExecution taskExecution) {
isTaskEnd = true;
this.taskExecution = taskExecution;
this.taskExecution.setExitMessage(END_MESSAGE);
}
@Override
public void onTaskFailed(TaskExecution taskExecution, Throwable throwable) {
isTaskFailed = true;
this.taskExecution = taskExecution;
this.throwable = throwable;
this.taskExecution.setExitMessage(ERROR_MESSAGE);
}
public boolean isTaskStartup() {
return isTaskStartup;
}
public boolean isTaskEnd() {
return isTaskEnd;
}
public boolean isTaskFailed() {
return isTaskFailed;
}
public TaskExecution getTaskExecution() {
return this.taskExecution;
}
public Throwable getThrowable(){
return throwable;
}
}
}