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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -68,10 +68,6 @@ public class JobRepositoryTestUtils {
|
||||
|
||||
};
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
@@ -80,16 +76,10 @@ public class JobRepositoryTestUtils {
|
||||
|
||||
/**
|
||||
* Create a {@link JobRepositoryTestUtils} with all its mandatory properties.
|
||||
* @param jobRepository a {@link JobRepository} backed by a database
|
||||
* @param dataSource a {@link DataSource}
|
||||
* @param jobRepository a {@link JobRepository}.
|
||||
*/
|
||||
public JobRepositoryTestUtils(JobRepository jobRepository, DataSource dataSource) {
|
||||
public JobRepositoryTestUtils(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
setDataSource(dataSource);
|
||||
}
|
||||
|
||||
public final void setDataSource(DataSource dataSource) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,15 +89,6 @@ public class JobRepositoryTestUtils {
|
||||
this.jobParametersIncrementer = jobParametersIncrementer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix of batch tables.
|
||||
* @param tablePrefix of batch tables
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setTablePrefix(String tablePrefix) {
|
||||
this.tablePrefix = tablePrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param jobRepository the jobRepository to set
|
||||
*/
|
||||
@@ -160,58 +141,53 @@ public class JobRepositoryTestUtils {
|
||||
|
||||
/**
|
||||
* Remove the {@link JobExecution} instances, and all associated {@link JobInstance}
|
||||
* and {@link StepExecution} instances from the standard RDBMS locations used by
|
||||
* Spring Batch.
|
||||
* @param list a list of {@link JobExecution}
|
||||
* @throws DataAccessException if there is a problem
|
||||
* and {@link StepExecution} instances from the standard locations used by Spring
|
||||
* Batch.
|
||||
* @param jobExecutions a collection of {@link JobExecution}
|
||||
*/
|
||||
public void removeJobExecutions(Collection<JobExecution> list) throws DataAccessException {
|
||||
for (JobExecution jobExecution : list) {
|
||||
List<Long> stepExecutionIds = jdbcTemplate.query(
|
||||
getQuery("select STEP_EXECUTION_ID from %PREFIX%STEP_EXECUTION where JOB_EXECUTION_ID=?"),
|
||||
new RowMapper<Long>() {
|
||||
@Override
|
||||
public Long mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return rs.getLong(1);
|
||||
}
|
||||
}, jobExecution.getId());
|
||||
for (Long stepExecutionId : stepExecutionIds) {
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%STEP_EXECUTION_CONTEXT where STEP_EXECUTION_ID=?"),
|
||||
stepExecutionId);
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%STEP_EXECUTION where STEP_EXECUTION_ID=?"),
|
||||
stepExecutionId);
|
||||
}
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION_CONTEXT where JOB_EXECUTION_ID=?"),
|
||||
jobExecution.getId());
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION_PARAMS where JOB_EXECUTION_ID=?"),
|
||||
jobExecution.getId());
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION where JOB_EXECUTION_ID=?"),
|
||||
jobExecution.getId());
|
||||
public void removeJobExecutions(Collection<JobExecution> jobExecutions) {
|
||||
for (JobExecution jobExecution : jobExecutions) {
|
||||
removeJobExecution(jobExecution);
|
||||
}
|
||||
for (JobExecution jobExecution : list) {
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_INSTANCE where JOB_INSTANCE_ID=?"),
|
||||
jobExecution.getJobId());
|
||||
for (JobExecution jobExecution : jobExecutions) {
|
||||
this.jobRepository.deleteJobInstance(jobExecution.getJobInstance());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all the {@link JobExecution} instances, and all associated
|
||||
* {@link JobInstance} and {@link StepExecution} instances from the standard RDBMS
|
||||
* locations used by Spring Batch.
|
||||
* @throws DataAccessException if there is a problem
|
||||
* Remove the {@link JobExecution} and its associated {@link StepExecution} instances
|
||||
* Ôfrom the standard locations used by Spring Batch.
|
||||
* @param jobExecution the {@link JobExecution} to delete
|
||||
*/
|
||||
public void removeJobExecutions() throws DataAccessException {
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%STEP_EXECUTION_CONTEXT"));
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%STEP_EXECUTION"));
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION_CONTEXT"));
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION_PARAMS"));
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_EXECUTION"));
|
||||
jdbcTemplate.update(getQuery("delete from %PREFIX%JOB_INSTANCE"));
|
||||
|
||||
public void removeJobExecution(JobExecution jobExecution) {
|
||||
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
|
||||
this.jobRepository.deleteStepExecution(stepExecution);
|
||||
}
|
||||
this.jobRepository.deleteJobExecution(jobExecution);
|
||||
}
|
||||
|
||||
private String getQuery(String base) {
|
||||
return StringUtils.replace(base, "%PREFIX%", this.tablePrefix);
|
||||
/**
|
||||
* Remove all the {@link JobExecution} instances, and all associated
|
||||
* {@link JobInstance} and {@link StepExecution} instances from the standard locations
|
||||
* used by Spring Batch.
|
||||
*/
|
||||
public void removeJobExecutions() {
|
||||
List<String> jobNames = this.jobRepository.getJobNames();
|
||||
for (String jobName : jobNames) {
|
||||
int start = 0;
|
||||
int count = 100;
|
||||
List<JobInstance> jobInstances = this.jobRepository.findJobInstancesByName(jobName, start, count);
|
||||
while (!jobInstances.isEmpty()) {
|
||||
for (JobInstance jobInstance : jobInstances) {
|
||||
List<JobExecution> jobExecutions = this.jobRepository.findJobExecutions(jobInstance);
|
||||
if (jobExecutions != null && !jobExecutions.isEmpty()) {
|
||||
removeJobExecutions(jobExecutions);
|
||||
}
|
||||
}
|
||||
start += count;
|
||||
jobInstances = this.jobRepository.findJobInstancesByName(jobName, start, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,12 +63,8 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
* @Autowired
|
||||
* private Job jobUnderTest;
|
||||
*
|
||||
* @Autowired
|
||||
* private DataSource testDatabase;
|
||||
*
|
||||
* @Before
|
||||
* public void setup() {
|
||||
* this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
|
||||
* this.jobRepositoryTestUtils.removeJobExecutions();
|
||||
* this.jobLauncherTestUtils.setJob(this.jobUnderTest);
|
||||
* }
|
||||
@@ -104,9 +100,8 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
* private JobRepositoryTestUtils jobRepositoryTestUtils;
|
||||
*
|
||||
* @BeforeEach
|
||||
* public void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
|
||||
* public void setup(@Autowired Job jobUnderTest) {
|
||||
* this.jobLauncherTestUtils.setJob(jobUnderTest);
|
||||
* this.jobRepositoryTestUtils.setDataSource(testDatabase);
|
||||
* this.jobRepositoryTestUtils.removeJobExecutions();
|
||||
* }
|
||||
*
|
||||
@@ -125,15 +120,6 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* <strong> It should be noted that {@link JobRepositoryTestUtils} requires a
|
||||
* {@link javax.sql.DataSource} bean. Since this annotation registers a
|
||||
* {@link JobRepositoryTestUtils} in the test context, it is expected that the test
|
||||
* context contains a single autowire candidate for a {@link javax.sql.DataSource} (either
|
||||
* a single bean definition or one that is annotated with
|
||||
* {@link org.springframework.context.annotation.Primary}). </strong>
|
||||
* </p>
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.1
|
||||
* @see JobLauncherTestUtils
|
||||
|
||||
@@ -68,7 +68,7 @@ class JobRepositoryTestUtilsTests {
|
||||
|
||||
@Test
|
||||
void testCreateJobExecutions() throws Exception {
|
||||
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
|
||||
utils = new JobRepositoryTestUtils(jobRepository);
|
||||
List<JobExecution> list = utils.createJobExecutions(3);
|
||||
assertEquals(3, list.size());
|
||||
assertEquals(beforeJobs + 3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
|
||||
@@ -80,7 +80,7 @@ class JobRepositoryTestUtilsTests {
|
||||
|
||||
@Test
|
||||
void testRemoveJobExecutionsWithSameJobInstance() throws Exception {
|
||||
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
|
||||
utils = new JobRepositoryTestUtils(jobRepository);
|
||||
List<JobExecution> list = new ArrayList<>();
|
||||
JobExecution jobExecution = jobRepository.createJobExecution("job", new JobParameters());
|
||||
jobExecution.setEndTime(new Date());
|
||||
@@ -95,7 +95,7 @@ class JobRepositoryTestUtilsTests {
|
||||
|
||||
@Test
|
||||
void testCreateJobExecutionsByName() throws Exception {
|
||||
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
|
||||
utils = new JobRepositoryTestUtils(jobRepository);
|
||||
List<JobExecution> list = utils.createJobExecutions("foo", new String[] { "bar", "spam" }, 3);
|
||||
assertEquals(3, list.size());
|
||||
assertEquals(beforeJobs + 3, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
|
||||
@@ -107,7 +107,7 @@ class JobRepositoryTestUtilsTests {
|
||||
|
||||
@Test
|
||||
void testRemoveJobExecutionsIncrementally() throws Exception {
|
||||
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
|
||||
utils = new JobRepositoryTestUtils(jobRepository);
|
||||
List<JobExecution> list1 = utils.createJobExecutions(3);
|
||||
List<JobExecution> list2 = utils.createJobExecutions(2);
|
||||
assertEquals(beforeJobs + 5, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
|
||||
@@ -119,7 +119,7 @@ class JobRepositoryTestUtilsTests {
|
||||
|
||||
@Test
|
||||
void testCreateJobExecutionsWithIncrementer() throws Exception {
|
||||
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
|
||||
utils = new JobRepositoryTestUtils(jobRepository);
|
||||
utils.setJobParametersIncrementer(new JobParametersIncrementer() {
|
||||
@Override
|
||||
public JobParameters getNext(@Nullable JobParameters parameters) {
|
||||
|
||||
@@ -70,12 +70,8 @@ public class SpringBatchTestJUnit4Tests {
|
||||
@Autowired
|
||||
private Job jobUnderTest;
|
||||
|
||||
@Autowired
|
||||
private DataSource testDatabase;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.jobRepositoryTestUtils.setDataSource(this.testDatabase);
|
||||
this.jobRepositoryTestUtils.removeJobExecutions();
|
||||
}
|
||||
|
||||
|
||||
@@ -70,9 +70,8 @@ public class SpringBatchTestJUnit5Tests {
|
||||
private ItemReader<String> jobScopedItemReader;
|
||||
|
||||
@BeforeEach
|
||||
void setup(@Autowired Job jobUnderTest, @Autowired DataSource testDatabase) {
|
||||
void setup(@Autowired Job jobUnderTest) {
|
||||
this.jobLauncherTestUtils.setJob(jobUnderTest);
|
||||
this.jobRepositoryTestUtils.setDataSource(testDatabase);
|
||||
this.jobRepositoryTestUtils.removeJobExecutions();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user