|
24 | 24 |
|
25 | 25 | import org.springframework.aop.Advisor;
|
26 | 26 | import org.springframework.aop.framework.Advised;
|
| 27 | +import org.springframework.batch.core.DefaultJobKeyGenerator; |
| 28 | +import org.springframework.batch.core.JobKeyGenerator; |
27 | 29 | import org.springframework.batch.core.configuration.JobRegistry;
|
28 | 30 | import org.springframework.batch.core.explore.JobExplorer;
|
29 | 31 | import org.springframework.batch.core.launch.JobLauncher;
|
@@ -167,6 +169,35 @@ void testDefaultInfrastructureBeansRegistration() {
|
167 | 169 | Assertions.assertNotNull(jobOperator);
|
168 | 170 | }
|
169 | 171 |
|
| 172 | + @Test |
| 173 | + @DisplayName("When no JobKeyGenerator is provided the default implementation should be used") |
| 174 | + public void testDefaultJobKeyGeneratorConfiguration() { |
| 175 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); |
| 176 | + |
| 177 | + JobRepository jobRepository = context.getBean(JobRepository.class); |
| 178 | + JdbcJobInstanceDao jobInstanceDao = (JdbcJobInstanceDao) ReflectionTestUtils.getField(jobRepository, |
| 179 | + "jobInstanceDao"); |
| 180 | + JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(jobInstanceDao, |
| 181 | + "jobKeyGenerator"); |
| 182 | + |
| 183 | + Assertions.assertEquals(DefaultJobKeyGenerator.class, jobKeyGenerator.getClass()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + @DisplayName("When a custom JobKeyGenerator implementation is found that should be used") |
| 188 | + public void testCustomJobKeyGeneratorConfiguration() { |
| 189 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 190 | + CustomJobKeyGeneratorConfiguration.class); |
| 191 | + |
| 192 | + JobRepository jobRepository = context.getBean(JobRepository.class); |
| 193 | + JdbcJobInstanceDao jobInstanceDao = (JdbcJobInstanceDao) ReflectionTestUtils.getField(jobRepository, |
| 194 | + "jobInstanceDao"); |
| 195 | + JobKeyGenerator jobKeyGenerator = (JobKeyGenerator) ReflectionTestUtils.getField(jobInstanceDao, |
| 196 | + "jobKeyGenerator"); |
| 197 | + Assertions.assertEquals(CustomJobKeyGeneratorConfiguration.TestCustomJobKeyGenerator.class, |
| 198 | + jobKeyGenerator.getClass()); |
| 199 | + } |
| 200 | + |
170 | 201 | @Configuration
|
171 | 202 | @EnableBatchProcessing
|
172 | 203 | public static class JobConfigurationWithoutDataSource {
|
@@ -253,6 +284,39 @@ public JdbcTransactionManager batchTransactionManager(DataSource dataSource) {
|
253 | 284 |
|
254 | 285 | }
|
255 | 286 |
|
| 287 | + @Configuration |
| 288 | + @EnableBatchProcessing |
| 289 | + public static class CustomJobKeyGeneratorConfiguration { |
| 290 | + |
| 291 | + @Bean |
| 292 | + public DataSource dataSource() { |
| 293 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) |
| 294 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
| 295 | + .generateUniqueName(true) |
| 296 | + .build(); |
| 297 | + } |
| 298 | + |
| 299 | + @Bean |
| 300 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 301 | + return new JdbcTransactionManager(dataSource); |
| 302 | + } |
| 303 | + |
| 304 | + @Bean |
| 305 | + public JobKeyGenerator jobKeyGenerator() { |
| 306 | + return new TestCustomJobKeyGenerator(); |
| 307 | + } |
| 308 | + |
| 309 | + private class TestCustomJobKeyGenerator implements JobKeyGenerator { |
| 310 | + |
| 311 | + @Override |
| 312 | + public String generateKey(Object source) { |
| 313 | + return "1"; |
| 314 | + } |
| 315 | + |
| 316 | + } |
| 317 | + |
| 318 | + } |
| 319 | + |
256 | 320 | private PlatformTransactionManager getTransactionManagerSetOnJobRepository(JobRepository jobRepository) {
|
257 | 321 | Advised target = (Advised) jobRepository; // proxy created by
|
258 | 322 | // AbstractJobRepositoryFactoryBean
|
|
0 commit comments