diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java index 7a213ba3..446be4b4 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/listener/TaskLifecycleListener.java @@ -204,8 +204,13 @@ public class TaskLifecycleListener implements ApplicationListener arguments, String externalExecutionId); + TaskExecution createTaskExecution(TaskExecution taskExecution); /** * Creates an empty TaskExecution with just an id provided. This is intended to be diff --git a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java index f15a9976..712932ae 100644 --- a/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java +++ b/spring-cloud-task-core/src/main/java/org/springframework/cloud/task/repository/support/SimpleTaskRepository.java @@ -101,14 +101,17 @@ public class SimpleTaskRepository implements TaskRepository { } @Override - public TaskExecution createTaskExecution(String taskName, - Date startTime,List arguments, String externalExecutionId) { + public TaskExecution createTaskExecution(TaskExecution taskExecution) { initialize(); - validateCreateInformation(startTime, taskName); - TaskExecution taskExecution = - taskExecutionDao.createTaskExecution(taskName, startTime, arguments, externalExecutionId); + validateCreateInformation(taskExecution); + TaskExecution daoTaskExecution = + taskExecutionDao.createTaskExecution( + taskExecution.getTaskName(), + taskExecution.getStartTime(), + taskExecution.getArguments(), + taskExecution.getExternalExecutionId()); logger.debug("Creating: " + taskExecution.toString()); - return taskExecution; + return daoTaskExecution; } @Override @@ -155,11 +158,11 @@ public class SimpleTaskRepository implements TaskRepository { /** * Validate startTime and taskName are valid. */ - private void validateCreateInformation(Date startTime, String taskName) { - Assert.notNull(startTime, "TaskExecution start time cannot be null."); + private void validateCreateInformation(TaskExecution taskExecution) { + Assert.notNull(taskExecution.getStartTime(), "TaskExecution start time cannot be null."); - if (taskName != null && - taskName.length() > this.maxTaskNameSize) { + if (taskExecution.getTaskName() != null && + taskExecution.getTaskName().length() > this.maxTaskNameSize) { throw new IllegalArgumentException("TaskName length exceeds " + this.maxTaskNameSize + " characters"); } diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDaoTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDaoTests.java index 76044ed1..0660cd76 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDaoTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/dao/JdbcTaskExecutionDaoTests.java @@ -177,20 +177,23 @@ public class JdbcTaskExecutionDaoTests { } private void initializeRepository() { - repository.createTaskExecution("FOO3", - new Date(),new ArrayList(0), "externalA"); - repository.createTaskExecution("FOO2", - new Date(),new ArrayList(0), "externalB"); - repository.createTaskExecution("FOO1", - new Date(),new ArrayList(0), "externalC"); + repository.createTaskExecution(getTaskExecution("FOO3", "externalA")); + repository.createTaskExecution(getTaskExecution("FOO2", "externalB")); + repository.createTaskExecution(getTaskExecution("FOO1", "externalC")); } private void initializeRepositoryNotInOrder() { - repository.createTaskExecution("FOO1", - new Date(), new ArrayList(0), "externalC"); - repository.createTaskExecution("FOO2", - new Date(), new ArrayList(0), "externalA"); - repository.createTaskExecution("FOO3", - new Date(), new ArrayList(0), "externalB"); + repository.createTaskExecution(getTaskExecution("FOO1", "externalC")); + repository.createTaskExecution(getTaskExecution("FOO2", "externalA")); + repository.createTaskExecution(getTaskExecution("FOO3", "externalB")); + } + + private TaskExecution getTaskExecution(String taskName, + String externalExecutionId) { + TaskExecution taskExecution = new TaskExecution(); + taskExecution.setTaskName(taskName); + taskExecution.setExternalExecutionId(externalExecutionId); + taskExecution.setStartTime(new Date()); + return taskExecution; } } diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorerTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorerTests.java index 2b4e108b..a8a5e2a1 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorerTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskExplorerTests.java @@ -63,6 +63,10 @@ import org.springframework.data.domain.Pageable; @RunWith(Parameterized.class) public class SimpleTaskExplorerTests { + private final static String TASK_NAME = "FOOBAR"; + + private final static String EXTERNAL_EXECUTION_ID = "123ABC"; + private AnnotationConfigApplicationContext context; @Autowired @@ -154,8 +158,6 @@ public class SimpleTaskExplorerTests { public void findRunningTasks() { final int TEST_COUNT = 2; final int COMPLETE_COUNT = 5; - final String TASK_NAME = "FOOBAR"; - final String EXTERNAL_EXECUTION_ID = "123ABC"; Map expectedResults = new HashMap<>(); //Store completed jobs @@ -165,8 +167,8 @@ public class SimpleTaskExplorerTests { } for (; i < (COMPLETE_COUNT + TEST_COUNT); i++) { - TaskExecution expectedTaskExecution = this.taskRepository.createTaskExecution( - TASK_NAME, new Date(), new ArrayList(), EXTERNAL_EXECUTION_ID); + TaskExecution expectedTaskExecution = this.taskRepository. + createTaskExecution(getSimpleTaskExecution()); expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution); } Pageable pageable = new PageRequest(0, 10); @@ -190,8 +192,6 @@ public class SimpleTaskExplorerTests { public void findTasksByName() { final int TEST_COUNT = 5; final int COMPLETE_COUNT = 7; - final String TASK_NAME = "FOOBAR"; - final String EXTERNAL_EXECUTION_ID = "123ABC"; Random randomGenerator = new Random(); Map expectedResults = new HashMap<>(); @@ -201,8 +201,8 @@ public class SimpleTaskExplorerTests { } for (int i = 0; i < TEST_COUNT; i++) { - TaskExecution expectedTaskExecution = this.taskRepository.createTaskExecution( - TASK_NAME, new Date(), new ArrayList(), EXTERNAL_EXECUTION_ID); + TaskExecution expectedTaskExecution = this.taskRepository. + createTaskExecution(getSimpleTaskExecution()); expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution); } @@ -319,8 +319,7 @@ public class SimpleTaskExplorerTests { private TaskExecution createAndSaveTaskExecution(int i) { TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution(i); - taskExecution = this.taskRepository.createTaskExecution(taskExecution.getTaskName(), - taskExecution.getStartTime(), taskExecution.getArguments(), taskExecution.getExternalExecutionId()); + taskExecution = this.taskRepository.createTaskExecution(taskExecution); return taskExecution; } @@ -379,6 +378,14 @@ public class SimpleTaskExplorerTests { }); } + private TaskExecution getSimpleTaskExecution() { + TaskExecution taskExecution = new TaskExecution(); + taskExecution.setTaskName(TASK_NAME); + taskExecution.setStartTime(new Date()); + taskExecution.setExternalExecutionId(EXTERNAL_EXECUTION_ID); + return taskExecution; + } + private enum DaoType{jdbc, map} @Configuration diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java index 9e7ca05b..bf04be8f 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/support/SimpleTaskRepositoryJdbcTests.java @@ -193,9 +193,7 @@ public class SimpleTaskRepositoryJdbcTests { MAX_ERROR_MESSAGE_SIZE); TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg(); expectedTaskExecution.setTaskName(new String(new char[MAX_TASK_NAME_SIZE + 1])); - simpleTaskRepository.createTaskExecution(expectedTaskExecution.getTaskName(), - expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments(), - expectedTaskExecution.getExternalExecutionId()); + simpleTaskRepository.createTaskExecution(expectedTaskExecution); } @Test(expected = IllegalArgumentException.class) @@ -204,9 +202,7 @@ public class SimpleTaskRepositoryJdbcTests { new TaskExecutionDaoFactoryBean(this.dataSource), null, null, null); TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg(); expectedTaskExecution.setTaskName(new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE + 1])); - simpleTaskRepository.createTaskExecution(expectedTaskExecution.getTaskName(), - expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments(), - expectedTaskExecution.getExternalExecutionId()); + simpleTaskRepository.createTaskExecution(expectedTaskExecution); } @Test @@ -230,9 +226,11 @@ public class SimpleTaskRepositoryJdbcTests { @Test(expected=IllegalArgumentException.class) @DirtiesContext public void testCreateTaskExecutionNoParamMaxTaskName(){ - taskRepository.createTaskExecution( - new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE+1]), - new Date(), null, null); + TaskExecution taskExecution = new TaskExecution(); + taskExecution.setTaskName( + new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE+1])); + taskExecution.setStartTime(new Date()); + taskRepository.createTaskExecution(taskExecution); } @Test(expected=IllegalArgumentException.class) diff --git a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java index b8d344d8..218fc339 100644 --- a/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java +++ b/spring-cloud-task-core/src/test/java/org/springframework/cloud/task/util/TaskExecutionCreator.java @@ -48,9 +48,7 @@ public class TaskExecutionCreator { */ public static TaskExecution createAndStoreTaskExecutionNoParams(TaskRepository taskRepository) { TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg(); - expectedTaskExecution = taskRepository.createTaskExecution(expectedTaskExecution.getTaskName(), - expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments(), - expectedTaskExecution.getExternalExecutionId()); + expectedTaskExecution = taskRepository.createTaskExecution(); return expectedTaskExecution; } @@ -66,9 +64,7 @@ public class TaskExecutionCreator { params.add(UUID.randomUUID().toString()); params.add(UUID.randomUUID().toString()); expectedTaskExecution.setArguments(params); - expectedTaskExecution = taskRepository.createTaskExecution(expectedTaskExecution.getTaskName(), - expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments(), - expectedTaskExecution.getExternalExecutionId()); + expectedTaskExecution = taskRepository.createTaskExecution(expectedTaskExecution); return expectedTaskExecution; }