Remove datasource dependency in JobRepositoryTestUtils
Before this commit, the `JobRepositoryTestUtils` was tied to the JDBC implementation of the `JobRepository` as it was requiring a datasource. This makes it unusable with implementations that do not rely on a datasource to store batch meta-data (A MongoDB job repository for instance where no datasource is used). This commit decouples the `JobRepositoryTestUtils` from the implementation details of the `JobRepository` by making it working against the `JobRepository` interface. This commit also introduces the necessary methods in the `JobRepository` interface as well as various DAOs to implement the utilities without having to deal with the details of the underlying repository implementation. Resolves #4070
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2021 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -29,6 +29,8 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -47,6 +49,41 @@ import java.util.Collection;
|
||||
*/
|
||||
public interface JobRepository {
|
||||
|
||||
/**
|
||||
* Retrieve the names of all job instances sorted alphabetically - i.e. jobs that have
|
||||
* ever been executed.
|
||||
* @return the names of all job instances
|
||||
* @since 5.0
|
||||
*/
|
||||
default List<String> getJobNames() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the last job instances with the provided name, sorted backwards by primary
|
||||
* key, using a 'like' criteria
|
||||
* @param jobName {@link String} containing the name of the job.
|
||||
* @param start int containing the offset of where list of job instances results
|
||||
* should begin.
|
||||
* @param count int containing the number of job instances to return.
|
||||
* @return a list of {@link JobInstance} for the job name requested.
|
||||
* @since 5.0
|
||||
*/
|
||||
default List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all {@link JobExecution}s for given {@link JobInstance}, sorted backwards by
|
||||
* creation order (so the first element is the most recent).
|
||||
* @param jobInstance parent {@link JobInstance} of the {@link JobExecution}s to find.
|
||||
* @return {@link List} containing JobExecutions for the jobInstance.
|
||||
* @since 5.0
|
||||
*/
|
||||
default List<JobExecution> findJobExecutions(JobInstance jobInstance) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an instance of this job already exists with the parameters provided.
|
||||
* @param jobName the name of the job
|
||||
@@ -172,4 +209,33 @@ public interface JobRepository {
|
||||
@Nullable
|
||||
JobExecution getLastJobExecution(String jobName, JobParameters jobParameters);
|
||||
|
||||
/**
|
||||
* Delete the step execution along with its execution context.
|
||||
* @param stepExecution the step execution to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteStepExecution(StepExecution stepExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the job execution object graph (ie the job execution with its execution
|
||||
* context, all related step executions and their executions contexts, as well as
|
||||
* associated job parameters)
|
||||
* @param jobExecution the job execution to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteJobExecution(JobExecution jobExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the job instance.
|
||||
* @param jobInstance the job instance to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteJobInstance(JobInstance jobInstance) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2013 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.batch.item.ExecutionContext;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public interface ExecutionContextDao {
|
||||
|
||||
@@ -78,4 +79,22 @@ public interface ExecutionContextDao {
|
||||
*/
|
||||
void updateExecutionContext(final StepExecution stepExecution);
|
||||
|
||||
/**
|
||||
* Delete the execution context of the given {@link JobExecution}.
|
||||
* @param jobExecution {@link JobExecution} that contains the context to delete.
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteExecutionContext(JobExecution jobExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the execution context of the given {@link StepExecution}.
|
||||
* @param stepExecution {@link StepExecution} that contains the context to delete.
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteExecutionContext(StepExecution stepExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -77,6 +77,12 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
|
||||
private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT "
|
||||
+ "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?";
|
||||
|
||||
private static final String DELETE_STEP_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%STEP_EXECUTION_CONTEXT "
|
||||
+ "WHERE STEP_EXECUTION_ID = ?";
|
||||
|
||||
private static final String DELETE_JOB_EXECUTION_CONTEXT = "DELETE FROM %PREFIX%JOB_EXECUTION_CONTEXT "
|
||||
+ "WHERE JOB_EXECUTION_ID = ?";
|
||||
|
||||
private Charset charset = StandardCharsets.UTF_8;
|
||||
|
||||
private static final int DEFAULT_MAX_VARCHAR_LENGTH = 2500;
|
||||
@@ -217,6 +223,22 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
|
||||
persistSerializedContexts(serializedContexts, INSERT_STEP_EXECUTION_CONTEXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the execution context of the given {@link JobExecution}.
|
||||
* @param jobExecution {@link JobExecution} that contains the context to delete.
|
||||
*/
|
||||
public void deleteExecutionContext(JobExecution jobExecution) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_CONTEXT), jobExecution.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the execution context of the given {@link StepExecution}.
|
||||
* @param stepExecution {@link StepExecution} that contains the context to delete.
|
||||
*/
|
||||
public void deleteExecutionContext(StepExecution stepExecution) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION_CONTEXT), stepExecution.getId());
|
||||
}
|
||||
|
||||
public void setLobHandler(LobHandler lobHandler) {
|
||||
this.lobHandler = lobHandler;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2021 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -94,6 +94,10 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
|
||||
private static final String CREATE_JOB_PARAMETERS = "INSERT into %PREFIX%JOB_EXECUTION_PARAMS(JOB_EXECUTION_ID, KEY_NAME, TYPE_CD, "
|
||||
+ "STRING_VAL, DATE_VAL, LONG_VAL, DOUBLE_VAL, IDENTIFYING) values (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
private static final String DELETE_JOB_EXECUTION = "DELETE FROM %PREFIX%JOB_EXECUTION WHERE JOB_EXECUTION_ID = ?";
|
||||
|
||||
private static final String DELETE_JOB_EXECUTION_PARAMETERS = "DELETE FROM %PREFIX%JOB_EXECUTION_PARAMS WHERE JOB_EXECUTION_ID = ?";
|
||||
|
||||
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
|
||||
|
||||
private DataFieldMaxValueIncrementer jobExecutionIncrementer;
|
||||
@@ -303,6 +307,22 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the given job execution.
|
||||
* @param jobExecution the job execution to delete
|
||||
*/
|
||||
public void deleteJobExecution(JobExecution jobExecution) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION), jobExecution.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the parameters associated with the given job execution.
|
||||
* @param jobExecution the job execution for which job parameters should be deleted
|
||||
*/
|
||||
public void deleteJobExecutionParameters(JobExecution jobExecution) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_JOB_EXECUTION_PARAMETERS), jobExecution.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method that inserts all parameters from the provided JobParameters.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2021 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -85,6 +85,8 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
|
||||
|
||||
private static final String FIND_LAST_JOBS_LIKE_NAME = "SELECT JOB_INSTANCE_ID, JOB_NAME from %PREFIX%JOB_INSTANCE where JOB_NAME like ? order by JOB_INSTANCE_ID desc";
|
||||
|
||||
private static final String DELETE_JOB_INSTANCE = "DELETE FROM %PREFIX%JOB_INSTANCE WHERE JOB_INSTANCE_ID = ?";
|
||||
|
||||
private DataFieldMaxValueIncrementer jobInstanceIncrementer;
|
||||
|
||||
private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
|
||||
@@ -276,6 +278,14 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the job instance.
|
||||
* @param jobInstance the job instance to delete
|
||||
*/
|
||||
public void deleteJobInstance(JobInstance jobInstance) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_JOB_INSTANCE), jobInstance.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for {@link DataFieldMaxValueIncrementer} to be used when generating primary
|
||||
* keys for {@link JobInstance} instances.
|
||||
|
||||
@@ -105,6 +105,9 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
|
||||
+ " on SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " + "where JE.JOB_INSTANCE_ID = ?"
|
||||
+ " and SE.STEP_NAME = ?";
|
||||
|
||||
private static final String DELETE_STEP_EXECUTION = "DELETE FROM %PREFIX%STEP_EXECUTION "
|
||||
+ "WHERE STEP_EXECUTION_ID = ?";
|
||||
|
||||
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
|
||||
|
||||
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
|
||||
@@ -353,6 +356,14 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
|
||||
jobInstance.getInstanceId(), stepName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the given step execution.
|
||||
* @param stepExecution the step execution to delete
|
||||
*/
|
||||
public void deleteStepExecution(StepExecution stepExecution) {
|
||||
getJdbcTemplate().update(getQuery(DELETE_STEP_EXECUTION), stepExecution.getId());
|
||||
}
|
||||
|
||||
private static class StepExecutionRowMapper implements RowMapper<StepExecution> {
|
||||
|
||||
private final JobExecution jobExecution;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2018 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -21,6 +21,7 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -88,4 +89,22 @@ public interface JobExecutionDao {
|
||||
*/
|
||||
void synchronizeStatus(JobExecution jobExecution);
|
||||
|
||||
/**
|
||||
* Delete the given job execution.
|
||||
* @param jobExecution the job execution to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteJobExecution(JobExecution jobExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the parameters associated with the given job execution.
|
||||
* @param jobExecution the job execution for which job parameters should be deleted
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteJobExecutionParameters(JobExecution jobExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-2022 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.
|
||||
@@ -130,4 +130,13 @@ public interface JobInstanceDao {
|
||||
*/
|
||||
int getJobInstanceCount(@Nullable String jobName) throws NoSuchJobException;
|
||||
|
||||
/**
|
||||
* Delete the job instance.
|
||||
* @param jobInstance the job instance to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteJobInstance(JobInstance jobInstance) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -91,4 +91,13 @@ public interface StepExecutionDao {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the given step execution.
|
||||
* @param stepExecution the step execution to delete
|
||||
* @since 5.0
|
||||
*/
|
||||
default void deleteStepExecution(StepExecution stepExecution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -279,6 +279,24 @@ public class SimpleJobRepository implements JobRepository {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteStepExecution(StepExecution stepExecution) {
|
||||
this.ecDao.deleteExecutionContext(stepExecution);
|
||||
this.stepExecutionDao.deleteStepExecution(stepExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteJobExecution(JobExecution jobExecution) {
|
||||
this.ecDao.deleteExecutionContext(jobExecution);
|
||||
this.jobExecutionDao.deleteJobExecutionParameters(jobExecution);
|
||||
this.jobExecutionDao.deleteJobExecution(jobExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteJobInstance(JobInstance jobInstance) {
|
||||
this.jobInstanceDao.deleteJobInstance(jobInstance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {
|
||||
Assert.notNull(jobName, "A job name is required to create a JobInstance");
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -221,4 +222,40 @@ public abstract class AbstractExecutionContextDaoTests extends AbstractTransacti
|
||||
assertEquals(ec, restoredEc);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteStepExecutionContext() {
|
||||
// given
|
||||
ExecutionContext ec = new ExecutionContext();
|
||||
stepExecution.setExecutionContext(ec);
|
||||
contextDao.saveExecutionContext(stepExecution);
|
||||
|
||||
// when
|
||||
contextDao.deleteExecutionContext(stepExecution);
|
||||
|
||||
// then
|
||||
ExecutionContext restoredEc = contextDao.getExecutionContext(stepExecution);
|
||||
// FIXME contextDao.getExecutionContext should return null and not an empty
|
||||
// context
|
||||
assertEquals(new ExecutionContext(), restoredEc);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteJobExecutionContext() {
|
||||
// given
|
||||
ExecutionContext ec = new ExecutionContext();
|
||||
jobExecution.setExecutionContext(ec);
|
||||
contextDao.saveExecutionContext(jobExecution);
|
||||
|
||||
// when
|
||||
contextDao.deleteExecutionContext(jobExecution);
|
||||
|
||||
// then
|
||||
ExecutionContext restoredEc = contextDao.getExecutionContext(jobExecution);
|
||||
// FIXME contextDao.getExecutionContext should return null and not an empty
|
||||
// context
|
||||
assertEquals(new ExecutionContext(), restoredEc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
@@ -39,6 +41,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author Parikshit Dutta
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@SpringJUnitConfig(locations = { "sql-dao-test.xml" })
|
||||
public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
|
||||
@@ -108,4 +111,34 @@ public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteJobExecution() {
|
||||
// given
|
||||
JobExecution execution = new JobExecution(jobInstance, new JobParameters());
|
||||
dao.saveJobExecution(execution);
|
||||
|
||||
// when
|
||||
dao.deleteJobExecution(execution);
|
||||
|
||||
// then
|
||||
Assertions.assertNull(dao.getJobExecution(execution.getId()));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteJobExecutionParameters() {
|
||||
// given
|
||||
Map<String, JobParameter> parameters = new HashMap<>();
|
||||
parameters.put("string-param", new JobParameter("value"));
|
||||
JobExecution execution = new JobExecution(jobInstance, new JobParameters(parameters));
|
||||
dao.saveJobExecution(execution);
|
||||
|
||||
// when
|
||||
dao.deleteJobExecutionParameters(execution);
|
||||
|
||||
// then
|
||||
Assertions.assertEquals(0, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION_PARAMS"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
@@ -100,4 +101,17 @@ public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
assertTrue(jobInstances.isEmpty());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteJobInstance() {
|
||||
// given
|
||||
JobInstance jobInstance = dao.createJobInstance("someTestInstance", new JobParameters());
|
||||
|
||||
// when
|
||||
dao.deleteJobInstance(jobInstance);
|
||||
|
||||
// then
|
||||
Assertions.assertNull(dao.getJobInstance(jobInstance.getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -86,4 +87,17 @@ class JdbcStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
void testDeleteStepExecution() {
|
||||
// Given
|
||||
dao.saveStepExecution(stepExecution);
|
||||
|
||||
// When
|
||||
dao.deleteStepExecution(stepExecution);
|
||||
|
||||
// Then
|
||||
assertNull(dao.getStepExecution(jobExecution, stepExecution.getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user